- 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;}}};add(bf);//MyTooltip Class is given belowimport 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); // Whitegraphics.fillRect(0,0,getWidth(),getHeight());graphics.setColor(0x00000000); // Blackgraphics.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 TooltipPopupScreen _tooltip; // Screen we are going to displayUiApplication _ourApplication; // access to pushGlobalScreen and dismissStatus from our Applicationint _displayTime; // in secondspublic 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 waitingsynchronized(_notifyObject) {_notifyObject.notify();}}}}
Monday, April 16, 2012
Implementing TootTip in Blackberry
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment