📄 container.java
字号:
//#condition polish.usePolishGui/* * Created on 01-Mar-2004 at 09:45:32. * * Copyright (c) 2004-2005 Robert Virkus / Enough Software * * This file is part of J2ME Polish. * * J2ME Polish is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * J2ME Polish is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with J2ME Polish; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Commercial licenses are also available, please * refer to the accompanying LICENSE.txt or visit * http://www.j2mepolish.org for details. */package de.enough.polish.ui;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;import de.enough.polish.util.ArrayList;/** * <p>Contains a number of items.</p> * <p>Main purpose is to manage all items of a Form or similiar canvases.</p> * <p>Containers support following CSS attributes: * </p> * <ul> * <li><b>focused</b>: The name of the focused style, e.g. "style( funnyFocused );" * </li> * <li><b>columns</b>: The number of columns. If defined a table will be drawn.</li> * <li><b>columns-width</b>: The width of the columns. "equals" for an equal width * of each column, "normal" for a column width which depends on * the items. One can also specify the used widths directly with * a comma separated list of integers, e.g. * <pre> * columns: 2; * columns-width: 15,5; * </pre> * </li> * <li><b>scroll-mode</b>: Either "smooth" (=default) or "normal".</li> * <li><b>view-type</b>: The view of this container.</li> * </ul> * <p>Copyright Enough Software 2004, 2005</p> * <pre> * history * 01-Mar-2004 - rob creation * </pre> * @author Robert Virkus, robert@enough.de */public class Container extends Item { //#if polish.css.columns || polish.useTable //#define tmp.useTable //#endif public static final int SCROLL_DEFAULT = 0; public static final int SCROLL_SMOOTH = 1; protected ArrayList itemsList; protected Item[] items; protected boolean autoFocusEnabled; protected int autoFocusIndex; protected Style itemStyle; protected Item focusedItem; public int focusedIndex = -1; protected boolean enableScrolling; //#if polish.Container.allowCycling != false public boolean allowCycling = true; //#endif int yTop; int yBottom; protected int yOffset; protected int targetYOffset; private int focusedTopMargin; //#if polish.css.view-type || polish.css.columns //#define tmp.supportViewType protected ContainerView view; //#endif //#ifdef polish.css.scroll-mode protected boolean scrollSmooth = true; //#endif private boolean isScrollRequired; //protected boolean isFirstPaint; /** * Creates a new empty container. * * @param focusFirstElement true when the first focussable element should be focused automatically. */ public Container( boolean focusFirstElement ) { this( null, focusFirstElement, null, -1, -1 ); } /** * Creates a new empty container. * * @param focusFirstElement true when the first focussable element should be focused automatically. * @param style the style for this container */ public Container(boolean focusFirstElement, Style style) { this( null, focusFirstElement, style, -1, -1 ); } /** * Creates a new empty container. * * @param label the label of this container * @param focusFirstElement true when the first focussable element should be focused automatically. * @param style the style for this container * @param yTop the vertical start of the screen - used for scrolling. -1 when not set. * @param yBottom the vertical end of the scren - used for scrolling -1 when not set. * @see #setVerticalDimensions(int, int ) */ public Container(String label, boolean focusFirstElement, Style style, int yTop, int yBottom ) { super( label, LAYOUT_DEFAULT, INTERACTIVE, style ); this.itemsList = new ArrayList(); this.autoFocusEnabled = focusFirstElement; Style focStyle = StyleSheet.focusedStyle; //#if false // this code is needed for the JUnit tests only: if (focStyle == null) { focStyle = new Style( 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0x0, null, null, null, null, null, null ); } //#endif this.focusedStyle = focStyle; this.focusedTopMargin = focStyle.marginTop + focStyle.paddingTop; if (focStyle.border != null) { this.focusedTopMargin += focStyle.border.borderWidth; } if (focStyle.background != null) { this.focusedTopMargin += focStyle.background.borderWidth; } this.layout |= Item.LAYOUT_NEWLINE_BEFORE; setVerticalDimensions(yTop, yBottom); } /** * Sets the scrolling parameter for this container. * * @param yTop the start of the screen for this container, -1 when scrolling should not be done. * @param yBottom the end of the screen for this container, -1 when scrolling should not be done. */ public void setVerticalDimensions( int yTop, int yBottom ) { this.yTop = yTop + this.marginTop; this.yBottom = yBottom - this.marginBottom; this.enableScrolling = (yTop != -1 || yBottom != -1 ); } /** * Adds an item to this container. * * @param item the item which should be added. * @throws IllegalArgumentException when the given item is null */ public void add( Item item ) { item.yTopPos = item.yBottomPos = 0; item.internalX = -9999; item.parent = this; this.itemsList.add( item ); if (this.isInitialised) { this.isInitialised = false; repaint(); } } /** * Inserts the given item at the defined position. * Any following elements are shifted one position to the back. * * @param index the position at which the element should be inserted, * use 0 when the element should be inserted in the front of this list. * @param item the item which should be inserted * @throws IllegalArgumentException when the given item is null * @throws IndexOutOfBoundsException when the index < 0 || index >= size() */ public void add( int index, Item item ) { item.yTopPos = item.yBottomPos = 0; item.internalX = -9999; item.parent = this; this.itemsList.add( index, item ); if (index <= this.focusedIndex) { this.focusedIndex++; } if (this.isInitialised) { this.isInitialised = false; repaint(); } } /** * Replaces the item at the specified position in this list with the given item. * * @param index the position of the element, the first element has the index 0. * @param item the item which should be set * @return the replaced item * @throws IndexOutOfBoundsException when the index < 0 || index >= size() */ public Item set( int index, Item item ) { //#debug System.out.println("Container: setting item " + index + " " + item.toString() ); item.parent = this; Item last = (Item) this.itemsList.set( index, item ); if (index == this.focusedIndex) { last.defocus(this.itemStyle); if ( item.appearanceMode != PLAIN ) { this.itemStyle = item.focus( this.focusedStyle, 0 ); } } if (this.isInitialised) { this.isInitialised = false; repaint(); } return last; } /** * Returns the item at the specified position of this container. * * @param index the position of the desired item. * @return the item stored at the given position * @throws IndexOutOfBoundsException when the index < 0 || index >= size() */ public Item get( int index ) { return (Item) this.itemsList.get( index ); } /** * Removes the item at the specified position of this container. * * @param index the position of the desired item. * @return the item stored at the given position * @throws IndexOutOfBoundsException when the index < 0 || index >= size() */ public Item remove( int index ) { Item removedItem = (Item) this.itemsList.remove(index); //#debug System.out.println("Container: removing item " + index + " " + removedItem.toString() ); // adjust y-positions of following items: Item[] myItems = (Item[]) this.itemsList.toArray( new Item[ this.itemsList.size() ]); for (int i = 0; i < myItems.length; i++) { Item item = myItems[i]; item.internalX = -9999; item.yTopPos = item.yBottomPos = 0; /* int removedItemHeight = removedItem.itemHeight; if (item.yTopPos != item.yBottomPos) { item.yTopPos -= removedItemHeight; item.yBottomPos -= removedItemHeight; }*/ } // check if the currenlty focused item has been removed: if (index == this.focusedIndex) { // remove any items: Screen scr = getScreen(); if (scr != null) { scr.removeItemCommands(removedItem); } // focus the first possible item: boolean focusSet = false; for (int i = 0; i < myItems.length; i++) { Item item = myItems[i]; if (item.appearanceMode != PLAIN) { focus( i, item, Canvas.DOWN ); focusSet = true; break; } } if (!focusSet) { this.autoFocusEnabled = true; this.focusedItem = null; this.focusedIndex = -1; } } else if (index < this.focusedIndex) { this.focusedIndex--; } this.yOffset = 0; this.targetYOffset = 0; if (this.isInitialised) { this.isInitialised = false; repaint(); } return removedItem; } /** * Removes the given item. * * @param item the item which should be removed. * @return true when the item was found in this list. * @throws IllegalArgumentException when the given item is null */ public boolean remove( Item item ) { int index = this.itemsList.indexOf(item); if (index != -1) { remove( index ); return true; } else { return false; } } /** * Removes all items from this container. */ public void clear() { this.itemsList.clear(); this.items = new Item[0]; if (this.focusedIndex != -1) { this.autoFocusEnabled = this.isFocused; //#if polish.Container.clearResetsFocus != false this.autoFocusIndex = 0; //#else this.autoFocusIndex = this.focusedIndex; //#endif this.focusedIndex = -1; if (this.focusedItem != null) { if (this.focusedItem.commands != null) { Screen scr = getScreen(); if (scr != null) { scr.removeItemCommands(this.focusedItem); } } if (this.itemStyle != null) { this.focusedItem.defocus(this.itemStyle); } } this.focusedItem = null; this.autoFocusEnabled = true; } this.yOffset = 0; this.targetYOffset = 0; if (this.isInitialised) { this.isInitialised = false; //this.yBottom = this.yTop = 0; repaint(); } } /** * Retrieves the number of items stored in this container. * * @return The number of items stored in this container. */ public int size() { return this.itemsList.size(); } /** * Retrieves all items which this container holds. * The items might not have been intialised. * * @return an array of all items. */ public Item[] getItems() { if (!this.isInitialised) { return (Item[]) this.itemsList.toArray( new Item[ this.itemsList.size() ]); } else { return this.items; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -