Wednesday, January 18, 2012

Tooltip For Field

1. Class For Tooltip is
package a.uilibrary;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;

class MyTooltip extends PopupScreen{
    int _x;
    int _y;
    TooltipThread _tooltipThread;

    private MyTooltip(Manager manager) {
        super(manager);
     }
    public void sublayout(int width, int height)    {
        super.sublayout(width,height);
        setPosition(_x,_y);
        System.out.println("Tooltip x: " + Integer.toString(_x) + ", y: " + Integer.toString(_y));
    }
    protected void applyTheme() {
        // Overriden to suppress Border etc.
    }
    public void removeToolTip() {
        if ( _tooltipThread != null ) {
            _tooltipThread.dismiss();
        }
    }
    private void display(UiApplication uiApp, int x, int y, int displayTime) {
        _x = x;
        _y = y;
        _tooltipThread = new TooltipThread(uiApp, this, displayTime);
        _tooltipThread.start();
    }

    public static MyTooltip addToolTip(UiApplication uiApp, String toolTipString, int x, int y, int displayTime) {
        VerticalFieldManager manager = new VerticalFieldManager(Manager.FIELD_VCENTER|Manager.NON_FOCUSABLE) {
            protected void paint(Graphics graphics) {   
                //graphics.setColor(0x00FFFFFF); // White
                //graphics.fillRect(0,0,getWidth(),getHeight());
                graphics.setColor(0x00000000); // Black
               
               // graphics.drawRect(0,0,getWidth(),getHeight());
                super.paint(graphics);
            }
        };
       
        MyTooltip toolTip = new MyTooltip(manager);
        LabelField label = new LabelField(' ' + toolTipString + ' ', LabelField.NON_FOCUSABLE);
        label.setBorder(BorderFactory.createBitmapBorder(new XYEdges(5,5,5,5),Bitmap.getBitmapResource("bubble_left.png")));
        label.setFont(Font.getDefault().derive(Font.PLAIN, 16));
        toolTip.add(label);
        toolTip.display(uiApp, x, y, displayTime);
        return toolTip;
    }

    class TooltipThread extends Thread {

        Object _notifyObject = new Object(); // Used to allow user to dismiss this Tooltip
        PopupScreen _tooltip; // Screen we are going to display
        UiApplication _ourApplication; // access to pushGlobalScreen and dismissStatus from our Application
        int _displayTime; // in seconds

        public TooltipThread(UiApplication ourApplication, PopupScreen tooltip, int displayTime) {
            _tooltip = tooltip;
            _ourApplication = ourApplication;
            _displayTime = displayTime;
        }
            
        public void run() {
            _ourApplication.pushGlobalScreen(_tooltip, 999, false);
            synchronized(_notifyObject) {
                try {
                    _notifyObject.wait(_displayTime * 1000);
                } catch (Exception e) {
                }
            };
            _ourApplication.dismissStatus(_tooltip);
        }

        public void dismiss() {
            // notify the waiting object to stop the Thread waiting
            synchronized(_notifyObject) {
                _notifyObject.notify();
            }
        }

    }


}
2 Using this class Example add this as fields onFocus & onUnfocus method
protected void onFocus(int direction) 
 { 
  
    invalidate();
    
    if(this.getText().equalsIgnoreCase(promot))
    {
     this.setText("");
     if ( _tooltip != null ) 
    {
              _tooltip.removeToolTip();
              _tooltip = null;
    }
     
      
           contentArea = this.getContentRect();
           contentBottom = contentArea.y + contentArea.height-5;
           contentRight = contentArea.x + contentArea.width;  

           
           contentBottom = contentBottom - this.getManager().getVerticalScroll();
           
           if (!(contentBottom > (getHeight() - 10)))
           {
            contentBottom =contentBottom  - getHeight()-2;
           }
           _tooltip = MyTooltip.addToolTip(UiApplication.getUiApplication(), promot, 20, contentBottom,2); 
     flag=false;
     invalidate();
     
    }   
       
    super.onFocus(direction);
 }
 
 
 protected void onUnfocus() 
 {  
             // remove tooltip
   if ( _tooltip != null ) {
             // We have displayed a Tooltip - remove it
             _tooltip.removeToolTip();
             _tooltip = null;
         }
  invalidate();
   
  super.onUnfocus();
 }

No comments:

Post a Comment