📄 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;
/**
* The <code>MIDP2LayoutView</code> layouts all items in a row instead of placing each item on a new row.
* <p>
* Apply this layout by specifying the view-type in the screen's CSS style:
* <pre>
* .myForm {
* view-type: midp2;
* }
* </pre>
* </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;
private int currentContentHeight;
/**
* 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(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;
}
}
/*
protected void setStyle(Style style) {
super.setStyle(style);
this.style = style;
}
*/
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++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -