📄 choicetextfield.java
字号:
String currentText = getString();
if ( (currentText != null) && (currentText.length() > 1) ) {
if (this.appendDelimiterIndex != -1 && this.appendDelimiterIndex < currentText.length() ) {
currentText = currentText.substring( 0, this.appendDelimiterIndex );
}
if (this.appendChoiceDelimiter == null) {
choiceText = currentText + choiceText;
} else {
if ( currentText.endsWith( this.appendChoiceDelimiter ) ) {
choiceText = currentText + choiceText + this.appendChoiceDelimiter;
} else {
choiceText = currentText + this.appendChoiceDelimiter + choiceText + this.appendChoiceDelimiter;
}
}
} else if (this.appendChoiceDelimiter != null) {
choiceText += this.appendChoiceDelimiter;
}
this.appendDelimiterIndex = choiceText.length();
}
if (!this.isAllowFreeTextEntry) {
this.lastMatchingText = choiceText;
}
//#if polish.usePolishGui
setString( choiceText );
setCaretPosition( choiceText.length() );
//#endif
this.numberOfMatches = 0;
openChoices( false );
super.notifyStateChanged();
}
return true;
} else if ( (gameAction == Canvas.DOWN && keyCode != Canvas.KEY_NUM8)
&& this.numberOfMatches > 0) {
//System.out.println("focusing choices container");
enterChoices( true );
return true;
} else if (gameAction == Canvas.FIRE && keyCode != Canvas.KEY_NUM5) {
//if (this.numberOfMatches == 0) {
if (this.choices == null) {
//#if polish.usePolishGui
return super.handleKeyPressed(keyCode, gameAction);
//#else
//# return true;
//#endif
}
if (this.numberOfMatches == this.choices.length) {
this.numberOfMatches = 0; // close choices container
openChoices( false );
} else {
this.appendDelimiterIndex = -1;
// if (!this.isAllowFreeTextEntry) {
//
// }
this.choicesContainer.clear();
for (int i = 0; i < this.choices.length; i++) {
Item item = this.choiceItems[i];
if (item == null) {
// create new ChoiceItem (lazy initialisation)
item = new ChoiceItem( this.choices[i], null, Choice.IMPLICIT, this.choiceItemStyle );
}
this.choicesContainer.add( item );
}
this.numberOfMatches = this.choicesContainer.size();
openChoices( true );
}
//}
return true;
}
//#if polish.usePolishGui
return super.handleKeyPressed(keyCode, gameAction);
//#else
//# return true;
//#endif
}
private void enterChoices( boolean enter ) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.ChoiceTextField", 392, "enter choices: " + enter + ", have been entered already: ", this.isInChoice);
if (enter) {
this.choicesContainer.focus(0);
setStyle( this.originalStyle );
//#if polish.usePolishGui
this.flashCaret = false;
this.showCaret = false;
if (!this.isInChoice) {
getScreen().removeItemCommands( this );
}
//#endif
//#if polish.blackberry
//# PolishEditField field = (PolishEditField) this._bbField;
//# field.processKeyEvents = false;
//#endif
} else {
setStyle( this.focusingStyle );
this.flashCaret = this.reenableCaretFlashing;
this.showCaret = true;
this.choicesContainer.yOffset = 0;
this.choicesContainer.targetYOffset = 0;
// move focus to TextField input again
this.choicesContainer.defocus( this.originalStyle );
if (this.isInChoice) {
//#if polish.usePolishGui
getScreen().setItemCommands( this );
//#endif
}
//#if polish.blackberry
//# PolishEditField field = (PolishEditField) this._bbField;
//# field.processKeyEvents = true;
//#endif
}
this.isInChoice = enter;
}
private void openChoices( boolean open ) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.ChoiceTextField", 430, "open choices: " + open + ", have been opened already:", this.isOpen);
this.choicesContainer.focus( -1 );
if (open) {
if (this.parent instanceof Container) {
Container parentContainer = (Container) this.parent;
if ( parentContainer.enableScrolling ) {
int availableWidth = this.itemWidth - (this.marginLeft + this.marginRight);
int choicesHeight = this.choicesContainer.getItemHeight( availableWidth, availableWidth );
int choicesBottomY = this.contentY + this.contentHeight + this.paddingVertical + choicesHeight;
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.ChoiceTextField", 440, "choicesHeight " + choicesHeight + ", choicesBottom=" + choicesBottomY + ", parent.yBottom=", parentContainer.yBottom );
if ( choicesBottomY > parentContainer.yBottom ) {
// try to scroll up this item, so that the user sees all matches:
int yOffsetAdjustment = Math.min( this.yTopPos - parentContainer.yTop, choicesBottomY - parentContainer.yBottom );
this.choicesYOffsetAdjustment += yOffsetAdjustment;
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.ChoiceTextField", 446, "Adjusting yOffset of parent by ", yOffsetAdjustment );
parentContainer.targetYOffset -= yOffsetAdjustment;
//#if polish.scroll-mode
//# if (!parentContainer.scrollSmooth) {
//# parentContainer.yOffset = parentContainer.targetYOffset;
//# }
//#endif
} else {
int newYOffsetAdjustment = Math.max( this.choicesYOffsetAdjustment - ( parentContainer.yBottom - choicesBottomY), 0 );
int difference = this.choicesYOffsetAdjustment - newYOffsetAdjustment;
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.ChoiceTextField", 457, "Re-Adjusting yOffset of parent from " + this.choicesYOffsetAdjustment + " over " + difference + " to ", (this.choicesYOffsetAdjustment - difference ) );
this.choicesYOffsetAdjustment -= difference;
parentContainer.targetYOffset += difference;
//#if polish.scroll-mode
//# if (!parentContainer.scrollSmooth) {
//# parentContainer.yOffset = parentContainer.targetYOffset;
//# }
//#endif
}
}
}
} else {
this.choicesContainer.clear();
if (this.parent instanceof Container) {
Container parentContainer = (Container) this.parent;
parentContainer.targetYOffset += this.choicesYOffsetAdjustment;
this.choicesYOffsetAdjustment = 0;
//#if polish.scroll-mode
//# if (!parentContainer.scrollSmooth) {
//# parentContainer.yOffset = parentContainer.targetYOffset;
//# }
//#endif
}
}
this.isOpen = open;
}
/* (non-Javadoc)
* @see de.enough.polish.ui.TextField#paintContent(int, int, int, int, javax.microedition.lcdui.Graphics)
*/
public void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
//#if polish.usePolishGui
super.paintContent(x, y, leftBorder, rightBorder, g);
//#endif
if ( this.isFocused && this.numberOfMatches > 0 ) {
// paint containert
y += this.contentHeight + this.paddingVertical;
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipWidth = g.getClipWidth();
int clipHeight = g.getClipHeight();
this.choicesContainer.setVerticalDimensions( y, clipY + clipHeight );
g.clipRect( clipX, y, clipWidth, clipY - y + clipHeight);
this.choicesContainer.paint(x, y, leftBorder, rightBorder, g);
g.setClip( clipX, clipY, clipWidth, clipHeight );
}
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#notifyStateChanged()
*/
public void notifyStateChanged() {
//#if polish.usePolishGui
Screen scr = getScreen();
//#else
//# Screen scr = null;
//#endif
if (scr != null && scr instanceof Form && ((Form)scr).itemStateListener != null ) {
// let the external item state listener do the work
super.notifyStateChanged();
} else {
// find out possible matches yourself:
if ( this.lowerCaseChoices == null ) {
return; // no choices are known
}
if (this.isOpen) {
this.choicesContainer.focus(-1);
}
String currentText = getString();
if (currentText != null) {
if (this.isAppendMode) {
if (this.appendChoiceDelimiter != null) {
this.appendDelimiterIndex = TextUtil.lastIndexOf( currentText, this.appendChoiceDelimiter );
if (this.appendDelimiterIndex != -1) {
currentText = currentText.substring( this.appendDelimiterIndex + 1 );
}
} else if (this.appendDelimiterIndex != -1 && this.appendDelimiterIndex < currentText.length()) {
currentText = currentText.substring( this.appendDelimiterIndex );
}
}
currentText = currentText.toLowerCase();
// cycle through available choices and add the ones resulting in matches.
// There is one special case, though: when only one of the available choices
// can be used (=no free text entry alllowed), we need to ensure that there is at least one match, before updating
// the choicesContainer:
if (this.isAllowFreeTextEntry) {
this.choicesContainer.clear();
}
int foundMatches = 0;
for (int i = 0; i < this.lowerCaseChoices.length; i++) {
String choice = this.lowerCaseChoices[i];
if ( matches( currentText, choice ) ) {
// found a match!
foundMatches++;
Item item = this.choiceItems[i];
if (item == null) {
// create new ChoiceItem (lazy initialisation)
item = new ChoiceItem( this.choices[i], null, Choice.IMPLICIT, this.choiceItemStyle );
}
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.ChoiceTextField", 557, "found match: ", choice);
this.choicesContainer.add( item );
}
}
// handle case when there are no matches, but only matches are allowed as the input:
if ( this.isAllowFreeTextEntry ) {
this.numberOfMatches = foundMatches;
} else {
if ( foundMatches == 0 ) {
// re-set the text to the last match:
setString( this.lastMatchingText );
} else {
// remove all previous matches and remember this text:
this.lastMatchingText = getString();
for ( int i = this.numberOfMatches; --i >= 0; ) {
System.out.println("size before removal=" + this.choicesContainer.size() );
this.choicesContainer.remove( 0 );
System.out.println("size after removal=" + this.choicesContainer.size() );
}
this.numberOfMatches = foundMatches;
}
}
}
openChoices( this.numberOfMatches > 0 );
}
}
/**
* Checks if the input and the available choice do match.
*
* @param currentText the current input of the user
* @param choice one of the available choices
* @return true when they match - this depends on this chosen matching, usually the start need to be equal
* @see #setMatchMode(int)
*/
private boolean matches(String currentText, String choice) {
if (this.matchMode == MATCH_STARTS_WITH) {
return choice.startsWith( currentText );
} else {
return choice.indexOf(currentText) != -1;
}
}
/* (non-Javadoc)
* @see de.enough.polish.ui.TextField#setStyle(de.enough.polish.ui.Style)
*/
public void setStyle(Style style) {
//#if polish.usePolishGui
super.setStyle(style);
//#endif
//#ifdef polish.css.textfield-caret-flash
//# Boolean flashCursorBool = style.getBooleanProperty( 85 );
//# if ( flashCursorBool != null ) {
//# this.reenableCaretFlashing = flashCursorBool.booleanValue();
//# }
//#endif
//#if polish.css.choicetextfield-containerstyle
//# Style containerstyle = (Style) style.getObjectProperty(142);
//# if (containerstyle != null) {
//# this.choicesContainer.setStyle( containerstyle );
//# }
//#endif
//#if polish.css.choicetextfield-choicestyle
//# Style choicestyle = (Style) style.getObjectProperty(143);
//# if (choicestyle != null) {
//# this.choiceItemStyle = choicestyle;
//# }
//#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -