📄 midp2layoutview.java
字号:
//#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;/** * <code>MIDP2LayoutView</code>. * <p> * * @author Kin Wong */public class MIDP2LayoutView 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; class RowItem { int x; int y; int width; int height; Item item; } private ArrayList allRows; private ArrayList currentRow; private int rowWidth; private int rowHeight; //private Style style; private int horizontalOffset = -1; /** * Constructs an instance of <code>MIDP2LayoutView</code>. * <p> */ public MIDP2LayoutView() { // just creating a default instance } /* * (non-Javadoc) * * @see de.enough.polish.ui.ContainerView#initContent(de.enough.polish.ui.Container, * int, int) */ protected void initContent(Container parent, int firstLineWidth, int lineWidth) { Item[] myItems = parent.getItems(); this.contentHeight = this.contentWidth = this.rowWidth = this.rowHeight = 0; this.currentRow = new ArrayList(); this.allRows = new ArrayList(); boolean hasFocusableItem = false; 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); if (!hasFocusableItem) { this.appearanceMode = Item.PLAIN; } else { this.appearanceMode = Item.INTERACTIVE; } } /* protected void setStyle(Style style) { super.setStyle(style); this.style = style; } */ void appendItemToRow(int index, Item item, int firstLineWidth, int lineWidth) { 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); int layout = item.getLayout(); if ((Item.LAYOUT_NEWLINE_BEFORE == (layout & Item.LAYOUT_NEWLINE_BEFORE)) || (this.rowWidth + this.paddingHorizontal + width > lineWidth)) { // Break if the NEWLINE_BEFORE is specified or not enough // room in the current row rowBreak(lineWidth); } 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(rowItem); if (Item.LAYOUT_NEWLINE_AFTER == (layout & Item.LAYOUT_NEWLINE_AFTER)) { rowBreak(lineWidth); } } private void rowBreak(int lineWidth) { 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; int requiredExpandedIndex = 0; for (int i = 0; i < this.currentRow.size(); i++) { RowItem rowItem = (RowItem) this.currentRow.get(i); remainingWidth -= rowItem.width; if (Item.LAYOUT_EXPAND == (rowItem.item.getLayout() & Item.LAYOUT_EXPAND)) { if (requiredExpanded == null) { requiredExpanded = new RowItem[this.currentRow.size() - i]; } requiredExpanded[requiredExpandedIndex++] = rowItem; } } // 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]; 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); int rowHorizontalLayout = (rowItem.item.getLayout() & LAYOUT_HORIZONTAL); if (requiredExpanded != null) { rowHorizontalLayout = Item.LAYOUT_LEFT; } int x = 0; switch (rowHorizontalLayout) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -