components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/control/SystemTrayControl.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.swing.control;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import java.net.URL;
       
    31 import java.util.Map;
       
    32 import java.util.logging.Level;
       
    33 import javax.help.HelpSet;
       
    34 import com.oracle.solaris.vp.panel.common.*;
       
    35 import com.oracle.solaris.vp.panel.common.action.ActionAbortedException;
       
    36 import com.oracle.solaris.vp.panel.common.control.*;
       
    37 import com.oracle.solaris.vp.panel.common.model.PanelDescriptor;
       
    38 import com.oracle.solaris.vp.panel.swing.view.AuthPanel;
       
    39 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    40 
       
    41 public class SystemTrayControl<P extends PanelDescriptor>
       
    42     extends SwingControl<P, Component> implements ConnectionListener,
       
    43     ActionListener, MouseListener, MouseMotionListener {
       
    44 
       
    45     //
       
    46     // Instance data
       
    47     //
       
    48 
       
    49     private TrayIcon trayIcon;
       
    50     private PopupMenu popup;
       
    51 
       
    52     //
       
    53     // Constructors
       
    54     //
       
    55 
       
    56     public SystemTrayControl(String id, String name, ClientContext context) {
       
    57 	super(id, name, context);
       
    58     }
       
    59 
       
    60     public SystemTrayControl(String id, String name, P descriptor) {
       
    61 	super(id, name, descriptor);
       
    62     }
       
    63 
       
    64     //
       
    65     // ConnectionListener methods
       
    66     //
       
    67 
       
    68     @Override
       
    69     public void connectionChanged(ConnectionEvent event) {
       
    70 	if (popup != null) {
       
    71 	    String text = AuthPanel.toString(
       
    72 		getClientContext().getConnectionInfo(), true);
       
    73 	    popup.setLabel(text);
       
    74 	}
       
    75     }
       
    76 
       
    77     @Override
       
    78     public void connectionFailed(ConnectionEvent event) {
       
    79         // There's nothing particularly helpful we can do here except wait for
       
    80         // the repaired connection in connectionChanged
       
    81     }
       
    82 
       
    83     //
       
    84     // ActionListener methods
       
    85     //
       
    86 
       
    87     /**
       
    88      * As a convenience, this class is added as an {@code ActionListener} to the
       
    89      * {@link #createTrayIcon created} {@code TrayIcon}.  This method provides a
       
    90      * default empty implementation.
       
    91      */
       
    92     @Override
       
    93     public void actionPerformed(ActionEvent e) {
       
    94     }
       
    95 
       
    96     //
       
    97     // MouseListener methods
       
    98     //
       
    99 
       
   100     /**
       
   101      * As a convenience, this class is added as an {@code MouseListener} to the
       
   102      * {@link #createTrayIcon created} {@code TrayIcon}.  This method provides a
       
   103      * default empty implementation.
       
   104      */
       
   105     @Override
       
   106     public void mouseClicked(MouseEvent e) {
       
   107     }
       
   108 
       
   109     /**
       
   110      * As a convenience, this class is added as an {@code MouseListener} to the
       
   111      * {@link #createTrayIcon created} {@code TrayIcon}.  This method provides a
       
   112      * default empty implementation.
       
   113      */
       
   114     @Override
       
   115     public void mousePressed(MouseEvent e) {
       
   116     }
       
   117 
       
   118     /**
       
   119      * As a convenience, this class is added as an {@code MouseListener} to the
       
   120      * {@link #createTrayIcon created} {@code TrayIcon}.  This method provides a
       
   121      * default empty implementation.
       
   122      */
       
   123     @Override
       
   124     public void mouseReleased(MouseEvent e) {
       
   125     }
       
   126 
       
   127     /**
       
   128      * As a convenience, this class is added as an {@code MouseListener} to the
       
   129      * {@link #createTrayIcon created} {@code TrayIcon}.  This method provides a
       
   130      * default empty implementation.
       
   131      */
       
   132     @Override
       
   133     public void mouseEntered(MouseEvent e) {
       
   134     }
       
   135 
       
   136     /**
       
   137      * As a convenience, this class is added as an {@code MouseListener} to the
       
   138      * {@link #createTrayIcon created} {@code TrayIcon}.  This method provides a
       
   139      * default empty implementation.
       
   140      */
       
   141     @Override
       
   142     public void mouseExited(MouseEvent e) {
       
   143     }
       
   144 
       
   145     //
       
   146     // MouseMotionListener methods
       
   147     //
       
   148 
       
   149     /**
       
   150      * As a convenience, this class is added as an {@code MouseMotionListener}
       
   151      * to the {@link #createTrayIcon created} {@code TrayIcon}.  This method
       
   152      * provides a default empty implementation.
       
   153      */
       
   154     @Override
       
   155     public void mouseDragged(MouseEvent e) {
       
   156     }
       
   157 
       
   158     /**
       
   159      * As a convenience, this class is added as an {@code MouseMotionListener}
       
   160      * to the {@link #createTrayIcon created} {@code TrayIcon}.  This method
       
   161      * provides a default empty implementation.
       
   162      */
       
   163     @Override
       
   164     public void mouseMoved(MouseEvent e) {
       
   165     }
       
   166 
       
   167     //
       
   168     // Control methods
       
   169     //
       
   170 
       
   171     /**
       
   172      * Calls the superclass implementation and {@link #getTrayIconCreate
       
   173      * creates} this {@code SystemTrayControl}'s {@code TrayIcon} if necessary.
       
   174      */
       
   175     @Override
       
   176     public void start(Navigator navigator, Map<String, String> parameters)
       
   177 	throws NavigationAbortedException, InvalidParameterException,
       
   178 	NavigationFailedException {
       
   179 
       
   180 	super.start(navigator, parameters);
       
   181 
       
   182 	SystemTray tray;
       
   183 
       
   184 	try {
       
   185 	    tray = SystemTray.getSystemTray();
       
   186 	} catch (UnsupportedOperationException e) {
       
   187 	    throw new NavigationAbortedException(e);
       
   188 	}
       
   189 
       
   190 	trayIcon = getTrayIconCreate(tray);
       
   191 
       
   192 	try {
       
   193 	    tray.add(trayIcon);
       
   194 
       
   195 	// Shouldn't happen
       
   196 	} catch (AWTException e) {
       
   197 	    throw new NavigationAbortedException(e);
       
   198 	}
       
   199 
       
   200 	ClientContext context = getClientContext();
       
   201 	context.addConnectionListener(this);
       
   202 	connectionChanged(null);
       
   203     }
       
   204 
       
   205     @Override
       
   206     public void stop(boolean isCancel) throws NavigationAbortedException {
       
   207 	super.stop(isCancel);
       
   208 
       
   209 	ClientContext context = getClientContext();
       
   210 	context.removeConnectionListener(this);
       
   211 
       
   212 	try {
       
   213 	    SystemTray.getSystemTray().remove(trayIcon);
       
   214 	} catch (UnsupportedOperationException e) {
       
   215 	    getLog().log(Level.WARNING, "system tray no longer accessible!", e);
       
   216 	}
       
   217     }
       
   218 
       
   219     //
       
   220     // SystemTrayControl methods
       
   221     //
       
   222 
       
   223     /**
       
   224      * Configures the just-{@code #createTrayIcon created} {@code TrayIcon} for
       
   225      * this {@code SystemTrayControl}.	This method is called automatically just
       
   226      * after {@link #createTrayIcon}.
       
   227      * <p/>
       
   228      * Most subclasses can do all configuration in {@link #createTrayIcon}.
       
   229      * Only implementors that wish to provide a common configuration for all
       
   230      * {@code TrayIcon}s created by their subclasses need to implement this
       
   231      * method.
       
   232      * <p/>
       
   233      * Subclasses that override this method should call {@code
       
   234      * super.configTrayIcon(trayIcon)} to ensure that any present or future
       
   235      * functionality in any superclass implementation is executed.
       
   236      * <p/>
       
   237      * This default implementation adds a {@link #getPopupMenu popup menu} (if
       
   238      * non-null), then adds this class as an {@code ActionListener}, {@code
       
   239      * MouseListener}, and {@code MouseMotionListener} to the {@code TrayIcon}.
       
   240      *
       
   241      * @param	    trayIcon
       
   242      *		    the newly-created {@code TrayIcon}
       
   243      */
       
   244     protected void configTrayIcon(TrayIcon trayIcon) {
       
   245 	PopupMenu popup = getPopupMenu();
       
   246 	if (popup != null) {
       
   247 	    trayIcon.setPopupMenu(popup);
       
   248 	}
       
   249 
       
   250 	trayIcon.addActionListener(this);
       
   251 	trayIcon.addMouseListener(this);
       
   252 	trayIcon.addMouseMotionListener(this);
       
   253     }
       
   254 
       
   255     /**
       
   256      * Creates the {@code TrayIcon} for this {@code SystemTrayControl}.
       
   257      * <p/>
       
   258      * This default implementation does nothing and returns {@code null}.
       
   259      *
       
   260      * @return	    the newly-created {@code TrayIcon}
       
   261      */
       
   262     protected TrayIcon createTrayIcon(SystemTray tray) {
       
   263 	return null;
       
   264     }
       
   265 
       
   266     /**
       
   267      * Gets, creating if necessary, a {@code PopupMenu} to display when the
       
   268      * {@link #getTrayIcon system tray icon} is clicked. This popup menu shall
       
   269      * include {@link #createLoginItem login}, {@link #createHelpItem help},
       
   270      * and {@link #createExitItem exit}, menu items.
       
   271      *
       
   272      * @return	    a {@code PopupMenu}, or {@code null} to display no popop
       
   273      */
       
   274     protected PopupMenu getPopupMenu() {
       
   275 	if (popup == null) {
       
   276 	    popup = new PopupMenu();
       
   277 
       
   278 	    MenuItem loginItem = createLoginItem();
       
   279 	    MenuItem helpItem = createHelpItem();
       
   280 	    MenuItem exitItem = createExitItem();
       
   281 
       
   282 	    if (loginItem != null) {
       
   283 		popup.add(loginItem);
       
   284 	    }
       
   285 	    if (helpItem != null) {
       
   286 		popup.add(helpItem);
       
   287 	    }
       
   288 	    if (exitItem != null) {
       
   289 		popup.add(exitItem);
       
   290 	    }
       
   291 	}
       
   292 	return popup;
       
   293     }
       
   294 
       
   295     /**
       
   296      * Creates a login {@code MenuItem} object for use with the
       
   297      * {@link #getTrayIcon system tray icon} popup menu.
       
   298      *
       
   299      * @return	    a {@code MenuItem}, or {@code null} to display no item
       
   300      */
       
   301     protected MenuItem createLoginItem() {
       
   302 	MenuItem loginItem = new MenuItem(Finder.getString("tray.popup.login"));
       
   303 	loginItem.addActionListener(
       
   304 	    new ActionListener() {
       
   305 		@Override
       
   306 		public void actionPerformed(ActionEvent e) {
       
   307 		    PanelFrameControl.promptForLogin(getClientContext());
       
   308 		}
       
   309 	    });
       
   310 	return loginItem;
       
   311     }
       
   312 
       
   313     /**
       
   314      * Creates a help {@code MenuItem} object for use with the
       
   315      * {@link #getTrayIcon system tray icon} popup menu.
       
   316      *
       
   317      * @return	    a {@code MenuItem}, or {@code null} to display no item
       
   318      */
       
   319     protected MenuItem createHelpItem() {
       
   320 	MenuItem helpItem = null;
       
   321 	HelpSet helpSet = getPanelDescriptor().getHelpSet();
       
   322 	if (helpSet != null) {
       
   323 	    final URL url = getHelpURL(helpSet);
       
   324 	    if (url != null) {
       
   325 		helpItem = new MenuItem(Finder.getString("tray.popup.help"));
       
   326 		helpItem.addActionListener(
       
   327 		    new ActionListener() {
       
   328 			@Override
       
   329 			public void actionPerformed(ActionEvent e) {
       
   330 			    ClientContext context = getClientContext();
       
   331 			    context.getHelpBroker().setCurrentURL(url);
       
   332 			    context.showHelp();
       
   333 			}
       
   334 		    });
       
   335 	    }
       
   336 	}
       
   337         return helpItem;
       
   338     }
       
   339 
       
   340     /**
       
   341      * Creates a exit {@code MenuItem} object for use with the
       
   342      * {@link #getTrayIcon system tray icon} popup menu.
       
   343      *
       
   344      * @return	    a {@code MenuItem}, or {@code null} to display no item
       
   345      */
       
   346     protected MenuItem createExitItem() {
       
   347 	MenuItem exitItem = new MenuItem(Finder.getString("tray.popup.exit"));
       
   348 	exitItem.addActionListener(
       
   349 	    new ActionListener() {
       
   350 		@Override
       
   351 		public void actionPerformed(ActionEvent e) {
       
   352 		    getNavigator().asyncExec(
       
   353 			new Runnable() {
       
   354 			    @Override
       
   355 			    public void run() {
       
   356 				try {
       
   357 				    getClientContext().closeInstance(true);
       
   358 				} catch (ActionAbortedException ignore) {
       
   359 				}
       
   360 			    }
       
   361 			});
       
   362 		}
       
   363 	    });
       
   364 	return exitItem;
       
   365     }
       
   366 
       
   367     /**
       
   368      * Gets the {@code TrayIcon} for this {@code SystemTrayControl}.
       
   369      *
       
   370      * @return	    a {@code TrayIcon}, or {@code null} if this {@code
       
   371      *		    SystemTrayControl} has no {@code TrayIcon}.
       
   372      *
       
   373      * @see	    #setTrayIcon
       
   374      */
       
   375     public TrayIcon getTrayIcon() {
       
   376 	return trayIcon;
       
   377     }
       
   378 
       
   379     /**
       
   380      * Gets the {@code TrayIcon} for this {@code SystemTrayControl}, {@link
       
   381      * #createTrayIcon creating} it and {@code #setTrayIcon setting} it first if
       
   382      * necessary.
       
   383      * <p/>
       
   384      * This method is not thread-safe; external locking may be necessary to
       
   385      * ensure that {@link #createTrayIcon} is not called by multiple threads.
       
   386      *
       
   387      * @return	    a {@code TrayIcon}, or {@code null} if this {@code
       
   388      *		    SystemTrayControl} has no {@code TrayIcon}.
       
   389      */
       
   390     public TrayIcon getTrayIconCreate(SystemTray tray) {
       
   391 	TrayIcon trayIcon = getTrayIcon();
       
   392 	if (trayIcon == null) {
       
   393 	    trayIcon = createTrayIcon(tray);
       
   394 	    if (trayIcon != null) {
       
   395 		setTrayIcon(trayIcon);
       
   396 		configTrayIcon(trayIcon);
       
   397 	    }
       
   398 	}
       
   399 	return trayIcon;
       
   400     }
       
   401 
       
   402     /**
       
   403      * Gets the {@code TrayIcon} for this {@code SystemTrayControl}.
       
   404      *
       
   405      * @param	    trayIcon
       
   406      *		    a {@code TrayIcon}, or {@code null} if this {@code
       
   407      *		    SystemTrayControl} has no {@code TrayIcon}.
       
   408      *
       
   409      * @see	    #getTrayIcon
       
   410      */
       
   411     protected void setTrayIcon(TrayIcon trayIcon) {
       
   412 	this.trayIcon = trayIcon;
       
   413     }
       
   414 }