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

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

Fix bug with alarm update interval; update tooltip when time changes.

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 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 Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
31 super.onDecoded();
32 }
33
34 protected AlarmSetDialog alarmSetDialog() {
35 if (sAlarmSetDialog == null)
36 sAlarmSetDialog = AlarmSetDialog.getDialog();
37 return sAlarmSetDialog;
38 }
39
40 public boolean eventWidgetUp(int widget, Event e) {
41 switch (widget) {
42 case Event.DEVICE_BUTTON_BACK:
43 case Event.DEVICE_BUTTON_CANCEL:
44 Application.getCurrentApp().returnToLauncher();
45 return true;
46 }
47 return super.eventWidgetUp(widget, e);
48 }
49
50 public boolean receiveEvent(Event e) {
51 switch (e.type) {
52 case EVENT_NEW_ALARM:
53 alarmSetDialog().editAlarm(new Alarm(), true);
54 return true;
55 case EVENT_CONFIRM_DISCARD_ALARM:
56 AlertWindow alert =
57 Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
58 // XXX localize
59 alert.setMessage("Permanently discard \u201c" +
60 ((Alarm)getFocusedItem()).getMessage() +
61 "\u201d?");
62 alert.show();
63 return true;
64 case EVENT_DISCARD_ALARM:
65 Alarms.removeAlarm((Alarm)getFocusedItem());
66 return true;
67 case Event.EVENT_TIME_CHANGED:
68 if (mToolTipWindow == null || !mToolTipWindow.isVisible())
69 return true;
70 case Event.EVENT_ALARM: // tooltip invalid
71 showToolTip();
72 return true;
73 }
74 return super.receiveEvent(e);
75 }
76
77 protected void itemActivated(Object item) {
78 if (item == null)
79 return;
80 alarmSetDialog().editAlarm((Alarm)item, false);
81 }
82
83 protected void itemFocused(Object item) {
84 mToolTipAlarm.deactivate();
85 }
86
87 public void loseFocus() {
88 mToolTipAlarm.deactivate();
89 super.loseFocus();
90 }
91
92 public void showToolTip() {
93 Alarm alarm = (Alarm)getFocusedItem();
94 if (alarm == null)
95 return;
96 Rect rect = new Rect();
97 rect = localToGlobal(getInterestingRect(rect));
98 if (mToolTipWindow != null)
99 mToolTipWindow.hide();
100 mToolTipWindow =
101 ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
102 alarm.getIntervalString(),
103 10, rect.bottom, 1, sAlarmListWindow);
104 int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
105 if (secondsUntilUpdate == 0)
106 return;
107 mToolTipAlarm.resetWake(secondsUntilUpdate);
108 }
109
110 public int alarmsSet() {
111 return getListSize();
112 }
113 public void onItemAdded(ActiveList list, Object item, int index) {
114 sAlarmListWindow.updateAlarmCount();
115 super.onItemAdded(list, item, index);
116 // super's default behavior is to preserve the existing selection,
117 // which is admirable in general, but not what we want
118 setFocus(index);
119 setHasBorder(true);
120 }
121 public void onItemRemoved(ActiveList list, Object item, int index) {
122 ((Alarm)item).cancel();
123 sAlarmListWindow.updateAlarmCount();
124 super.onItemRemoved(list, item, index);
125 if (alarmsSet() == 0)
126 setHasBorder(false);
127 }
128}
Note: See TracBrowser for help on using the repository browser.