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

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

More UI.

File size: 6.9 KB
Line 
1package net.sabi.pester;
2
3import danger.app.Application;
4import danger.app.Event;
5import danger.internal.Date;
6import danger.ui.ActiveListView;
7import danger.ui.Button;
8import danger.ui.CheckBox;
9import danger.ui.DateTimeEditor;
10import danger.ui.DatePicker;
11import danger.ui.DialogWindow;
12import danger.ui.NumberEditor;
13import danger.ui.PopupMenu;
14import danger.ui.RadioButton;
15import danger.ui.Rect;
16import danger.ui.TypeAheadTextField;
17import danger.util.ActiveList;
18import danger.util.StdActiveList;
19import danger.util.DEBUG;
20
21public class AlarmListView extends ActiveListView
22 implements Resources, Commands {
23 private static Pester sPester;
24 private static AlarmListWindow sAlarmListWindow;
25 // XXX some of these shouldn't conceptually be static, but who cares
26 private static StdActiveList sAlarmList;
27 private static Alarms sAlarms;
28
29 private static DialogWindow sEditWindow;
30 private TypeAheadTextField sMessageField;
31 private RadioButton sInButton, sAtButton;
32 private NumberEditor sPeriodField;
33 private PopupMenu sPeriodUnitsPopup;
34 private CheckBox sRepeatCheckBox;
35 private DateTimeEditor sTimeEditor, sDateEditor;
36 private DatePicker sDatePicker;
37 private Button sDiscardAlarmButton, sSetAlarmButton;
38
39 private static Alarm sFocusedAlarm, sEditingAlarm;
40
41 private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
42
43 public void onDecoded() {
44 sPester = (Pester)Application.getCurrentApp();
45 sAlarmListWindow = (AlarmListWindow)getWindow();
46 sAlarmList = new StdActiveList();
47 setList(sAlarmList);
48
49 sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
50 sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
51 sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
52 sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
53 sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
54 sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
55 sPeriodField = (NumberEditor)sEditWindow.getDescendantWithID(ID_PERIOD_FIELD);
56 sPeriodUnitsPopup = (PopupMenu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_POPUP);
57 sRepeatCheckBox = (CheckBox)sEditWindow.getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
58 sTimeEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_TIME_EDITOR);
59 sDateEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_DATE_EDITOR);
60 sDatePicker = (DatePicker)sEditWindow.getDescendantWithID(ID_DATE_PICKER);
61 sSetAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_SET_ALARM_BUTTON);
62 sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
63 sInButton.setValue(1);
64 sDateEditor.limitToDangerEpoch();
65
66 super.onDecoded();
67 }
68
69 public boolean eventWidgetUp(int widget, Event e) {
70 switch (widget) {
71 case Event.DEVICE_BUTTON_BACK:
72 case Event.DEVICE_BUTTON_CANCEL:
73 sPester.returnToLauncher();
74 return true;
75 }
76 return super.eventWidgetUp(widget, e);
77 }
78
79 protected void editDateOrPeriod(Alarm alarm) {
80 boolean usesPeriod = alarm.getUsesPeriod();
81 if (usesPeriod) {
82 int period = alarm.getPeriod();
83 int unitsPerSec = 1;
84 DEBUG.p("period is " + period);
85 for (int i = sPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
86 unitsPerSec =
87 sPeriodUnitsPopup.getIDOfItem(sPeriodUnitsPopup.getItem(i));
88 DEBUG.p("unitsPerSec = " + unitsPerSec);
89 if (period % unitsPerSec == 0) break;
90 }
91 sPeriodField.setValue(period / unitsPerSec);
92 sPeriodUnitsPopup.setValueWithItemID(unitsPerSec);
93 sRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
94 }
95
96 Date date = alarm.getDate();
97 sTimeEditor.setDate(date);
98 sDateEditor.setDate(date);
99 sDatePicker.setDate(date);
100
101 sInButton.setValue(usesPeriod ? 1 : 0);
102 sAtButton.setValue(usesPeriod ? 0 : 1);
103 }
104
105 protected void editAlarm(Alarm alarm, boolean asNew) {
106 sEditingAlarm = alarm;
107 alarm.beginEditing();
108 sMessageField.setText(alarm.getMessage());
109 if (asNew) {
110 sDiscardAlarmButton.hide();
111 } else {
112 sDiscardAlarmButton.show();
113 editDateOrPeriod(alarm);
114 }
115 constrainDate();
116 sEditWindow.show();
117 }
118
119 protected Date editingDate() {
120 Date date = sDateEditor.getDate();
121 date.setTime(sTimeEditor.getDate().getTime());
122 return date;
123 }
124
125 protected void constrainDate() {
126 // XXX schedule every minute (second?) if in AT mode
127 Date now = new Date();
128 sDateEditor.min(now);
129 sDatePicker.min(now);
130 sSetAlarmButton.setEnabled(editingDate().compareTo(now) > 0);
131 }
132
133 public boolean receiveEvent(Event e) {
134 switch (e.type) {
135 case EVENT_NEW_ALARM:
136 editAlarm(new Alarm(), true);
137 return true;
138 case EVENT_SET_ALARM:
139 sEditingAlarm.setMessage(sMessageField.toString());
140 if (sInButton.getValue() == 1) {
141 DEBUG.p("period: " + sPeriodField.getValue());
142 DEBUG.p(" units: " + sPeriodUnitsPopup.getValueAsItemID());
143 sEditingAlarm.setPeriod(sPeriodField.getValue() *
144 sPeriodUnitsPopup.getValueAsItemID(),
145 sRepeatCheckBox.getValue() == 1);
146 } else {
147 sEditingAlarm.setDate(editingDate());
148 }
149 if (sEditingAlarm != sFocusedAlarm) { // new alarm
150 sAlarmList.addItem(sEditingAlarm);
151 setFocusedItem(sEditingAlarm);
152 } else {
153 sEditingAlarm.update();
154 }
155 case EVENT_CANCEL_ALARM:
156 // XXX (re)schedule alarm
157 sEditingAlarm.endEditing();
158 return true;
159 case EVENT_DISCARD_ALARM:
160 // XXX confirmation
161 sAlarmList.removeItem(sFocusedAlarm);
162 return true;
163 case EVENT_IN:
164 sAtButton.setValue(0);
165 return true;
166 case EVENT_AT:
167 sInButton.setValue(0);
168 constrainDate();
169 return true;
170 case EVENT_TIME_EDITOR:
171 Date date = editingDate();
172 // XXX factor this out (it's used twice)
173 // XXX do this on exit from the field, not on change
174 if (date.compareTo(new Date()) <= 0) {
175 date.addDays(1);
176 sDatePicker.setDate(date);
177 sDateEditor.setDate(date);
178 }
179 constrainDate();
180 return true;
181 case EVENT_DATE_EDITOR:
182 sDatePicker.setDate(sDateEditor.getDate());
183 constrainDate();
184 return true;
185 case EVENT_DATE_PICKER:
186 sDateEditor.setDate((Date)e.argument);
187 constrainDate();
188 return true;
189 }
190 return super.receiveEvent(e);
191 }
192
193 protected void itemActivated(Object item) {
194 // XXX deschedule alarm
195 editAlarm(sFocusedAlarm, false);
196 }
197 protected void itemFocused(Object item) {
198 sFocusedAlarm = (Alarm)item;
199 }
200
201 public int alarmsSet() {
202 return getListSize();
203 }
204 public void onItemAdded(ActiveList list, Object item, int index) {
205 sAlarmListWindow.updateAlarmCount();
206 super.onItemAdded(list, item, index);
207 }
208 public void onItemRemoved(ActiveList list, Object item, int index) {
209 sAlarmListWindow.updateAlarmCount();
210 super.onItemRemoved(list, item, index);
211 }
212
213 protected CharSequence getToolTipForItem(Object item) {
214 if (item == null) // this is dumb, why do we get asked for a tooltip?
215 return null;
216 // XXX only set a tooltip if the message is truncated
217 return ((Alarm)item).getMessage();
218 }
219}
Note: See TracBrowser for help on using the repository browser.