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