1   package org.minetti.astrodevice.client.gui;
2   
3   import org.eclipse.swt.SWT;
4   import org.eclipse.swt.graphics.Image;
5   import org.eclipse.swt.widgets.Display;
6   import org.eclipse.swt.widgets.Event;
7   import org.eclipse.swt.widgets.Listener;
8   import org.eclipse.swt.widgets.Menu;
9   import org.eclipse.swt.widgets.MenuItem;
10  import org.eclipse.swt.widgets.Shell;
11  import org.eclipse.swt.widgets.Tray;
12  import org.eclipse.swt.widgets.TrayItem;
13  import org.minetti.astrodevice.client.gui.menu.SwtObservingSiteMenu;
14  import org.minetti.astrodevice.client.gui.menu.SwtTelescopeMenu;
15  import org.minetti.astrodevice.client.i18n.I18n;
16  
17  
18  
19  
20  
21  public final class SwtGui
22  {
23  
24     
25  
26  
27     private static final SwtGui INSTANCE = new SwtGui ();
28     
29     
30  
31  
32     protected final class ExitSelectionListener
33              implements Listener
34     {
35  
36        
37  
38  
39  
40        public void handleEvent (final Event event)
41        {
42           System.exit(0);
43        }
44  
45     }
46  
47     
48  
49  
50     private final Display display;
51     
52     
53  
54  
55     private final Shell shell;
56     
57     
58     
59  
60  
61     private final SwtObservingSiteMenu observingSiteMenu = new SwtObservingSiteMenu ();
62     
63     
64  
65  
66     private final SwtTelescopeMenu telescopeMenu = new SwtTelescopeMenu();
67  
68  
69     
70  
71  
72     private SwtGui ()
73     {
74        super ();
75        this.display = new Display();
76        this.shell = new Shell(this.display);
77     }
78  
79     
80  
81  
82  
83     public static SwtGui getInstance ()
84     {
85        return SwtGui.INSTANCE;
86     }
87  
88     
89  
90  
91     public void build ()
92     {
93        Image image = new Image(this.display, 16, 16);
94        final Tray tray = this.display.getSystemTray();
95        if (tray == null) {
96           System.err.println("The system tray is not available");
97         } else {
98  
99            final TrayItem item = new TrayItem(tray, SWT.NONE);
100           item.setToolTipText("SWT TrayItem");
101           
102           final Menu menu = new Menu(this.shell, SWT.POP_UP);
103           
104           
105           this.observingSiteMenu.build (this.shell, menu);
106           
107           
108           this.telescopeMenu.build (this.shell, menu);
109           
110           
111           
112           
113           
114           MenuItem menuItem = new MenuItem (menu, SWT.PUSH);
115           menuItem.setText (I18n.getGui ().getString ("menu.exit.text"));
116           menuItem.addListener (SWT.Selection, new ExitSelectionListener ());
117           
118           
119            item.addListener(SWT.MenuDetect, new Listener() {
120              public void handleEvent(Event event) {
121                menu.setVisible(true);
122              }
123            });
124            
125            item.setImage(image);
126           
127           
128 
129           while (!this.shell.isDisposed()) {
130              if (!this.display.readAndDispatch())
131                 this.display.sleep();
132           }
133           
134           this.display.dispose();
135           
136        }
137    }
138 
139    
140 
141 
142    public void release ()
143    {
144 
145       
146       
147       this.display.dispose();
148    }
149 
150 }