1 package org.votech.ds6.plastlets;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5
6 import javax.swing.Icon;
7 import javax.swing.JFrame;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jdesktop.jdic.tray.SystemTray;
12 import org.jdesktop.jdic.tray.TrayIcon;
13
14 /***
15 * Looks after whether the frame is visible or not.
16 * Adds the supplied icon to the systray, if possible.
17 * @author jdt
18 *
19 */
20 public class VisibilityManager {
21 /***
22 * Logger for this class
23 */
24 private static final Log logger = LogFactory.getLog(VisibilityManager.class);
25
26 private JFrame frame;
27
28 private TrayIcon trayIcon;
29
30 public VisibilityManager(JFrame monitoredFrame, Icon imageicon) {
31 this.frame = monitoredFrame;
32 if (!CheckJDICPresent.systemTray.check()) {
33 logger.info("No system tray integration available");
34 return;
35 }
36 trayIcon = new TrayIcon(imageicon);
37 trayIcon.setToolTip(VISIBLE_TIP);
38 trayIcon.addActionListener(new ActionListener() {
39
40 public void actionPerformed(ActionEvent arg0) {
41 boolean visible = frame.isVisible();
42 setVisible(!visible);
43
44 }
45
46 });
47 SystemTray.getDefaultSystemTray().addTrayIcon(trayIcon);
48 }
49 final private String VISIBLE_TIP="Left click to hide Plaslets Window";
50 final private String INVISIBLE_TIP="Left click to show Plaslets Window";
51 /***
52 * Set the controlled frame visible, or not.
53 * @param vis
54 */
55 public void setVisible(boolean vis) {
56 frame.setVisible(vis);
57 frame.setExtendedState(JFrame.NORMAL);
58 String trayIconTip = vis ? VISIBLE_TIP : INVISIBLE_TIP ;
59 if (trayIcon !=null) {
60 trayIcon.setToolTip(trayIconTip);
61 }
62 }
63
64
65
66 }