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

J2ME

开发平台:

Java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package com.framework;
  6. import javax.microedition.lcdui.Graphics;
  7. import javax.microedition.lcdui.Image;
  8. /**
  9.  *
  10.  * @author Swaroop Kumar
  11.  */
  12. public class SoftkeyBar {
  13.     GTantra font;
  14.     private int height,width;
  15.     private Image bgImage;
  16.     private UiDisplayble parentScreen;
  17.     private TickerTextControl left;
  18.     private TickerTextControl right;
  19.     private EventManager eventManager;
  20.     private int bgColor = -1;
  21.     protected SoftkeyBar(UiDisplayble screen) {
  22.         this.parentScreen = screen;
  23.         resize();
  24.     }
  25.     public void setFont(GTantra font) {
  26.         this.font = font;
  27.     }
  28.     
  29.     public String getSoftKeyText(int position)
  30.     {
  31.         if(position == Graphics.LEFT && left != null)
  32.         {
  33.             return left.getText();
  34.         }else if(position == Graphics.RIGHT && right != null)
  35.         {
  36.             return right.getText();
  37.         }else
  38.             return "";
  39.     }
  40.     protected boolean pointerReleased(int x, int y) {
  41.         if(eventManager != null)
  42.         {
  43.             if(left != null)
  44.             {
  45.                 if(Util.isInRect(left, x, y))
  46.                 {
  47.                     eventManager.event(EventManager.LEFT_SOFT_KEY_PRESSED, this,null);
  48.                     return true;
  49.                 }
  50.             }if(right != null)
  51.             {
  52.                 if(Util.isInRect(right, x, y))
  53.                 {
  54.                     eventManager.event(EventManager.RIGHT_SOFT_KEY_PRESSED, this,null);
  55.                     return true;
  56.                 }
  57.             }
  58.         }
  59.         return false;
  60.     }
  61.     public boolean keyPressed(int keyCode)
  62.     {
  63.         if(eventManager != null)
  64.         {
  65.             if(left != null && keyCode == KeyEvent.KEY_SOFT_LEFT)
  66.             {
  67.                 eventManager.event(EventManager.LEFT_SOFT_KEY_PRESSED, this,null);
  68.                 return true;
  69.             }else if(right != null && keyCode == KeyEvent.KEY_SOFT_RIGHT)
  70.             {
  71.                 eventManager.event(EventManager.RIGHT_SOFT_KEY_PRESSED, this,null);
  72.                 return true;
  73.             }
  74.         }
  75.         return false;
  76.     }
  77.     public void setEventManager(EventManager manager)
  78.     {
  79.         eventManager = manager;
  80.     }
  81.     public void resize()
  82.     {
  83.         width = DisplayManager.getInst().getWidth();
  84.         if(font != null)
  85.         {
  86.             height = font.getFontHeight() + (Theme.DEFAULT_PADDING << 1);
  87.         }
  88.         if(left != null)
  89.         {
  90.             height  = Math.max(height, left.getHeight());
  91.             
  92.         }if(right != null)
  93.         {
  94.             height  = Math.max(height, right.getHeight());
  95.         }
  96.         if(bgImage != null)
  97.         {
  98.             height = Math.max(height, bgImage.getHeight());
  99.         }
  100.     }
  101.     public void setBgImage(Image bgImage) {
  102.         this.bgImage = bgImage;
  103.     }
  104.     
  105.     public void paintUi(Graphics g)
  106.     {
  107.        if(bgColor != -1)
  108.        {
  109.            g.setColor(bgColor);
  110.            g.fillRect( 0, DisplayManager.getInst().getHeight() - height , width , height);
  111.        }
  112.         if(bgImage != null)
  113.         {
  114.             g.drawImage(bgImage, 0, DisplayManager.getInst().getHeight() - height, 0);
  115.         }
  116.         if(left != null)
  117.         {
  118.             left.paintUI(g);
  119.         }if(right != null)
  120.         {
  121.             right.paintUI(g);
  122.         }
  123.         
  124.     }
  125.     public void setSetSoftkey(int position,String text)
  126.     {
  127.         if(position == Graphics.LEFT)
  128.         {
  129.             left = new TickerTextControl(text, font);
  130.             left.setPoistion(0, DisplayManager.getInst().getHeight() - left.getHeight());
  131.         }else if(position == Graphics.RIGHT)
  132.         {
  133.             right = new TickerTextControl(text, font);
  134.             right.setPoistion(DisplayManager.getInst().getWidth() - right.getWidth(), DisplayManager.getInst().getHeight() - right.getHeight());
  135.         }
  136.         invalidate();
  137.     }
  138.     public void invalidate()
  139.     {
  140.         resize();
  141.         parentScreen.invalidate();
  142.     }
  143.     public int getHeight() {
  144.         return height;
  145.     }
  146.     public void setBgColor(int bgColor) {
  147.         this.bgColor = bgColor;
  148.     }
  149.     public int getWidth() {
  150.         return width;
  151.     }
  152. }