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

📄 horizontalchoiceview.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//#condition polish.usePolishGui
/*
 * Created on 08-Apr-2005 at 11:17:51.
 * 
 * Copyright (c) 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.containerviews;

import java.io.IOException;

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

import de.enough.polish.ui.Background;
import de.enough.polish.ui.ChoiceGroup;
import de.enough.polish.ui.ChoiceItem;
import de.enough.polish.ui.Container;
import de.enough.polish.ui.ContainerView;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.Style;
import de.enough.polish.ui.StyleSheet;

//TODO implement horizontal view type:
/*
 * 1. set appropriate style when item is unselected
 * 2. xOffset, xTargetOffset
 * 3. getNextItem > modify xOffset
 * 4. Allow checkboxes
 * 
 */

/**
 * <p>Shows  the available items of an ChoiceGroup or a horizontal list.</p>
 * <p>Apply this view by specifying "view-type: horizontal-choice;" in your polish.css file.</p>
 *
 * <p>Copyright (c) 2005, 2006 Enough Software</p>
 * <pre>
 * history
 *        02-March-2006 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public class HorizontalChoiceView extends ContainerView {
	
	
	private final static int POSITION_BOTH_SIDES = 0; 
	private final static int POSITION_RIGHT = 1; 
	private final static int POSITION_LEFT = 2; 
	private final static int POSITION_NONE = 3;
	private int arrowColor;
	//#ifdef polish.css.horizontalview-left-arrow
		private Image leftArrow;
		private int leftYOffset;
	//#endif
	//#ifdef polish.css.horizontalview-right-arrow
		private Image rightArrow;
		private int rightYOffset;
	//#endif
	//#ifdef polish.css.horizontalview-arrows-image
		private Image arrowsImage;
		private int yOffset;
	//#endif 
	//#ifdef polish.css.horizontalview-arrow-position
		private int arrowPosition;
		//#ifdef polish.css.horizontalview-arrow-padding
			private int arrowPadding;
		//#endif
	//#endif
	//#ifdef polish.css.horizontalview-roundtrip
		private boolean allowRoundTrip;
	//#endif
	//#ifdef polish.css.horizontalview-expand-background
		private Background background;
		private boolean expandBackground;
	//#endif	
	private int arrowWidth = 10;
	private int currentItemIndex;
	private int leftArrowStartX;
	private int leftArrowEndX;
	private int rightArrowStartX;
	//private int rightArrowEndX;
	private int xStart;
	private Background parentBackground;
	//private boolean isInitialized;
	private int xOffset;

	/**
	 * Creates a new view
	 */
	public HorizontalChoiceView() {
		super();
	}

	/* (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) 
	{
		//#debug
		System.out.println("Initalizing ExclusiveSingleLineView");
		if (this.isFocused && this.parentBackground == null) {
			Background bg = parent.background;
			if (bg != null) {
				this.parentBackground = bg; 
				parent.background = null;
			}
			//System.out.println("EXCLUSIVE:   INIT CONTENT WITH NO PARENT BG, now parentBackround != null: " + (this.parentBackground != null));
		}
		//TODO allow no selection for MULTIPLE
		int selectedItemIndex = ((ChoiceGroup) parent).getSelectedIndex();
		if (selectedItemIndex == -1) {
			selectedItemIndex = 0;
		}
		if ( selectedItemIndex < parent.size() ) {
			parent.focus (selectedItemIndex, parent.get( selectedItemIndex ), 0);
		}
		//parent.focusedIndex = selectedItemIndex;
		int height = 0;
		//#if polish.css.horizontalview-left-arrow || polish.css.horizontalview-right-arrow
			int width = 0;
			//#ifdef polish.css.horizontalview-left-arrow
				if (this.leftArrow != null) {
					width = this.leftArrow.getWidth();
					height = this.leftArrow.getHeight();
				}
			//#endif
			//#ifdef polish.css.horizontalview-right-arrow
				if (this.rightArrow != null) {
					if ( this.rightArrow.getWidth() > width) {
						width = this.rightArrow.getWidth();
						if (this.leftArrow.getHeight() > height) {
							height = this.leftArrow.getHeight();
						}
					}
				}
			//#endif
			//#if polish.css.horizontalview-left-arrow && polish.css.horizontalview-right-arrow
				if (this.rightArrow != null && this.leftArrow != null) {
					this.arrowWidth = width;
				} else {
			//#endif
					if (width > this.arrowWidth) {
						this.arrowWidth = width;
					}
			//#if polish.css.horizontalview-left-arrow && polish.css.horizontalview-right-arrow
				}
			//#endif
		//#endif
		int completeArrowWidth;
		//#if polish.css.horizontalview-arrows-image
			if (this.arrowsImage != null) {
				height = this.arrowsImage.getHeight();
				completeArrowWidth = this.arrowsImage.getWidth();
				this.arrowWidth = completeArrowWidth / 2;
			} else {
		//#endif
				//#if polish.css.horizontalview-arrow-padding
					completeArrowWidth = ( this.arrowWidth * 2 ) + this.paddingHorizontal + this.arrowPadding;
				//#else
					completeArrowWidth = ( this.arrowWidth + this.paddingHorizontal ) << 1;
				//#endif
		//#if polish.css.horizontalview-arrows-image
			}
		//#endif
		//#ifdef polish.css.horizontalview-arrow-position
			if (this.arrowPosition == POSITION_BOTH_SIDES) {
		//#endif
				this.leftArrowStartX = 0;
				this.leftArrowEndX = this.arrowWidth;
				this.rightArrowStartX = lineWidth - this.arrowWidth;
				//this.rightArrowEndX = lineWidth;
		//#ifdef polish.css.horizontalview-arrow-position
			} else if (this.arrowPosition == POSITION_RIGHT ){
				this.leftArrowStartX = lineWidth - completeArrowWidth + this.paddingHorizontal;
				this.leftArrowEndX = this.leftArrowStartX + this.arrowWidth;
				this.rightArrowStartX = lineWidth - this.arrowWidth;
				//this.rightArrowEndX = lineWidth;
			} else if (this.arrowPosition == POSITION_RIGHT ) {
				this.leftArrowStartX = 0;
				this.leftArrowEndX = this.arrowWidth;
				this.rightArrowStartX = this.arrowWidth + this.paddingHorizontal;
				//this.rightArrowEndX = this.rightArrowStartX + this.arrowWidth;
			} else {
				completeArrowWidth = 0;
			}
		//#endif
		lineWidth -= completeArrowWidth;
		int completeWidth = 0;
		Item[] items = parent.getItems();
		for (int i = 0; i < items.length; i++) {
			Item item = items[i];
			//TODO allow drawing of boxes as well
			((ChoiceItem) item).drawBox = false;
			int itemHeight = item.getItemHeight(lineWidth, lineWidth);
			int itemWidth = item.itemWidth;
			if (itemHeight > height ) {
				height = itemHeight;
			}
			int startX = completeWidth;
			completeWidth += itemWidth + this.paddingHorizontal;
			if ( i == selectedItemIndex) {
				if ( startX + this.xOffset < 0 ) {
					this.xOffset = -startX; 
				} else if ( completeWidth + this.xOffset > lineWidth ) {
					this.xOffset = lineWidth - completeWidth;
				}
			}
		}
		this.contentHeight = height;
		this.contentWidth = lineWidth + completeArrowWidth;
		
		if (items.length > 0) {
			this.appearanceMode = Item.INTERACTIVE;
		} else {
			this.appearanceMode = Item.PLAIN;
		}
		if (selectedItemIndex < items.length ) {
			this.focusedItem = (ChoiceItem) items[ selectedItemIndex ];
			this.currentItemIndex = selectedItemIndex;
		}
		//if ( selectedItem.isFocused ) {
			//System.out.println("Exclusive Single Line View: contentHeight=" + this.contentHeight);
		//}
		//this.isInitialized = true;
		
		//#if polish.css.horizontalview-arrows-image
			if (this.arrowsImage != null) {
				int offset = (this.contentHeight - this.arrowsImage.getHeight()) / 2; // always center vertically
				this.yOffset = offset;
			}
		//#endif
		//#if polish.css.horizontalview-left-arrow			
			if (this.leftArrow != null) {
				this.leftYOffset = (this.contentHeight - this.leftArrow.getHeight()) / 2; // always center vertically
			}
		//#endif
		//#if polish.css.horizontalview-right-arrow
			if (this.rightArrow != null) {
				this.rightYOffset = (this.contentHeight - this.rightArrow.getHeight()) / 2; // always center vertically
			}
		//#endif

		
//		System.out.println("leftX=" + this.leftArrowStartX);
//		System.out.println("rightX=" + this.rightArrowStartX);
//		System.out.println("arrowColor=" + Integer.toHexString(this.arrowColor));
	}
	
	

	protected void setStyle(Style style) {
		//#ifdef polish.css.horizontalview-expand-background
			Boolean expandBackgroundBool = style.getBooleanProperty("horizontalview-expand-background");
			if (expandBackgroundBool != null) {
				this.expandBackground = expandBackgroundBool.booleanValue(); 
			}
			if (this.expandBackground) {
				this.background = style.background;				
			}
		//#endif
		super.setStyle(style);
		//#ifdef polish.css.horizontalview-arrows-image
			String arrowImageUrl = style.getProperty("horizontalview-arrows-image");
			if (arrowImageUrl != null) {
				try {
					this.arrowsImage = StyleSheet.getImage( arrowImageUrl, this, true );
				} catch (IOException e) {
					//#debug error
					System.out.println("Unable to load left arrow image [" + arrowImageUrl + "]" + e );
				}
			}
		//#endif
		//#ifdef polish.css.horizontalview-left-arrow
			String leftArrowUrl = style.getProperty("horizontalview-left-arrow");
			if (leftArrowUrl != null) {
				try {
					this.leftArrow = StyleSheet.getImage( leftArrowUrl, this, true );
				} catch (IOException e) {
					//#debug error
					System.out.println("Unable to load left arrow image [" + leftArrowUrl + "]" + e );
				}
			}
		//#endif
		//#ifdef polish.css.horizontalview-right-arrow
			String rightArrowUrl = style.getProperty("horizontalview-right-arrow");
			if (rightArrowUrl != null) {
				try {
					this.rightArrow = StyleSheet.getImage( rightArrowUrl, this, true );
				} catch (IOException e) {
					//#debug error
					System.out.println("Unable to load right arrow image [" + rightArrowUrl + "]" + e );
				}

⌨️ 快捷键说明

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