View Javadoc

1   package com.melloware.jukes.tray;
2   
3   import java.awt.AWTException;
4   import java.awt.Frame;
5   import java.awt.Image;
6   import java.awt.SystemTray;
7   import java.awt.TrayIcon;
8   
9   import javax.swing.JFrame;
10  import javax.swing.JPopupMenu;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  import com.jgoodies.uif.action.ActionManager;
16  import com.jgoodies.uif.builder.PopupMenuBuilder;
17  import com.melloware.jukes.file.image.ImageFactory;
18  import com.melloware.jukes.gui.tool.Actions;
19  import com.melloware.jukes.gui.view.MainMenuBuilder;
20  
21  /**
22   * Tray icon using JDK 6 SystemTray class.  
23   * <p>
24   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
25   * @author Emil A. Lefkof III <info@melloware.com>
26   * @version 4.0
27   */
28  public class JukesTrayIcon
29      implements ITrayIcon {
30  
31      private static final Log LOG = LogFactory.getLog(JukesTrayIcon.class);
32      private final JFrame parentWindow;
33      private final SystemTray tray;
34      private TrayIcon trayIcon;
35  
36      /**
37      * Default Constructor.
38      * <p>
39      * @param aParentWindow the JFrame to control this tray icon
40      * @throws AWTException if any error occurs initializing
41      */
42      public JukesTrayIcon(JFrame aParentWindow) throws AWTException {
43          super();
44          this.parentWindow = aParentWindow;
45          tray = SystemTray.getSystemTray();
46          initialize();
47      }
48  
49      /* (non-Javadoc)
50       * @see com.melloware.jukes.tray.ITrayIcon#isAvailable()
51       */
52      public boolean isAvailable() {
53          return true;
54      }
55  
56      /**
57       * Builds and returns the Tray Icon Menu.
58       */
59      private JPopupMenu buildTrayMenu() {
60          PopupMenuBuilder builder = new PopupMenuBuilder(MainMenuBuilder.CATALOG);
61          builder.add(ActionManager.get(Actions.HELP_ABOUT_DIALOG_ID));
62          builder.addSeparator();
63          builder.add(ActionManager.get(Actions.PREFERENCES_ID));
64          builder.addSeparator();
65          builder.add(ActionManager.get(Actions.PLAYER_PLAY_ID));
66          builder.add(ActionManager.get(Actions.PLAYER_PAUSE_ID));
67          builder.add(ActionManager.get(Actions.PLAYER_STOP_ID));
68          builder.add(ActionManager.get(Actions.PLAYER_PREVIOUS_ID));
69          builder.add(ActionManager.get(Actions.PLAYER_NEXT_ID));
70          builder.addSeparator();
71          builder.add(ActionManager.get(Actions.APP_HIDE_ID));
72          builder.add(ActionManager.get(Actions.APP_SHOW_ID));
73          builder.add(ActionManager.get(Actions.EXIT_ID));
74          return builder.getPopupMenu();
75      }
76  
77      /* (non-Javadoc)
78       * @see com.melloware.jukes.tray.ITrayIcon#cleanUp()
79       */
80      public void cleanUp() {
81          LOG.debug("Cleaning up Unix Tray Icon");
82  
83      }
84  
85      /* (non-Javadoc)
86       * @see com.melloware.jukes.tray.ITrayIcon#hideWindow()
87       */
88      public void hideWindow() {
89          if (parentWindow.isVisible()) {
90              parentWindow.setVisible(false);
91              ActionManager.get(Actions.APP_HIDE_ID).setEnabled(false);
92              ActionManager.get(Actions.APP_SHOW_ID).setEnabled(true);
93          }
94      }
95  
96      /**
97      * When the icon is single clicked in the tray. For now it will call pause
98      * and play of the player.
99      */
100     public void leftClicked() {
101        ActionManager.get(Actions.PLAYER_PAUSE_ID).actionPerformed(null);
102     }
103 
104     /**
105      * When the icon is middle button clicked.  For now advance to next song
106      * when this button is pressed.
107      */
108     public void middleClicked() {
109         ActionManager.get(Actions.PLAYER_NEXT_ID).actionPerformed(null);
110     }
111 
112     /* (non-Javadoc)
113      * @see com.melloware.jukes.tray.ITrayIcon#showWindow()
114      */
115     public void showWindow() {
116         if (!parentWindow.isVisible()) {
117             parentWindow.setVisible(true);
118             ActionManager.get(Actions.APP_HIDE_ID).setEnabled(true);
119             ActionManager.get(Actions.APP_SHOW_ID).setEnabled(false);
120         }
121 
122         // restore the window if it was minimized
123         parentWindow.setState(Frame.NORMAL);
124     }
125 
126     /**
127      * Initialize the Tray Icon and resources.
128      * @throws AWTException if any tray icon exception occurs
129      */
130     private void initialize() throws AWTException {
131         LOG.info("Initialize Unix Tray Icon.");
132 
133         // Init the Tray Icon library given the name for the hidden window
134         trayIcon = new java.awt.TrayIcon(ImageFactory.ICO_TRAYICON.getImage(), parentWindow.getTitle(), null);
135         trayIcon.setImageAutoSize(true);
136 
137         // add all listeners
138         trayIcon.addMouseListener(new JukesTrayMouseAdapter(this, buildTrayMenu()));
139         
140         tray.add(trayIcon);
141     }
142 
143 	/* (non-Javadoc)
144 	 * @see com.melloware.jukes.tray.ITrayIcon#changeImage(java.awt.Image)
145 	 */
146 	public void changeImage(Image aImage) {
147 		trayIcon.setImage(aImage);
148 	}
149 
150 	/* (non-Javadoc)
151 	 * @see com.melloware.jukes.tray.ITrayIcon#setToolTip(java.lang.String)
152 	 */
153 	public void setToolTip(String aTip) {
154 		trayIcon.setToolTip(aTip);
155 	}
156 
157 }