package net.sabi.pester; import danger.app.Application; import danger.app.Event; import danger.ui.AlertWindow; import danger.ui.ActiveListView; import danger.ui.Font; import danger.ui.Pen; import danger.ui.Rect; import danger.ui.Style; import danger.ui.ToolTipWindow; import danger.util.ActiveList; import danger.util.DEBUG; public class AlarmListView extends ActiveListView implements Resources, Commands { // XXX some of these shouldn't conceptually be static private static AlarmListWindow sAlarmListWindow; private static AlarmSetDialog sAlarmSetDialog; private danger.app.Alarm mToolTipAlarm; private ToolTipWindow mToolTipWindow; public void onDecoded() { sAlarmListWindow = (AlarmListWindow)getWindow(); mToolTipAlarm = new danger.app.Alarm(0, this); setAutoResize(true); setList(Alarms.getList()); Application.registerForEvent(this, Event.EVENT_TIME_CHANGED); super.onDecoded(); } protected AlarmSetDialog alarmSetDialog() { if (sAlarmSetDialog == null) sAlarmSetDialog = AlarmSetDialog.getDialog(); return sAlarmSetDialog; } public boolean eventWidgetUp(int widget, Event e) { switch (widget) { case Event.DEVICE_BUTTON_BACK: case Event.DEVICE_BUTTON_CANCEL: Application.getCurrentApp().returnToLauncher(); return true; } return super.eventWidgetUp(widget, e); } public boolean receiveEvent(Event e) { switch (e.type) { case EVENT_NEW_ALARM: // XXX check for max alarms alarmSetDialog().editAlarm(new Alarm(), true); return true; case EVENT_DISCARD_ALARM: mToolTipAlarm.deactivate(); Alarm alarm = (Alarm)getFocusedItem(); alarm.beginEditing(); AlertWindow alert = Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this); alert.setMessage("Permanently discard \u201c" + alarm.getMessage() + "\u201d?"); // XXX localize alert.show(); return true; case EVENT_CONFIRM_DISCARD: Alarms.removeAlarm((Alarm)getFocusedItem()); return true; case EVENT_CANCEL_DISCARD: ((Alarm)getFocusedItem()).resume(); return true; // XXX for EVENT_TIME_FORMAT_CHANGED, we also need to force a // full redraw (in addition to invalidating the tooltip) case Event.EVENT_TIME_CHANGED: if (mToolTipWindow == null || !mToolTipWindow.isVisible()) return true; case Event.EVENT_ALARM: // tooltip invalid showToolTip(); return true; } return super.receiveEvent(e); } protected void itemActivated(Object item) { if (item == null) return; alarmSetDialog().editAlarm((Alarm)item, false); } protected void itemFocused(Object item) { mToolTipAlarm.deactivate(); } public void loseFocus() { mToolTipAlarm.deactivate(); super.loseFocus(); } public void showToolTip() { Alarm alarm = (Alarm)getFocusedItem(); if (alarm == null) return; Rect rect = new Rect(); rect = localToGlobal(getInterestingRect(rect)); if (mToolTipWindow != null) mToolTipWindow.hide(); mToolTipWindow = ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " + alarm.getIntervalString(), localToGlobalH(10), localToGlobalV(mToolTipY), 1, sAlarmListWindow); int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate(); if (secondsUntilUpdate == 0) return; mToolTipAlarm.resetWake(secondsUntilUpdate); } protected void paintEmptyList(Pen p, int width, int height) { Style style = getStyle(); Font font = style.getFont(Style.DISABLED_LABEL_FONT); int y = (height / 4) - (font.getAscent() / 2); if (y < 0) { setHeight(sAlarmListWindow.getHeight()); return; } // XXX localize String emptyMessage = "Press " + Font.GLYPH_MENU + " to set an alarm."; int x = (width / 2) - (font.getWidth(emptyMessage) / 2); p.setFont(font); p.setColor(style.getColor(Style.DISABLED_LABEL_COLOR)); p.drawText(x, y, emptyMessage); } protected void paintItemLine(Pen p, int left, int top, int right, int bottom, boolean focused, Object item) { if (focused) { paintSelection(p, left, top, right, bottom); p.setColor(getStyle().getColor(Style.BACKGROUND_COLOR)); } else { p.setColor(getStyle().getColor(Style.FOREGROUND_COLOR)); } Font font = p.getFont(); int descent = font.getDescent(); int height = descent + font.getAscent(); top = ((top + bottom) / 2 + height / 2) - descent; Alarm alarm = (Alarm)item; left += 2; left += p.drawText(left, top, alarm.getDateTimeString(true)); p.setFont(font.findVariant(null, -1, Font.F_BOLD)); // XXX ellipsize p.drawText(left, top, " " + alarm.getMessage()); p.setFont(font); } public int alarmsSet() { return getListSize(); } public void onItemAdded(ActiveList list, Object item, int index) { sAlarmListWindow.updateAlarmCount(); super.onItemAdded(list, item, index); // super's default behavior is to preserve the existing selection, // which is admirable in general, but not what we want setFocus(index); } public void onItemRemoved(ActiveList list, Object item, int index) { ((Alarm)item).cancel(); sAlarmListWindow.updateAlarmCount(); super.onItemRemoved(list, item, index); } }