97ee93a8a07c2e5a317baf0df3ecaf76b1a76365
[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 = Messages.MainWin_0;
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 )
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                 
288                 
289                 prefs = Preferences.userNodeForPackage(this.getClass());
290                 
291                 FkManager.getInstance().sortById( prefs.getBoolean(PREF_SORT_BY_ID_KEY, false) );               
292                 
293
294                 mySelf = this;
295                 shell.setLayout(new FillLayout(SWT.HORIZONTAL));
296                 
297                 tabFolder = new TabFolder(shell, SWT.NONE);
298                 tabFolder.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
299                 
300                 TabItem tbtmConnection = new TabItem(tabFolder, SWT.NONE);
301                 tbtmConnection.setText(Messages.MainWin_12);
302                 
303                 cmpConnect = new Composite(tabFolder, SWT.BORDER);
304                 tbtmConnection.setControl(cmpConnect);
305                 cmpConnect.setLayout(new FormLayout());
306                 
307
308                 btnConnect = new Button(cmpConnect, SWT.CENTER);
309                 btnConnect.setImage( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightning.png") ); //$NON-NLS-1$
310                 FormData fd_btnConnect = new FormData();
311                 fd_btnConnect.left = new FormAttachment(100, -125);
312                 fd_btnConnect.right = new FormAttachment(100, -10);
313                 btnConnect.setLayoutData(fd_btnConnect);
314
315                 btnConnect.addSelectionListener(new SelectionAdapter() {
316                         public void widgetSelected(SelectionEvent e) {
317                                 
318                                 
319                                 if( fkSerial!=null && fkSerial.state == SerialState.Connected )
320                                 {
321                                         fkSerial.disconnect();
322                                 } else {
323                                         
324                                         connect();
325                                 }
326                         }
327
328                 });
329                 btnConnect.setText(Messages.MainWin_15);
330                 
331                 txtPsw = new Text(cmpConnect, SWT.BORDER | SWT.PASSWORD);
332                 fd_btnConnect.bottom = new FormAttachment(txtPsw, 0, SWT.BOTTOM);
333                 FormData fd_txtPsw = new FormData();
334                 fd_txtPsw.bottom = new FormAttachment(0, 52);
335                 fd_txtPsw.right = new FormAttachment(0, 501);
336                 fd_txtPsw.top = new FormAttachment(0, 29);
337                 fd_txtPsw.left = new FormAttachment(0, 102);
338                 txtPsw.setLayoutData(fd_txtPsw);
339                 txtPsw.setFocus();
340                 txtPsw.addKeyListener( new KeyListener() {
341                         
342                         @Override
343                         public void keyReleased(KeyEvent arg0) {
344
345                                 
346                         }
347                         
348                         @Override
349                         public void keyPressed(KeyEvent arg0) {
350                                 if( arg0.character==(char)13 )
351                                 {
352                                         connect();
353                                 }
354                         }
355                 });
356                 
357                 txtLog = new Text(cmpConnect, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
358                 FormData fd_txtLog = new FormData();
359                 fd_txtLog.top = new FormAttachment(btnConnect, 6);
360                 fd_txtLog.left = new FormAttachment(0, 10);
361                 fd_txtLog.right = new FormAttachment(100, -10);
362                 fd_txtLog.bottom = new FormAttachment(100, -10);
363                 txtLog.setLayoutData(fd_txtLog);
364                 txtLog.setEditable(false);
365                 
366                 
367                 lblPort = new Label(cmpConnect, SWT.NONE);
368                 lblPort.setAlignment(SWT.RIGHT);
369                 fd_btnConnect.top = new FormAttachment(lblPort, 0, SWT.TOP);
370                 FormData fd_lblPort = new FormData();
371                 fd_lblPort.right = new FormAttachment(0, 86);
372                 fd_lblPort.top = new FormAttachment(0);
373                 fd_lblPort.left = new FormAttachment(0, 10);
374                 lblPort.setLayoutData(fd_lblPort);
375                 lblPort.setText("Port"); //$NON-NLS-1$
376                 
377                 txtDev = new Text(cmpConnect, SWT.BORDER);
378                 FormData fd_txtDev = new FormData();
379                 fd_txtDev.bottom = new FormAttachment(0, 23);
380                 fd_txtDev.right = new FormAttachment(0, 256);
381                 fd_txtDev.top = new FormAttachment(0);
382                 fd_txtDev.left = new FormAttachment(0, 102);
383                 txtDev.setLayoutData(fd_txtDev);
384                 txtDev.setFont(SWTResourceManager.getFont("Cantarell", 9, SWT.NORMAL)); //$NON-NLS-1$
385                 txtDev.setText( prefs.get(PREF_PORT, PREF_DEFAULT_DEVICE));
386                 lblPassword = new Label(cmpConnect, SWT.NONE);
387                 lblPassword.setAlignment(SWT.RIGHT);
388                 FormData fd_lblPassword = new FormData();
389                 fd_lblPassword.right = new FormAttachment(0, 95);
390                 fd_lblPassword.top = new FormAttachment(0, 29);
391                 fd_lblPassword.left = new FormAttachment(0, 10);
392                 lblPassword.setLayoutData(fd_lblPassword);
393                 lblPassword.setText(Messages.MainWin_18);
394                 
395                 chkAutoHide = new Button(cmpConnect, SWT.CHECK);
396                 FormData fd_chkAutoHide = new FormData();
397                 fd_chkAutoHide.top = new FormAttachment(btnConnect, 0, SWT.TOP);
398                 fd_chkAutoHide.left = new FormAttachment(txtDev, 6);
399                 fd_chkAutoHide.right = new FormAttachment(0, 501);
400                 chkAutoHide.setLayoutData(fd_chkAutoHide);
401                 chkAutoHide.setText(Messages.MainWin_19);
402                 
403                 chkAutoHide.setSelection( prefs.getBoolean(PREF_AUTOHIDE, false)) ;
404                 
405                 animation = new Animation(cmpConnect, SWT.NONE, 4);
406
407                 FormData fd_animation = new FormData();
408                 fd_animation.right = new FormAttachment(0, 86);
409                 fd_animation.top = new FormAttachment(0);
410                 fd_animation.left = new FormAttachment(0, 10);          
411                 
412                 animation.setLayoutData(fd_animation);
413
414
415                 
416                 animation.setVisible(false);
417                 animation.setPlaying(false);
418                 cmpConnect.setTabList(new Control[]{txtPsw, btnConnect});
419                 
420         
421                 
422                 log(Messages.MainWin_22);
423
424
425                 shell.addShellListener( new ShellListener() {
426                         
427                         public void shellIconified(ShellEvent e) {
428                         }
429
430                         public void shellDeiconified(ShellEvent e) {
431                         }
432                         
433                         public void shellDeactivated(ShellEvent e) {
434                         }
435                         
436                         public void shellClosed(ShellEvent e) {
437                                 shutDownApp();
438                         }
439                         
440                         public void shellActivated(ShellEvent e) {
441                                 
442                         }
443                 } );
444                 
445
446                 if( System.getProperty("os.name").compareTo("Linux") == 0 )
447                 {
448                         Boolean gtkOk=false;
449                 Map<String, String> env = System.getenv();
450                 for (String envName : env.keySet()) {
451                         if( envName.compareTo("SWT_GTK3") == 0 && env.get(envName).compareTo("0") == 0 )
452                         {
453                                 gtkOk=true;
454                         }
455                 }
456                 
457                 if( !gtkOk )
458                 {
459                         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.");
460                 }
461                 }
462
463         }
464
465         
466         private void connect() {
467                 prefs.putBoolean(PREF_AUTOHIDE, chkAutoHide.getSelection() );
468                 fkSerial = new SerialWorker(mySelf);
469                 prefs.put(PREF_PORT, txtDev.getText() );
470                 fkSerial.connect(txtDev.getText(),txtPsw.getText());
471                 txtPsw.setText(""); //$NON-NLS-1$
472         }
473         
474         
475         @Override
476         public void updateAccountList()
477         {
478                 int free=256;
479                 clearSystray();
480                 lstAccounts.getList().removeAll();
481                 
482                 for( FkManager.Account a : FkManager.getInstance().getList() )
483                 {
484                         free--;
485                         lstAccounts.add( a );
486                                                         
487                         Menu menu = new Menu(a.name+" ["+a.num+"]"); //$NON-NLS-1$ //$NON-NLS-1$ //$NON-NLS-2$
488                         MenuItem both = new MenuItem(Messages.MainWin_25);
489                         MenuItem usr = new MenuItem(Messages.MainWin_26);
490                         MenuItem psw = new MenuItem(Messages.MainWin_27);
491                         menu.add(both);
492                         menu.add(usr);
493                         menu.add(psw);
494
495
496                         both.addActionListener(FkManager.getInstance());
497                         both.setActionCommand( "%"+a.num ); //$NON-NLS-1$
498
499
500
501                         psw.addActionListener(FkManager.getInstance()); 
502                         psw.setActionCommand( "p"+a.num); //$NON-NLS-1$
503
504
505                         usr.addActionListener(FkManager.getInstance()); 
506                         usr.setActionCommand( "u"+a.num ); //$NON-NLS-1$
507
508                         popup.add(menu);
509                 }
510                 
511                 lblNumFree.setText(" "+free+Messages.MainWin_32); //$NON-NLS-1$
512                 lblNumFree.pack();
513                 if( free == 0 )
514                 {
515                         btnNewAccoount.setVisible(false);
516                 } else {
517                         btnNewAccoount.setVisible(true);
518                 }
519                 
520         }
521
522         @Override
523         public void serialEvent(SerialState state) {
524                 switch(state)
525                 {
526                 case Connected:
527                         shell.setText(Messages.MainWin_33);
528                         
529                         animation.setVisible(false);
530                         animation.setPlaying(false);
531
532
533                         btnConnect.setText(Messages.MainWin_34);
534                         btnConnect.setVisible(true);
535                         
536                         //Should we hide?
537                         if( prefs.getBoolean(PREF_AUTOHIDE, false) == true)
538                         {
539                                 hideToTray();
540                         }
541                         
542                         addAccountsTab();
543                         
544                         
545                         tabFolder.setSelection(1);
546                         
547                         updateAccountList();
548
549                         //Update icons for systray and window
550                         shell.setImage( iconProgramOnline );
551                         trayIcon.setImage( iconSystrayOnline );
552
553                         int numAccounts=FkManager.getInstance().getList().size();
554                         if( numAccounts>0 )
555                         {
556                                 trayIcon.displayMessage("FinalKey", numAccounts + Messages.MainWin_36+(( numAccounts >1)?"s":Messages.MainWin_38)+Messages.MainWin_39,  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
557                                     TrayIcon.MessageType.INFO);
558                         }
559
560                         
561                         log(Messages.MainWin_40);
562                         break;
563                 case Connecting:
564                         shell.setText(Messages.MainWin_41);
565                         animation.setVisible(true);
566                         animation.setPlaying(true);
567
568                         txtPsw.setVisible(false);
569                         txtDev.setVisible(false);
570                         btnConnect.setVisible(false);
571                         lblPort.setVisible(false);
572                         lblPassword.setVisible(false);
573                         chkAutoHide.setVisible(false);
574                         break;
575                 case Disconnected:
576                         fkSerial=null;
577                         animation.setVisible(false);
578                         animation.setPlaying(false);
579
580                         remAccountsTab();
581                         
582                         tabFolder.setSelection(0);
583
584                         shell.setText(Messages.MainWin_42);
585                         txtPsw.setVisible(true);
586                         txtDev.setVisible(true);
587                         btnConnect.setText(Messages.MainWin_43);
588                         btnConnect.setVisible(true);
589                         lblPort.setVisible(true);
590                         lblPassword.setVisible(true);
591                         chkAutoHide.setVisible(true);
592                         clearSystray();
593
594                         //Update icons for systray and window
595                         shell.setImage( iconProgramOffline );
596                         trayIcon.setImage( iconSystrayOffline );
597
598                         if(lastState != state)
599                         {
600                                 log(Messages.MainWin_44);
601                         }
602                         break;
603                 
604                 case Working:
605                         animation.setPlaying(false);
606                         animation.setVisible(false);
607                         break;
608                         
609                 default:
610                         break;
611                 }
612                 lastState=state;
613                 cmpConnect.layout();
614         }
615
616         private void remAccountsTab() {
617                 if( tabFolder.getItemCount() > 1 )
618                 {
619                         tabFolder.getItem(1).dispose();
620                 }
621                 
622         }
623
624         private void addAccountsTab() {
625                 TabItem tbtmAccounts = new TabItem(tabFolder, SWT.NONE);
626                 tbtmAccounts.setText(Messages.MainWin_45);
627                 
628                 cmpAccounts = new Composite(tabFolder, SWT.BORDER);
629                 tbtmAccounts.setControl(cmpAccounts);
630                 cmpAccounts.setLayout(new FormLayout());
631                 
632                 btnNewAccoount = new Button(cmpAccounts, SWT.NONE);
633                 btnNewAccoount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/new.png")); //$NON-NLS-1$
634                 btnNewAccoount.addSelectionListener(new SelectionAdapter() {
635                         @Override
636                         public void widgetSelected(SelectionEvent e) {
637                                 NewAccountDialog dialog = new NewAccountDialog(shell, shell.getStyle() );
638                                 shell.setEnabled(false);
639                                 dialog.open();
640                                 shell.setEnabled(true);
641                                 updateAccountList();
642                                 
643                         }
644                 });
645                 FormData fd_btnNewAccoount = new FormData();
646                 fd_btnNewAccoount.right = new FormAttachment(100, -10);
647                 fd_btnNewAccoount.top = new FormAttachment(0, 549);
648                 fd_btnNewAccoount.bottom = new FormAttachment(100, -6);
649                 btnNewAccoount.setLayoutData(fd_btnNewAccoount);
650                 btnNewAccoount.setText(Messages.MainWin_47);
651                 
652                 lblNumFree = new Label(cmpAccounts, SWT.NONE);
653                 lblNumFree.setText("Hello World!"); //$NON-NLS-1$
654                 
655                 FormData fd_lblNumFree = new FormData();
656                 fd_lblNumFree.bottom = new FormAttachment(100, -10);
657                 lblNumFree.setLayoutData(fd_lblNumFree);
658                 
659                 
660                 lstAccounts = new ListViewer(cmpAccounts, SWT.BORDER | SWT.V_SCROLL);
661                 List list = lstAccounts.getList();
662                 fd_lblNumFree.left = new FormAttachment(list, 0, SWT.LEFT);
663                 list.setLayoutData(new FormData());
664                 FormData fd_lstAccounts = new FormData();
665                 fd_lstAccounts.bottom = new FormAttachment(btnNewAccoount, -6);
666                 
667                 btnActivateAccount = new Button(cmpAccounts, SWT.NONE);
668                 fd_btnNewAccoount.left = new FormAttachment(btnActivateAccount, 6);
669                 btnActivateAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightbulb.png")); //$NON-NLS-1$
670                 FormData fd_btnActivateAccount = new FormData();
671                 fd_btnActivateAccount.right = new FormAttachment(100, -169);
672                 fd_btnActivateAccount.bottom = new FormAttachment(btnNewAccoount, 0, SWT.BOTTOM);
673                 fd_btnActivateAccount.top = new FormAttachment(btnNewAccoount, 0, SWT.TOP);
674                 btnActivateAccount.setLayoutData(fd_btnActivateAccount);
675                 fd_lstAccounts.top = new FormAttachment(0, 10);
676                 fd_lstAccounts.left = new FormAttachment(0, 10);
677                 fd_lstAccounts.right = new FormAttachment(100, -10);
678                 btnActivateAccount.setVisible(false);
679                 
680                 Button btnByAccountId = new Button(cmpAccounts, SWT.CHECK);
681                 fd_btnActivateAccount.left = new FormAttachment(btnByAccountId, 6);
682                 fd_lblNumFree.right = new FormAttachment(btnByAccountId, -6);
683                 btnByAccountId.addSelectionListener(new SelectionAdapter() {
684                         @Override
685                         public void widgetSelected(SelectionEvent e) {
686                                 Button btnAccId = (Button)e.getSource();
687                                 FkManager.getInstance().sortById( btnAccId.getSelection() );
688                                 prefs.putBoolean( PREF_SORT_BY_ID_KEY, btnAccId.getSelection() );
689                                 updateAccountList();
690                         }
691                 });
692                 FormData fd_btnByAccountId = new FormData();
693                 fd_btnByAccountId.left = new FormAttachment(0, 139);
694                 fd_btnByAccountId.right = new FormAttachment(100, -398);
695                 fd_btnByAccountId.bottom = new FormAttachment(btnNewAccoount, 0, SWT.BOTTOM);
696                 btnByAccountId.setLayoutData(fd_btnByAccountId);
697                 btnByAccountId.setText(Messages.MainWin_btnByAccountId_text);
698                 btnByAccountId.setSelection( prefs.getBoolean( PREF_SORT_BY_ID_KEY, false));
699                 
700                 
701                 //lstAccounts.setLayoutData(fd_lstAccounts);
702                 lstAccounts.getControl().setLayoutData(fd_lstAccounts);
703                 lstAccounts.addDoubleClickListener( new IDoubleClickListener() {
704                         
705                         @Override
706                         public void doubleClick(DoubleClickEvent arg0) {
707                                 // TODO Auto-generated method stub
708                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
709                                 if( !selection.isEmpty() )
710                                 {
711                                         Account acc = (Account)selection.getFirstElement();
712                                         showTrigDialog(acc);
713                                 } else {
714                                         System.out.println("Selected nothing."); //$NON-NLS-1$
715                                 }
716                                 
717                                 
718                         }
719
720
721                 });
722                 
723                 lstAccounts.addSelectionChangedListener( new ISelectionChangedListener() {
724                         
725                         @Override
726                         public void selectionChanged(SelectionChangedEvent arg0) {
727                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
728                                 if( !selection.isEmpty() )
729                                 {
730                                         Account acc = (Account)selection.getFirstElement();
731                                         btnActivateAccount.setVisible(true);
732                                         btnActivateAccount.setText(acc.name);
733                                         
734                                         //Remove any listeners
735                                         for( Listener s :btnActivateAccount.getListeners(SWT.Selection) )
736                                         {
737                                                 btnActivateAccount.removeListener(SWT.Selection, s);
738                                         }
739                                         
740                                         btnActivateAccount.addSelectionListener( new SelectionListener() {
741                                                 
742                                                 @Override
743                                                 public void widgetSelected(SelectionEvent arg0) {
744                                                         
745                                                         StructuredSelection selection = (StructuredSelection) lstAccounts.getSelection();
746                                                         Account acc = (Account)selection.getFirstElement();
747                                                         showTrigDialog(acc);
748                                                 }
749                                                 
750                                                 @Override
751                                                 public void widgetDefaultSelected(SelectionEvent arg0) {
752                                                         // TODO Auto-generated method stub
753                                                         
754                                                 }
755                                         });
756                                         
757                                 }
758                         }
759                 });
760                 
761                                 
762         }
763
764         private void showTrigDialog(Account acc) {
765                 TriggerDialog diag = new TriggerDialog(shell, shell.getStyle(), acc, mySelf );
766
767                 //shell.setMinimized(true);
768                 shell.setEnabled(false);
769                 if( !((Boolean)diag.open()) )
770                 {
771                         //shell.setMinimized(false);
772                 }
773                 
774                 shell.setEnabled(true);
775                 
776         }
777
778         @Override
779         public void updateCheckFinished(UpdateResultEvent event) {
780                 switch(event.result)
781                 {
782                 case CHECK_FAILED:
783                         System.out.println("Trouble checking for updates."); //$NON-NLS-1$
784                         break;
785                 case NO_UPDATE:
786                         System.out.println("No update avaiable at this time."); //$NON-NLS-1$
787                         break;
788                 case UPDATE_AVAILABLE:
789                         MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
790                         dialog.setText(Messages.MainWin_52+event.version+Messages.MainWin_53);
791                         dialog.setMessage(Messages.MainWin_54+event.message);
792                         dialog.open();  
793                         break;
794                 }
795                 
796         }
797 }