1 | package net.sabi.pester;
|
---|
2 |
|
---|
3 | import danger.app.Application;
|
---|
4 | import danger.app.Event;
|
---|
5 | import danger.ui.AlertWindow;
|
---|
6 | import danger.ui.ActiveListView;
|
---|
7 | import danger.ui.Font;
|
---|
8 | import danger.ui.Pen;
|
---|
9 | import danger.ui.Rect;
|
---|
10 | import danger.ui.Style;
|
---|
11 | import danger.ui.ToolTipWindow;
|
---|
12 | import danger.util.ActiveList;
|
---|
13 | import danger.util.DEBUG;
|
---|
14 | import danger.util.StringUtils;
|
---|
15 |
|
---|
16 | public class AlarmListView extends ActiveListView
|
---|
17 | implements Resources, Commands {
|
---|
18 |
|
---|
19 | // XXX some of these shouldn't conceptually be static
|
---|
20 | private static AlarmListWindow sAlarmListWindow;
|
---|
21 | private static AlarmSetDialog sAlarmSetDialog;
|
---|
22 |
|
---|
23 | private danger.app.Alarm mToolTipAlarm;
|
---|
24 | private ToolTipWindow mToolTipWindow;
|
---|
25 |
|
---|
26 | public void onDecoded() {
|
---|
27 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
28 | mToolTipAlarm = new danger.app.Alarm(0, this);
|
---|
29 | setHasBorder(false);
|
---|
30 | setList(Alarms.getList());
|
---|
31 | Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
|
---|
32 | super.onDecoded();
|
---|
33 | }
|
---|
34 |
|
---|
35 | protected AlarmSetDialog alarmSetDialog() {
|
---|
36 | if (sAlarmSetDialog == null)
|
---|
37 | sAlarmSetDialog = AlarmSetDialog.getDialog();
|
---|
38 | return sAlarmSetDialog;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
42 | switch (widget) {
|
---|
43 | case Event.DEVICE_BUTTON_BACK:
|
---|
44 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
45 | Application.getCurrentApp().returnToLauncher();
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 | return super.eventWidgetUp(widget, e);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public boolean receiveEvent(Event e) {
|
---|
52 | switch (e.type) {
|
---|
53 | case EVENT_NEW_ALARM:
|
---|
54 | // XXX check for max alarms
|
---|
55 | alarmSetDialog().editAlarm(new Alarm(), true);
|
---|
56 | return true;
|
---|
57 | case EVENT_DISCARD_ALARM:
|
---|
58 | mToolTipAlarm.deactivate();
|
---|
59 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
60 | alarm.beginEditing();
|
---|
61 | AlertWindow alert =
|
---|
62 | Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
|
---|
63 | alert.setMessage("Permanently discard \u201c" +
|
---|
64 | alarm.getMessage() + "\u201d?"); // XXX localize
|
---|
65 | alert.show();
|
---|
66 | return true;
|
---|
67 | case EVENT_CONFIRM_DISCARD:
|
---|
68 | Alarms.removeAlarm((Alarm)getFocusedItem());
|
---|
69 | return true;
|
---|
70 | case EVENT_CANCEL_DISCARD:
|
---|
71 | ((Alarm)getFocusedItem()).resume();
|
---|
72 | return true;
|
---|
73 | // XXX for EVENT_TIME_FORMAT_CHANGED, we also need to force a
|
---|
74 | // full redraw (in addition to invalidating the tooltip)
|
---|
75 | case Event.EVENT_TIME_CHANGED:
|
---|
76 | if (mToolTipWindow == null || !mToolTipWindow.isVisible())
|
---|
77 | return true;
|
---|
78 | case Event.EVENT_ALARM: // tooltip invalid
|
---|
79 | showToolTip();
|
---|
80 | return true;
|
---|
81 | }
|
---|
82 | return super.receiveEvent(e);
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected void itemActivated(Object item) {
|
---|
86 | if (item == null)
|
---|
87 | return;
|
---|
88 | alarmSetDialog().editAlarm((Alarm)item, false);
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void itemFocused(Object item) {
|
---|
92 | mToolTipAlarm.deactivate();
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void loseFocus() {
|
---|
96 | mToolTipAlarm.deactivate();
|
---|
97 | super.loseFocus();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void showToolTip() {
|
---|
101 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
102 | if (alarm == null)
|
---|
103 | return;
|
---|
104 | Rect rect = new Rect();
|
---|
105 | rect = localToGlobal(getInterestingRect(rect));
|
---|
106 | if (mToolTipWindow != null)
|
---|
107 | mToolTipWindow.hide();
|
---|
108 | mToolTipWindow =
|
---|
109 | ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
|
---|
110 | alarm.getIntervalString(),
|
---|
111 | localToGlobalH(10),
|
---|
112 | localToGlobalV(mToolTipY),
|
---|
113 | 1, sAlarmListWindow);
|
---|
114 | int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
|
---|
115 | if (secondsUntilUpdate == 0)
|
---|
116 | return;
|
---|
117 | mToolTipAlarm.resetWake(secondsUntilUpdate);
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected void paintEmptyList(Pen p, int width, int height) {
|
---|
121 | Style style = getStyle();
|
---|
122 | Font font = style.getFont(Style.DISABLED_LABEL_FONT);
|
---|
123 | int y = (height / 4) - (font.getAscent() / 2);
|
---|
124 | if (y < 0) {
|
---|
125 | setHeight(sAlarmListWindow.getHeight());
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | // XXX localize
|
---|
129 | String emptyMessage =
|
---|
130 | "Press " + Font.GLYPH_MENU + " to set an alarm.";
|
---|
131 | int x = (width / 2) - (font.getWidth(emptyMessage) / 2);
|
---|
132 | p.setFont(font);
|
---|
133 | p.setColor(style.getColor(Style.DISABLED_LABEL_COLOR));
|
---|
134 | p.drawText(x, y, emptyMessage);
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected void paintItemLine(Pen p, int left, int top, int right, int bottom,
|
---|
138 | boolean focused, Object item) {
|
---|
139 | int oldOutlineColor = p.getTextOutlineColor();
|
---|
140 | if (focused) {
|
---|
141 | paintSelection(p, left, top, right, bottom);
|
---|
142 | p.setColor(getStyle().getColor(Style.BACKGROUND_COLOR));
|
---|
143 | p.setTextOutlineColor(getStyle().getColor(Style.FOREGROUND_COLOR));
|
---|
144 | } else {
|
---|
145 | p.setColor(getStyle().getColor(Style.FOREGROUND_COLOR));
|
---|
146 | }
|
---|
147 | Font font = p.getFont();
|
---|
148 | int descent = font.getDescent();
|
---|
149 | int height = descent + font.getAscent();
|
---|
150 | top = ((top + bottom) / 2 + height / 2) - descent;
|
---|
151 | Alarm alarm = (Alarm)item;
|
---|
152 | left += 2;
|
---|
153 | left += p.drawText(left, top, alarm.getDateTimeString(true));
|
---|
154 | Font msgFont = font.findVariant(null, -1, Font.F_BOLD | Font.F_OUTLINE);
|
---|
155 | p.setFont(msgFont);
|
---|
156 | StringUtils.drawDisplayString(p, left, top, " " + alarm.getMessage(),
|
---|
157 | (right - left - 2), msgFont);
|
---|
158 | // restore
|
---|
159 | p.setTextOutlineColor(oldOutlineColor);
|
---|
160 | p.setFont(font);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public int alarmsSet() {
|
---|
164 | return getListSize();
|
---|
165 | }
|
---|
166 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
167 | sAlarmListWindow.updateAlarmCount();
|
---|
168 | super.onItemAdded(list, item, index);
|
---|
169 | // super's default behavior is to preserve the existing selection,
|
---|
170 | // which is admirable in general, but not what we want
|
---|
171 | setFocus(index);
|
---|
172 | }
|
---|
173 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
174 | ((Alarm)item).cancel();
|
---|
175 | sAlarmListWindow.updateAlarmCount();
|
---|
176 | super.onItemRemoved(list, item, index);
|
---|
177 | }
|
---|
178 | }
|
---|