📄 datefield.java
字号:
if (hour >= 12) { return ((minute == 0) && (hour == 12)) ? "noon" : "PM"; } else { return ((minute == 0) && (hour == 00)) ? "mid." : "AM"; } } /** * Get the day of the week text given a Calendar * * @param calendar The Calendar object to retrieve the date from * @return String The day of the week text based on the date in the * calendar */ static String dayOfWeekString(Calendar calendar) { String str; switch (calendar.get(Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY: str = "Sun"; break; case Calendar.MONDAY: str = "Mon"; break; case Calendar.TUESDAY: str = "Tue"; break; case Calendar.WEDNESDAY: str = "Wed"; break; case Calendar.THURSDAY: str = "Thu"; break; case Calendar.FRIDAY: str = "Fri"; break; case Calendar.SATURDAY: str = "Sat"; break; default: str = Integer.toString(calendar.get(Calendar.DAY_OF_WEEK)); } return str; } /** * Translate the mode of a DateField into a readable string * * @param mode The mode to translate * @return String A human readable string representing the mode of the * DateField */ String toString(int mode) { if (mode == DATE) { if (!initialized) { return Resource.getString("<date>"); } return Resource.getDateString( dayOfWeekString(currentDate), twoDigits(currentDate.get(Calendar.DATE)), MONTH_NAMES[currentDate.get(Calendar.MONTH)].substring(0, 3), Integer.toString(currentDate.get(Calendar.YEAR))); } else if (mode == TIME) { if (!initialized) { return Resource.getString("<time>"); } if (CLOCK_USES_AM_PM) { return Resource.getTimeString( twoDigits(currentDate.get(Calendar.HOUR)), twoDigits(currentDate.get(Calendar.MINUTE)), twoDigits(currentDate.get(Calendar.SECOND)), ampmString(currentDate)); } else { return Resource.getTimeString( twoDigits(currentDate.get(Calendar.HOUR_OF_DAY)), twoDigits(currentDate.get(Calendar.MINUTE)), twoDigits(currentDate.get(Calendar.SECOND)), null); } } else { if (!initialized) { return Resource.getString("<date/time>"); } return Resource.getDateTimeString( dayOfWeekString(currentDate), twoDigits(currentDate.get(Calendar.DATE)), MONTH_NAMES[currentDate.get(Calendar.MONTH)].substring(0, 3), Integer.toString(currentDate.get(Calendar.YEAR)), twoDigits(currentDate.get(Calendar.HOUR_OF_DAY)), twoDigits(currentDate.get(Calendar.MINUTE)), twoDigits(currentDate.get(Calendar.SECOND)), ampmString(currentDate)); } } /** * Get the height of this DateField * * @return int The height of this DateField */ int getHeight() { return height; } /** * Set the allowable width of this DateField * * @param width The allowable width of this DateField * @return int The height of this DateField based on the * allowable width */ int setWidth(int width) { height = 0; if (layouts != null) { for (int i = 0; i < layouts.length; i++) { if (layouts[i] != null) { height += layouts[i].setWidth(width); } } } return height; } /** * Initialize the highlight of this DateField * * @param vpY * @param vpH * @return int Always returns 0; */ int initHilight(int vpY, int vpH) { // Label has focus only if it is taller than the viewPort if (layouts[0].getHeight() > vpH) { highlight = 0; } else { highlight = 1; } return 0; } /** * Perform a selection on this DateField * * @return boolean Always returns false */ boolean select() { Screen returnScreen = getOwner(); if (editor == null) { editor = new EditScreen(returnScreen, this); } switch (mode) { case DATE: if (!initialized) { currentDate.set(Calendar.HOUR, 0); currentDate.set(Calendar.MINUTE, 0); currentDate.set(Calendar.SECOND, 0); currentDate.set(Calendar.MILLISECOND, 0); } editor.setDateTime(currentDate.getTime(), DATE); break; case TIME: editor.setDateTime(initialized ? currentDate.getTime() : EPOCH, TIME); break; case DATE_TIME: editor.setDateTime(currentDate.getTime(), (highlight < 2) ? DATE : TIME); } editor.layoutChanged(); returnScreen.currentDisplay.setCurrent(editor); return false; } /** * Traverse this DateField * * @param dir * @param top * @param bottom * @param traversingIn * @param allowTraverseOut * @return int */ int traverse(int dir, int top, int bottom, boolean traversingIn, boolean allowTraverseOut) { int s = Form.traverse(dir == Canvas.DOWN, top, bottom, getHeight(), Screen.CONTENT_HEIGHT, traversingIn, takesFocus()); int labelHeight = layouts[0].getHeight(); // NOTE: We assume that each Date and Time are // represented each by one line // Traversing into this item (focus of that item was set to false) if (traversingIn) { if (dir == Canvas.DOWN) { highlight = labelHeight > (bottom - top) ? 0 : 1; } else { highlight = (mode == DATE_TIME ? 2 : 1); } return s; } // Item already has focus (further traversal) if (dir == Canvas.DOWN) { if (highlight == 0) { // currently label is shown // after scrolling by height = s is done // portion of Date or Time (depending on the mode) // will be displayed and it should be highlighted if (labelHeight < bottom + s) { highlight = 1; } } else if (highlight == 1) { // currently first field has focus int endOfField = labelHeight + layouts[1].getHeight(); // possibly only portion of this field was shown if (endOfField > bottom) { s = endOfField - bottom; } else if (mode == DATE_TIME) { highlight = 2; // possibly no scrolling is needed to transfer focus if (s < 0) { s = 0; } } } } else { if (highlight == 2) { highlight = 1; // possibly no scrolling is needed to transfer focus if (s < 0 || (top < labelHeight)) { s = 0; } } else if (highlight == 1) { // after scrolling by height = s is done // only label will be displayed if (bottom - s < labelHeight) { highlight = 0; } } } if (s == 0 && highlight != 0) { repaint(0, labelHeight, Display.WIDTH, height - labelHeight); } return s; } /** * Static zero epoch Date - used as a default value for * uninitialized DateFields with TIME mode. */ static final java.util.Date EPOCH = new java.util.Date(0); // REMIND: Needs to be localized? /** * Static array holding the names of the 12 months */ static final String MONTH_NAMES[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; /** * table of trigonometric functions, in 16.16 fixed point */ static final int TRIG_TABLE[] = { 65535, // cos 0 65525, // cos 1 65495, // cos 2 65445, // cos 3 65375, // cos 4 65285, // cos 5 65175, // cos 6 65046, // cos 7 64897, // cos 8 64728, // cos 9 64539, // cos 10 64330, // cos 11 64102, // cos 12 63855, // cos 13 63588, // cos 14 63301, // cos 15 62996, // cos 16 62671, // cos 17 62327, // cos 18 61964, // cos 19 61582, // cos 20 61182, // cos 21 60762, // cos 22 60325, // cos 23 59869, // cos 24 59394, // cos 25 58902, // cos 26 58392, // cos 27 57863, // cos 28 57318, // cos 29 56754, // cos 30 56174, // cos 31 55576, // cos 32 54962, // cos 33 54330, // cos 34 53683, // cos 35 53018, // cos 36 52338, // cos 37 51642, // cos 38 50930, // cos 39 50202, // cos 40 49459, // cos 41 48701, // cos 42 47929, // cos 43 47141, // cos 44 46340, // cos 45 45524, // cos 46 44694, // cos 47 43851, // cos 48 42994, // cos 49 42125, // cos 50 41242, // cos 51 40347, // cos 52 39439, // cos 53 38520, // cos 54 37589, // cos 55 36646, // cos 56 35692, // cos 57 34728, // cos 58 33753, // cos 59 32767, // cos 60 31771, // cos 61 30766, // cos 62 29752, // cos 63 28728, // cos 64 27696, // cos 65 26655, // cos 66 25606, // cos 67 24549, // cos 68 23485, // cos 69 22414, // cos 70 21336, // cos 71 20251, // cos 72 19160, // cos 73 18063, // cos 74 16961, // cos 75 15854, // cos 76 14742, // cos 77 13625, // cos 78 12504, // cos 79 11380, // cos 80 10251, // cos 81 9120, // cos 82 7986, // cos 83 6850, // cos 84 5711, // cos 85 4571, // cos 86 3429, // cos 87 2287, // cos 88 1143, // cos 89 0 // cos 90 }; /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -