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