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

📄 tabbar.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//#condition polish.usePolishGui
/*
 * Created on 23-Jan-2005 at 19:04:14.
 * 
 * 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;

import java.io.IOException;

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


/**
 * <p>Manages and paints the tabs of a tabbed form (or another Screen).</p>
 *
 * <p>Copyright (c) 2005, 2006 Enough Software</p>
 * <pre>
 * history
 *        23-Jan-2005 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public class TabBar extends Item {

	private final ImageItem[] tabs;
	//#if true
		private final Style activeStyle;
		private final Style inactiveStyle;
	//#else
		//# private Style activeStyle;
		//# private Style inactiveStyle;
	//#endif
	
	private int activeTabIndex;
	//#ifdef polish.hasPointerEvents
		//# protected int newActiveTabIndex;
	//#endif
	private int xOffset;
	private int scrollArrowHeight = 10;
	private int scrollArrowPadding = 2;
	private int scrollArrowColor = 0xFFFFFF;
	//#ifdef polish.css.tabbar-left-arrow
		//# private Image leftArrow;
	//#endif
	//#ifdef polish.css.tabbar-right-arrow
		//# private Image rightArrow;
	//#endif
	private int arrowYOffset;
	private int arrowXOffset;
	private boolean allowRoundtrip;
	private int nextTabIndex;
	

	/**
	 * Creates a new tab bar.
	 * 
	 * @param tabNames the names of the tabs
	 * @param tabImages the images of the tabs, can be null
	 */
	public TabBar(String[] tabNames, Image[] tabImages) {
		this( tabNames, tabImages, null );
	}

	/**
	 * Creates a new tab bar.
	 * 
	 * @param tabNames the names of the tabs
	 * @param tabImages the images of the tabs, can be null
	 * @param style the style of the bar
	 */
	public TabBar(String[] tabNames, Image[] tabImages, Style style) {
		super( null, 0, Item.BUTTON, style);
		if (tabImages == null) {
			tabImages = new Image[ tabNames.length ];
		} else if (tabNames == null) {
			tabNames = new String[ tabImages.length ];
		}
		// getting styles:
		//#if true
			//#style activetab, tab, default
			this.activeStyle = (StyleSheet.defaultStyle );
			//#style inactivetab, tab, default
			this.inactiveStyle = (StyleSheet.defaultStyle );
		//#endif
		
		this.tabs = new ImageItem[ tabNames.length ];
		for (int i = 0; i < tabImages.length; i++) {
			String name = tabNames[i];
			Image image = tabImages[i];
			ImageItem tab = new ImageItem( null, image, 0, name, this.inactiveStyle );
			this.tabs[i] = tab;
		}

		this.tabs[0].style = this.activeStyle;
	}
	
	/**
	 * Changes the active/selected tab.
	 * 
	 * @param index the index of the active tab, the first tab has the index 0.
	 */
	public void setActiveTab( int index ) {
		// deactivating the old tab:
		this.tabs[ this.activeTabIndex ].setStyle(this.inactiveStyle);
		// activating the new tab:
		this.tabs[ index ].setStyle(this.activeStyle);
		this.activeTabIndex = index;
		this.isInitialised = false;
	}



	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#initContent(int, int)
	 */
	protected void initContent(int firstLineWidth, int lineWidth) {
		int scrollerWidth = this.scrollArrowHeight + 2 * this.scrollArrowPadding; 
		int maxHeight = scrollerWidth;
		int completeWidth = 0;
		int rightBorder = lineWidth - scrollerWidth;
		//#if polish.css.tabbar-roundtrip
			//# if (this.allowRoundtrip || this.activeTabIndex == 0 || this.activeTabIndex == this.tabs.length -1) {
		//#else
			if (this.activeTabIndex == 0 || this.activeTabIndex == this.tabs.length -1) {
		//#endif
			// only one scroll indicator needs to be painted
			//#if polish.css.tabbar-roundtrip
				//# if (this.activeTabIndex != 0 && !this.allowRoundtrip) {
			//#else
				if (this.activeTabIndex != 0) {
			//#endif
				rightBorder = lineWidth;
			}
			lineWidth -= maxHeight;
			completeWidth = maxHeight;
		} else {
			lineWidth -= 2 * maxHeight;
			completeWidth = 2 * maxHeight;
		}
		
		int activeTabXPos = 0;
		int activeTabWidth = 0;
		for (int i = 0; i < this.tabs.length; i++) {
			ImageItem tab = this.tabs[i];
			int tabHeight = tab.getItemHeight(firstLineWidth, lineWidth);
			if (tabHeight > maxHeight ) { 
				maxHeight = tabHeight;
			}
			if (i == this.activeTabIndex) {
				activeTabXPos = completeWidth;
				activeTabWidth = tab.itemWidth;
			}
			// I can use the itemWidth field, since I have called getItemHeight(..) above,
			// which initialises the tab.
			completeWidth += tab.itemWidth;
		}
		this.contentHeight = maxHeight;
		this.contentWidth = completeWidth;
		if (this.activeTabIndex == 0) {
			this.xOffset = 0;
		} else if ( this.xOffset + activeTabXPos < scrollerWidth ) {
			// tab is too much left:
			this.xOffset = scrollerWidth - activeTabXPos;
			//System.out.println("this.xOffset + activeTabXPos < scrollerWidth ");
		} else if ( this.xOffset + activeTabXPos + activeTabWidth > rightBorder ) {
			// tab is too much right:
			//#if polish.css.tabbar-roundtrip
				//# if (this.allowRoundtrip) {
					//# this.xOffset = (rightBorder - activeTabWidth - activeTabXPos);
				//# } else {
			//#endif
					this.xOffset = (rightBorder - activeTabWidth) - (activeTabXPos - scrollerWidth);
			//#if polish.css.tabbar-roundtrip
				//# }
			//#endif
			//System.out.println("this.xOffset + activeTabXPos + activeTabWidth > rightBorder");
			//System.out.println("xOffset=" + this.xOffset + ", activeTabXPos=" + activeTabXPos + ", activeTabWidth=" + activeTabWidth + ", rightBorder=" + rightBorder);
		}
	}

	/* (non-Javadoc)
	 * @see de.enough.polish.ui.Item#paintContent(int, int, int, int, javax.microedition.lcdui.Graphics)
	 */
	protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) 
	{
		// draw scrolling indicators:
		g.setColor( this.scrollArrowColor );
		int cHeight = this.contentHeight;
		y += (cHeight - this.scrollArrowHeight) / 2;
		int originalX = x;
		//#if polish.css.tabbar-roundtrip
			//# if ( this.allowRoundtrip || this.activeTabIndex > 0 ) {
		//#else
			if ( this.activeTabIndex > 0 ) {
		//#endif
			// draw left scrolling indicator:
			x += this.scrollArrowPadding;
			//#ifdef polish.css.tabbar-left-arrow
				//# if (this.leftArrow != null) {
					//# g.drawImage(this.leftArrow, x + this.arrowXOffset, y + this.arrowYOffset, Graphics.LEFT |  Graphics.TOP );
				//# } else {
			//#endif
					int halfWidth = this.scrollArrowHeight / 2;

⌨️ 快捷键说明

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