src/macosx/classes/sun/lwawt/macosx/CTrayIcon.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import sun.awt.SunToolkit;

  29 
  30 import javax.swing.*;
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import java.awt.geom.Point2D;
  34 import java.awt.image.BufferedImage;
  35 import java.awt.peer.TrayIconPeer;
  36 import java.beans.PropertyChangeEvent;
  37 import java.beans.PropertyChangeListener;
  38 
  39 public class CTrayIcon extends CFRetainedResource implements TrayIconPeer {
  40     private TrayIcon target;
  41     private PopupMenu popup;
  42     private JDialog messageDialog;
  43     private DialogEventHandler handler;
  44 










  45     CTrayIcon(TrayIcon target) {
  46         super(0, true);
  47 
  48         this.messageDialog = null;
  49         this.handler = null;
  50         this.target = target;
  51         this.popup = target.getPopupMenu();

  52         setPtr(createModel());
  53 
  54         //if no one else is creating the peer.
  55         checkAndCreatePopupPeer();
  56         updateImage();
  57     }
  58 
  59     private CPopupMenu checkAndCreatePopupPeer() {
  60         CPopupMenu menuPeer = null;
  61         if (popup != null) {
  62             try {
  63                 menuPeer = (CPopupMenu)popup.getPeer();
  64                 if (menuPeer == null) {
  65                     popup.addNotify();
  66                 }
  67             }catch (Exception e){
  68                 e.printStackTrace();
  69             }
  70         }
  71         return menuPeer;


 102             displayMessageOnEDT(caption, text, messageType);
 103         } else {
 104             try {
 105                 SwingUtilities.invokeAndWait(new Runnable() {
 106                     public void run() {
 107                         displayMessageOnEDT(caption, text, messageType);
 108                     }
 109                 });
 110             } catch (Exception e) {
 111                 throw new AssertionError(e);
 112             }
 113         }
 114     }
 115 
 116     @Override
 117     public void dispose() {
 118         if (messageDialog != null) {
 119             disposeMessageDialog();
 120         }
 121 


 122         LWCToolkit.targetDisposedPeer(target, this);
 123         target = null;
 124 
 125         super.dispose();
 126     }
 127 
 128     @Override
 129     public void setToolTip(String tooltip) {
 130         nativeSetToolTip(getModel(), tooltip);
 131     }
 132 
 133     //adds tooltip to the NSStatusBar's NSButton.
 134     private native void nativeSetToolTip(long trayIconModel, String tooltip);
 135 
 136     @Override
 137     public void showPopupMenu(int x, int y) {
 138         //Not used. The popupmenu is shown from the native code.
 139     }
 140 
 141     @Override


 144         if (image == null) return;
 145 
 146         MediaTracker tracker = new MediaTracker(new Button(""));
 147         tracker.addImage(image, 0);
 148         try {
 149             tracker.waitForAll();
 150         } catch (InterruptedException ignore) { }
 151 
 152         if (image.getWidth(null) <= 0 ||
 153             image.getHeight(null) <= 0)
 154         {
 155             return;
 156         }
 157 
 158         CImage cimage = CImage.getCreator().createFromImage(image);
 159         setNativeImage(getModel(), cimage.ptr, target.isImageAutoSize());
 160     }
 161 
 162     private native void setNativeImage(final long model, final long nsimage, final boolean autosize);
 163 
 164     //invocation from the AWTTrayIcon.m
 165     public void performAction() {
 166         SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
 167             public void run() {
 168                 final String cmd = target.getActionCommand();
 169                 final ActionEvent event = new ActionEvent(target, ActionEvent.ACTION_PERFORMED, cmd);
 170                 SunToolkit.postEvent(SunToolkit.targetToAppContext(target), event);
 171             }
 172         });
 173     }
 174 
































































 175     private native Point2D nativeGetIconLocation(long trayIconModel);
 176 
 177     public void displayMessageOnEDT(String caption, String text,
 178                                     String messageType) {
 179         if (messageDialog != null) {
 180             disposeMessageDialog();
 181         }
 182 
 183         // obtain icon to show along the message
 184         Icon icon = getIconForMessageType(messageType);
 185         if (icon != null) {
 186             icon = new ImageIcon(scaleIcon(icon, 0.75));
 187         }
 188 
 189         // We want the message dialog text area to be about 1/8 of the screen
 190         // size. There is nothing special about this value, it's just makes the
 191         // message dialog to look nice
 192         Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
 193         int textWidth = screenSize.width / 8;
 194 


 239         op.addPropertyChangeListener(handler);
 240 
 241         // Make Ok button small. Most likely won't work for L&F other then Aqua
 242         try {
 243             JPanel buttonPanel = (JPanel)op.getComponent(1);
 244             JButton ok = (JButton)buttonPanel.getComponent(0);
 245             ok.putClientProperty("JComponent.sizeVariant", "small");
 246         } catch (Throwable t) {
 247             // do nothing, we tried and failed, no big deal
 248         }
 249 
 250         dialog = new JDialog((Dialog) null);
 251         JRootPane rp = dialog.getRootPane();
 252 
 253         // gives us dialog window with small title bar and not zoomable
 254         rp.putClientProperty(CPlatformWindow.WINDOW_STYLE, "small");
 255         rp.putClientProperty(CPlatformWindow.WINDOW_ZOOMABLE, "false");
 256 
 257         dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
 258         dialog.setModal(false);



 259         dialog.setResizable(false);
 260         dialog.setContentPane(op);
 261 
 262         dialog.addWindowListener(handler);
 263 
 264         dialog.pack();
 265 
 266         return dialog;
 267     }
 268 
 269     private void showMessageDialog() {
 270 
 271         Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
 272         Point2D iconLoc = nativeGetIconLocation(getModel());
 273 
 274         int dialogY = (int)iconLoc.getY();
 275         int dialogX = (int)iconLoc.getX();
 276         if (dialogX + messageDialog.getWidth() > screenSize.width) {
 277             dialogX = screenSize.width - messageDialog.getWidth();
 278         }




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import sun.awt.SunToolkit;
  29 import sun.lwawt.macosx.event.NSEvent;
  30 
  31 import javax.swing.*;
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.awt.geom.Point2D;
  35 import java.awt.image.BufferedImage;
  36 import java.awt.peer.TrayIconPeer;
  37 import java.beans.PropertyChangeEvent;
  38 import java.beans.PropertyChangeListener;
  39 
  40 public class CTrayIcon extends CFRetainedResource implements TrayIconPeer {
  41     private TrayIcon target;
  42     private PopupMenu popup;
  43     private JDialog messageDialog;
  44     private DialogEventHandler handler;
  45 
  46     // In order to construct MouseEvent object, we need to specify a
  47     // Component target. Because TrayIcon isn't Component's subclass,
  48     // we use this dummy frame instead
  49     private final Frame dummyFrame;
  50 
  51     // A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
  52     // on MOUSE_RELEASE. Click events are only generated if there were no drag
  53     // events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
  54     private static int mouseClickButtons = 0;
  55 
  56     CTrayIcon(TrayIcon target) {
  57         super(0, true);
  58 
  59         this.messageDialog = null;
  60         this.handler = null;
  61         this.target = target;
  62         this.popup = target.getPopupMenu();
  63         this.dummyFrame = new Frame();
  64         setPtr(createModel());
  65 
  66         //if no one else is creating the peer.
  67         checkAndCreatePopupPeer();
  68         updateImage();
  69     }
  70 
  71     private CPopupMenu checkAndCreatePopupPeer() {
  72         CPopupMenu menuPeer = null;
  73         if (popup != null) {
  74             try {
  75                 menuPeer = (CPopupMenu)popup.getPeer();
  76                 if (menuPeer == null) {
  77                     popup.addNotify();
  78                 }
  79             }catch (Exception e){
  80                 e.printStackTrace();
  81             }
  82         }
  83         return menuPeer;


 114             displayMessageOnEDT(caption, text, messageType);
 115         } else {
 116             try {
 117                 SwingUtilities.invokeAndWait(new Runnable() {
 118                     public void run() {
 119                         displayMessageOnEDT(caption, text, messageType);
 120                     }
 121                 });
 122             } catch (Exception e) {
 123                 throw new AssertionError(e);
 124             }
 125         }
 126     }
 127 
 128     @Override
 129     public void dispose() {
 130         if (messageDialog != null) {
 131             disposeMessageDialog();
 132         }
 133 
 134         dummyFrame.dispose();
 135 
 136         LWCToolkit.targetDisposedPeer(target, this);
 137         target = null;
 138 
 139         super.dispose();
 140     }
 141 
 142     @Override
 143     public void setToolTip(String tooltip) {
 144         nativeSetToolTip(getModel(), tooltip);
 145     }
 146 
 147     //adds tooltip to the NSStatusBar's NSButton.
 148     private native void nativeSetToolTip(long trayIconModel, String tooltip);
 149 
 150     @Override
 151     public void showPopupMenu(int x, int y) {
 152         //Not used. The popupmenu is shown from the native code.
 153     }
 154 
 155     @Override


 158         if (image == null) return;
 159 
 160         MediaTracker tracker = new MediaTracker(new Button(""));
 161         tracker.addImage(image, 0);
 162         try {
 163             tracker.waitForAll();
 164         } catch (InterruptedException ignore) { }
 165 
 166         if (image.getWidth(null) <= 0 ||
 167             image.getHeight(null) <= 0)
 168         {
 169             return;
 170         }
 171 
 172         CImage cimage = CImage.getCreator().createFromImage(image);
 173         setNativeImage(getModel(), cimage.ptr, target.isImageAutoSize());
 174     }
 175 
 176     private native void setNativeImage(final long model, final long nsimage, final boolean autosize);
 177 
 178     private void postEvent(final AWTEvent event) {

 179         SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
 180             public void run() {


 181                 SunToolkit.postEvent(SunToolkit.targetToAppContext(target), event);
 182             }
 183         });
 184     }
 185 
 186     //invocation from the AWTTrayIcon.m
 187     private void handleMouseEvent(NSEvent nsEvent) {
 188         int buttonNumber = nsEvent.getButtonNumber();
 189         final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
 190         if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
 191                 || buttonNumber > tk.getNumberOfButtons() - 1) {
 192             return;
 193         }
 194 
 195         int jeventType = NSEvent.nsToJavaEventType(nsEvent.getType());
 196 
 197         int jbuttonNumber = MouseEvent.NOBUTTON;
 198         int jclickCount = 0;
 199         if (jeventType != MouseEvent.MOUSE_MOVED) {
 200             jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
 201             jclickCount = nsEvent.getClickCount();
 202         }
 203 
 204         int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
 205                 nsEvent.getModifierFlags());
 206         boolean isPopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
 207 
 208         int eventButtonMask = (jbuttonNumber > 0)?
 209                 MouseEvent.getMaskForButton(jbuttonNumber) : 0;
 210         long when = System.currentTimeMillis();
 211 
 212         if (jeventType == MouseEvent.MOUSE_PRESSED) {
 213             mouseClickButtons |= eventButtonMask;
 214         } else if (jeventType == MouseEvent.MOUSE_DRAGGED) {
 215             mouseClickButtons = 0;
 216         }
 217 
 218         // The MouseEvent's coordinates are relative to screen
 219         int absX = nsEvent.getAbsX();
 220         int absY = nsEvent.getAbsY();
 221 
 222         MouseEvent mouseEvent = new MouseEvent(dummyFrame, jeventType, when,
 223                 jmodifiers, absX, absY, absX, absY, jclickCount, isPopupTrigger,
 224                 jbuttonNumber);
 225         mouseEvent.setSource(target);
 226         postEvent(mouseEvent);
 227 
 228         // fire ACTION event
 229         if (jeventType == MouseEvent.MOUSE_PRESSED && isPopupTrigger) {
 230             final String cmd = target.getActionCommand();
 231             final ActionEvent event = new ActionEvent(target,
 232                     ActionEvent.ACTION_PERFORMED, cmd);
 233             postEvent(event);
 234         }
 235 
 236         // synthesize CLICKED event
 237         if (jeventType == MouseEvent.MOUSE_RELEASED) {
 238             if ((mouseClickButtons & eventButtonMask) != 0) {
 239                 MouseEvent clickEvent = new MouseEvent(dummyFrame,
 240                         MouseEvent.MOUSE_CLICKED, when, jmodifiers, absX, absY,
 241                         absX, absY, jclickCount, isPopupTrigger, jbuttonNumber);
 242                 clickEvent.setSource(target);
 243                 postEvent(clickEvent);
 244             }
 245 
 246             mouseClickButtons &= ~eventButtonMask;
 247         }
 248     }
 249 
 250     private native Point2D nativeGetIconLocation(long trayIconModel);
 251 
 252     public void displayMessageOnEDT(String caption, String text,
 253                                     String messageType) {
 254         if (messageDialog != null) {
 255             disposeMessageDialog();
 256         }
 257 
 258         // obtain icon to show along the message
 259         Icon icon = getIconForMessageType(messageType);
 260         if (icon != null) {
 261             icon = new ImageIcon(scaleIcon(icon, 0.75));
 262         }
 263 
 264         // We want the message dialog text area to be about 1/8 of the screen
 265         // size. There is nothing special about this value, it's just makes the
 266         // message dialog to look nice
 267         Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
 268         int textWidth = screenSize.width / 8;
 269 


 314         op.addPropertyChangeListener(handler);
 315 
 316         // Make Ok button small. Most likely won't work for L&F other then Aqua
 317         try {
 318             JPanel buttonPanel = (JPanel)op.getComponent(1);
 319             JButton ok = (JButton)buttonPanel.getComponent(0);
 320             ok.putClientProperty("JComponent.sizeVariant", "small");
 321         } catch (Throwable t) {
 322             // do nothing, we tried and failed, no big deal
 323         }
 324 
 325         dialog = new JDialog((Dialog) null);
 326         JRootPane rp = dialog.getRootPane();
 327 
 328         // gives us dialog window with small title bar and not zoomable
 329         rp.putClientProperty(CPlatformWindow.WINDOW_STYLE, "small");
 330         rp.putClientProperty(CPlatformWindow.WINDOW_ZOOMABLE, "false");
 331 
 332         dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
 333         dialog.setModal(false);
 334         dialog.setModalExclusionType(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
 335         dialog.setAlwaysOnTop(true);
 336         dialog.setAutoRequestFocus(false);
 337         dialog.setResizable(false);
 338         dialog.setContentPane(op);
 339 
 340         dialog.addWindowListener(handler);
 341 
 342         dialog.pack();
 343 
 344         return dialog;
 345     }
 346 
 347     private void showMessageDialog() {
 348 
 349         Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
 350         Point2D iconLoc = nativeGetIconLocation(getModel());
 351 
 352         int dialogY = (int)iconLoc.getY();
 353         int dialogX = (int)iconLoc.getX();
 354         if (dialogX + messageDialog.getWidth() > screenSize.width) {
 355             dialogX = screenSize.width - messageDialog.getWidth();
 356         }