javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/qt/org/eclipse/swt/widgets/TrayItem.java
changeset 21 2a9601315dfc
child 35 85266cc22c7f
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2000, 2007 IBM Corporation and others.
       
     3  * Portion Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     4  * All rights reserved. This program and the accompanying materials
       
     5  * are made available under the terms of the Eclipse Public License v1.0
       
     6  * which accompanies this distribution, and is available at
       
     7  * http://www.eclipse.org/legal/epl-v10.html
       
     8  *
       
     9  * Contributors:
       
    10  *     IBM Corporation - initial API and implementation
       
    11  *     Nokia Corporation - Qt implementation
       
    12  *******************************************************************************/
       
    13 package org.eclipse.swt.widgets;
       
    14 
       
    15 import org.eclipse.swt.SWT;
       
    16 import org.eclipse.swt.events.SelectionListener;
       
    17 import org.eclipse.swt.graphics.Image;
       
    18 import org.eclipse.swt.graphics.Internal_GfxPackageSupport;
       
    19 import org.eclipse.swt.internal.qt.OS;
       
    20 import org.eclipse.swt.internal.qt.WidgetState;
       
    21 
       
    22 /**
       
    23  * Instances of this class represent icons that can be placed on the
       
    24  * system tray or task bar status area.
       
    25  * <p>
       
    26  * <dl>
       
    27  * <dt><b>Styles:</b></dt>
       
    28  * <dd>(none)</dd>
       
    29  * <dt><b>Events:</b></dt>
       
    30  * <dd>DefaultSelection, MenuDetect, Selection</dd>
       
    31  * </dl>
       
    32  * </p><p>
       
    33  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
       
    34  * </p>
       
    35  * 
       
    36  * @since 3.0
       
    37  */
       
    38 public class TrayItem extends Item {
       
    39     Tray parent;
       
    40     String toolTipText;
       
    41 
       
    42 
       
    43 /**
       
    44  * Constructs a new instance of this class given its parent
       
    45  * (which must be a <code>Tray</code>) and a style value
       
    46  * describing its behavior and appearance. The item is added
       
    47  * to the end of the items maintained by its parent.
       
    48  * <p>
       
    49  * The style value is either one of the style constants defined in
       
    50  * class <code>SWT</code> which is applicable to instances of this
       
    51  * class, or must be built by <em>bitwise OR</em>'ing together 
       
    52  * (that is, using the <code>int</code> "|" operator) two or more
       
    53  * of those <code>SWT</code> style constants. The class description
       
    54  * lists the style constants that are applicable to the class.
       
    55  * Style bits are also inherited from superclasses.
       
    56  * </p>
       
    57  *
       
    58  * @param parent a composite control which will be the parent of the new instance (cannot be null)
       
    59  * @param style the style of control to construct
       
    60  *
       
    61  * @exception IllegalArgumentException <ul>
       
    62  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
       
    63  * </ul>
       
    64  * @exception SWTException <ul>
       
    65  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
       
    66  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
       
    67  * </ul>
       
    68  *
       
    69  * @see SWT
       
    70  * @see Widget#checkSubclass
       
    71  * @see Widget#getStyle
       
    72  */
       
    73 public TrayItem (Tray parent, int style) {
       
    74     super (parent, style);
       
    75     this.parent = parent;
       
    76     createWidget (parent.getItemCount ());
       
    77 }
       
    78 
       
    79 /**
       
    80  * Adds the listener to the collection of listeners who will
       
    81  * be notified when the receiver is selected by the user, by sending
       
    82  * it one of the messages defined in the <code>SelectionListener</code>
       
    83  * interface.
       
    84  * <p>
       
    85  * <code>widgetSelected</code> is called when the receiver is selected
       
    86  * <code>widgetDefaultSelected</code> is called when the receiver is double-clicked
       
    87  * </p>
       
    88  *
       
    89  * @param listener the listener which should be notified when the receiver is selected by the user
       
    90  *
       
    91  * @exception IllegalArgumentException <ul>
       
    92  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
       
    93  * </ul>
       
    94  * @exception SWTException <ul>
       
    95  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
    96  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
    97  * </ul>
       
    98  *
       
    99  * @see SelectionListener
       
   100  * @see #removeSelectionListener
       
   101  * @see SelectionEvent
       
   102  */
       
   103 public void addSelectionListener(SelectionListener listener) {
       
   104     checkWidget ();
       
   105     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
       
   106     TypedListener typedListener = new TypedListener (listener);
       
   107     addListener (SWT.Selection, typedListener);
       
   108     addListener (SWT.DefaultSelection, typedListener);
       
   109 }
       
   110 
       
   111 protected void checkSubclass () {
       
   112     if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
       
   113 }
       
   114 
       
   115 void createWidget (int index) {
       
   116     super.createWidget (index);
       
   117     parent.createItem (this, index);
       
   118     OS.QSystemTrayIcon_setVisible(handle, true);
       
   119 }
       
   120 
       
   121 void createHandle_pp (int index) {
       
   122     getDisplay();
       
   123     topHandle = handle = OS.QSystemTrayIcon_new(Display.handle);
       
   124     state |= WidgetState.HANDLE;
       
   125 }
       
   126 
       
   127 void releaseParent_pp (){
       
   128     parent.destroyItem (this);
       
   129 }
       
   130 
       
   131 /**
       
   132  * Returns the receiver's parent, which must be a <code>Tray</code>.
       
   133  *
       
   134  * @return the receiver's parent
       
   135  *
       
   136  * @exception SWTException <ul>
       
   137  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   138  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   139  * </ul>
       
   140  * 
       
   141  * @since 3.2
       
   142  */
       
   143 public Tray getParent () {
       
   144     checkWidget ();
       
   145     return parent;
       
   146 }
       
   147 
       
   148 
       
   149 /**
       
   150  * Returns the receiver's tool tip text, or null if it has
       
   151  * not been set.
       
   152  *
       
   153  * @return the receiver's tool tip text
       
   154  *
       
   155  * @exception SWTException <ul>
       
   156  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   157  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   158  * </ul>
       
   159  */
       
   160 public String getToolTipText () {
       
   161     checkWidget ();
       
   162     return toolTipText;
       
   163 }
       
   164 
       
   165 void qt_signal_tray_activated(int reason) {
       
   166     switch (reason ) {
       
   167     case OS.QT_TRAY_REASON_CONTEXT:
       
   168         sendEvent (SWT.MenuDetect);
       
   169         break;
       
   170     case OS.QT_TRAY_REASON_TRIGGER:
       
   171         sendEvent (SWT.Selection);
       
   172         break;
       
   173     case OS.QT_TRAY_REASON_DOUBLECLICK:
       
   174         sendEvent (SWT.DefaultSelection);
       
   175         break;
       
   176     default:
       
   177         break;
       
   178     }
       
   179 }
       
   180 
       
   181 
       
   182 void hookEvents_pp () {
       
   183     int activatedProxy = OS.SignalHandler_new(handle, display, OS.QSIGNAL_TRAY_ACTIVATED);
       
   184     OS.QObject_connectOrThrow(handle, "activated(QSystemTrayIcon::ActivationReason)", activatedProxy, "widgetSignal(QSystemTrayIcon::ActivationReason)", OS.QT_AUTOCONNECTION);
       
   185  }
       
   186 
       
   187 /**
       
   188  * Returns <code>true</code> if the receiver is visible and 
       
   189  * <code>false</code> otherwise.
       
   190  *
       
   191  * @return the receiver's visibility
       
   192  *
       
   193  * @exception SWTException <ul>
       
   194  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   195  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   196  * </ul>
       
   197  */
       
   198 public boolean getVisible () {
       
   199     checkWidget ();
       
   200     return OS.QSystemTrayIcon_isVisible(handle);
       
   201 }
       
   202 
       
   203 void releaseWidget_pp () {
       
   204     super.releaseWidget_pp ();
       
   205     toolTipText = null;
       
   206     parent = null;
       
   207 }
       
   208 
       
   209 /**
       
   210  * Removes the listener from the collection of listeners who will
       
   211  * be notified when the receiver is selected by the user.
       
   212  *
       
   213  * @param listener the listener which should no longer be notified
       
   214  *
       
   215  * @exception IllegalArgumentException <ul>
       
   216  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
       
   217  * </ul>
       
   218  * @exception SWTException <ul>
       
   219  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   220  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   221  * </ul>
       
   222  *
       
   223  * @see SelectionListener
       
   224  * @see #addSelectionListener
       
   225  */
       
   226 public void removeSelectionListener (SelectionListener listener) {
       
   227     checkWidget ();
       
   228     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
       
   229     if (eventTable == null) return;
       
   230     eventTable.unhook (SWT.Selection, listener);
       
   231     eventTable.unhook (SWT.DefaultSelection, listener);
       
   232 }
       
   233 
       
   234 /**
       
   235  * Sets the receiver's image.
       
   236  *
       
   237  * @param image the new image
       
   238  *
       
   239  * @exception IllegalArgumentException <ul>
       
   240  *    <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
       
   241  * </ul>
       
   242  * @exception SWTException <ul>
       
   243  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   244  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   245  * </ul>
       
   246  */
       
   247 public void setImage (Image image) {
       
   248     checkWidget ();
       
   249     if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
       
   250     this.image = image;
       
   251     if (image != null ) {
       
   252         OS.QSystemTrayIcon_setIcon(handle, Internal_GfxPackageSupport.getIconHandle(image));
       
   253     } else {
       
   254         OS.QSystemTrayIcon_setIcon(handle, Internal_GfxPackageSupport.getNullIconHandle());
       
   255     }
       
   256 }
       
   257 
       
   258 /**
       
   259  * Sets the receiver's tool tip text to the argument, which
       
   260  * may be null indicating that no tool tip text should be shown.
       
   261  *
       
   262  * @param value the new tool tip text (or null)
       
   263  *
       
   264  * @exception SWTException <ul>
       
   265  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   266  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   267  * </ul>
       
   268  */
       
   269 public void setToolTipText (String string) {
       
   270     checkWidget ();
       
   271     toolTipText = string;
       
   272     OS.QSystemTrayIcon_setToolTip( handle, string);
       
   273 }
       
   274 
       
   275 /**
       
   276  * Makes the receiver visible if the argument is <code>true</code>,
       
   277  * and makes it invisible otherwise. 
       
   278  *
       
   279  * @param visible the new visibility state
       
   280  *
       
   281  * @exception SWTException <ul>
       
   282  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
       
   283  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
       
   284  * </ul>
       
   285  */
       
   286 public void setVisible (boolean visible) {
       
   287     checkWidget ();
       
   288     if (OS.QSystemTrayIcon_isVisible(handle) == visible) return;
       
   289     if (visible) {
       
   290         /*
       
   291         * It is possible (but unlikely), that application
       
   292         * code could have disposed the widget in the show
       
   293         * event.  If this happens, just return.
       
   294         */
       
   295         sendEvent (SWT.Show);
       
   296         if (isDisposed ()) return;
       
   297     } else {
       
   298         sendEvent (SWT.Hide);
       
   299     }
       
   300     OS.QSystemTrayIcon_setVisible(handle, visible);
       
   301 }
       
   302 }