📄 datefield.java
字号:
*/ int timeSel = 0; /** * Traverse the clock * * @param action * @param top * @param bottom * @return int */ int traverseClock(int action, int top, int bottom) { int hrInc = 1; switch (action) { case Canvas.LEFT: if (timeSel == Calendar.MINUTE) { timeSel = Calendar.HOUR; return 0; } else if (CLOCK_USES_AM_PM) { if (timeSel == Calendar.AM_PM) { timeSel = Calendar.MINUTE; } else if (timeSel == Calendar.HOUR) { timeSel = Calendar.AM_PM; } return 0; } return -1; case Canvas.RIGHT: if (timeSel == Calendar.HOUR) { timeSel = Calendar.MINUTE; return 0; } else if (CLOCK_USES_AM_PM) { if (timeSel == Calendar.MINUTE) { timeSel = Calendar.AM_PM; } else if (timeSel == Calendar.AM_PM) { timeSel = Calendar.HOUR; } return 0; } return -1; case Canvas.UP: if (CLOCK_USES_AM_PM && (timeSel == Calendar.AM_PM)) { hrInc = 12; } else if (timeSel == Calendar.MINUTE) { int m = calendar.get(Calendar.MINUTE); if (m == 59) { calendar.set(Calendar.MINUTE, 0); } else { calendar.set(Calendar.MINUTE, m + 1); hrInc = 0; } } calendar.set(Calendar.HOUR_OF_DAY, (hrInc + calendar.get(Calendar.HOUR_OF_DAY)) % 24); return 0; case Canvas.DOWN: if (CLOCK_USES_AM_PM && (timeSel == Calendar.AM_PM)) { hrInc = 12; } else if (timeSel == Calendar.MINUTE) { int m = calendar.get(Calendar.MINUTE); if (m == 0) { calendar.set(Calendar.MINUTE, 59); } else { calendar.set(Calendar.MINUTE, m - 1); hrInc = 0; } } hrInc = 24 - hrInc; calendar.set(Calendar.HOUR_OF_DAY, (hrInc + calendar.get(Calendar.HOUR_OF_DAY)) % 24); return 0; } return -1; } // traverseClock() /** * Traverse the Date editor * * @param action * @param top * @param bottom * @return int */ int traverseDate(int action, int top, int bottom) { int scrollHeight = 0; switch (action) { case Canvas.LEFT: if (highlight == 1) { return -1; } if (highlight > 1) { highlight--; if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { int topOfHighlight = highlightY(false); if (topOfHighlight < top) { scrollHeight = top - topOfHighlight; } } break; } int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); if (highlight == 0) { if (month > Calendar.JANUARY) { int day = calendar.get(Calendar.DATE); lastDay = daysInMonth(month - 1, year); if (day > lastDay) { calendar.set(Calendar.DATE, lastDay); } calendar.set(Calendar.MONTH, month - 1); } else { lastDay = 31; calendar.set(Calendar.MONTH, Calendar.DECEMBER); calendar.set(Calendar.YEAR, year - 1); } } else if (highlight == -1) { calendar.set(Calendar.YEAR, year - 1); lastDay = daysInMonth(month, year); } setDayOffset(); break; case Canvas.RIGHT: if (highlight == lastDay) { return -1; } if ((highlight > 0) && (highlight < lastDay)) { highlight++; if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { int bottomOfHighlight = highlightY(true); if (bottomOfHighlight > bottom) { scrollHeight = bottomOfHighlight - bottom; } } break; } year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); if (highlight == 0) { if (month < Calendar.DECEMBER) { int day = calendar.get(Calendar.DATE); lastDay = daysInMonth(month + 1, year); if (day > lastDay) { calendar.set(Calendar.DATE, lastDay); } calendar.set(Calendar.MONTH, month + 1); } else { calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.YEAR, year + 1); } } else if (highlight == -1) { calendar.set(Calendar.YEAR, year + 1); lastDay = daysInMonth(month, year); } setDayOffset(); break; case Canvas.UP: if (highlight == -1) { return -1; } if (highlight == 0) { highlight = -1; } else if (highlight <= 7) { highlight = 0; } else { highlight -= 7; } int topOfHighlight = highlightY(false); if (topOfHighlight < top) { scrollHeight = top - topOfHighlight; } break; case Canvas.DOWN: if (highlight == lastDay) { return -1; } if (highlight == -1) { highlight = 0; } else if (highlight == 0) { highlight = 1; } else if ((highlight + 7) < lastDay) { highlight += 7; } else if ((highlight + 7) > lastDay) { highlight = lastDay; } int bottomOfHighlight = highlightY(true); if (bottomOfHighlight > bottom) { scrollHeight = bottomOfHighlight - bottom; } break; default: return -1; } if (highlight > 0) { calendar.set(Calendar.DATE, highlight); } return scrollHeight; } // traverseDate() /** * The initial highlight */ private int highlight; /** * The last day of the month */ private int lastDay; /** * The day offset */ private int dayOffset; /** * Utility method to calculate the number of days * in a month * * @param month The month to use * @param year The year the month occurs in * @return int The number of days in the month */ private int daysInMonth(int month, int year) { switch (month) { case Calendar.JANUARY: case Calendar.MARCH: case Calendar.MAY: case Calendar.JULY: case Calendar.AUGUST: case Calendar.OCTOBER: case Calendar.DECEMBER: return 31; case Calendar.FEBRUARY: if (((year % 400) == 0) || (((year & 3) == 0) && ((year % 100) != 0))) { return 29; } return 28; case Calendar.APRIL: case Calendar.JUNE: case Calendar.SEPTEMBER: case Calendar.NOVEMBER: default: return 30; } } /** * The "large" Font */ Font large = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE); /** * The "regular" font */ Font regular = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); /** * The "bold" font */ Font bold = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL); /** * Set the highlight * * @param addLineHeight * @return int */ int highlightY(boolean addLineHeight) { if (highlight == -1) { return addLineHeight ? regular.getBaselinePosition() : 0; } else if (highlight == 0) { return regular.getBaselinePosition() + (addLineHeight ? regular.getHeight() : 0); } else { int line = 1 + (highlight + dayOffset - 2) / 7; return (regular.getBaselinePosition() + 1) * line + regular.getHeight() + 1 + (addLineHeight ? regular.getBaselinePosition() +2 : 0); } } /** * Paint the Calendar * * @param g The Graphics context to paint to */ void paintCalendar(Graphics g) { g.setColor(Display.ERASE_COLOR); g.fillRect(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight()); boolean currentDateOnDisplay = false; int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int selection = calendar.get(Calendar.DATE); // check if currentDate is on display int currYear = currentDate.get(Calendar.YEAR); int currMonth = currentDate.get(Calendar.MONTH); int currDate = currentDate.get(Calendar.DATE); if ((currYear == year) && (currMonth == month)) { currentDateOnDisplay = true; } g.setFont(regular); int w = regular.stringWidth("0000"); int h = regular.getBaselinePosition(); g.translate(0, -1); if (highlight == -1) { g.setColor(Display.BG_H_COLOR); g.fillRect(((width - w) / 2) - 1, 1, w + 1, h); g.setColor(Display.FG_H_COLOR); } else { g.setColor(Display.FG_COLOR); } g.drawString("" + year, width / 2, 0, Graphics.TOP | Graphics.HCENTER); g.drawImage(ARROW_LEFT, (width - w) / 2 - 2, h / 2, Graphics.VCENTER | Graphics.RIGHT); g.drawImage(ARROW_RIGHT, (width + w) / 2 + 2, h / 2, Graphics.VCENTER | Graphics.LEFT); g.translate(0, h + 1); w = regular.stringWidth(Resource.getString(MONTH_NAMES[month])); h = regular.getHeight(); if (highlight == 0) { g.setColor(Display.BG_H_COLOR); g.fillRect(((width - w) / 2) - 1, 1, w + 2, h); g.setColor(Display.FG_H_COLOR); } else { g.setColor(Display.FG_COLOR); } g.setFont(regular); g.drawString(Resource.getString(MONTH_NAMES[month]), width / 2, 0, Graphics.TOP | Graphics.HCENTER); g.drawImage(ARROW_LEFT, (width - w) / 2 - 2, h / 2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -