📄 datefield.java
字号:
Screen returnScreen; /** * The selected time element */ int timeSel = 0; /** * Flag to signal whether a select action transfers focus or not * (false by default) */ private static final boolean SELECT_TRANSFERS_FOCUS = false; /** * Special command to go "back" from the editor to the DateField */ Command Back = new Command( Resource.getString("Back"), Command.BACK, 0); /** * Special command to "ok" the changes done in the editor */ Command OK = new Command (Resource.getString("Save"), Command.OK, 1); /** * Flag to format am/pm string */ private boolean ampmAfterTime = Resource.isAMPMafterTime(); /** * The width and height of the editor */ private int width, height; /** * The initial highlight */ private int highlight; /** * The last day of the month */ private int lastDay; /** * The day offset */ private int dayOffset; /** * Create a new EditScreen * * @param returnScreen The Screen to return to from the editor * @param field The DateField this EditScreen is editing */ EditScreen(Screen returnScreen, DateField field) { super(field.getLabel()); this.returnScreen = returnScreen; this.field = field; this.calendar = Calendar.getInstance(); addCommand(OK); addCommand(Back); setCommandListener(this); } /** * Handle a command action * * @param cmd The Command to handle * @param s The Displayable with the Command */ public void commandAction(Command cmd, Displayable s) { Form form = null; Item item = null; synchronized (Display.LCDUILock) { if (cmd == OK) { field.saveDate(calendar.getTime()); item = field; form = (Form)item.getOwner(); } currentDisplay.setCurrent(returnScreen); } // synchronized // SYNC NOTE: Move the call to the application's // ItemStateChangedListener outside the lock if (form != null) { form.itemStateChanged(item); } } /** * Set the current date and time * * @param currentValue The Date to set the editor to * @param mode The operating mode of the DateField */ void setDateTime(Date currentValue, int mode) { calendar.setTime(currentValue); this.mode = mode; if (CLOCK_USES_AM_PM && !ampmAfterTime) { timeSel = Calendar.AM_PM; } else { timeSel = Calendar.HOUR; } // highlight = calendar.get(Calendar.DATE); highlight = -1; // highlight the year to start with this.callRepaint(); // Call Screen.callRepaint() } /** * notify this editor it is being shown on the given Display * * @param d the Display showing this Form */ void callShowNotify(Display d) { super.callShowNotify(d); layout(); setDayOffset(); lastDay = daysInMonth(calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR)); } /** * Paint the content of this editor * * @param g The Graphics object to paint to * @param target the target Object of this repaint */ void callPaint(Graphics g, Object target) { super.callPaint(g, target); g.translate(viewport[X], viewport[Y] + 5); if (mode == TIME) { paintClock(g); } else { paintCalendar(g); } g.translate(-viewport[X], -(viewport[Y] + 5)); } /** * Layout the content of this editor given the width/height */ void layout() { super.layout(); this.width = viewport[WIDTH]; this.height = viewport[HEIGHT]; } /** * Initialize the highlight of this editor * * @param vpY * @param vpH * @return int Always returns 0 */ int initHilight(int vpY, int vpH) { if (mode == DATE) { int hilightBottom = highlightY(true); if (hilightBottom > vpH) { return (hilightBottom - vpH); } } return 0; } /** * Handle a key press * * @param keyCode the key which was pressed */ void callKeyPressed(int keyCode) { int gameAction = Display.getGameAction(keyCode); switch (gameAction) { case Canvas.FIRE: selectFired(); break; case Canvas.UP: case Canvas.DOWN: case Canvas.LEFT: case Canvas.RIGHT: if (mode == DATE) { traverseDate(gameAction, bounds[Y], bounds[Y] + bounds[HEIGHT]); } else { traverseClock(gameAction, bounds[Y], bounds[Y] + bounds[HEIGHT]); } this.callRepaint(); // Call Screen.callRepaint() break; } } /** * Handle a key repeat * * @param keyCode the key which was repeated */ void callKeyRepeated(int keyCode) { int gameAction = Display.getGameAction(keyCode); switch (gameAction) { case Canvas.UP: case Canvas.DOWN: case Canvas.LEFT: case Canvas.RIGHT: if (mode == DATE) { traverseDate(gameAction, bounds[Y], bounds[Y] + bounds[HEIGHT]); } else { traverseClock(gameAction, bounds[Y], bounds[Y] + bounds[HEIGHT]); } this.callRepaint(); // Call Screen.callRepaint() } } /** * Handle a selection */ void selectFired() { if (!SELECT_TRANSFERS_FOCUS) { return; } synchronized (Display.LCDUILock) { if (mode == DATE) { if (highlight > 0) { highlight = -1; } else if (highlight == -1) { highlight = 0; } else { highlight = calendar.get(Calendar.DATE); } if (highlight > 0) { calendar.set(Calendar.DATE, highlight); } } else { if (timeSel == Calendar.MINUTE) { timeSel = Calendar.HOUR; } else { timeSel = Calendar.MINUTE; } } this.callRepaint(); // Call Screen.callRepaint() } // synchronized } /** * Paint the clock * * @param g The Graphics to paint to */ void paintClock(Graphics g) { int hour = calendar.get(Calendar.HOUR) % 12; int minute = calendar.get(Calendar.MINUTE); g.setColor(Display.ERASE_COLOR); g.fillRect(0, 0, width, height); int digits_height = large.getHeight() + ARROW_UP.getHeight() + ARROW_DOWN.getHeight() + 2; int clockSize = height - digits_height; if (width < clockSize) { clockSize = width; } if (60 < clockSize) { clockSize = 60; } // For reference, the above if statements are replacing // this Math() call below. // Math.min(60, Math.min(width, height - digits_height)); g.translate((width - clockSize) / 2, (height - (clockSize + digits_height)) / 2); g.setColor(Display.FG_COLOR); g.drawRoundRect(0, 0, clockSize, clockSize, clockSize / 2, clockSize / 2); g.drawLine(clockSize / 2, 0, clockSize / 2, 5); g.drawLine(clockSize / 2, clockSize, clockSize / 2, clockSize - 5); g.drawLine(0, clockSize / 2, 5, clockSize / 2); g.drawLine(clockSize, clockSize / 2, clockSize - 5, clockSize / 2); int minuteAngle = 90 - (minute * 6); int hourAngle = 90 - (hour * 30 + (minute / 2)); g.translate(clockSize / 2, clockSize / 2); g.drawLine(0, 0, (cos(hourAngle)*clockSize / 4) >> 16, -(sin(hourAngle)*clockSize / 4) >> 16); g.drawLine(0, 0, (cos(minuteAngle)*(clockSize / 2 - 10)) >> 16, -(sin(minuteAngle)*(clockSize / 2 - 10)) >> 16); g.translate(0, clockSize / 2 + 2 + ARROW_UP.getHeight()); g.setFont(large); if (CLOCK_USES_AM_PM) { String ampm = Resource.getString(ampmString(calendar)); if (hour == 0) { hour = 12; } String timeString; if (ampmAfterTime) { timeString = twoDigits(hour) + ":" + twoDigits(minute) + " " + ampm; } else { timeString = ampm + " " + twoDigits(hour) + ":" + twoDigits(minute); } g.translate(-large.stringWidth(timeString) / 2, 0); g.drawString(timeString, 0, 0, Graphics.LEFT | Graphics.TOP); int dX; int w; int h = large.getBaselinePosition() + 1; int offset; int len; if (ampmAfterTime) { if (timeSel == Calendar.HOUR) { offset = 0; len = 2; } else if (timeSel == Calendar.MINUTE) { offset = 3; len = 2; } else { offset = 6; len = timeString.length() - offset; } } else { offset = ampm.length(); if (timeSel == Calendar.HOUR) { offset += 1; len = 2; } else if (timeSel == Calendar.MINUTE) { offset += 4; len = 2; } else { len = offset; offset = 0; } } dX = large.substringWidth(timeString, 0, offset); w = large.substringWidth(timeString, offset, len); g.fillRect(dX, 1, w, h -1); g.setColor(Display.FG_H_COLOR); g.drawSubstring(timeString, offset, len, dX, 0, Graphics.LEFT | Graphics.TOP); if (ampmAfterTime) { if (timeSel != Calendar.HOUR) { g.drawImage(ARROW_LEFT, -1, h / 2 + 2, Graphics.RIGHT | Graphics.VCENTER); } if (timeSel != Calendar.AM_PM) { g.drawImage(ARROW_RIGHT, large.stringWidth(timeString) + 1, h / 2 + 2, Graphics.LEFT | Graphics.VCENTER); } } else { if (timeSel != Calendar.AM_PM) { g.drawImage(ARROW_LEFT, -1, h / 2 + 2, Graphics.RIGHT | Graphics.VCENTER); } if (timeSel != Calendar.MINUTE) { g.drawImage(ARROW_RIGHT, large.stringWidth(timeString) + 1, h / 2 + 2, Graphics.LEFT | Graphics.VCENTER); } } g.drawImage(ARROW_UP, dX + w / 2, 0, Graphics.HCENTER | Graphics.BOTTOM); g.drawImage(ARROW_DOWN, dX + w / 2, h + 1, Graphics.HCENTER | Graphics.TOP); } else { g.drawString(":", 0, 0, Graphics.LEFT | Graphics.TOP); hour = calendar.get(Calendar.HOUR_OF_DAY); String str = hour + ""; int w = large.stringWidth(str); int h = large.getBaselinePosition() + 1; g.translate(-1, 0); if (timeSel == Calendar.HOUR) { g.setColor(Display.BG_H_COLOR); g.fillRect(-w, 1, w + 1, h - 1); g.setColor(Display.FG_H_COLOR); g.drawImage(ARROW_UP, -w / 2, 0, Graphics.HCENTER | Graphics.BOTTOM); g.drawImage(ARROW_DOWN, -w / 2, h + 1, Graphics.HCENTER | Graphics.TOP); } else { g.setColor(Display.FG_COLOR); g.drawImage(ARROW_LEFT, -(w + 1), h / 2, Graphics.RIGHT | Graphics.VCENTER); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -