⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choicetextfield.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					String currentText = getString();
					if ( (currentText != null) ) {
						if (this.appendDelimiterIndex != -1 && this.appendDelimiterIndex < currentText.length() ) {
							currentText = currentText.substring( 0, this.appendDelimiterIndex );
						}
						if ( choiceText.startsWith( currentText) ) {
							if (this.appendChoiceDelimiter != null ) {
								choiceText += this.appendChoiceDelimiter;
							}
						} else {
							if (this.appendChoiceDelimiter == null) {
								choiceText = currentText + choiceText; 
							} else {
								if ( this.choiceTriggerEnabled) {
									choiceText = currentText + choiceText + this.appendChoiceDelimiter;
								} 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) {
			// open all available choices:
			if (this.choiceTriggerEnabled && !this.choiceTriggerAllowInputBeforeTrigger) {
				String currentText = getString();
				if (currentText == null || currentText.length() == 0 || currentText.charAt( currentText.length() -1 ) != this.choiceTrigger) {
					return super.handleKeyPressed(keyCode, gameAction);
				}

			}
			//if (this.numberOfMatches == 0) {
				if (this.choices == null) {
					return super.handleKeyPressed(keyCode, gameAction);
				}
				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.Key.ReturnKey:defined
		//# } else if (this.isOpen
				//#= && (keyCode == ${polish.Key.ReturnKey})
				//# ) {
			//# openChoices(false);
			//# return true;
		//#endif
		}
		return super.handleKeyPressed(keyCode, gameAction);
	}
	
	//#ifdef polish.hasPointerEvents
	//# /* (non-Javadoc)
	 //# * @see de.enough.polish.ui.Item#handlePointerPressed(int, int)
	 //# */
	//# protected boolean handlePointerPressed(int x, int y) {
		//# boolean handled = super.handlePointerPressed(x, y);
		//# if (!handled && this.isOpen) {
			//# handled = this.choicesContainer.handlePointerPressed(x, y - (this.choicesContainer.relativeY + this.contentY) );
			//# if (handled) {
				//# // select the current element:
				//# this.isInChoice = true;
				//# handleKeyPressed( 0, Canvas.FIRE );
			//# } else {
				//# openChoices( false );
				//# handled = true;
			//# }
		//# }
		//# return handled;
	//# }
	//#endif

	private void enterChoices( boolean enter ) {
		//#debug
		//# System.out.println("enter choices: " + enter + ", have been entered already: " + this.isInChoice);
		if (enter) {
			this.choicesContainer.focus(0);
			setStyle( this.originalStyle );
			//#if polish.usePolishGui && !polish.LibraryBuild
				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  && !polish.LibraryBuild
					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
		//# System.out.println("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
					//# System.out.println("choicesHeight " + choicesHeight + ", choicesBottom=" + choicesBottomY + ", parent.height=" + parentContainer.availableHeight  );
					int parentYOffset = parentContainer.getScrollYOffset();
					int overlap = choicesBottomY - (parentContainer.getContentScrollHeight() - (this.relativeY + parentYOffset));
					//System.out.println("overlap=" + overlap );
					if (overlap > 0) {
						// try to scroll up this item, so that the user sees all matches:
						int yOffsetAdjustment = Math.min( this.relativeY + parentYOffset, overlap );
						this.choicesYOffsetAdjustment = yOffsetAdjustment;
						//#debug
						//# System.out.println("Adjusting yOffset of parent by " + yOffsetAdjustment );
						parentContainer.setScrollYOffset( parentYOffset - yOffsetAdjustment, true );
						//System.out.println("choice.itemHeight=" + this.choicesContainer.itemHeight + ", parentContainer.availableHeight=" + parentContainer.availableHeight + ", (this.contentY + this.contentHeight + this.paddingVertical)=" + (this.contentY + this.contentHeight + this.paddingVertical) + ", children.relativeY=" + this.choicesContainer.relativeY );
						//TODO this needs some finetuning!
						int itHeight = this.itemHeight;
						int ctHeight = this.contentY + this.contentHeight + this.paddingVertical;
						int max = Math.max( itHeight, ctHeight);
						this.choicesContainer.setScrollHeight( parentContainer.getContentScrollHeight()  - max );
					} else {
						this.choicesYOffsetAdjustment = 0;
					}
				}
			}			
		} else {
			this.choicesContainer.clear();
			if (this.choicesYOffsetAdjustment != 0 && this.parent instanceof Container) {
				Container parentContainer = (Container) this.parent;
				parentContainer.setScrollYOffset( parentContainer.getScrollYOffset() + this.choicesYOffsetAdjustment, true );
				this.choicesYOffsetAdjustment = 0;
			}
		}
		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) {
		super.paintContent(x, y, leftBorder, rightBorder, g);
		if ( this.isFocused && this.numberOfMatches > 0 ) {
			// paint containert
			y += this.contentHeight + this.paddingVertical;
			this.choicesContainer.paint(x, y, leftBorder, rightBorder, g);			
		}
	}
	

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#notifyStateChanged()
	 */
	public void notifyStateChanged() {
		Screen scr = getScreen();
		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) {
						int caretPosition = getCaretPosition();
						if (caretPosition < currentText.length() && caretPosition != -1) {
							currentText = currentText.substring( 0, caretPosition );
						}
						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 );
					}
				}
				if (this.choiceTriggerEnabled && currentText.length() > 0) {
					int lastChar = currentText.charAt( currentText.length() - 1);
					if (lastChar == this.choiceTrigger && !this.isOpen) {
						handleKeyPressed(0, Canvas.FIRE );
					} 
					return;
				}
				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
						//# System.out.println("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 + -