MOMTabControl.java
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * MOMTabControl.java
  3.  *
  4.  * Created on April 22, 2010, 12:40 PM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9. package com.MOM.ui.control;
  10. import com.MOM.resources.Resources;
  11. import com.framework.DisplayManager;
  12. import com.framework.EventManager;
  13. import com.framework.HorizontalLayout;
  14. import com.framework.ImageControl;
  15. import com.framework.ListLayout;
  16. import com.framework.ScrollableContainer;
  17. import com.framework.SpacerControl;
  18. import com.framework.UIControl;
  19. import com.sun.midp.lcdui.Resource;
  20. import java.util.Vector;
  21. import javax.microedition.lcdui.Graphics;
  22. import javax.microedition.lcdui.Image;
  23. /**
  24.  *
  25.  * @author Tejaswi
  26.  */
  27. public class MOMTabControl extends ScrollableContainer implements EventManager{
  28.     
  29.     Vector tabPanes = new Vector();
  30.     ScrollableContainer tabButtonBar;
  31.     HorizontalLayout horLayout = new HorizontalLayout(HorizontalLayout.STRETCH_NONE,4,0,2);
  32.    
  33.     int previousIndex = -1;
  34.     Image backupBgImage;
  35.     private static final int EXTRA_SPACE = 2;
  36.     
  37.     boolean init = false;    
  38.    SpacerControl spacercontrol = new SpacerControl();
  39.     
  40.     /** Creates a new instance of MOMTabControl */
  41.     public MOMTabControl() {
  42.         tabButtonBar = new ScrollableContainer();
  43.         tabButtonBar.setWidth(DisplayManager.getInst().getWidth());
  44.         tabButtonBar.setLayout(horLayout);
  45.         tabButtonBar.setPosition(0 , 2);
  46.         tabButtonBar.setSelectable(false);
  47.         spacercontrol.setBgColor(0x2882b4);
  48.         tabButtonBar.setSelected(false);
  49.         spacercontrol.setWidth(DisplayManager.getInst().getWidth());
  50.         spacercontrol.setHeight(3);
  51.         spacercontrol.setSelectable(false);
  52.         
  53.         setBgColor(0xffffff);
  54.         setSelectionBgColor(0xffffff);
  55.         addChildren(tabButtonBar);
  56.         addChildren(spacercontrol);
  57.         
  58.     }
  59.     public void event(int eventId, Object source, Object data) {
  60.         if(source.equals(tabButtonBar) && eventId == SELECTION_CHANGED)
  61.         {
  62.              selectTab(((Integer)data).intValue());
  63.         }
  64.     }
  65.     public void showNotify() {
  66.         setHeight(DisplayManager.getInst().getCurrentScreen().getBody().getHeight());
  67.         if(!init)
  68.         {
  69.             ImageControl control = new ImageControl(Resources.getInstance().getImage(Resources.SCROLLER_ARROW_IMAGE));
  70.             control.setAllignment(Graphics.HCENTER | Graphics.VCENTER);
  71.             control.setSelectable(false);
  72.             control.setHeight(tabButtonBar.getHeight());
  73.             tabButtonBar.addChildren(control);
  74.             init = true;
  75.         }
  76.         
  77.         if(previousIndex == -1)
  78.         {
  79.             selectTab(0);
  80.         }
  81.         tabButtonBar.setEventManager(this);
  82.         super.showNotify();
  83.     }
  84.     public void hideNotify() {
  85.         for (int i = 0; i < tabPanes.size(); i++) {
  86.              ((MOMTabPane)tabPanes.elementAt(i)).setInitStatus(false);
  87.         }
  88.         super.hideNotify();
  89.     }
  90.     public void addTabPane(MOMTabPane tabPane)
  91.     {
  92.         tabPanes.addElement(tabPane);
  93.         tabButtonBar.addChildren(tabPane.getTabButton());
  94.         tabButtonBar.setHeight(Math.max(tabButtonBar.getHeight() , tabPane.getTabButton().getHeight()));
  95.     }
  96.     public void selectTab(int index)
  97.     {
  98.         if(previousIndex != index)
  99.         {
  100.             // restore old image
  101.             if(previousIndex != -1)
  102.             {
  103.                 ScrollableContainer tabPane = getTabPane(previousIndex);
  104.                 UIControl tabButton = getTabButton(previousIndex);
  105.                 tabButton.setBgImage(backupBgImage);
  106.                 tabPane.setSelected(false);
  107.                 removeChildren(tabPane);
  108.             }
  109.             previousIndex = index;
  110.             UIControl tabButton = getTabButton(index);
  111.             backupBgImage = tabButton.getBgImage();
  112.             tabButton.setBgImage(tabButton.getSelectionBgImage());
  113.             ScrollableContainer pane = getTabPane(previousIndex);
  114.             spacercontrol.setY(tabButtonBar.getY() + tabButtonBar.getHeight() - 1);
  115.             pane.setHeight(getHeight() - spacercontrol.getY() - spacercontrol.getHeight() - 1 - EXTRA_SPACE);
  116.             pane.setWidth(getWidth());
  117.            
  118.             if(!((MOMTabPane)tabPanes.elementAt(index)).getInitStatus())
  119.             {
  120.                 ((MOMTabPane)tabPanes.elementAt(index)).setInitStatus(true);
  121.                 pane.setPosition(0 ,spacercontrol.getY() + spacercontrol.getHeight() + EXTRA_SPACE);
  122.                 pane.showNotify();
  123.                 
  124.             }
  125.             addChildren(pane);
  126.             pane.setSelected(true);
  127.             DisplayManager.getInst().invalidate();
  128.         }
  129.     }
  130.     private UIControl getTabButton(int index)
  131.     {
  132.       
  133.         return ((MOMTabPane)tabPanes.elementAt(index)).getTabButton();
  134.     }    
  135.     private ScrollableContainer getTabPane(int index)
  136.     {
  137.         return ((MOMTabPane)tabPanes.elementAt(index)).getTabPaneBody();
  138.     }
  139.     public boolean keyPressed(int keycode) {
  140.     int gameKey = DisplayManager.getInst().getGameAction(keycode);
  141.     ScrollableContainer tabPane = getTabPane(previousIndex);
  142.     boolean handled = tabPane.keyPressed(keycode);
  143.        if(tabPane.isSelected() && (gameKey == DisplayManager.LEFT || gameKey == DisplayManager.RIGHT) && !handled ) {
  144.             return tabButtonBar.keyPressed(keycode);
  145.         }else{
  146.            return handled;
  147.         }
  148.     }
  149. }