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

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

Better (informative, not yet pretty) date display.

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