📄 container.java
字号:
} //#debug System.out.println("Container(" + this + "): handleKeyReleased consumed by item " + item.getClass().getName() + "/" + item ); return true; } } return super.handleKeyReleased(keyCode, gameAction); } /* (non-Javadoc) * @see de.enough.polish.ui.Item#handleKeyRepeated(int, int) */ protected boolean handleKeyRepeated(int keyCode, int gameAction) { if (this.itemsList.size() == 0) { return false; } if (this.focusedItem != null) { Item item = this.focusedItem; if ( item.handleKeyRepeated( keyCode, gameAction ) ) { if (this.enableScrolling && item.internalX != -9999) { adjustScrolling(item); } //#debug System.out.println("Container(" + this + "): handleKeyRepeated consumed by item " + item.getClass().getName() + "/" + item ); return true; } } return super.handleKeyRepeated(keyCode, gameAction); } /** * Adjusts the scrolling for the given item. * * @param item the item for which the scrolling should be adjusted */ private void adjustScrolling(Item item) { if ( item.contentY + item.internalY + item.internalHeight > this.yBottom) { //#if polish.css.scroll-mode if (!this.scrollSmooth) { this.yOffset -= ( item.contentY + item.internalY + item.internalHeight - this.yBottom ); } else { //#endif this.targetYOffset -= ( item.contentY + item.internalY + item.internalHeight - this.yBottom ); //#if polish.css.scroll-mode } //#endif //#debug System.out.println("Container (" + getClass().getName() + "): lowered yOffset to " + this.yOffset + "/" + this.targetYOffset ); } else if ( item.contentY + item.internalY < this.yTop ) { //#if polish.css.scroll-mode if (!this.scrollSmooth) { this.yOffset += ( this.yTop - (item.contentY + item.internalY )); } else { //#endif this.targetYOffset += ( this.yTop - (item.contentY + item.internalY )); //#if polish.css.scroll-mode } //#endif //#debug System.out.println("Container (" + getClass().getName() + "): increased yOffset to " + this.yOffset + "/" + this.targetYOffset + ", yTop=" + this.yTop + ", item.class=" + item.getClass().getName() + ", item.contentY=" + item.contentY + ", item.internalY=" + item.internalY ); } } /** * Shifts the focus to the next or the previous item. * * @param forwardFocus true when the next item should be focused, false when * the previous item should be focused. * @param steps how many steps forward or backward the search for the next focusable item should be started, * 0 for the current item, negative values go backwards. * @return true when the focus could be moved to either the next or the previous item. */ private boolean shiftFocus(boolean forwardFocus, int steps ) { if ( this.items == null ) { //#debug System.out.println("shiftFocus fails: this.items==null"); return false; } //System.out.println("|"); Item focItem = this.focusedItem; //#if polish.css.colspan int i = this.focusedIndex; if (steps != 0) { //System.out.println("ShiftFocus: steps=" + steps + ", forward=" + forwardFocus); int doneSteps = 0; steps = Math.abs( steps ) + 1; Item item = this.items[i]; while( doneSteps <= steps) { doneSteps += item.colSpan; if (doneSteps >= steps) { //System.out.println("bailing out at too many steps: focusedIndex=" + this.focusedIndex + ", startIndex=" + i + ", steps=" + steps + ", doneSteps=" + doneSteps); break; } if (forwardFocus) { i++; if (i == this.items.length - 1 ) { i = this.items.length - 2; break; } else if (i == this.items.length) { i = this.items.length - 1; break; } } else { i--; if (i < 0) { i = 1; break; } } item = this.items[i]; //System.out.println("focusedIndex=" + this.focusedIndex + ", startIndex=" + i + ", steps=" + steps + ", doneSteps=" + doneSteps); } if (doneSteps >= steps && item.colSpan != 1) { if (forwardFocus) { i--; if (i < 0) { i = this.items.length - 1; } //System.out.println("forward: Adjusting startIndex to " + i ); } else { i = (i + 1) % this.items.length; //System.out.println("backward: Adjusting startIndex to " + i ); } } } //#else //# int i = this.focusedIndex + steps; if (i > this.items.length) { i = this.items.length - 2; } if (i < 0) { i = 1; } //#endif Item item = null; //#if polish.Container.allowCycling != false boolean allowCycle = this.enableScrolling && this.allowCycling; if (allowCycle) { //#if polish.css.scroll-mode if (!this.scrollSmooth) { if (forwardFocus) { // when you scroll to the bottom and // there is still space, do // scroll first before cycling to the // first item: allowCycle = (this.yOffset + this.itemHeight <= this.yBottom); // #debug // System.out.println("allowCycle-calculation ( forward non-smoothScroll): targetYOffset=" + this.targetYOffset + ", itemHeight=" + this.itemHeight + " (together="+ (this.targetYOffset + this.itemHeight) + ", yBottom=" + this.yBottom); } else { // when you scroll to the top and // there is still space, do // scroll first before cycling to the // last item: allowCycle = (this.yOffset == 0); } } else { //#endif if (forwardFocus) { // when you scroll to the bottom and // there is still space, do // scroll first before cycling to the // first item: allowCycle = (this.targetYOffset + this.itemHeight <= this.yBottom); // #debug // System.out.println("allowCycle-calculation ( forward smoothScroll): targetYOffset=" + this.targetYOffset + ", itemHeight=" + this.itemHeight + " (together="+ (this.targetYOffset + this.itemHeight) + ", yBottom=" + this.yBottom); } else { // when you scroll to the top and // there is still space, do // scroll first before cycling to the // last item: allowCycle = (this.targetYOffset == 0) || (this.targetYOffset == 1); } //#if polish.css.scroll-mode } //#endif } //#if polish.Container.allowCycling != false //#debug System.out.println("shiftFocus of " + this + ": allowCycle(locale)=" + allowCycle + ", allowCycle(global)=" + this.allowCycling + ", isFoward=" + forwardFocus + ", enableScrolling=" + this.enableScrolling + ", targetYOffset=" + this.targetYOffset + ", yOffset=" + this.yOffset + ", focusedIndex=" + this.focusedIndex + ", start=" + i ); //#endif //#endif while (true) { if (forwardFocus) { i++; if (i >= this.items.length) { //#if polish.Container.allowCycling != false if (allowCycle) { allowCycle = false; i = 0; //#debug System.out.println("allowCycle: Restarting at the beginning"); } else { break; } //#else break; //#endif } } else { i--; if (i < 0) { //#if polish.Container.allowCycling != false if (allowCycle) { allowCycle = false; i = this.items.length - 1; //#debug System.out.println("allowCycle: Restarting at the end"); } else { break; } //#else break; //#endif } } item = this.items[i]; if (item.appearanceMode != Item.PLAIN) { break; } } if (item == null || item.appearanceMode == Item.PLAIN || item == focItem) { //#debug System.out.println("got original focused item: " + (item == focItem) + ", item==null:" + (item == null) + ", mode==PLAIN:" + (item == null ? false:(item.appearanceMode == PLAIN)) ); return false; } int direction = Canvas.UP; if (forwardFocus) { direction = Canvas.DOWN; } focus(i, item, direction ); return true; } /** * Retrieves the index of the item which is currently focused. * * @return the index of the focused item, -1 when none is focused. */ public int getFocusedIndex() { return this.focusedIndex; } /** * Retrieves the currently focused item. * * @return the currently focused item, null when there is no focusable item in this container. */ public Item getFocusedItem() { return this.focusedItem; } /* (non-Javadoc) * @see de.enough.polish.ui.Item#setStyle(de.enough.polish.ui.Style) */ public void setStyle(Style style) { //#if polish.debug.debug if (this.parent == null) { //#debug System.out.println("Container.setStyle without boolean parameter for container " + toString() ); } //#endif setStyle(style, false); } /** * Sets the style of this container. * * @param style the style * @param ignoreBackground when true is given, the background and border-settings * will be ignored. */ public void setStyle( Style style, boolean ignoreBackground) { super.setStyle(style); if (ignoreBackground) { this.background = null; this.border = null; this.borderWidth = 0; this.marginTop = 0; this.marginBottom = 0; this.marginLeft = 0; this.marginRight = 0; } //#if polish.css.focused-style this.focusedTopMargin = this.focusedStyle.marginTop + this.focusedStyle.paddingTop; if (this.focusedStyle.border != null) { this.focusedTopMargin += this.focusedStyle.border.borderWidth; } if (this.focusedStyle.background != null) { this.focusedTopMargin += this.focusedStyle.background.borderWidth; } //#endif //#ifdef polish.css.view-type ContainerView viewType = (ContainerView) style.getObjectProperty("view-type");// if (this instanceof ChoiceGroup) {// System.out.println("SET.STYLE / CHOICEGROUP: found view-type (1): " + (viewType != null) + " for " + this);// } if (viewType != null) { if (this.view != null && this.view.getClass() == viewType.getClass()) { this.view.focusFirstElement = this.autoFocusEnabled; //System.out.println("SET.STYLE / CHOICEGROUP: found OLD view-type (2): " + viewType + " for " + this); } else { //System.out.println("SET.STYLE / CHOICEGROUP: found new view-type (2): " + viewType + " for " + this); try { if (viewType.parentContainer != null) { viewType = (ContainerView) viewType.getClass().newInstance(); } viewType.parentContainer = this; viewType.focusFirstElement = this.autoFocusEnabled; //#if polish.Container.allowCycling != false viewType.allowCycling = this.allowCycling; //#else viewType.allowCycling = false; //#endif this.view = viewType; } catch (Exception e) { //#debug error System.out.println("Container: Unable to init view-type " + e ); viewType = null; } } } //#endif //#ifdef polish.css.columns if (this.view == null) { Integer columns = style.getIntProperty("columns"); if (columns != null) { if (columns.intValue() > 1) { //System.out.println("Container: Using default container view for displaying table"); this.view = new ContainerView(); this.view.parentContainer = this; this.view.focusFirstElement = this.autoFocusEnabled; //#if polish.Container.allowCycling != false this.view.allowCycling = this.allowCycling; //#else this.view.allowCycling = false; //#endif } } } //#endif //#if polish.css.scroll-mode Integer scrollModeInt = style.getIntProperty("scroll-mode"); if ( scrollModeInt != null ) { this.scrollSmooth = (scrollModeInt.intValue() == SCROLL_SMOOTH); } //#endif //#ifdef tmp.supportViewType if (this.view != null) { this.view.setStyle(style); } //#endif } /** * Parses the given URL and includes the index of the item, when there is an "%INDEX%" within the given url. * @param url the resource URL which might include the substring "%INDEX%" * @param item the item to which the URL belongs to. The item must be * included in this container. * @return the URL in which the %INDEX% is substituted by the index of the * item in this container. The url "icon%INDEX%.png" is resolved * to "icon1.png" when the item is the second item in this container. * @throws NullPointerException when the given url or item is null */ public String parseIndexUrl(String url, Item item) { int pos = url.indexOf("%INDEX%"); if (pos != -1) { int index = this.itemsList.indexOf( item ); //TODO rob check if valid, when url ends with %INDEX% url = url.substring(0, pos) + index + url.substring( pos + 7 ); } return url; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -