📄 fakecustomitem.java
字号:
// paint content: this.contentX = x; this.contentY = y; paintContent( x, y, leftBorder, rightBorder, g ); } /** * Initialises this item. * * @param firstLineWidth the maximum width of the first line * @param lineWidth the maximum width of any following lines */ protected void init( int firstLineWidth, int lineWidth ) { //#debug System.out.println("intialising item " + this.getClass().getName() ); if (this.style != null && !this.isStyleInitialised) { setStyle( this.style ); } //#ifdef polish.useDynamicStyles else if (this.style == null) { initStyle(); } //#else else if (this.style == null && !this.isStyleInitialised) { //#debug System.out.println("Setting default style for item " + getClass().getName() ); setStyle( StyleSheet.defaultStyle ); } //#endif int labelWidth = 0; int labelHeight = 0; if (this.label != null) { if (!this.label.isInitialised) { this.label.init( firstLineWidth, lineWidth ); } labelWidth = this.label.itemWidth; labelHeight = this.label.itemHeight; } // calculate content width and content height: int noneContentWidth = this.marginLeft + this.borderWidth + this.paddingLeft + this.paddingRight + this.borderWidth + this.marginRight; //#ifdef polish.useBeforeStyle noneContentWidth += this.beforeWidth; //#endif //#ifdef polish.useAfterStyle noneContentWidth += this.afterWidth; //#endif /* if (noneContentWidth >= firstLineWidth) { System.out.println("INVALID NONE CONTENT WIDTH=" + noneContentWidth); } */ //#ifdef polish.css.max-width int firstLineAdjustedWidth = firstLineWidth; int lineAdjustedWidth = lineWidth; if (this.maximumWidth != 0 ) { if (firstLineAdjustedWidth > this.maximumWidth ) { firstLineAdjustedWidth = this.maximumWidth; } if (lineAdjustedWidth > this.maximumWidth ) { lineAdjustedWidth = this.maximumWidth; } } int firstLineContentWidth = firstLineAdjustedWidth - noneContentWidth; int availableContentWidth = lineAdjustedWidth - noneContentWidth; //#else //# int firstLineContentWidth = firstLineWidth - noneContentWidth; //# int availableContentWidth = lineWidth - noneContentWidth; //#endif // initialise content by subclass: initContent( firstLineContentWidth, availableContentWidth ); if (this.contentWidth == 0 && this.contentHeight == 0) { this.itemWidth = labelWidth; this.itemHeight = labelHeight; this.backgroundHeight = 0; this.backgroundWidth = 0; this.isInitialised = true; return; } this.itemWidth = noneContentWidth + this.contentWidth; //#ifdef polish.css.min-width if (this.itemWidth < this.minimumWidth ) { this.itemWidth = this.minimumWidth; } //#endif int cHeight = this.contentHeight; //#ifdef polish.useBeforeStyle if (this.contentHeight < this.beforeHeight) { cHeight = this.beforeHeight; } //#endif //#ifdef polish.useAfterStyle if (this.contentHeight < this.afterHeight) { cHeight = this.afterHeight; } //#endif int noneContentHeight = this.marginTop + this.borderWidth + this.paddingTop + this.paddingBottom + this.borderWidth + this.marginBottom; if (this.itemWidth + labelWidth <= lineWidth) { // label and content fit on one row: this.itemWidth += labelWidth; this.useSingleRow = true; if (this.label != null) { if ( (this.label.layout & LAYOUT_NEWLINE_AFTER) != 0 ) { this.useSingleRow = false; cHeight += labelHeight; } } if ( cHeight + noneContentHeight < labelHeight ) { cHeight = labelHeight - noneContentHeight; } } else { this.useSingleRow = false; cHeight += labelHeight; } if ( this.isLayoutExpand ) { this.itemWidth = lineWidth; //#ifdef polish.css.max-width if (this.maximumWidth != 0 && lineWidth > this.maximumWidth ) { this.itemWidth = this.maximumWidth; } //#endif } else if (this.itemWidth > lineWidth) { this.itemWidth = lineWidth; } if (cHeight + noneContentHeight < this.minimumHeight) { cHeight = this.minimumHeight - noneContentHeight; } //#if polish.css.max-height if (this.maximumHeight != 0 && cHeight + noneContentHeight > this.maximumHeight) { cHeight = this.maximumHeight - noneContentHeight; } //#endif this.itemHeight = cHeight + noneContentHeight; if (this.useSingleRow) { this.backgroundWidth = this.itemWidth - this.marginLeft - this.marginRight - labelWidth; this.backgroundHeight = cHeight + noneContentHeight - this.marginTop - this.marginBottom; } else { this.backgroundWidth = this.itemWidth - this.marginLeft - this.marginRight; this.backgroundHeight = cHeight + noneContentHeight - this.marginTop - this.marginBottom - labelHeight; } this.isInitialised = true; } //#ifdef polish.useDynamicStyles /** * Initialises the appropriate style for this item. */ protected void initStyle() { //System.out.println("item [" + this.getClass().getName() + "/" + this.cssSelector + "/" + this.hashCode() + "] has been initalised: " + this.isStyleInitialised ); if (this.screen == null) { if (this.parent != null) { this.screen = getScreen(); } else { this.screen = StyleSheet.currentScreen; } } if (this.style == null) { this.cssSelector = createCssSelector(); //#debug System.out.println("getting style for item [" + this.cssSelector + "]."); //setStyle( StyleSheet.getStyle( this ) ); } else { //System.out.println("item has already style [" + this.style.name + "]."); this.cssSelector = this.style.name; } this.isStyleInitialised = true; } //#endif /** * Initialises this item. * The implementation needs to calculate and set the contentWidth and * contentHeight fields. * The implementation should take the fields preferredWidth and preferredHeight * into account. * * * @param firstLineWidth the maximum width of the first line * @param lineWidth the maximum width of any following lines * @see #contentWidth * @see #contentHeight * @see #preferredWidth * @see #preferredHeight */ protected abstract void initContent(int firstLineWidth, int lineWidth); /** * Paints the content of this item. * The background has already been painted and the border will be added after * this method returns. * * @param x the left start position * @param y the upper start position * @param leftBorder the left border, nothing must be painted left of this position * @param rightBorder the right border, nothing must be painted right of this position * @param g the Graphics on which this item should be painted. */ protected abstract void paintContent( int x, int y, int leftBorder, int rightBorder, Graphics g ); //#ifdef polish.useDynamicStyles /** * Retrieves the CSS selector for this item. * The CSS selector is used for the dynamic assignment of styles - * that is the styles are assigned by the usage of the item and * not by a predefined style-name. * With the #style preprocessing command styles are set fix, this method * yields in a faster GUI and is recommended. When in a style-sheet * dynamic styles are used, e.g. "Form>p", than the selector of the * item is needed. * <br/> * This abstract method needs only be implemented, when dynamic styles * are used: #ifdef polish.useDynamicStyles * <br/> * The returned selector needs to be in lower case. * * @return the appropriate CSS selector for this item. * The selector needs to be in lower case. */ protected abstract String createCssSelector(); //#endif /** * Handles the key-pressed event. * Please note, that implementation should first try to handle the * given key-code, before the game-action is processed. * * The default implementation just handles the FIRE game-action * when a default-command and an item-command-listener have been * registered. * * @param keyCode the code of the pressed key, e.g. Canvas.KEY_NUM2 * @param gameAction the corresponding game-action, e.g. Canvas.UP * @return true when the key has been handled / recognized */ protected boolean handleKeyPressed( int keyCode, int gameAction ) { if ((gameAction == Canvas.FIRE) && (this.defaultCommand != null) && (this.itemCommandListener != null)) { //this.itemCommandListener.commandAction(this.defaultCommand, this); return true; } return false; } /** * Handles the key-repeated event. * Please note, that implementation should first try to handle the * given key-code, before the game-action is processed. * * The default implementation forwards the event to the handleKeyPressed method. * * @param keyCode the code of the pressed key, e.g. Canvas.KEY_NUM2 * @param gameAction the corresponding game-action, e.g. Canvas.UP * @return true when the key has been handled / recognized * @see #handleKeyPressed(int, int) */ protected boolean handleKeyRepeated( int keyCode, int gameAction ) { return handleKeyPressed(keyCode, gameAction); } /** * Handles the key-released event. * Please note, that implementation should first try to handle the * given key-code, before the game-action is processed. * * The default does nothing. * * @param keyCode the code of the pressed key, e.g. Canvas.KEY_NUM2 * @param gameAction the corresponding game-action, e.g. Canvas.UP * @return true when the key has been handled / recognized * @see #handleKeyPressed(int, int) */ protected boolean handleKeyReleased( int keyCode, int gameAction ) { return false; } //#ifdef polish.hasPointerEvents /** * Handles the event when a pointer has been pressed at the specified position. * The default method translates the pointer-event into an artificial * pressing of the FIRE game-action, which is subsequently handled * bu the handleKeyPressed(-1, Canvas.FIRE) method. * This method needs should be overwritten only when the "polish.hasPointerEvents" * preprocessing symbol is defined: "//#ifdef polish.hasPointerEvents". * * @param x the x position of the pointer pressing * @param y the y position of the pointer pressing * @return true when the pressing of the pointer was actually handled by this item. */ protected boolean handlePointerPressed( int x, int y ) { return handleKeyPressed( -1, Canvas.FIRE ); } //#endif /** * Animates this item. * Subclasses can override this method to create animations. * The default implementation just animates the background if present. * * @return true when this item has been animated. */ public boolean animate() { if (this.background != null) { return this.background.animate(); } return false; } /** * Focuses this item. * * @param newStyle the style which is used to indicate the focused state * @param direction the direction from which this item is focused, * either Canvas.UP, Canvas.DOWN, Canvas.LEFT, Canvas.RIGHT or 0. * When 0 is given, the direction is unknown. * @return the current style of this item */ protected Style focus( Style newStyle, int direction ) { Style oldStyle = this.style; if (this.focusedStyle != null) { newStyle = this.focusedStyle; } setStyle( newStyle ); this.isFocused = true; // now set any commands of this item: if (this.commands != null) { Screen scr = getScreen(); if (scr != null) { //scr.setItemCommands(this); } } // when an item is focused, it usually grows bigger, so // increase the bottom position a bit: if (this.yTopPos != this.yBottomPos) { this.yBottomPos += 5; this.itemHeight += 5; } if (oldStyle == null) { oldStyle = StyleSheet.defaultStyle; } return oldStyle; } /** * Removes the focus from this item. * * @param originalStyle the original style which will be restored. */ protected void defocus( Style originalStyle ) { this.backgroundYOffset = 0; if (originalStyle != null) { setStyle( originalStyle ); } else { this.background = null; this.border = null; this.style = null; } this.isFocused = false; // now remove any commands which are associated with this item: if (this.commands != null) { Screen scr = getScreen(); if (scr != null) { //scr.removeItemCommands(this); } } } /** * Called by the system to notify the item that it is now at least * partially visible, when it previously had been completely invisible. * The item may receive <code>paint()</code> calls after * <code>showNotify()</code> has been called. * * <p>The default implementation of this method does nothing.</p> */ protected void showNotify() { //default implementation does nothing } /** * Called by the system to notify the item that it is now completely * invisible, when it previously had been at least partially visible. No * further <code>paint()</code> calls will be made on this item * until after a <code>showNotify()</code> has been called again. * * <p>The default implementation of this method does nothing.</p> */ protected void hideNotify() { //default implementation does nothing } /** * Shows the screen to which item belongs to and focusses this item. * This method is the equivalent to display.setCurrentItem( item ). * * @param display the display of the MIDlet. */ public void show( Display display ) { Screen myScreen = getScreen(); if ( myScreen == null ) { //#debug warn System.out.println("Unable to show this item, since the screen is not known."); return; } //myScreen.focus( this ); display.setCurrent( myScreen ); } /** * Releases all (memory intensive) resources such as images or RGB arrays of this item. * The default implementation releases any background resources. */ public void releaseResources() { if (this.background != null) { this.background.releaseResources(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -