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

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

Display message on empty list (no longer need to add/remove border). Don't let alarms go off while you're deciding whether to discard them. First, untested attempt at handling hard resets (waiting on advice from Danger). Very ugly rewrite of alarm alert; needs much fixing.

File size: 4.4 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.Font;
8import danger.ui.Pen;
9import danger.ui.Rect;
10import danger.ui.Style;
11import danger.ui.ToolTipWindow;
12import danger.util.ActiveList;
13import danger.util.DEBUG;
14
15public class AlarmListView extends ActiveListView
16 implements Resources, Commands {
17
18 // XXX some of these shouldn't conceptually be static
19 private static AlarmListWindow sAlarmListWindow;
20 private static AlarmSetDialog sAlarmSetDialog;
21
22 private danger.app.Alarm mToolTipAlarm;
23 private ToolTipWindow mToolTipWindow;
24
25 public void onDecoded() {
26 sAlarmListWindow = (AlarmListWindow)getWindow();
27 mToolTipAlarm = new danger.app.Alarm(0, this);
28 setAutoResize(true);
29 setList(Alarms.getList());
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 // XXX check for max alarms
54 alarmSetDialog().editAlarm(new Alarm(), true);
55 return true;
56 case EVENT_DISCARD_ALARM:
57 mToolTipAlarm.deactivate();
58 Alarm alarm = (Alarm)getFocusedItem();
59 alarm.beginEditing();
60 AlertWindow alert =
61 Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
62 alert.setMessage("Permanently discard \u201c" +
63 alarm.getMessage() + "\u201d?"); // XXX localize
64 alert.show();
65 return true;
66 case EVENT_CONFIRM_DISCARD:
67 Alarms.removeAlarm((Alarm)getFocusedItem());
68 return true;
69 case EVENT_CANCEL_DISCARD:
70 ((Alarm)getFocusedItem()).resume();
71 return true;
72 // XXX for EVENT_TIME_FORMAT_CHANGED, we also need to force a
73 // full redraw (in addition to invalidating the tooltip)
74 case Event.EVENT_TIME_CHANGED:
75 if (mToolTipWindow == null || !mToolTipWindow.isVisible())
76 return true;
77 case Event.EVENT_ALARM: // tooltip invalid
78 showToolTip();
79 return true;
80 }
81 return super.receiveEvent(e);
82 }
83
84 protected void itemActivated(Object item) {
85 if (item == null)
86 return;
87 alarmSetDialog().editAlarm((Alarm)item, false);
88 }
89
90 protected void itemFocused(Object item) {
91 mToolTipAlarm.deactivate();
92 }
93
94 public void loseFocus() {
95 mToolTipAlarm.deactivate();
96 super.loseFocus();
97 }
98
99 public void showToolTip() {
100 Alarm alarm = (Alarm)getFocusedItem();
101 if (alarm == null)
102 return;
103 Rect rect = new Rect();
104 rect = localToGlobal(getInterestingRect(rect));
105 if (mToolTipWindow != null)
106 mToolTipWindow.hide();
107 mToolTipWindow =
108 ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
109 alarm.getIntervalString(),
110 localToGlobalH(10),
111 localToGlobalV(mToolTipY),
112 1, sAlarmListWindow);
113 int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
114 if (secondsUntilUpdate == 0)
115 return;
116 mToolTipAlarm.resetWake(secondsUntilUpdate);
117 }
118
119 protected void paintEmptyList(Pen p, int width, int height) {
120 Style style = getStyle();
121 Font font = style.getFont(Style.DISABLED_LABEL_FONT);
122 int y = (height / 4) - (font.getAscent() / 2);
123 if (y < 0) {
124 setHeight(sAlarmListWindow.getHeight());
125 return;
126 }
127 // XXX localize
128 String emptyMessage =
129 "Press " + Font.GLYPH_MENU + " to set an alarm.";
130 int x = (width / 2) - (font.getWidth(emptyMessage) / 2);
131 p.setFont(font);
132 p.setColor(style.getColor(Style.DISABLED_LABEL_COLOR));
133 p.drawText(x, y, emptyMessage);
134 }
135
136 public int alarmsSet() {
137 return getListSize();
138 }
139 public void onItemAdded(ActiveList list, Object item, int index) {
140 sAlarmListWindow.updateAlarmCount();
141 super.onItemAdded(list, item, index);
142 // super's default behavior is to preserve the existing selection,
143 // which is admirable in general, but not what we want
144 setFocus(index);
145 }
146 public void onItemRemoved(ActiveList list, Object item, int index) {
147 ((Alarm)item).cancel();
148 sAlarmListWindow.updateAlarmCount();
149 super.onItemRemoved(list, item, index);
150 }
151}
Note: See TracBrowser for help on using the repository browser.