Tag: popup

Tooltips class for BlackBerry

here is the copy of tooltips class for BlackBerry,
with 2 different approach.

first: [1]
Easy solution to create popup style tooltips on BlackBerry.
Extend TooltipScreen instead of the usual MainScreen, usage:

add(new ButtonField(“myButton”), “My Tooltip text”);
and the source:

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.MainScreen;

public class TooltipScreen extends MainScreen {

    TooltipScreen screen = this;
    boolean doRedraw = false;//prevent infinte redrawing
    Vector tooltips = new Vector();//vector to hold tooltip strings
    private Timer tooltipTimer = new Timer();
    private TimerTask tooltipTask;
    boolean alive = false;//is the tooltip alive? used to pop it after our timeout
    int count = 0;//used to calculate time tooltip is displayed
    //tooltip popup colours:
    int backgroundColour = 0xeeeeee;
    int borderColour = 0xaaaaaa;
    int fontColour = 0x666666;
    //the tooltip:
    String tooltip;
    int tooltipWidth;
    int yCoord;
    int xCoord;
    //region parameters:
    XYRect contentArea;
    int contentBottom;
    int contentRight;

    public TooltipScreen() {
        super();

        //when timeout reaches 100ms*20 ie. 2seconds set alive to false and redraw screen:
        tooltipTask = new TimerTask() {

            public void run() {
                if (alive) {
                    count++;
                    if (count == 20) {
                        alive = false;
                        invalidate();
                    }
                }
            }
        };

        tooltipTimer.scheduleAtFixedRate(tooltipTask, 100, 100);

    }

    //override add method adds an empty string to tooltip vector:
    public void add(Field field) {
        tooltips.addElement("");
        super.add(field);
    }

    //custom add method for fields with tooltip: add(myField, "myTooltip");
    public void add(Field field, String tooltip) {
        super.add(field);
        tooltips.addElement(tooltip);
    }

    public void setColours(int backgroundColour, int borderColour, int fontColour) {
        this.backgroundColour = backgroundColour;
        this.borderColour = borderColour;
        this.fontColour = fontColour;
    }

    //reset everything when user changes focus,
    //possibly needs logic to check field has actually changed (for listfields, objectchoicefields etc etc)
    protected boolean navigationMovement(int dx, int dy, int status, int time) {
        count = 0;
        alive = true;
        doRedraw = true;
        return super.navigationMovement(dx, dy, status, time);
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        if (alive) {
            Field focusField = getFieldWithFocus();
            tooltip = (String) tooltips.elementAt(screen.getFieldWithFocusIndex());

            //don't do anything outside the norm unless this field has a tooltip:
            if (!tooltip.equals("")) {
                //get the field content region, this may fall inside the field actual region/coordinates:
                contentArea = focusField.getContentRect();
                contentBottom = contentArea.y + contentArea.height;
                contentRight = contentArea.x + contentArea.width;

                //+4 to accomodate 2 pixel padding on either side:
                tooltipWidth = graphics.getFont().getAdvance(tooltip) + 4;

                yCoord = contentBottom - focusField.getManager().getVerticalScroll();
                //check the tooltip is being drawn fully inside the screen height:
                if (yCoord > (getHeight() - 30)) {
                    yCoord = getHeight() - 30;
                }

                //check the tooltip doesn't get drawn off the right side of the screen:
                if (contentRight + tooltipWidth < getWidth()) {
                    xCoord = contentRight;
                } else {
                    xCoord = getWidth() - tooltipWidth;
                }

                //draw the tooltip
                graphics.setColor(backgroundColour);
                graphics.fillRect(xCoord, yCoord, tooltipWidth, 30);
                graphics.setColor(borderColour);
                graphics.drawRect(xCoord, yCoord, tooltipWidth, 30);
                graphics.setColor(fontColour);
                graphics.drawText(tooltip, xCoord + 2, yCoord);
            }
        }
        //doRedraw logic prevents infinite loop
        if (doRedraw) {
            //System.out.println("redrawing screen: " + System.currentTimeMillis());
            screen.invalidate();
            doRedraw = false;
        }
    }
}

second: [2]

package ..;

/*

Display a 'Tooltip" (String) at a specified location for s specified time
This Tooltip can also be removed.
Note that in my testing, the display time is not consistent.

Following example shows it being added and removed using focus events.

ButtonField bf = new ButtonField("Test") {
    MyTooltip _tooltip;
    protected void onFocus(int direction) {
        if ( _tooltip != null ) {
            _tooltip.removeToolTip();
            _tooltip = null;
        }
        // Display tooltip at 50,50 for 5 seconds
        _tooltip = MyTooltip.addToolTip(UiApplication.getUiApplication(), "Press to test", 50, 50, 5);;
    }
    protected void onUnfocus() {
        if ( _tooltip != null ) {
            // We have displayed a Tooltip - remove it
            _tooltip.removeToolTip();
            _tooltip = null;
        }
    }
 };

*/

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;

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.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();
            }
        }

    }

}

[1] http://www.naviina.eu/wp/blackberry/390/
[2] http://supportforums.blackberry.com/t5/Java-Development/Problem-with-Tooltip/td-p/102847


lock the screen from background thread

this is sample how to lock the screen from background thread by pushing a Dialog.

private void notifyUser(String msg)
    {
        UiEngine ui = Ui.getUiEngine();
        EncodedImage icon = Theme.getIcon(false, true);
        Bitmap bm = null;
        if (icon != null) {
            bm = icon.getBitmap();
        } else {
            bm = Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION);
        }
        synchronized (Application.getEventLock()) {
            Screen screen = new Dialog(Dialog.D_OK, msg, Dialog.OK,
                    bm, Manager.VERTICAL_SCROLL) {

                protected void onUiEngineAttached(boolean attached) {

                    if (locked) {
                        locked = false;
                        foreGround();
                    }

                    super.onUiEngineAttached(attached);
                    addKeyListener(new myKeyPadListener());
                }
            };
            ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_SHOW_LOWER);
        }
    }
private void notifyUser(String msg) {
        UiEngine ui = Ui.getUiEngine();
        EncodedImage icon = Theme.getIcon(false, true);
        Bitmap bm = null;
        if (icon != null) {
            bm = icon.getBitmap();
        } else {
            bm = Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION);
        }
        synchronized (Application.getEventLock()) {
            Screen screen = new Dialog(Dialog.D_OK, msg, Dialog.OK, bm, Manager.VERTICAL_SCROLL) {

                protected void onUiEngineAttached(boolean attached) {
                    super.onUiEngineAttached(attached);
                    addKeyListener(new SalatKeyPadListener());
                    Timer tmr = new Timer();
                    TimerTask tt = new TimerTask() {

                        public void run() {
                            invalidate();
                        }
                    };
                    tmr.scheduleAtFixedRate(tt, 500, 500);
                }

                protected void onVisibilityChange(boolean visible) {
                    if (notified) {
                        CancelNotification();
                    }
                }

              public boolean onClose() {
                    if (notified) {
                        CancelNotification();
                    }
                    return super.onClose();
                }
            };
            ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_SHOW_LOWER);
        }
    }

[1] http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=21625


  • Copyright © 1996-2010 vaks.in. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress