dateeditor.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,806 行 · 第 1/4 页
JAVA
1,806 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package javax.microedition.lcdui;import java.util.Date;import java.util.Calendar;import com.sun.midp.lcdui.*;import com.sun.midp.i18n.Resource;import com.sun.midp.i18n.ResourceConstants;import com.sun.midp.configurator.Constants;import com.sun.midp.chameleon.skins.DateEditorSkin;import com.sun.midp.chameleon.skins.ScreenSkin;import com.sun.midp.chameleon.layers.PopupLayer;import javax.microedition.lcdui.game.Sprite;import com.sun.midp.log.Logging;import com.sun.midp.log.LogChannels;/** * A utility class for editing date/time components for a DateField. */class DateEditor extends PopupLayer implements CommandListener { /** * Create a new DateEditor layer. * * @param lf The DateFieldLFImpl that triggered this date editor */ public DateEditor(DateFieldLFImpl lf) { super(DateEditorSkin.IMAGE_BG, DateEditorSkin.COLOR_BG); this.lf = lf; } /** * Initialize Date editor */ public void init() { mode = lf.df.mode; initialized = lf.df.initialized; editDate = Calendar.getInstance(); Date date = lf.df.getDate(); if (date != null) { editDate.setTime(date); } selectedDate = hilightedDate = editDate.get(Calendar.DATE); if (editDate.get(Calendar.AM_PM) == Calendar.AM) { amSelected = true; amHilighted = true; } switch (mode) { case DateField.DATE: focusOn = MONTH_POPUP; populateDateComponents(); break; case DateField.TIME: focusOn = HOURS_POPUP; timeComponentsOffset = 0; populateTimeComponents(); break; case DateField.DATE_TIME: focusOn = MONTH_POPUP; timeComponentsOffset = 98; populateDateComponents(); populateTimeComponents(); break; default: Logging.report(Logging.ERROR, LogChannels.LC_HIGHUI, "DateEditor constructor, mode=" +mode); break; } // init the bounds(used for pointer input) with invariant // relative coordinate of the uppper left corner and with invalid // width, height values dependent on skin images month_bounds = new int[] { (mode == DateField.DATE)? 10: 4, 5, 0, 0 }; year_bounds = new int[] { month_bounds[X] + 45, month_bounds[Y], 0, 0 }; hours_bounds = new int[] { timeComponentsOffset + ((mode == DateField.TIME)? 17: 0), ((mode == DateField.TIME)? 10: 5), 0, 0 }; minutes_bounds = new int[] { hours_bounds[X] + 34, hours_bounds[Y], 0, 0 }; calendar_bounds = new int[] { (mode == DateField.DATE)? 10: 4, 29, 0, 0 }; ampm_bounds = new int[] { timeComponentsOffset + ((mode == DateField.TIME)? 15: 0), 29, 0, 0 }; setCommands(commands); setCommandListener(this); sizeChanged = true; isIitialized = true; } /** * Sets the location of the popup layer. * * @param x the x-coordinate of the popup layer location * @param y the y-coordinate of the popup layer location */ public void setLocation(int x, int y) { if (!isIitialized) { init(); } bounds[X] = x; bounds[Y] = y; bounds[H] = DateEditorSkin.HEIGHT; switch (mode) { case DateField.DATE: bounds[W] = DateEditorSkin.WIDTH_DATE; break; case DateField.TIME: bounds[W] = DateEditorSkin.WIDTH_TIME; break; case DateField.DATE_TIME: bounds[W] = DateEditorSkin.WIDTH_DATETIME; break; default: Logging.report(Logging.ERROR, LogChannels.LC_HIGHUI, "DateEditor.setLocation(), mode=" +mode); break; } int w = ((ScreenLFImpl)lf.df.owner.getLF()).getDisplayableWidth(); if (bounds[X] + bounds[W] > w) { bounds[X] = w - bounds[W]; } else if (bounds[X] < 0) { bounds[X] = 0; } if (sizeChanged) { callSizeChanged(); } sizeChanged = false; } /** * Paints the background of the date editor layer. * * @param g The graphics context to paint to */ public void paintBackground(Graphics g) { super.paintBackground(g); if (DateEditorSkin.IMAGE_BG == null) { g.setColor(DateEditorSkin.COLOR_BORDER); g.drawRect(0, 0, bounds[W] - 1, bounds[H] - 1); g.setColor(0); } } /** * Paints the body (open state) of the date editor layer. * * @param g The graphics context to paint to */ public void paintBody(Graphics g) { setDayOffset(); lastDay = daysInMonth(editDate.get(Calendar.MONTH), editDate.get(Calendar.YEAR)); nextX = 0; nextY = 0; switch (mode) { case DateField.DATE: drawDateComponents(g); break; case DateField.TIME: drawTimeComponents(g); break; case DateField.DATE_TIME: drawDateComponents(g); drawTimeComponents(g); break; default: Logging.report(Logging.ERROR, LogChannels.LC_HIGHUI, "DateEditor.paintBody(), mode=" +mode); break; } } /** * Handles key input to the popup layer. * * @param type the type of this key event (pressed, released) * @param code the code of this key event * @return true always, since popupLayers swallow all key events */ public boolean keyInput(int type, int code) { if (type == EventConstants.PRESSED && lf != null) { if (code == Constants.KEYCODE_SELECT) { selectFired(); requestRepaint(); } else { traverseEditor(code); requestRepaint(); } } // PopupLayers always swallow all key events return (code != EventConstants.SOFT_BUTTON1 && code != EventConstants.SOFT_BUTTON2); } /** * Handles pointer input to the popup layer. * * @param type the type of this key event (pressed, released) * @param x x coordinate of pointer * @param y y coordinate of pointer * @return true always, since popupLayers swallow all pointer events */ public boolean pointerInput(int type, int x, int y) { boolean consume = true; switch (type) { case EventConstants.PRESSED: itemIndexWhenPressed = itemIndexAtPointerPosition(x,y); switch (itemIndexWhenPressed) { case AM_PM: amHilighted = ( x - ampm_bounds[X] < 35); break; case CALENDAR: pressedDate = getDateAtPointerPosition(x, y); if (pressedDate > 0) { hilightedDate = pressedDate; } break; case PRESS_OUT_OF_BOUNDS: commandAction(cancel, lf.df.owner); consume = false; break; } if (itemIndexWhenPressed > 0 && focusOn != itemIndexWhenPressed) { DEPopupLayer popup = null; switch (focusOn) { case MONTH_POPUP: popup = monthPopup; break; case YEAR_POPUP: popup = yearPopup; break; case HOURS_POPUP: popup = hoursPopup; break; case MINUTES_POPUP: popup = minutesPopup; break; default: break; } if (popup != null && popup.open) { popup.hide(); } focusOn = itemIndexWhenPressed; requestRepaint(); } break; case EventConstants.RELEASED: int itemIndexWhenReleased = itemIndexAtPointerPosition(x,y); if (itemIndexWhenPressed == itemIndexWhenReleased) { if (itemIndexWhenPressed > 0) { if ( (itemIndexWhenPressed == AM_PM && amHilighted == (x - ampm_bounds[X] < 35)) || (itemIndexWhenPressed == CALENDAR && pressedDate == getDateAtPointerPosition(x, y) && pressedDate > 0) || (itemIndexWhenPressed != AM_PM && itemIndexWhenPressed != CALENDAR) ) { selectFired(); if (itemIndexWhenPressed > 0) { focusOn = itemIndexWhenPressed; requestRepaint(); } } } } if (itemIndexWhenReleased == PRESS_OUT_OF_BOUNDS) { consume = false; } itemIndexWhenPressed = PRESS_OUT_OF_BOUNDS; // remember to reset the variables pressedDate = 0; break; } return consume; } /** * Helper function to determine the date index at the x,y position * * @param x pointer x coordinate * @param y pointer y coordinate * * @return 0 (invalid value) or 1 - lastDay(valid value) * depends on the pointer position. */ private int getDateAtPointerPosition(int x, int y) { int dateAt = 0; int transX = x - calendar_bounds[X]; int transY = y - calendar_bounds[Y]; int o = DateEditorSkin.IMAGE_CAL_BG.getWidth() / 7; int rowH = 11; //variable o, rowH, h is same as in paintCaalendar() int h = DateEditorSkin.IMAGE_DATES.getHeight() / 31; if (transX >= 0 && transX <= calendar_bounds[W] && transY >= 0 && transY <= calendar_bounds[H] && transY >= h + 3) { int row = (transY - h - 3) / rowH; int col = (transX - 1) / o; int row_Day1 = 0; int col_Day1 = dayOffset -1; //index from 0 if (row != row_Day1 || col >= col_Day1) { //index from 1 int dateAtPointer = (row - row_Day1) * 7 + (col - col_Day1) + 1; if (dateAtPointer <= lastDay) { dateAt = dateAtPointer; } } } return dateAt; } /** * Helper function to determine the focusable area Index at the x,y position * * @param x x pointer coordinate * @param y y pointer coordinate * @return focusable area index, can be PRESS_OUT_OF_BOUNDS, * 0, MONTH_POPUP, YEAR_POPUP, HOURS_POPUP, MINUTES_POPUP, * CALENDAR, or AM_PM, depends on the pointer position. */ private int itemIndexAtPointerPosition(int x, int y) { int area = PRESS_OUT_OF_BOUNDS; if (containsPoint(x + this.bounds[X], y + this.bounds[Y])) { if (x >= month_bounds[X] && x < month_bounds[X] + month_bounds[W] && y >= month_bounds[Y] && y < month_bounds[Y] + month_bounds[H]) { area = MONTH_POPUP; } else if (x >= year_bounds[X] && x < year_bounds[X] + year_bounds[W] && y >= year_bounds[Y] && y < year_bounds[Y] + year_bounds[H]) { area = YEAR_POPUP; } else if (x >= hours_bounds[X] && x < hours_bounds[X] + hours_bounds[W] && y >= hours_bounds[Y] && y < hours_bounds[Y] + hours_bounds[H]) { area = HOURS_POPUP; } else if (x > minutes_bounds[X] && x < minutes_bounds[X] + minutes_bounds[W] && y >= minutes_bounds[Y] && y < minutes_bounds[Y] + minutes_bounds[H]) { area = MINUTES_POPUP; } else if (x >= calendar_bounds[X] && x < calendar_bounds[X] + calendar_bounds[W] && y >= calendar_bounds[Y] && y < calendar_bounds[Y] + calendar_bounds[H]) { area = CALENDAR; } else if (x >= ampm_bounds[X] && x < ampm_bounds[X] + ampm_bounds[W] && y >= ampm_bounds[Y] && y < ampm_bounds[Y] + ampm_bounds[H]) { area = AM_PM; } else { area = 0; } } return area; // Value 0: invaliad but inside one focusable area } /** * Handle a command action. * * @param cmd The Command to handle * @param s The Displayable with the Command */ public void commandAction(Command cmd, Displayable s) { lf.uCallKeyPressed(Constants.KEYCODE_SELECT); if (cmd == set) { if (mode == DateField.TIME) { lf.saveDate(new Date(editDate.getTime().getTime() % (24*60*60*1000))); } else { lf.saveDate(editDate.getTime()); } } // SYNC NOTE: Move the call to the application's // ItemStateListener outside LCDUILock Form form = null; synchronized (Display.LCDUILock) { if (lf.df.owner instanceof Form) { form = (Form)lf.df.owner; } } if (form != null) { form.uCallItemStateChanged(lf.df); } } // ********** package private *********** // /** * Show the date editor popup.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?