source: trunk/hiptop/pester/net/sabi/pester/AlarmListView.java@ 276

Last change on this file since 276 was 276, checked in by Nicholas Riley, 18 years ago

First pass at alarm persistence.

File size: 3.6 KB
Line 
1package net.sabi.pester;
2
3import danger.app.Application;
4import danger.app.Event;
5import danger.ui.AlertWindow;
6import danger.ui.ActiveListView;
7import danger.ui.Rect;
8import danger.ui.ToolTipWindow;
9import danger.util.ActiveList;
10import danger.util.DEBUG;
11
12public 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 mToolTipAlarm = new danger.app.Alarm(0, this);
25 setAutoResize(true);
26 setList(Alarms.getList());
27 if (alarmsSet() == 0)
28 setHasBorder(false); // this doesn't work in the rsrc file
29 Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
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_TIME_CHANGED: // XXX EVENT_TIME_FORMAT_CHANGED too
67 if (mToolTipWindow == null || !mToolTipWindow.isVisible())
68 return true;
69 case Event.EVENT_ALARM: // tooltip invalid
70 showToolTip();
71 return true;
72 }
73 return super.receiveEvent(e);
74 }
75
76 protected void itemActivated(Object item) {
77 if (item == null)
78 return;
79 alarmSetDialog().editAlarm((Alarm)item, false);
80 }
81
82 protected void itemFocused(Object item) {
83 mToolTipAlarm.deactivate();
84 }
85
86 public void loseFocus() {
87 mToolTipAlarm.deactivate();
88 super.loseFocus();
89 }
90
91 public void showToolTip() {
92 Alarm alarm = (Alarm)getFocusedItem();
93 if (alarm == null)
94 return;
95 Rect rect = new Rect();
96 rect = localToGlobal(getInterestingRect(rect));
97 if (mToolTipWindow != null)
98 mToolTipWindow.hide();
99 mToolTipWindow =
100 ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
101 alarm.getIntervalString(),
102 10, rect.bottom, 1, sAlarmListWindow);
103 int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
104 if (secondsUntilUpdate == 0)
105 return;
106 mToolTipAlarm.resetWake(secondsUntilUpdate);
107 }
108
109 public int alarmsSet() {
110 return getListSize();
111 }
112 public void onItemAdded(ActiveList list, Object item, int index) {
113 sAlarmListWindow.updateAlarmCount();
114 super.onItemAdded(list, item, index);
115 // super's default behavior is to preserve the existing selection,
116 // which is admirable in general, but not what we want
117 setFocus(index);
118 setHasBorder(true);
119 }
120 public void onItemRemoved(ActiveList list, Object item, int index) {
121 ((Alarm)item).cancel();
122 sAlarmListWindow.updateAlarmCount();
123 super.onItemRemoved(list, item, index);
124 if (alarmsSet() == 0)
125 setHasBorder(false);
126 }
127}
Note: See TracBrowser for help on using the repository browser.