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
RevLine 
[237]1package net.sabi.pester;
2
3import danger.app.Application;
4import danger.app.Event;
[260]5import danger.ui.AlertWindow;
[237]6import danger.ui.ActiveListView;
[273]7import danger.ui.Rect;
8import danger.ui.ToolTipWindow;
[237]9import danger.util.ActiveList;
[259]10import danger.util.DEBUG;
[237]11
12public class AlarmListView extends ActiveListView
13 implements Resources, Commands {
[250]14
15 // XXX some of these shouldn't conceptually be static
[237]16 private static AlarmListWindow sAlarmListWindow;
[250]17 private static AlarmSetDialog sAlarmSetDialog;
[237]18
[273]19 private danger.app.Alarm mToolTipAlarm;
20 private ToolTipWindow mToolTipWindow;
21
[237]22 public void onDecoded() {
23 sAlarmListWindow = (AlarmListWindow)getWindow();
[276]24 mToolTipAlarm = new danger.app.Alarm(0, this);
[274]25 setAutoResize(true);
[250]26 setList(Alarms.getList());
[276]27 if (alarmsSet() == 0)
28 setHasBorder(false); // this doesn't work in the rsrc file
[275]29 Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
[237]30 super.onDecoded();
31 }
32
[250]33 protected AlarmSetDialog alarmSetDialog() {
34 if (sAlarmSetDialog == null)
35 sAlarmSetDialog = AlarmSetDialog.getDialog();
36 return sAlarmSetDialog;
37 }
38
[237]39 public boolean eventWidgetUp(int widget, Event e) {
40 switch (widget) {
41 case Event.DEVICE_BUTTON_BACK:
42 case Event.DEVICE_BUTTON_CANCEL:
[250]43 Application.getCurrentApp().returnToLauncher();
[237]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:
[250]52 alarmSetDialog().editAlarm(new Alarm(), true);
[237]53 return true;
[260]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;
[237]63 case EVENT_DISCARD_ALARM:
[250]64 Alarms.removeAlarm((Alarm)getFocusedItem());
[237]65 return true;
[276]66 case Event.EVENT_TIME_CHANGED: // XXX EVENT_TIME_FORMAT_CHANGED too
[275]67 if (mToolTipWindow == null || !mToolTipWindow.isVisible())
68 return true;
[273]69 case Event.EVENT_ALARM: // tooltip invalid
70 showToolTip();
71 return true;
[237]72 }
73 return super.receiveEvent(e);
74 }
75
76 protected void itemActivated(Object item) {
[250]77 if (item == null)
78 return;
79 alarmSetDialog().editAlarm((Alarm)item, false);
[237]80 }
81
[273]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
[237]109 public int alarmsSet() {
110 return getListSize();
111 }
112 public void onItemAdded(ActiveList list, Object item, int index) {
113 sAlarmListWindow.updateAlarmCount();
[259]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
[250]117 setFocus(index);
[274]118 setHasBorder(true);
[237]119 }
120 public void onItemRemoved(ActiveList list, Object item, int index) {
[242]121 ((Alarm)item).cancel();
[237]122 sAlarmListWindow.updateAlarmCount();
123 super.onItemRemoved(list, item, index);
[274]124 if (alarmsSet() == 0)
125 setHasBorder(false);
[237]126 }
127}
Note: See TracBrowser for help on using the repository browser.