MOMTabControl.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:5k
源码类别:
J2ME
开发平台:
Java
- /*
- * MOMTabControl.java
- *
- * Created on April 22, 2010, 12:40 PM
- *
- * To change this template, choose Tools | Template Manager
- * and open the template in the editor.
- */
- package com.MOM.ui.control;
- import com.MOM.resources.Resources;
- import com.framework.DisplayManager;
- import com.framework.EventManager;
- import com.framework.HorizontalLayout;
- import com.framework.ImageControl;
- import com.framework.ListLayout;
- import com.framework.ScrollableContainer;
- import com.framework.SpacerControl;
- import com.framework.UIControl;
- import com.sun.midp.lcdui.Resource;
- import java.util.Vector;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- /**
- *
- * @author Tejaswi
- */
- public class MOMTabControl extends ScrollableContainer implements EventManager{
- Vector tabPanes = new Vector();
- ScrollableContainer tabButtonBar;
- HorizontalLayout horLayout = new HorizontalLayout(HorizontalLayout.STRETCH_NONE,4,0,2);
- int previousIndex = -1;
- Image backupBgImage;
- private static final int EXTRA_SPACE = 2;
- boolean init = false;
- SpacerControl spacercontrol = new SpacerControl();
- /** Creates a new instance of MOMTabControl */
- public MOMTabControl() {
- tabButtonBar = new ScrollableContainer();
- tabButtonBar.setWidth(DisplayManager.getInst().getWidth());
- tabButtonBar.setLayout(horLayout);
- tabButtonBar.setPosition(0 , 2);
- tabButtonBar.setSelectable(false);
- spacercontrol.setBgColor(0x2882b4);
- tabButtonBar.setSelected(false);
- spacercontrol.setWidth(DisplayManager.getInst().getWidth());
- spacercontrol.setHeight(3);
- spacercontrol.setSelectable(false);
- setBgColor(0xffffff);
- setSelectionBgColor(0xffffff);
- addChildren(tabButtonBar);
- addChildren(spacercontrol);
- }
- public void event(int eventId, Object source, Object data) {
- if(source.equals(tabButtonBar) && eventId == SELECTION_CHANGED)
- {
- selectTab(((Integer)data).intValue());
- }
- }
- public void showNotify() {
- setHeight(DisplayManager.getInst().getCurrentScreen().getBody().getHeight());
- if(!init)
- {
- ImageControl control = new ImageControl(Resources.getInstance().getImage(Resources.SCROLLER_ARROW_IMAGE));
- control.setAllignment(Graphics.HCENTER | Graphics.VCENTER);
- control.setSelectable(false);
- control.setHeight(tabButtonBar.getHeight());
- tabButtonBar.addChildren(control);
- init = true;
- }
- if(previousIndex == -1)
- {
- selectTab(0);
- }
- tabButtonBar.setEventManager(this);
- super.showNotify();
- }
- public void hideNotify() {
- for (int i = 0; i < tabPanes.size(); i++) {
- ((MOMTabPane)tabPanes.elementAt(i)).setInitStatus(false);
- }
- super.hideNotify();
- }
- public void addTabPane(MOMTabPane tabPane)
- {
- tabPanes.addElement(tabPane);
- tabButtonBar.addChildren(tabPane.getTabButton());
- tabButtonBar.setHeight(Math.max(tabButtonBar.getHeight() , tabPane.getTabButton().getHeight()));
- }
- public void selectTab(int index)
- {
- if(previousIndex != index)
- {
- // restore old image
- if(previousIndex != -1)
- {
- ScrollableContainer tabPane = getTabPane(previousIndex);
- UIControl tabButton = getTabButton(previousIndex);
- tabButton.setBgImage(backupBgImage);
- tabPane.setSelected(false);
- removeChildren(tabPane);
- }
- previousIndex = index;
- UIControl tabButton = getTabButton(index);
- backupBgImage = tabButton.getBgImage();
- tabButton.setBgImage(tabButton.getSelectionBgImage());
- ScrollableContainer pane = getTabPane(previousIndex);
- spacercontrol.setY(tabButtonBar.getY() + tabButtonBar.getHeight() - 1);
- pane.setHeight(getHeight() - spacercontrol.getY() - spacercontrol.getHeight() - 1 - EXTRA_SPACE);
- pane.setWidth(getWidth());
- if(!((MOMTabPane)tabPanes.elementAt(index)).getInitStatus())
- {
- ((MOMTabPane)tabPanes.elementAt(index)).setInitStatus(true);
- pane.setPosition(0 ,spacercontrol.getY() + spacercontrol.getHeight() + EXTRA_SPACE);
- pane.showNotify();
- }
- addChildren(pane);
- pane.setSelected(true);
- DisplayManager.getInst().invalidate();
- }
- }
- private UIControl getTabButton(int index)
- {
- return ((MOMTabPane)tabPanes.elementAt(index)).getTabButton();
- }
- private ScrollableContainer getTabPane(int index)
- {
- return ((MOMTabPane)tabPanes.elementAt(index)).getTabPaneBody();
- }
- public boolean keyPressed(int keycode) {
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- ScrollableContainer tabPane = getTabPane(previousIndex);
- boolean handled = tabPane.keyPressed(keycode);
- if(tabPane.isSelected() && (gameKey == DisplayManager.LEFT || gameKey == DisplayManager.RIGHT) && !handled ) {
- return tabButtonBar.keyPressed(keycode);
- }else{
- return handled;
- }
- }
- }