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

📄 relativelayoutview.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//#condition polish.usePolishGui
/*
 * @(#)MIDP2LayoutView.java
 * Created on 6/02/2005
 * Copyright 2005 by Majitek International Pte. Ltd.  All Rights Reserved.
 *
 * This software is the proprietary information of Majitek International Pte Ltd.
 * Use is subject to license terms.
 * 
 * 
 *
 * 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.containerviews;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import de.enough.polish.ui.Container;
import de.enough.polish.ui.ContainerView;
import de.enough.polish.ui.Item;
import de.enough.polish.util.ArrayList;
/**
 * <p>Layouts and positions items relatively to it's parent items.</p>
 *
 * <p>Copyright Enough Software 2007</p>
 * <pre>
 * history
 *        Apr 15, 2007 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public class RelativeLayoutView extends ContainerView {
        static private final int LAYOUT_HORIZONTAL = Item.LAYOUT_LEFT
                        | Item.LAYOUT_CENTER | Item.LAYOUT_RIGHT;

        static private final int LAYOUT_VERTICAL = Item.LAYOUT_TOP
                        | Item.LAYOUT_VCENTER | Item.LAYOUT_BOTTOM;

        private ArrayList allRows;
        private ArrayList currentRow;
        private int rowWidth;
        private int rowHeight;
        private int horizontalOffset = -1;

		private int currentContentHeight;

        /**
         * Constructs an instance of <code>RelativeLayoutView</code>.
         */
        public RelativeLayoutView() {
        	// just creating a default instance
        }

        /*
         * (non-Javadoc)
         * 
         * @see de.enough.polish.ui.ContainerView#initContent(de.enough.polish.ui.Container,int, int)
         */
        protected void initContent(Item parent, int firstLineWidth, int lineWidth) 
        {
        	Container parContainer = (Container) parent;
        	this.parentContainer = parContainer;
            Item[] myItems = parContainer.getItems();
            	//#if polish.Container.allowCycling != false
                	this.allowCycling = parContainer.allowCycling;
                //#endif
                this.contentHeight = this.contentWidth = this.rowWidth = this.rowHeight = 0;
                this.currentRow = new ArrayList();
                this.allRows = new ArrayList();

                boolean hasFocusableItem = false;
                this.currentContentHeight = 0;
                for (int i = 0; i < myItems.length; i++) {
                    Item item = myItems[i];
                    if (item.appearanceMode != Item.PLAIN) {
                            hasFocusableItem = true;
                    }
                    appendItemToRow(i, item, firstLineWidth, lineWidth);
                }
                // Make the remaining items a final line
                rowBreak(lineWidth, 0 );
                if (!hasFocusableItem) {
                	this.appearanceMode = Item.PLAIN;
                } else {
                    this.appearanceMode = Item.INTERACTIVE;
                }
        }

        void appendItemToRow(int index, Item item, int firstLineWidth, int lineWidth) {
	        	int itemLayout = item.getLayout();
	        	boolean isExpand = (itemLayout & Item.LAYOUT_EXPAND) == Item.LAYOUT_EXPAND;
	        	if (isExpand) {
	        		item.setLayout( itemLayout ^ Item.LAYOUT_EXPAND );
	        	}
                if (this.focusFirstElement && (item.appearanceMode != Item.PLAIN)) {
                        focusItem(index, item);
                        this.focusFirstElement = false;
                }
                int width = item.getItemWidth(firstLineWidth, lineWidth);
                int height = item.getItemHeight(firstLineWidth, lineWidth);
                if (isExpand) {
                	item.setLayout(itemLayout);
                }
                

                if ((Item.LAYOUT_NEWLINE_BEFORE == (itemLayout & Item.LAYOUT_NEWLINE_BEFORE) || isExpand )
                                || (this.rowWidth + this.paddingHorizontal + width > lineWidth)) 
                {
                        // Break if the NEWLINE_BEFORE is specified or not enough
                        // room in the current row
                        rowBreak(lineWidth, itemLayout);
                }

                this.rowWidth += width;
                if (this.currentRow.size() == 0) {
                    this.rowHeight = height;
                } else {
                	if (this.rowHeight < height) {
                		this.rowHeight = height;
                    }
                    this.rowWidth += this.paddingHorizontal;
                }

//                RowItem rowItem = new RowItem();
//                rowItem.width = width;
//                rowItem.height = height;
//                rowItem.item = item;
                this.currentRow.add(item);

                if (Item.LAYOUT_NEWLINE_AFTER == (itemLayout & Item.LAYOUT_NEWLINE_AFTER)) {
                        rowBreak(lineWidth, itemLayout);
                }
        }

        private void rowBreak(int lineWidth, int itemLayout) {
                if (this.currentRow.size() == 0) {
                        return; // Current row is empty
                }
                /**
                 * Horizontal Layout Starts Here!
                 */
                // Take away all the horizontal paddings
                int remainingWidth = lineWidth
                                - ((this.currentRow.size() - 1) * this.paddingHorizontal);
//                RowItem[] requiredExpanded = null;
                //Item[] requiredExpanded = null;
                int requiredExpandedIndex = 0;
                int top = this.currentContentHeight;
                int bottom = top + this.rowHeight;
                this.currentContentHeight += this.rowHeight + this.paddingVertical;
                int currentWidth = 0;
                for (int i = 0; i < this.currentRow.size(); i++) {
//                    RowItem rowItem = (RowItem) this.currentRow.get(i);
                    Item rowItem = (Item) this.currentRow.get(i);
                    rowItem.relativeY = top;
                    rowItem.relativeX = currentWidth;
                    if (Item.LAYOUT_EXPAND == (itemLayout & Item.LAYOUT_EXPAND)) {
                    	rowItem.getItemWidth( lineWidth - currentWidth, lineWidth - currentWidth );
//                        if (requiredExpanded == null) {
////                            requiredExpanded = new RowItem[this.currentRow.size() - i];
//                        	requiredExpanded = new Item[this.currentRow.size() - i];
//                        }
//                        requiredExpanded[requiredExpandedIndex++] = rowItem;
                    	
                    }
                    currentWidth += rowItem.itemWidth;
                    remainingWidth -= rowItem.itemWidth;
                }
                // Distribute the remaining width to the items that require expanding
//                if (requiredExpanded != null) {
//                    int expansion = remainingWidth / requiredExpandedIndex;
//                    remainingWidth = remainingWidth % requiredExpandedIndex;
//                    for (int i = 0; i < requiredExpandedIndex; i++) {
////                        RowItem rowItem = requiredExpanded[i];
//                    	Item rowItem = requiredExpanded[i];
//                        rowItem.width += expansion;
//                        if (i == 0) {
//                            // The first item get all the rounding
//                            rowItem.width += remainingWidth;
//                        }
//                    }
//                }

                // Horizontal Positioning determined by the first item in a row
//                RowItem rowItem = (RowItem) this.currentRow.get(0);
                Item rowItem = (Item) this.currentRow.get(0);
                int rowHorizontalLayout = (itemLayout & LAYOUT_HORIZONTAL);
//                if (requiredExpanded != null) {
//                	rowHorizontalLayout = Item.LAYOUT_LEFT;
//                }

                int x = 0;
                switch (rowHorizontalLayout) {
                    case Item.LAYOUT_CENTER :
                        x = (remainingWidth >> 1);
                        break;

                    case Item.LAYOUT_RIGHT :
                        x = remainingWidth;
                        break;
                }

                for (int i = 0; i < this.currentRow.size(); i++) {
//	                rowItem = (RowItem) this.currentRow.get(i);
//	                rowItem.x = x;
//	                x += rowItem.width + this.paddingHorizontal; // Next Item

	                /**
	                 * Vertical Layout starts here
	                 */
//	                int layout = rowItem.item.getLayout();
//	                rowItem.y = this.contentHeight;
//	                if (Item.LAYOUT_VEXPAND == (layout & Item.LAYOUT_VEXPAND)) {
//                        // Vertical expansion is required, ignore all other

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -