Updated .gitignore
[FinalKeyGui.git] / src / fkgui / MainWin.java
1 package fkgui;
2
3
4 import  java.util.prefs.*;
5 import java.awt.*;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.beans.PropertyChangeEvent;
9 import java.beans.PropertyChangeListener;
10
11 import javax.swing.UIManager;
12 import javax.swing.UnsupportedLookAndFeelException;
13
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.Shell;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.ShellEvent;
21 import org.eclipse.swt.events.ShellListener;
22 import org.eclipse.swt.widgets.Text;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.wb.swt.SWTResourceManager;
26
27 import fkgui.SerialWorker.SerialState;
28
29 public class MainWin implements PropertyChangeListener, ConsoleMsg {
30
31         protected Shell shell;
32         private Text txtPsw;
33         public TrayIcon trayIcon;
34         public PopupMenu popup;
35         public MenuItem showMain;
36         public MenuItem hideMain;
37         public Text txtLog;
38         public Button btnStart;
39         private Text txtDev;
40         Preferences prefs;
41         
42         SerialWorker fkSerial;
43         private boolean sysTrayIconVisible;
44
45         
46         static final String PORT_PREF ="lastUsedPortPref";
47         static final String DEFAULT_DEVICE = "/dev/FinalKey";
48  
49         
50         /**
51          * Launch the application.
52          * @param args
53          */
54         public static void main(String[] args) {
55
56                 try {
57                         MainWin window = new MainWin();
58                         window.open();
59                 } catch (Exception e) {
60                         e.printStackTrace();
61                 }
62                 
63         }
64
65         /**
66          * Open the window.
67          */
68         public void open() {
69                 Display display = Display.getDefault();
70                 createContents();
71                 shell.open();
72                 shell.layout();
73                 
74                 while (!shell.isDisposed()) {
75                         if (!display.readAndDispatch()) {
76                                 display.sleep();                                
77                         }
78                 }
79         }
80         
81         public void log( String str )
82         {
83                 txtLog.append(str+"\n");
84                 txtLog.redraw();
85                 shell.redraw();
86                 System.out.println(str);
87         }
88
89         /**
90          * Create a systemTray icon
91          */
92         private void createSysTrayIcon()
93         {
94                 sysTrayIconVisible=true;
95         //Check the SystemTray is supported
96         if (!SystemTray.isSupported()) {
97             log("SystemTray is not supported, app is useless");
98             return;
99         }
100         
101         try {
102                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
103                 } catch (ClassNotFoundException e1) {
104                         // TODO Auto-generated catch block
105                         e1.printStackTrace();
106                 } catch (InstantiationException e1) {
107                         // TODO Auto-generated catch block
108                         e1.printStackTrace();
109                 } catch (IllegalAccessException e1) {
110                         // TODO Auto-generated catch block
111                         e1.printStackTrace();
112                 } catch (UnsupportedLookAndFeelException e1) {
113                         // TODO Auto-generated catch block
114                         e1.printStackTrace();
115                 }
116
117         popup = new PopupMenu();
118         trayIcon =
119                 new TrayIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource("finalkey.png")));
120         trayIcon.setImageAutoSize(true);
121         final SystemTray tray = SystemTray.getSystemTray();
122        
123         // Create a pop-up menu components
124         showMain = new MenuItem("Show FinalKey");
125         hideMain = new MenuItem("Hide FinalKey");
126
127         showMain.addActionListener(new ActionListener() {
128                         @Override
129                         public void actionPerformed(ActionEvent e) {
130                                 Display.getDefault().syncExec( new Runnable(){
131                                         
132                                         public void run()
133                                         {
134                                 //Remove myself and make main window visible
135                                                 showFromTray();
136                                         }
137                                 } );
138                         }
139                                 
140                 });
141         
142         hideMain.addActionListener(new ActionListener() {
143                         @Override
144                         public void actionPerformed(ActionEvent e) {
145                                 Display.getDefault().syncExec( new Runnable(){
146                                         
147                                         public void run()
148                                         {
149                                                 hideToTray();
150                                         }
151                                 } );
152                         }
153                                 
154                 });
155         
156
157
158         
159         //Add components to pop-up menu
160         popup.add(hideMain);
161         popup.addSeparator();
162
163
164         trayIcon.setPopupMenu(popup);
165        
166         try {
167             tray.add(trayIcon);
168         } catch (AWTException e) {
169                 log("TrayIcon could not be added.");
170         }       
171         
172         }
173         
174         private void hideToTray()
175         {
176                 popup.remove(0);
177                 popup.insert(showMain, 0);
178                 shell.setVisible(false);
179         }
180         
181         private void showFromTray()
182         {
183                 popup.remove(0);
184                 popup.insert(hideMain, 0);
185                 shell.setVisible(true);
186                 shell.forceActive();
187         }
188         
189         private void destroySysTrayIcon()
190         {
191                 sysTrayIconVisible=false;
192                 SystemTray.getSystemTray().remove(trayIcon);
193         }
194         
195         public void shutDownApp()
196         {
197                 if(fkSerial != null)
198                 {
199                         fkSerial.disconnect();
200                 }
201                 if(sysTrayIconVisible == true)
202                 {
203                         destroySysTrayIcon();
204                 }
205                 System.exit(0);
206         }
207         
208         /**
209          * Create contents of the window.
210          */
211         protected void createContents() {
212                 shell = new Shell();
213                 shell.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/finalkey.png"));
214                 shell.setSize(450, 495);
215                 shell.setText("Final Key (Not connected)");
216                 
217                 shell.setLayout(null);
218
219                 
220                 prefs = Preferences.userNodeForPackage(this.getClass());
221                 
222
223                 fkSerial = new SerialWorker(this);
224                 fkSerial.addPropertyChangeListener(this);
225                 
226
227                 btnStart = new Button(shell, SWT.NONE);
228                 btnStart.setBounds(330, 10, 104, 52);
229                 btnStart.addSelectionListener(new SelectionAdapter() {
230                         public void widgetSelected(SelectionEvent e) {
231                                 
232                                 if( fkSerial.state == SerialState.Connected )
233                                 {
234                                         shell.setText("Final Key (Not connected)");
235                                         fkSerial.disconnect();
236                                         txtPsw.setVisible(true);
237                                         txtDev.setVisible(true);
238                                         
239                                         btnStart.setText("Connect");
240                                 } else {
241                                         prefs.put(PORT_PREF, txtDev.getText() );
242                                         fkSerial.connect(txtDev.getText(),txtPsw.getText());
243                                         //Eat the password for security reasons.
244                                         txtPsw.setText("");
245                                         txtPsw.setVisible(false);
246                                         txtDev.setVisible(false);
247                                         btnStart.setText("Disconnect");
248                                         shell.setText("Final Key (Connected)");
249                                         
250                                 }
251                         }
252                 });
253                 btnStart.setText("Connect");
254                 Label lblPassword = new Label(shell, SWT.NONE);
255                 lblPassword.setText("Password");
256                 lblPassword.setBounds(10, 39, 85, 23);
257                 
258                 txtPsw = new Text(shell, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
259                 txtPsw.setBounds(101, 39, 223, 23);
260                 
261                 txtLog = new Text(shell, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
262                 txtLog.setEditable(false);
263                 txtLog.setBounds(10, 68, 424, 388);
264                 
265                 Label lblPort = new Label(shell, SWT.NONE);
266                 lblPort.setBounds(10, 10, 76, 23);
267                 lblPort.setText("Port");
268                 
269                 txtDev = new Text(shell, SWT.BORDER);
270                 txtDev.setFont(SWTResourceManager.getFont("Cantarell", 9, SWT.NORMAL));
271                 txtDev.setText( prefs.get(PORT_PREF, DEFAULT_DEVICE));
272                 txtDev.setBounds(101, 10, 223, 23);
273                 shell.setTabList(new Control[]{txtPsw, btnStart});
274                 
275                 log("Welcome!\nConnect your Final Key and enter password.\nThen press connect.\nPress the button when it blinks.\n----------\n");
276
277
278                 createSysTrayIcon();
279                 
280                 
281                 
282                 shell.addShellListener( new ShellListener() {
283                         
284                         public void shellIconified(ShellEvent e) {
285
286                                         
287
288                         }
289                         
290                         public void shellDeiconified(ShellEvent e) {
291                                 // TODO Auto-generated method stub
292                                 
293                         }
294                         
295                         public void shellDeactivated(ShellEvent e) {
296                                 // TODO Auto-generated method stub
297                                 
298                         }
299                         
300                         public void shellClosed(ShellEvent e) {
301                                 shutDownApp();
302                                 
303                         }
304                         
305                         public void shellActivated(ShellEvent e) {
306                                 // TODO Auto-generated method stub
307                                 
308                         }
309                 } );
310                 
311
312
313         }
314
315         @Override
316         public void propertyChange(PropertyChangeEvent evt) {
317                 
318                 if( evt.getPropertyName().equals("serialState") )
319                 {
320                         System.out.println( "serialState changed from "+((SerialState)evt.getOldValue())+" to "+((SerialState)evt.getNewValue()) );
321                 }
322                 
323                 
324         }
325
326         @Override
327         public PopupMenu getPopup() {
328                 return this.popup;
329         }
330 }