1
2
3
4
5
6
7 package net.sf.gumshoe;
8
9 import java.util.Iterator;
10 import java.util.Vector;
11
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.widgets.*;
16
17 /***
18 * @author Gabor
19 *
20 * TODO To change the template for this generated type comment go to
21 * Window - Preferences - Java - Code Style - Code Templates
22 */
23 public abstract class TrayApp {
24
25 protected Display display = new Display();
26
27 protected Shell shell = new Shell(display);
28
29 TrayItem item = null;
30
31 protected final Menu liMenu = new Menu(shell, SWT.POP_UP);
32
33 protected final Menu reMenu = new Menu(shell, SWT.POP_UP);
34
35 private String tooltip = null;
36
37 private Image icon = null;
38
39 private Vector forDispose = new Vector();
40
41 public void addForDispose(Object disposable) {
42 forDispose.add(disposable);
43 }
44
45 public void start(String[] args) {
46 processArgs(args);
47 initGUI();
48 showGUI();
49 }
50
51 public abstract void initGUI();
52
53 public void setToolTip(String tooltip) {
54 this.tooltip = tooltip;
55 }
56
57 public void setIcon(Image icon) {
58 this.icon = icon;
59 addForDispose(this.icon);
60 }
61
62 public void setIcon(String iconPath) {
63 setIcon(new Image(display, getClass().getResourceAsStream(iconPath)));
64 }
65
66 /***
67 * @return the current Shell for this App
68 */
69 protected Shell getShell() {
70 return shell;
71 }
72
73 private void showGUI() {
74 item = createTray(display, tooltip, icon);
75 addForDispose(item);
76 addForDispose(icon);
77
78 if (liMenu.getItemCount() > 0) {
79 item.addListener(SWT.Selection, new Listener() {
80 public void handleEvent(Event event) {
81 liMenu.setVisible(true);
82 }
83 });
84 }
85 if (reMenu.getItemCount() > 0) {
86 item.addListener(SWT.MenuDetect, new Listener() {
87 public void handleEvent(Event event) {
88 reMenu.setVisible(true);
89 }
90 });
91 }
92
93
94 shell.setBounds(0, 0, 0, 0);
95 shell.open();
96 shell.setVisible(false);
97 while (!shell.isDisposed()) {
98 if (!display.readAndDispatch())
99 display.sleep();
100 }
101 display.dispose();
102 }
103
104 public void exit() {
105 dispose();
106 System.exit(0);
107 }
108
109 public void dispose() {
110 for (Iterator iter = forDispose.iterator(); iter.hasNext();) {
111 Object o = iter.next();
112 try {
113 o.getClass().getMethod("dispose", null).invoke(o, null);
114 } catch (Exception e) {
115
116 }
117 }
118 }
119
120 protected abstract void processArgs(String[] args);
121
122 private TrayItem createTray(Display display, String tooltip, final Image image) {
123 final Tray tray = display.getSystemTray();
124 final TrayItem item = new TrayItem(tray, SWT.NONE);
125 item.setImage(image);
126 if (tooltip != null) {
127 item.setToolTipText(tooltip);
128 }
129 return item;
130 }
131
132 public MenuItem createMenu(Menu menu, String text, MenuCommand cmd, int style) {
133 MenuItem mi = new MenuItem(menu, style);
134 mi.setText(text);
135 mi.addSelectionListener(new MenuCommandAdapter(cmd) {
136 public void widgetSelected(SelectionEvent arg0) {
137 cmd.perform();
138 }
139 });
140 return mi;
141 }
142 }