📄 datefield.java
字号:
* @see de.enough.polish.ui.Item#handleKeyPressed(int, int) */ protected synchronized boolean handleKeyPressed(int keyCode, int gameAction) { //#if polish.blackberry if (true) { return false; } //#endif //#if tmp.directInput // there are several possble situations: // backspace: delete last char, move internal focus left // left, right: move internal focus left/right // input of number: a) plausibility check, b) move internal focus right // up, down: exit editing mode, set default values. // parse time/date and so on. // check for input of numbers if ( keyCode >= Canvas.KEY_NUM0 && keyCode <= Canvas.KEY_NUM9 ) { if (this.date == null) { setDate( new Date( System.currentTimeMillis() ) ); } char c = Integer.toString( keyCode - Canvas.KEY_NUM0 ).charAt( 0 ); String newText = this.text.substring( 0, this.editIndex ) + c; if ( this.editIndex < this.text.length() -1 ) { newText += this.text.substring( this.editIndex + 1 ); } setText( newText ); moveForward(); //#ifdef polish.key.ClearKey:defined //#= } else if ( this.date != null && (gameAction == Canvas.LEFT || keyCode == ${polish.key.ClearKey}) ) { //#else } else if ( this.date != null && gameAction == Canvas.LEFT ) { //#endif moveBackward(); } else if ( this.date != null && gameAction == Canvas.RIGHT ) { moveForward(); } else if (gameAction != Canvas.FIRE){ // force check before leaving this date=-field: if (this.date != null) { moveForward(); this.currentField = 0; this.currentFieldStartIndex = 0; this.editIndex = 0; //#if polish.css.font-bitmap this.caretViewer = null; //#endif } return false; } //#else if ((gameAction == Canvas.UP && keyCode != Canvas.KEY_NUM2) || (gameAction == Canvas.DOWN && keyCode != Canvas.KEY_NUM8) || (gameAction == Canvas.LEFT && keyCode != Canvas.KEY_NUM4) || (gameAction == Canvas.RIGHT && keyCode != Canvas.KEY_NUM6) ) { return false; } showDateForm(); //#endif return true; } //#if tmp.directInput private void moveBackward() { if (this.date == null) { return; } int forwardIndex = this.editIndex; while (Character.isDigit( this.text.charAt(forwardIndex))) { forwardIndex++; if (forwardIndex == this.text.length() ) { forwardIndex = 0; break; } } int newIndex = this.editIndex -1; while (true) { if ( newIndex < 0 ) { checkField( forwardIndex ); newIndex = this.text.length() - 1; if (this.inputMode == DATE) { this.currentField = 2; } else if ( this.inputMode == TIME ) { this.currentField = 1; } else { this.currentField = 4; } } char c = this.text.charAt( newIndex ); // break when the character is a digit: if ( Character.isDigit(c) ) { break; } checkField( forwardIndex ); this.currentField--; newIndex--; } this.editIndex = newIndex; // now find the correct start-index for the current field: if (newIndex == 0) { this.currentFieldStartIndex = 0; } else { while ( Character.isDigit( this.text.charAt( newIndex )) ) { newIndex--; if (newIndex == -1) { break; } } this.currentFieldStartIndex = newIndex + 1; } //#if polish.css.font-bitmap if (this.bitMapFontViewer != null) { char editChar = this.text.charAt( this.editIndex ); BitMapFontViewer viewer = this.bitMapFont.getViewer( "" + editChar ); this.caretViewer = viewer; this.caretWidth = viewer.getWidth(); if (this.editIndex == 0) { this.caretX = 0; } else { viewer = this.bitMapFont.getViewer( this.text.substring( 0, this.editIndex )); this.caretX = viewer.getWidth(); } this.fontHeight = viewer.getFontHeight(); } //#endif } //#endif //#if tmp.directInput private void moveForward() { int newIndex = this.editIndex + 1; while (true) { if ( newIndex >= this.text.length() ) { newIndex = 0; checkField( newIndex ); this.currentField = 0; this.currentFieldStartIndex = 0; } char c = this.text.charAt( newIndex ); // break when the character is a digit: if ( Character.isDigit(c) ) { break; } // okay, we're entering a new field, so check the value of the last field now: checkField( newIndex ); newIndex++; this.currentFieldStartIndex = newIndex; this.currentField++; } this.editIndex = newIndex; //#if polish.css.font-bitmap if (this.bitMapFontViewer != null) { if (newIndex == 0) { this.caretX = 0; } else { String header = this.text.substring( 0, newIndex ); BitMapFontViewer viewer = this.bitMapFont.getViewer( header ); this.caretX = viewer.getWidth(); //System.out.println("Width of [" + header + "]=" + this.caretX ); } char editChar = this.text.charAt( newIndex ); //System.out.println("MoveForward: text[" + newIndex + "]=" + editChar ); BitMapFontViewer viewer = this.bitMapFont.getViewer( "" + editChar ); this.caretViewer = viewer; this.caretWidth = viewer.getWidth(); this.fontHeight = viewer.getFontHeight(); //System.out.println("moveForward: caretX=" + this.caretX ); } //#endif } //#endif //#if tmp.directInput private void checkField(int newIndex) { String fieldStr; if ( newIndex == 0 ) { fieldStr = this.text.substring( this.currentFieldStartIndex ); } else { fieldStr = this.text.substring( this.currentFieldStartIndex, newIndex ); } int fieldValue = Integer.parseInt( fieldStr ); // check the actual value: if (this.calendar == null) { this.calendar = Calendar.getInstance(); this.calendar.setTimeZone(this.timeZone); } this.calendar.setTime( this.date ); int calendarField = -1; if ((this.inputMode == DATE) || (this.inputMode == DATE_TIME)) { //#if (polish.DateFormat == us) || (polish.DateFormat == mdy) switch (this.currentField) { case 0: calendarField = Calendar.MONTH; break; case 1: calendarField = Calendar.DAY_OF_MONTH; break; case 2: calendarField = Calendar.YEAR; break; } //#elif (polish.DateFormat == de) || (polish.DateFormat == fr) || (polish.DateFormat == dmy) switch (this.currentField) { case 0: calendarField = Calendar.DAY_OF_MONTH; break; case 1: calendarField = Calendar.MONTH; break; case 2: calendarField = Calendar.YEAR; break; } //#else switch (this.currentField) { case 0: calendarField = Calendar.YEAR; break; case 1: calendarField = Calendar.MONTH; break; case 2: calendarField = Calendar.DAY_OF_MONTH; break; } //#endif } if ( this.inputMode == DATE_TIME ) { switch (this.currentField) { case 3: calendarField = Calendar.HOUR_OF_DAY; break; case 4: calendarField = Calendar.MINUTE; break; } } else if ( this.inputMode == TIME ) { switch (this.currentField) { case 0: calendarField = Calendar.HOUR_OF_DAY; break; case 1: calendarField = Calendar.MINUTE; break; } } if ( calendarField == -1 ) { //#debug warn System.out.println("Unable to to set field [" + this.currentField + "]: unknown index."); return; } else { switch (calendarField) { case Calendar.DAY_OF_MONTH: if ( fieldValue < 1 ) { fieldValue = 1; } else { int month = this.calendar.get( Calendar.MONTH ); int maxDay = DAYS_IN_MONTHS[ month ]; if ( fieldValue > maxDay ) { fieldValue = maxDay; } } break; case Calendar.MONTH: if (fieldValue < 1) { fieldValue = 1; } else if ( fieldValue > 12 ) { fieldValue = 12; } fieldValue--; break; case Calendar.HOUR_OF_DAY: if (fieldValue == 24) { fieldValue = 0; } else if (fieldValue > 24) { fieldValue = 23; } break; case Calendar.MINUTE: if (fieldValue > 59) { fieldValue = 59; } break; } this.calendar.set( calendarField, fieldValue ); boolean changed = false; try { Date newDate = this.calendar.getTime(); setDate( newDate ); changed = true; } catch (IllegalArgumentException e) { if (calendarField == Calendar.DAY_OF_MONTH || calendarField == Calendar.MONTH ) { int month = calendarField == Calendar.MONTH ? fieldValue : this.calendar.get( Calendar.MONTH ); int day = calendarField == Calendar.DAY_OF_MONTH ? fieldValue : this.calendar.get( Calendar.DAY_OF_MONTH ); int maxDay = DAYS_IN_MONTHS[ month ]; if ( month == 2 ) { maxDay = 28; } if ( day > maxDay ) { this.calendar.set( Calendar.DAY_OF_MONTH, maxDay ); try { setDate( this.calendar.getTime() ); changed = true; } catch ( IllegalArgumentException e2 ) { // ignore } } } //#if polish.debug.error if (!changed) { //#debug error System.out.println("Unable to set date: " + e ); } //#endif } if ( changed && ( getScreen() instanceof Form ) ) { notifyStateChanged(); } } //System.out.println("Field [" + this.currentField + "]/[Calendar." + calendarField + "] is: " + fieldStr ); } //#endif //#if !tmp.directInput /** * Shows the TextBox for entering texts. */ private void showDateForm() { if (this.midpDateField == null) { this.midpDateField = new javax.microedition.lcdui.DateField( getLabel(), this.inputMode, this.timeZone ); //#ifdef polish.Bugs.dateFieldAcceptsNoNullDate if (this.date == null) { if (this.inputMode == TIME) { this.midpDateField.setDate( new Date(0) ); } else { this.midpDateField.setDate( new Date() ); } } else { this.midpDateField.setDate( this.date ); } //#else this.midpDateField.setDate( this.date ); //#endif this.form = new javax.microedition.lcdui.Form( StyleSheet.currentScreen.getTitle() ); this.form.append( this.midpDateField ); //TODO add i18n support this.form.addCommand(StyleSheet.OK_CMD); this.form.addCommand(StyleSheet.CANCEL_CMD); /* */ //#if !tmp.directInput //# this.form.setCommandListener( this ); //#endif } this.screen = StyleSheet.currentScreen; StyleSheet.display.setCurrent( this.form ); } //#endif //#if !tmp.directInput /* (non-Javadoc) * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) */ public void commandAction(Command cmd, Displayable box) { if (cmd == StyleSheet.CANCEL_CMD) { this.midpDateField.setDate( this.date ); } else { setDate( this.midpDateField.getDate() ); notifyStateChanged(); } StyleSheet.display.setCurrent( this.screen ); } //#endif //#ifdef tmp.directInput public void setItemCommandListener(ItemCommandListener l) { this.additionalItemCommandListener = l; } //#endif //#if tmp.directInput public void commandAction(Command c, Item item) { if (c == TextField.CLEAR_CMD) { this.editIndex = 0; this.currentField = 0; this.currentFieldStartIndex = 0; setDate( null ); } else if (this.additionalItemCommandListener != null) { this.additionalItemCommandListener.commandAction(c, item); } } //#endif //#if polish.blackberry public void fieldChanged(Field field, int context) { if (context != FieldChangeListener.PROGRAMMATIC && this.isInitialised ) { setDate( new Date( this.blackberryDateField.getDate()) ); if (getScreen() instanceof Form ) { notifyStateChanged(); } } } //#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -