Fixed stuff.
[FinalKeyGui.git] / src / fkgui / MainWin.java
1 package fkgui;
2
3
4 import java.util.Map;
5 import java.util.Vector;
6 import  java.util.prefs.*;
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10
11 import javax.swing.UIManager;
12
13 import org.eclipse.jface.viewers.DoubleClickEvent;
14 import org.eclipse.jface.viewers.IDoubleClickListener;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ListViewer;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Listener;
21 import org.eclipse.swt.widgets.MessageBox;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.FocusListener;
27 import org.eclipse.swt.events.KeyEvent;
28 import org.eclipse.swt.events.KeyListener;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.events.ShellEvent;
33 import org.eclipse.swt.events.ShellListener;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.Device;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.wb.swt.SWTResourceManager;
40
41 import fkgui.FkManager.Account;
42 import fkgui.SerialWorker.SerialState;
43 import fkgui.UpdateChecker.UpdateCheckResultListener;
44
45 import org.eclipse.swt.widgets.TabFolder;
46 import org.eclipse.swt.widgets.TabItem;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.layout.FormLayout;
49 import org.eclipse.swt.layout.FormData;
50 import org.eclipse.swt.layout.FormAttachment;
51 import org.eclipse.swt.layout.FillLayout;
52 import org.eclipse.swt.widgets.List;
53
54
55 public class MainWin implements ConsoleMsg, UpdateCheckResultListener {
56
57         protected Shell shell;
58         private Text txtPsw;
59         public TrayIcon trayIcon;
60         public PopupMenu popup;
61         public MenuItem showMain;
62         public MenuItem hideMain;
63
64         public Text txtLog;
65         public Button btnConnect;
66         public Label lblPort;
67         public Label lblPassword;
68         
69         public Animation animation;
70         
71         public TabFolder tabFolder;
72
73         private Text txtDev;
74         Preferences prefs;
75         MainWin mySelf;
76         private SerialState lastState = SerialState.Disconnected;
77         
78         SerialWorker fkSerial;
79         private boolean sysTrayIconVisible;
80         
81         Button btnOpenAccount;
82         private Color blackColor;
83         private Color grayColor;
84         private Color defaultBgColor;
85         private Color greenColor;
86         private Color redColor;
87         
88         private static final String PREF_PORT ="lastUsedPortPref"; //$NON-NLS-1$
89         private static final String PREF_DEFAULT_DEVICE = "/dev/FinalKey"; //$NON-NLS-1$
90         private static final String PREF_AUTOHIDE = "hideMainWinAfterConnect"; //$NON-NLS-1$
91         private static final String PREF_SORT_BY_ID_KEY = "sortAccountListById"; //$NON-NLS-1$
92         private static final String PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY = "showAccountIdInName"; //$NON-NLS-1$
93         private static final String PREF_ALLOW_UPDATE_CHECK = "allowUpdateCheck"; //$NON-NLS-1$
94         private static final String PREF_SHOW_ACCOUNTS_READY_NOTICE = "showAccountsReadyNotice"; //$NON-NLS-1$
95
96         public Composite cmpConnect;
97         private Composite cmpAccounts;
98         ListViewer lstAccounts;
99         private List lstAccountsControl;
100         Label lblNumFree;
101         Button btnNewAccount;
102         Button chkCheckForUpdates;
103         Boolean hiddenAtStart=false;
104         Button btnShowaccountsReady;
105  
106         //Icons
107         private org.eclipse.swt.graphics.Image iconProgramOnline  = SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/finalkey.png");  //$NON-NLS-1$
108         private org.eclipse.swt.graphics.Image iconProgramOffline  = SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/finalkey-big-offline.png");  //$NON-NLS-1$
109         private Image iconSystrayOnline = Toolkit.getDefaultToolkit().createImage(getClass().getResource("gfx/systray-color.png"));  //$NON-NLS-1$
110         private Image iconSystrayOffline= Toolkit.getDefaultToolkit().createImage(getClass().getResource("gfx/systray-offline.png"));  //$NON-NLS-1$
111         private TabItem tbtmSettings;
112         private Composite cmpSettings;
113         private Button chkAutoHide;
114         private Button chkSortByAccountId;
115         private Button chkShowAccountId;
116         private Thread updateCheckThread;
117         private Text txtFilter;
118         
119         
120         /**
121          * Launch the application.
122          * @param args
123          */
124         public static void main(String[] args) {
125
126                 try {
127                         MainWin window = new MainWin();
128
129                         for(String s : args )
130                         {
131                                 if( s.compareTo("--hide") == 0 ) //$NON-NLS-1$
132                                 {
133                                         window.hiddenAtStart=true;
134                                 }
135                         }
136
137                         window.open();
138                 } catch (Exception e) {
139                         e.printStackTrace();
140                 }
141                 
142         }
143
144         /**
145          * Open the window.
146          */
147         public void open() {
148                 Display display = Display.getDefault();
149                 createContents();
150                 addAccountsTab();
151                 addSettingsTab();
152                 createSysTrayIcon();
153
154                 serialEvent(SerialState.Disconnected);
155
156
157                 checkForUpdates();
158
159                 if( hiddenAtStart )
160                 {
161                         hideToTray();
162                 } else {
163                         shell.open();
164                         shell.layout();
165                 }
166
167                 while (!shell.isDisposed()) {
168                         if (!display.readAndDispatch()) {
169                                 display.sleep();                                
170                         }
171                 }
172         }
173         
174         private void checkForUpdates() {
175                 if( prefs.getBoolean(PREF_ALLOW_UPDATE_CHECK, true) )
176                 {
177                         if( updateCheckThread == null )
178                         {
179                                 updateCheckThread = new Thread(new UpdateChecker(this));
180                                 updateCheckThread.start();
181                         } else {
182                                 System.out.println(Messages.MainWin_0);
183                         }
184                 } else {
185                         System.out.println(Messages.MainWin_3);
186                 }
187         }
188
189         public void log( String str )
190         {
191                 txtLog.append(str+"\n"); //$NON-NLS-1$
192                 if( tabFolder.getSelectionIndex() == 0 )
193                 {
194                         txtLog.redraw();
195                         shell.redraw();
196                 }
197                 System.out.println(str);
198
199         }
200
201         /**
202          * Create a systemTray icon
203          */
204         private void createSysTrayIcon()
205         {
206                 sysTrayIconVisible=true;
207                 //Check the SystemTray is supported
208                 if (!SystemTray.isSupported()) {
209                         log("SystemTray is not supported, app is useless"); //$NON-NLS-1$
210                         return;
211                 }
212
213                 try {
214                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
215                 } catch (Exception e1) {
216                         e1.printStackTrace();
217                 } 
218
219                 popup = new PopupMenu();
220                 trayIcon = new TrayIcon( iconSystrayOffline ); //$NON-NLS-1$
221                 trayIcon.setToolTip("The Final Key - Hardware password manager"); //$NON-NLS-1$
222                 trayIcon.setImageAutoSize(true);
223                 final SystemTray tray = SystemTray.getSystemTray();
224
225                 // Create a pop-up menu components
226                 showMain = new MenuItem(Messages.MainWin_7);
227                 hideMain = new MenuItem(Messages.MainWin_8);
228
229                 showMain.addActionListener(new ActionListener() {
230                         @Override
231                         public void actionPerformed(ActionEvent e) {
232                                 Display.getDefault().syncExec( new Runnable(){
233
234                                         public void run()
235                                         {
236                                                 showFromTray();
237                                         }
238                                 } );
239                         }
240
241                 });
242
243                 hideMain.addActionListener(new ActionListener() {
244                         @Override
245                         public void actionPerformed(ActionEvent e) {
246                                 Display.getDefault().syncExec( new Runnable(){
247
248                                         public void run()
249                                         {
250                                                 hideToTray();
251                                         }
252                                 } );
253                         }
254
255                 });
256
257                 clearSystray();
258
259
260                 trayIcon.setPopupMenu(popup);
261
262                 try {
263                         tray.add(trayIcon);
264                 } catch (AWTException e) {
265                         log("TrayIcon could not be added."); //$NON-NLS-1$
266                 }
267
268         }
269
270         private void hideToTray()
271         {
272                 popup.remove(0);
273                 popup.insert(showMain, 0);
274                 shell.setVisible(false);
275         }
276
277         private void showFromTray()
278         {
279                 popup.remove(0);
280                 popup.insert(hideMain, 0);
281                 shell.setVisible(true);
282                 shell.forceActive();
283         }
284
285         private void destroySysTrayIcon()
286         {
287                 sysTrayIconVisible=false;
288                 SystemTray.getSystemTray().remove(trayIcon);
289         }
290
291         private void clearSystray() {
292                         popup.removeAll();
293                         //Add components to pop-up menu
294                         if(shell.isVisible()==true)
295                         {
296                                 popup.add(hideMain);
297                         } else {
298                                 popup.add(showMain);
299                         }
300                         popup.addSeparator();
301         }
302
303         public void shutDownApp()
304         {
305                 if(fkSerial != null)
306                 {
307                         fkSerial.disconnect();
308                 }
309                 if(sysTrayIconVisible == true)
310                 {
311                         destroySysTrayIcon();
312                 }
313                 System.exit(0);
314         }
315
316         /**
317          * Create contents of the window.
318          */
319         protected void createContents() {
320
321
322                 shell = new Shell();
323                 shell.setImage( iconProgramOffline ); //$NON-NLS-1$
324                 shell.setSize(711, 655);
325
326
327                 prefs = Preferences.userNodeForPackage(this.getClass());
328
329                 FkManager.getInstance().sortById( prefs.getBoolean(PREF_SORT_BY_ID_KEY, false) );
330
331                 mySelf = this;
332                 shell.setLayout(new FillLayout(SWT.HORIZONTAL));
333
334                 tabFolder = new TabFolder(shell, SWT.NONE);
335                 tabFolder.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
336
337                 TabItem tbtmConnection = new TabItem(tabFolder, SWT.NONE);
338                 tbtmConnection.setText(Messages.MainWin_12);
339
340                 cmpConnect = new Composite(tabFolder, SWT.BORDER);
341                 tbtmConnection.setControl(cmpConnect);
342                 cmpConnect.setLayout(new FormLayout());
343
344                 btnConnect = new Button(cmpConnect, SWT.CENTER);
345                 btnConnect.setImage( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightning.png") ); //$NON-NLS-1$
346                 FormData fd_btnConnect = new FormData();
347                 fd_btnConnect.top = new FormAttachment(0, 8);
348                 fd_btnConnect.left = new FormAttachment(100, -125);
349                 fd_btnConnect.right = new FormAttachment(100, -10);
350                 
351                 btnConnect.setLayoutData(fd_btnConnect);
352
353                 btnConnect.addSelectionListener(new SelectionAdapter() {
354                         public void widgetSelected(SelectionEvent e) {
355
356                                 if( fkSerial!=null && fkSerial.state == SerialState.Connected )
357                                 {
358                                         fkSerial.disconnect();
359                                 } else {
360                                         connect();
361                                 }
362                         }
363
364                 });
365                 btnConnect.setText(Messages.MainWin_15);
366                 txtPsw = new Text(cmpConnect, SWT.BORDER | SWT.PASSWORD);
367                 FormData fd_txtPsw = new FormData();
368                 
369                 fd_txtPsw.left = new FormAttachment(0, 102);
370                 txtPsw.setLayoutData(fd_txtPsw);
371                 txtPsw.setFocus();
372                 txtPsw.addKeyListener( new KeyListener() {
373                         @Override
374                         public void keyReleased(KeyEvent arg0) { }
375
376                         @Override
377                         public void keyPressed(KeyEvent arg0) {
378                                 if( arg0.character==(char)13 )
379                                 {
380                                         connect();
381                                 }
382                         }
383                 });
384
385                 txtLog = new Text(cmpConnect, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
386                 fd_btnConnect.bottom = new FormAttachment(txtPsw, 0, SWT.BOTTOM);
387                 FormData fd_txtLog = new FormData();
388                 fd_txtLog.bottom = new FormAttachment(100, -8);
389                 fd_txtLog.top = new FormAttachment(btnConnect, 8, SWT.BOTTOM);
390                 fd_txtLog.left = new FormAttachment(0, 8);
391                 fd_txtLog.right = new FormAttachment(btnConnect,0, SWT.RIGHT);
392                 txtLog.setLayoutData(fd_txtLog);
393                 txtLog.setEditable(false);
394
395                 lblPort = new Label(cmpConnect, SWT.NONE);
396                 lblPort.setAlignment(SWT.RIGHT);
397                 FormData fd_lblPort = new FormData();
398                 fd_lblPort.left = new FormAttachment(0, 10);
399                 lblPort.setLayoutData(fd_lblPort);
400                 lblPort.setText(Messages.MainWin_4);
401
402                 txtDev = new Text(cmpConnect, SWT.BORDER);
403                 fd_txtPsw.top = new FormAttachment(txtDev,4, SWT.BOTTOM);
404                 fd_lblPort.top = new FormAttachment(txtDev,0,SWT.CENTER);
405                 fd_lblPort.right = new FormAttachment(txtDev, -7);
406                 FormData fd_txtDev = new FormData();
407                 //fd_txtDev.bottom = new FormAttachment(txtPsw, -6);
408                 fd_txtDev.top = new FormAttachment(btnConnect,0,SWT.TOP);
409                 fd_txtDev.left = new FormAttachment(0, 102);
410                 txtDev.setLayoutData(fd_txtDev);
411 //              txtDev.setFont(SWTResourceManager.getFont("Cantarell", 9, SWT.NORMAL)); //$NON-NLS-1$
412                 txtDev.setText( prefs.get(PREF_PORT, PREF_DEFAULT_DEVICE));
413                 lblPassword = new Label(cmpConnect, SWT.NONE);
414                 lblPassword.setAlignment(SWT.RIGHT);
415                 FormData fd_lblPassword = new FormData();
416                 fd_lblPassword.right = new FormAttachment(0, 95);
417                 fd_lblPassword.top = new FormAttachment(0, 29);
418                 fd_lblPassword.left = new FormAttachment(0, 10);
419                 lblPassword.setLayoutData(fd_lblPassword);
420                 lblPassword.setText(Messages.MainWin_18);
421
422                 animation = new Animation(cmpConnect, SWT.NONE, 4);
423                 fd_txtDev.right = new FormAttachment(animation,-16, SWT.LEFT );
424                 fd_txtPsw.right = new FormAttachment(animation,-16, SWT.LEFT );
425                 
426                 FormData fd_animation = new FormData();
427                 fd_animation.right = new FormAttachment(btnConnect, -16);
428                 fd_animation.top = new FormAttachment(0, 10);
429
430                 animation.setLayoutData(fd_animation);
431
432                 animation.setVisible(false);
433                 animation.setPlaying(false);
434                 cmpConnect.setTabList(new Control[]{txtPsw, btnConnect, txtDev});
435
436                 log(Messages.MainWin_22);
437
438
439                 shell.addShellListener( new ShellListener() {
440
441                         public void shellIconified(ShellEvent e) {
442                         }
443
444                         public void shellDeiconified(ShellEvent e) {
445                         }
446
447                         public void shellDeactivated(ShellEvent e) {
448                         }
449
450                         public void shellClosed(ShellEvent e) {
451                                 shutDownApp();
452                         }
453
454                         public void shellActivated(ShellEvent e) {
455
456                         }
457                 } );
458
459
460                 if( System.getProperty("os.name").compareTo("Linux") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
461                 {
462                         Boolean gtkOk=false;
463                         Map<String, String> env = System.getenv();
464                         for (String envName : env.keySet()) {
465                                 if( envName.compareTo("SWT_GTK3") == 0 && env.get(envName).compareTo("0") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
466                                 {
467                                         gtkOk=true;
468                                 }
469                         }
470
471                         if( !gtkOk )
472                         {
473                                 log("Warning: Enviroment variable SWT_GTK3 is not set to 0, if FinalKey GUI looks weird or crashes after connecting, try export GTK_SWT3=0 before running."); //$NON-NLS-1$
474                         }
475                 }
476                 
477                 Device display = Display.getCurrent();
478                  blackColor = display.getSystemColor(SWT.COLOR_BLACK );
479                   grayColor = display.getSystemColor(SWT.COLOR_GRAY );
480                   defaultBgColor = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND );
481                   greenColor = display.getSystemColor(SWT.COLOR_DARK_GREEN );
482                   redColor = display.getSystemColor(SWT.COLOR_RED );            
483                 
484         }
485
486         
487         private void connect() {
488                 fkSerial = new SerialWorker(mySelf);
489                 prefs.put(PREF_PORT, txtDev.getText() );
490                 fkSerial.connect(txtDev.getText(),txtPsw.getText());
491                 txtPsw.setText(""); //$NON-NLS-1$
492         }
493         
494         
495         @Override
496         public void updateAccountList()
497         {
498                 int free=256;
499                 clearSystray();
500                 lstAccounts.getList().removeAll();
501
502                 Boolean showAccountId = prefs.getBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false);
503                 
504                 for( FkManager.Account a : FkManager.getInstance().getList() )
505                 {
506                         free--;
507                         lstAccounts.add( a.showNumInName( showAccountId ) );
508
509                         Menu menu = new Menu(a.toString());
510                         MenuItem both = new MenuItem(Messages.MainWin_25);
511                         MenuItem usr = new MenuItem(Messages.MainWin_26);
512                         MenuItem psw = new MenuItem(Messages.MainWin_27);
513                         menu.add(both);
514                         menu.add(usr);
515                         menu.add(psw);
516
517
518                         both.addActionListener(FkManager.getInstance());
519                         both.setActionCommand( "%"+a.num ); //$NON-NLS-1$
520
521
522
523                         psw.addActionListener(FkManager.getInstance()); 
524                         psw.setActionCommand( "p"+a.num); //$NON-NLS-1$
525
526
527                         usr.addActionListener(FkManager.getInstance()); 
528                         usr.setActionCommand( "u"+a.num ); //$NON-NLS-1$
529
530                         popup.add(menu);
531                 }
532
533                 lblNumFree.setText(" "+free+Messages.MainWin_32); //$NON-NLS-1$
534                 lblNumFree.pack();
535                 if( free == 0 )
536                 {
537                         btnNewAccount.setVisible(false);
538                 } else {
539                         btnNewAccount.setVisible(true);
540                 }
541                 
542         }
543
544         @Override
545         public void serialEvent(SerialState state) {
546                 switch(state)
547                 {
548                 case Connected:
549                         shell.setText(Messages.MainWin_33);
550
551                         animation.setVisible(false);
552                         animation.setPlaying(false);
553
554                         btnConnect.setText(Messages.MainWin_34);
555                         btnConnect.setEnabled(true);
556
557                         //Should we hide?
558                         if( prefs.getBoolean(PREF_AUTOHIDE, false) == true)
559                         {
560                                 hideToTray();
561                         }
562
563                         addAccountsTab();
564
565                         tabFolder.setSelection(1);
566
567                         updateAccountList();
568
569                         //Update icons for systray and window
570                         shell.setImage( iconProgramOnline );
571                         trayIcon.setImage( iconSystrayOnline );
572
573                         int numAccounts=FkManager.getInstance().getList().size();
574                         if( numAccounts>0 && prefs.getBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, true) )
575                         {
576                                 trayIcon.displayMessage("FinalKey", numAccounts +" "+ (( numAccounts >1)?Messages.MainWin_1:Messages.MainWin_2), //$NON-NLS-1$ //$NON-NLS-2$
577                                                 TrayIcon.MessageType.INFO);
578                         }
579
580                         log(Messages.MainWin_40);
581                         break;
582                 case Connecting:
583                         shell.setText(Messages.MainWin_41);
584                         animation.setVisible(true);
585                         animation.setPlaying(true);
586
587                         txtPsw.setEnabled(false);
588                         txtDev.setEnabled(false);
589                         btnConnect.setEnabled(false);
590
591                         lblPort.setEnabled(false);
592                         lblPassword.setEnabled(false);
593                         break;
594                 case Disconnected:
595                         fkSerial=null;
596                         animation.setVisible(false);
597                         animation.setPlaying(false);
598
599                         remAccountsTab();
600
601                         tabFolder.setSelection(0);
602
603                         shell.setText(Messages.MainWin_42);
604                         btnConnect.setText(Messages.MainWin_43);
605
606                         btnConnect.setEnabled(true);
607
608                         lblPort.setEnabled(true);
609                         lblPassword.setEnabled(true);
610                         txtPsw.setEnabled(true);
611                         txtDev.setEnabled(true);
612
613                         cmpConnect.update();
614
615                         clearSystray();
616
617                         //Update icons for systray and window
618                         shell.setImage( iconProgramOffline );
619                         trayIcon.setImage( iconSystrayOffline );
620
621                         if(lastState != state)
622                         {
623                                 log(Messages.MainWin_44);
624                         }
625                         break;
626
627                 case Working:
628                         animation.setPlaying(false);
629                         animation.setVisible(false);
630                         break;
631
632                 default:
633                         break;
634                 }
635                 lastState=state;
636                 cmpConnect.layout();
637         }
638
639         private void remAccountsTab() {
640                 if( tabFolder.getItemCount() > 1 )
641                 {
642                         tabFolder.getItem(1).dispose();
643                 }
644
645         }
646         
647         void updateFilteredList(Vector<Account> source)
648         {
649                 lstAccounts.getList().removeAll();
650
651                 Boolean showAccountId = prefs.getBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false);
652                 
653                 for( FkManager.Account a : source )
654                 {
655                         lstAccounts.add( a.showNumInName( showAccountId ) );
656                 }
657         }
658
659         private void addAccountsTab() {
660
661                 TabItem tbtmAccounts = new TabItem(tabFolder, SWT.NONE, 1);//TabItem(tabFolder, SWT.NONE);
662                 tbtmAccounts.setText(Messages.MainWin_45);
663
664                 cmpAccounts = new Composite(tabFolder, SWT.BORDER);
665                 tbtmAccounts.setControl(cmpAccounts);
666                 cmpAccounts.setLayout(new FormLayout());
667
668                 btnNewAccount = new Button(cmpAccounts, SWT.NONE);
669                 btnNewAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/new.png")); //$NON-NLS-1$
670                 btnNewAccount.addSelectionListener(new SelectionAdapter() {
671                         @Override
672                         public void widgetSelected(SelectionEvent e) {
673                                 NewAccountDialog dialog = new NewAccountDialog(shell, shell.getStyle() );
674                                 shell.setEnabled(false);
675                                 dialog.open();
676                                 shell.setEnabled(true);
677                                 updateAccountList();
678                         }
679                 });
680                 FormData fd_btnNewAccoount = new FormData();
681                 fd_btnNewAccoount.right = new FormAttachment(100, -10);
682                 fd_btnNewAccoount.top = new FormAttachment(100, -40);
683                 fd_btnNewAccoount.bottom = new FormAttachment(100, -6);
684                 fd_btnNewAccoount.left = new FormAttachment(100, -200);
685
686                 btnNewAccount.setLayoutData(fd_btnNewAccoount);
687                 btnNewAccount.setText(Messages.MainWin_47);
688
689                 lstAccounts = new ListViewer(cmpAccounts, SWT.BORDER | SWT.V_SCROLL);
690                 lstAccountsControl = lstAccounts.getList();
691                 lstAccountsControl.setLayoutData(new FormData());
692
693                 lblNumFree = new Label(cmpAccounts, SWT.NONE);
694                 lblNumFree.setText("Hello World!"); //$NON-NLS-1$
695                 FormData fd_lblNumFree = new FormData();
696                 fd_lblNumFree.left = new FormAttachment(lstAccountsControl,0,SWT.LEFT);
697                 fd_lblNumFree.bottom = new FormAttachment(btnNewAccount, 0, SWT.CENTER);
698                 lblNumFree.setLayoutData(fd_lblNumFree);
699
700                 btnOpenAccount = new Button(cmpAccounts, SWT.NONE);
701                 fd_lblNumFree.right = new FormAttachment(btnOpenAccount, -224);
702                 btnOpenAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightbulb.png")); //$NON-NLS-1$
703                 FormData fd_btnOpenAccount = new FormData();
704                 fd_btnOpenAccount.top = new FormAttachment(btnNewAccount, 0, SWT.TOP);
705                 fd_btnOpenAccount.bottom = new FormAttachment(btnNewAccount, 0, SWT.BOTTOM);
706                 fd_btnOpenAccount.right = new FormAttachment(btnNewAccount, -8, SWT.LEFT );
707                 btnOpenAccount.setLayoutData(fd_btnOpenAccount);
708                 btnOpenAccount.setText(Messages.MainWin_5);
709                 btnOpenAccount.getShell().layout();
710                 btnOpenAccount.setEnabled(false);
711
712
713                 FormData fd_lstAccounts = new FormData();
714                 fd_lstAccounts.top = new FormAttachment(0, 10);
715                 fd_lstAccounts.bottom = new FormAttachment(btnNewAccount, -6);
716                 
717                 txtFilter = new Text(cmpAccounts, SWT.BORDER);
718                 txtFilter.setText(Messages.MainWin_10);
719                 FormData fd_txtFilter = new FormData();
720                 fd_txtFilter.left = new FormAttachment(lblNumFree, 8, SWT.RIGHT);
721                 fd_txtFilter.bottom = new FormAttachment(btnOpenAccount,0, SWT.CENTER);
722                 fd_txtFilter.right = new FormAttachment(btnOpenAccount, -8, SWT.LEFT);
723                 txtFilter.setLayoutData(fd_txtFilter);
724                 txtFilter.setForeground( grayColor );
725                 txtFilter.addFocusListener( new FocusListener() {
726                         @Override
727                         public void focusLost(FocusEvent arg0) {
728                                 txtFilter.setText(Messages.MainWin_10);
729                                 txtFilter.setForeground( grayColor );
730                                 txtFilter.setBackground( defaultBgColor );
731                                 btnOpenAccount.setEnabled(false);
732                         }
733                         
734                         @Override
735                         public void focusGained(FocusEvent arg0) {
736                                 btnOpenAccount.setEnabled(false);
737                                 updateFilteredList( FkManager.getInstance().getList() );
738                                 txtFilter.setForeground( blackColor );
739                                 txtFilter.setText(""); //$NON-NLS-1$
740                                 
741                         }
742                 });
743                 
744                 txtFilter.addKeyListener( new KeyListener() {
745                         
746
747                         
748                         @Override
749                         public void keyReleased(KeyEvent arg0) {
750                                 btnOpenAccount.setEnabled(false);
751                                 
752                                 if( txtFilter.getText().length() == 0 )
753                                 {
754                                         updateFilteredList( FkManager.getInstance().getList() );
755                                         txtFilter.setBackground( defaultBgColor );
756                                 } else {
757                                         
758                                         Vector<Account> res = FkManager.getInstance().getList(txtFilter.getText());
759                                         
760                                         if( res.size() < 1 )
761                                         {
762                                                 txtFilter.setBackground( redColor );
763         
764                                         } else if( res.size() == 1 )
765                                         {
766                                                 txtFilter.setBackground( greenColor );
767                                                 btnOpenAccount.setEnabled(true);
768                                                 if( arg0.character==(char)13 )
769                                                 {
770                                                         showTrigDialog(res.get(0));
771                                                 }
772                                                 
773                                         } else {
774                                                 txtFilter.setBackground( defaultBgColor );
775                                         }
776                                         
777                                         updateFilteredList( res );
778                                         
779                                 }
780                         }
781                         
782                         @Override
783                         public void keyPressed(KeyEvent arg0) {
784
785                         }
786                 });
787                 
788                 fd_lstAccounts.left = new FormAttachment(0, 10);
789                 fd_lstAccounts.right = new FormAttachment(100, -10);
790
791                 lstAccounts.getControl().setLayoutData(fd_lstAccounts);
792                 lstAccounts.addDoubleClickListener( new IDoubleClickListener() {
793
794                         @Override
795                         public void doubleClick(DoubleClickEvent arg0) {
796                                 // TODO Auto-generated method stub
797                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
798                                 if( !selection.isEmpty() )
799                                 {
800                                         Account acc = (Account)selection.getFirstElement();
801                                         showTrigDialog(acc);
802                                 }
803                         }
804
805
806                 });
807
808                 lstAccounts.addSelectionChangedListener( new ISelectionChangedListener() {
809
810                         @Override
811                         public void selectionChanged(SelectionChangedEvent arg0) {
812                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
813                                 if( !selection.isEmpty() )
814                                 {
815                                         btnOpenAccount.setEnabled(true);
816
817                                         //Remove any listeners
818                                         for( Listener s :btnOpenAccount.getListeners(SWT.Selection) )
819                                         {
820                                                 btnOpenAccount.removeListener(SWT.Selection, s);
821                                         }
822
823                                         btnOpenAccount.addSelectionListener( new SelectionListener() {
824
825                                                 @Override
826                                                 public void widgetSelected(SelectionEvent arg0) {
827                                                         StructuredSelection selection = (StructuredSelection) lstAccounts.getSelection();
828                                                         Account acc = (Account)selection.getFirstElement();
829                                                         showTrigDialog(acc);
830                                                 }
831
832                                                 @Override
833                                                 public void widgetDefaultSelected(SelectionEvent arg0) {
834                                                         // TODO Auto-generated method stub
835                                                 }
836                                         });
837                                 }
838                         }
839                 });
840
841                 cmpAccounts.setTabList(new Control[]{lstAccountsControl, txtFilter, btnOpenAccount, btnNewAccount});
842         }
843
844         private void addSettingsTab()
845         {
846                 tbtmSettings = new TabItem(tabFolder, SWT.NONE);
847                 tbtmSettings.setText(Messages.MainWin_tbtmSettings_text);
848
849                 cmpSettings = new Composite(tabFolder, SWT.BORDER);
850                 tbtmSettings.setControl(cmpSettings);
851                 cmpSettings.setLayout(new FormLayout());
852
853                 chkAutoHide = new Button(cmpSettings, SWT.CHECK);
854                 chkAutoHide.setText(Messages.MainWin_6);
855                 chkAutoHide.setSelection( prefs.getBoolean( PREF_AUTOHIDE, false ) );
856                 FormData fd_chkAutoHide = new FormData();
857                 fd_chkAutoHide.top = new FormAttachment(0, 10);
858                 fd_chkAutoHide.left = new FormAttachment(0, 10);
859                 chkAutoHide.setLayoutData(fd_chkAutoHide);
860                 chkAutoHide.addSelectionListener( new SelectionAdapter() {
861                         public void widgetSelected(SelectionEvent e) {
862                                 prefs.putBoolean(PREF_AUTOHIDE, chkAutoHide.getSelection() );
863                         }
864                 });
865
866                 chkSortByAccountId = new Button(cmpSettings, SWT.CHECK);
867                 chkSortByAccountId.setText(Messages.MainWin_9);
868                 chkSortByAccountId.setSelection( prefs.getBoolean( PREF_SORT_BY_ID_KEY, false) );
869                 FormData fd_btnSortByAccountId = new FormData();
870                 fd_btnSortByAccountId.top = new FormAttachment(chkAutoHide, 6);
871                 fd_btnSortByAccountId.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
872                 chkSortByAccountId.setLayoutData(fd_btnSortByAccountId);
873                 chkSortByAccountId.addSelectionListener( new SelectionAdapter() {
874                         public void widgetSelected(SelectionEvent e) {
875                                 prefs.putBoolean( PREF_SORT_BY_ID_KEY, chkSortByAccountId.getSelection() );
876                                 FkManager.getInstance().sortById( chkSortByAccountId.getSelection() );
877                                 updateAccountList();
878                         }
879                 });
880
881                 
882                 chkShowAccountId = new Button(cmpSettings, SWT.CHECK);
883                 FormData fd_chkShowAccountId = new FormData();
884                 fd_chkShowAccountId.top = new FormAttachment(chkSortByAccountId, 6);
885                 fd_chkShowAccountId.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
886                 chkShowAccountId.setLayoutData(fd_chkShowAccountId);
887                 chkShowAccountId.setText(Messages.MainWin_btnShowAccountId_text);
888                 chkShowAccountId.setSelection( prefs.getBoolean(PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false) );
889                 chkShowAccountId.addSelectionListener( new SelectionAdapter() {
890                         public void widgetSelected(SelectionEvent e) {
891                                 prefs.putBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, chkShowAccountId.getSelection() );
892                                 updateAccountList();
893                         }
894                 });
895                 
896                 chkCheckForUpdates = new Button(cmpSettings, SWT.CHECK);
897                 FormData fd_chkCheckForUpdates = new FormData();
898                 fd_chkCheckForUpdates.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
899                 chkCheckForUpdates.setLayoutData(fd_chkCheckForUpdates);
900                 chkCheckForUpdates.setText(Messages.MainWin_btnCheckForUpdates_text);
901                 chkCheckForUpdates.setSelection( prefs.getBoolean( PREF_ALLOW_UPDATE_CHECK, true) );
902                 chkCheckForUpdates.addSelectionListener( new SelectionAdapter() {
903                         public void widgetSelected(SelectionEvent e) {
904                                 prefs.putBoolean( PREF_ALLOW_UPDATE_CHECK, chkCheckForUpdates.getSelection() );
905                                 checkForUpdates();
906                         }
907                 });
908                 fd_chkCheckForUpdates.top = new FormAttachment(chkShowAccountId, 6);
909                 
910                 btnShowaccountsReady = new Button(cmpSettings, SWT.CHECK);
911                 btnShowaccountsReady.setSelection( prefs.getBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, true) );
912                 FormData fd_btnShowaccountsReady = new FormData();
913                 fd_btnShowaccountsReady.top = new FormAttachment(chkCheckForUpdates, 6);
914                 fd_btnShowaccountsReady.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
915                 btnShowaccountsReady.setLayoutData(fd_btnShowaccountsReady);
916
917                 btnShowaccountsReady.setText(Messages.MainWin_btnShowaccountsReady_text);
918                 btnShowaccountsReady.addSelectionListener( new SelectionAdapter() {
919                 public void widgetSelected(SelectionEvent e) {
920                         prefs.putBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, btnShowaccountsReady.getSelection() );
921                         checkForUpdates();
922                 }
923         });
924                 
925
926
927         }
928
929         private void showTrigDialog(Account acc) {
930                 Account selectedAccount = acc;
931                 TriggerDialog diag = new TriggerDialog(shell, shell.getStyle(), acc, mySelf );
932
933                 //shell.setMinimized(true);
934                 shell.setEnabled(false);
935                 if( !((Boolean)diag.open()) )
936                 {
937                         //shell.setMinimized(false);
938                 }
939
940                 //Check if the account still exists
941                 Boolean deleted=true;
942                 for( Account a : FkManager.getInstance().getList() )
943                 {
944                         if( a.equals(selectedAccount) )
945                         {
946                                 deleted=false;
947                                 break;
948                         }
949                 }
950
951                 if(deleted)
952                 {
953                         btnOpenAccount.setEnabled(false);
954                 }
955
956                 txtFilter.setFocus();
957                 shell.setEnabled(true);
958
959         }
960
961         @Override
962         public void updateCheckFinished(UpdateResultEvent event) {
963                 updateCheckThread=null;
964                 switch(event.result)
965                 {
966                 case CHECK_FAILED:
967                         System.out.println("Trouble checking for updates."); //$NON-NLS-1$
968                         break;
969                 case NO_UPDATE:
970                         System.out.println("No update avaiable at this time."); //$NON-NLS-1$
971                         break;
972                 case UPDATE_AVAILABLE:
973                         MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
974                         dialog.setText(Messages.MainWin_52+event.version+Messages.MainWin_53);
975                         dialog.setMessage(Messages.MainWin_54+event.message);
976                         dialog.open();  
977                         break;
978                 }
979
980         }
981 }