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.Rect;
|
---|
8 | import danger.ui.ToolTipWindow;
|
---|
9 | import danger.util.ActiveList;
|
---|
10 | import danger.util.DEBUG;
|
---|
11 |
|
---|
12 | public class AlarmListView extends ActiveListView
|
---|
13 | implements Resources, Commands {
|
---|
14 |
|
---|
15 | // XXX some of these shouldn't conceptually be static
|
---|
16 | private static AlarmListWindow sAlarmListWindow;
|
---|
17 | private static AlarmSetDialog sAlarmSetDialog;
|
---|
18 |
|
---|
19 | private danger.app.Alarm mToolTipAlarm;
|
---|
20 | private ToolTipWindow mToolTipWindow;
|
---|
21 |
|
---|
22 | public void onDecoded() {
|
---|
23 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
24 | setAutoResize(true);
|
---|
25 | setHasBorder(false); // this doesn't work in the rsrc file
|
---|
26 | setList(Alarms.getList());
|
---|
27 | if (alarmsSet() > 0)
|
---|
28 | setHasBorder(true);
|
---|
29 | mToolTipAlarm = new danger.app.Alarm(0, this);
|
---|
30 | super.onDecoded();
|
---|
31 | }
|
---|
32 |
|
---|
33 | protected AlarmSetDialog alarmSetDialog() {
|
---|
34 | if (sAlarmSetDialog == null)
|
---|
35 | sAlarmSetDialog = AlarmSetDialog.getDialog();
|
---|
36 | return sAlarmSetDialog;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
40 | switch (widget) {
|
---|
41 | case Event.DEVICE_BUTTON_BACK:
|
---|
42 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
43 | Application.getCurrentApp().returnToLauncher();
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 | return super.eventWidgetUp(widget, e);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public boolean receiveEvent(Event e) {
|
---|
50 | switch (e.type) {
|
---|
51 | case EVENT_NEW_ALARM:
|
---|
52 | alarmSetDialog().editAlarm(new Alarm(), true);
|
---|
53 | return true;
|
---|
54 | case EVENT_CONFIRM_DISCARD_ALARM:
|
---|
55 | AlertWindow alert =
|
---|
56 | Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
|
---|
57 | // XXX localize
|
---|
58 | alert.setMessage("Permanently discard \u201c" +
|
---|
59 | ((Alarm)getFocusedItem()).getMessage() +
|
---|
60 | "\u201d?");
|
---|
61 | alert.show();
|
---|
62 | return true;
|
---|
63 | case EVENT_DISCARD_ALARM:
|
---|
64 | Alarms.removeAlarm((Alarm)getFocusedItem());
|
---|
65 | return true;
|
---|
66 | case Event.EVENT_ALARM: // tooltip invalid
|
---|
67 | // XXX should do this too if the time changes
|
---|
68 | showToolTip();
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 | return super.receiveEvent(e);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void itemActivated(Object item) {
|
---|
75 | if (item == null)
|
---|
76 | return;
|
---|
77 | alarmSetDialog().editAlarm((Alarm)item, false);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected void itemFocused(Object item) {
|
---|
81 | mToolTipAlarm.deactivate();
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void loseFocus() {
|
---|
85 | mToolTipAlarm.deactivate();
|
---|
86 | super.loseFocus();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void showToolTip() {
|
---|
90 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
91 | if (alarm == null)
|
---|
92 | return;
|
---|
93 | Rect rect = new Rect();
|
---|
94 | rect = localToGlobal(getInterestingRect(rect));
|
---|
95 | if (mToolTipWindow != null)
|
---|
96 | mToolTipWindow.hide();
|
---|
97 | mToolTipWindow =
|
---|
98 | ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
|
---|
99 | alarm.getIntervalString(),
|
---|
100 | 10, rect.bottom, 1, sAlarmListWindow);
|
---|
101 | int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
|
---|
102 | if (secondsUntilUpdate == 0)
|
---|
103 | return;
|
---|
104 | mToolTipAlarm.resetWake(secondsUntilUpdate);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public int alarmsSet() {
|
---|
108 | return getListSize();
|
---|
109 | }
|
---|
110 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
111 | sAlarmListWindow.updateAlarmCount();
|
---|
112 | super.onItemAdded(list, item, index);
|
---|
113 | // super's default behavior is to preserve the existing selection,
|
---|
114 | // which is admirable in general, but not what we want
|
---|
115 | setFocus(index);
|
---|
116 | setHasBorder(true);
|
---|
117 | }
|
---|
118 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
119 | ((Alarm)item).cancel();
|
---|
120 | sAlarmListWindow.updateAlarmCount();
|
---|
121 | super.onItemRemoved(list, item, index);
|
---|
122 | if (alarmsSet() == 0)
|
---|
123 | setHasBorder(false);
|
---|
124 | }
|
---|
125 | }
|
---|