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

📄 commanditem.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//#condition polish.usePolishGui
/*
 * Created on Mar 4, 2006 at 3:17:15 PM.
 * 
 * Copyright (c) 2006 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 java.io.IOException;

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


/**
 * <p>Wraps a javax.microedition.lcdui.Command object and allows to add subcommands, specific styles etc to single commands.</p>
 *
 * <p>Copyright Enough Software 2006</p>
 * <pre>
 * history
 *        Mar 4, 2006 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public class CommandItem extends IconItem {
	
	protected final Command command;
	private Container children;
	protected boolean hasChildren;
	private int childIndicatorWidth = 5;
	private int childIndicatorYOffset;
	private int childIndicatorHeight = 5;
	//#if polish.css.command-child-indicator
		//# private Image childIndicator;
	//#endif
	//#if polish.css.command-child-indicator-color
		//# private int childIndicatorColor = -1;
	//#endif
	private boolean isOpen;

	
	/**
	 * Creates a new command item.
	 * 
	 * @param command the commmand represented by this item.
	 * @param parent the parent item
	 */
	public CommandItem( Command command, Item parent ) {
		this( command, parent, null );
	}

	/**
	 * Creates a new command item.
	 * 
	 * @param command the commmand represented by this item.
	 * @param parent the parent item
	 * @param style the style for this item
	 */
	public CommandItem( Command command, Item parent, Style style ) {
		super( null, command.getLabel(), null, style );
		this.appearanceMode = Item.INTERACTIVE;
		this.command = command;
		this.parent = parent;
	}
	
	/**
	 * Adds a subcommand to this node.
	 * 
	 * @param childCommand the child command
	 */
	public void addChild( Command childCommand ) {
		addChild( childCommand, null );
	}
	
	/**
	 * Adds a subcommand to this node.
	 * 
	 * @param childCommand the child command
	 * @param childStyle the style for the child command
	 */
	public void addChild( Command childCommand, Style childStyle ) {
		CommandItem child = new CommandItem( childCommand, this, childStyle );
		addChild( child );
	}
	
	/**
	 * Adds a subcommand to this node.
	 * 
	 * @param child the child command item
	 */
	public void addChild(CommandItem child) {
		boolean inserted = false;
		if ( this.children == null ) {
			int layer = getLayer();
			if (layer == 0) {
				//#style menu1?
				this.children = new Container( true );
			} else if (layer == 1) {
				//#style menu2?
				this.children = new Container( true );
			} else if (layer == 2) {
				//#style menu3?
				this.children = new Container( true );
			}
			if (this.children == null) {
				this.children = new Container( true, this.parent.style );
			} else if (this.children.style == null) {
				this.children.style = this.parent.style;
			}
			this.hasChildren = true;
			this.children.parent = this;
		} else {
			int priority = child.command.getPriority();
			for (int i = 0; i < this.children.size(); i++) {
				CommandItem item = (CommandItem) this.children.get(i);
				if (item.command.getPriority() > priority ) {
					this.children.add( i, child );
					inserted = true;
					break;
				}
			}
		}
		if (!inserted) {
			this.children.add( child );
		}
	}

	/**
	 * Retrieves the layer to which this command item belongs.
	 * This method is useful for sub commands.
	 * 
	 * @return the layer, 0 means this command item belongs to the main commands.
	 */
	public int getLayer() {
		Item parentItem = this.parent;
		int layer = 0;
		while (parentItem != null) {
			while (! (parentItem instanceof CommandItem) ) {
				//System.out.println("getLayer - skipping parent " + parentItem );
				parentItem = parentItem.parent;
				if (parentItem == null) {
					//System.out.println("getLayer - returning because of null parent ");
					return layer;
				}
			}
			if (parentItem == null) {
				return layer;
			}
			parentItem = parentItem.parent;
			layer++;
		}
		return layer;
	}
	
	/**
	 * Removes a child from this command item.
	 * 
	 * @param childCommand the child that should be removed
	 * @return true when the child was found
	 */
	public boolean removeChild( Command childCommand ) {
		if (this.children == null) {
			return false;
		}
		for ( int i=0; i < this.children.size(); i++ ) {
			CommandItem item = (CommandItem) this.children.get(i);
			if ( item.command == childCommand ) {
				this.children.remove( i );
				return true;
			}
		}
		return false;
	}


	/* (non-Javadoc)
	 * @see de.enough.polish.ui.IconItem#initContent(int, int)
	 */
	protected void initContent(int firstLineWidth, int lineWidth) {
		if (this.hasChildren) {
			firstLineWidth -= this.childIndicatorWidth + this.paddingHorizontal;
			lineWidth -= this.childIndicatorWidth + this.paddingHorizontal;
		}
		
		super.initContent(firstLineWidth, lineWidth);
		
		if (this.hasChildren) {
			this.contentWidth += this.childIndicatorWidth + this.paddingHorizontal;
			if ( this.childIndicatorHeight > this.contentHeight ) {
				this.contentHeight = this.childIndicatorHeight;
			} else {
				this.childIndicatorYOffset = (this.contentHeight - this.childIndicatorHeight) / 2;
			}
		}
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.IconItem#paintContent(int, int, int, int, javax.microedition.lcdui.Graphics)
	 */
	public void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
		// TODO robertvirkus adjust yOffset when childIndicatorHeight > contentHeight
		super.paintContent(x, y, leftBorder, rightBorder, g);
		
//		if (this.isFocused) {
//			System.out.println( this + ": backgroundYOffset=" + this.backgroundYOffset);
//		}
		if (this.hasChildren) {
			// paint child indicator
			//rightBorder -= this.childIndicatorWidth;
			//#if polish.css.command-child-indicator
				//# if (this.childIndicator != null) {
					//# int height = this.childIndicator.getHeight(); // center image if lower than the font:
					//# g.drawImage( this.childIndicator, rightBorder, y + (this.contentHeight - height)/2, Graphics.TOP | Graphics.RIGHT );
				//# } else {
			//#endif
					//#if polish.css.command-child-indicator-color
						//# if ( this.childIndicatorColor != -1 ) {
							//# g.setColor( this.childIndicatorColor );							
						//# }

⌨️ 快捷键说明

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