Removed some whitespace, cleaned up strings.
[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                 
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                 animation.setVisible(false);
416                 animation.setPlaying(false);
417                 cmpConnect.setTabList(new Control[]{txtPsw, btnConnect});
418
419                 log(Messages.MainWin_22);
420
421
422                 shell.addShellListener( new ShellListener() {
423
424                         public void shellIconified(ShellEvent e) {
425                         }
426
427                         public void shellDeiconified(ShellEvent e) {
428                         }
429
430                         public void shellDeactivated(ShellEvent e) {
431                         }
432
433                         public void shellClosed(ShellEvent e) {
434                                 shutDownApp();
435                         }
436
437                         public void shellActivated(ShellEvent e) {
438
439                         }
440                 } );
441
442
443                 if( System.getProperty("os.name").compareTo("Linux") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
444                 {
445                         Boolean gtkOk=false;
446                         Map<String, String> env = System.getenv();
447                         for (String envName : env.keySet()) {
448                                 if( envName.compareTo("SWT_GTK3") == 0 && env.get(envName).compareTo("0") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
449                                 {
450                                         gtkOk=true;
451                                 }
452                         }
453
454                         if( !gtkOk )
455                         {
456                                 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$
457                         }
458                 }
459
460         }
461
462         
463         private void connect() {
464                 prefs.putBoolean(PREF_AUTOHIDE, chkAutoHide.getSelection() );
465                 fkSerial = new SerialWorker(mySelf);
466                 prefs.put(PREF_PORT, txtDev.getText() );
467                 fkSerial.connect(txtDev.getText(),txtPsw.getText());
468                 txtPsw.setText(""); //$NON-NLS-1$
469         }
470         
471         
472         @Override
473         public void updateAccountList()
474         {
475                 int free=256;
476                 clearSystray();
477                 lstAccounts.getList().removeAll();
478                 
479                 for( FkManager.Account a : FkManager.getInstance().getList() )
480                 {
481                         free--;
482                         lstAccounts.add( a );
483                                                         
484                         Menu menu = new Menu(a.name+" ["+a.num+"]"); //$NON-NLS-1$ //$NON-NLS-1$ //$NON-NLS-2$
485                         MenuItem both = new MenuItem(Messages.MainWin_25);
486                         MenuItem usr = new MenuItem(Messages.MainWin_26);
487                         MenuItem psw = new MenuItem(Messages.MainWin_27);
488                         menu.add(both);
489                         menu.add(usr);
490                         menu.add(psw);
491
492
493                         both.addActionListener(FkManager.getInstance());
494                         both.setActionCommand( "%"+a.num ); //$NON-NLS-1$
495
496
497
498                         psw.addActionListener(FkManager.getInstance()); 
499                         psw.setActionCommand( "p"+a.num); //$NON-NLS-1$
500
501
502                         usr.addActionListener(FkManager.getInstance()); 
503                         usr.setActionCommand( "u"+a.num ); //$NON-NLS-1$
504
505                         popup.add(menu);
506                 }
507                 
508                 lblNumFree.setText(" "+free+Messages.MainWin_32); //$NON-NLS-1$
509                 lblNumFree.pack();
510                 if( free == 0 )
511                 {
512                         btnNewAccoount.setVisible(false);
513                 } else {
514                         btnNewAccoount.setVisible(true);
515                 }
516                 
517         }
518
519         @Override
520         public void serialEvent(SerialState state) {
521                 switch(state)
522                 {
523                 case Connected:
524                         shell.setText(Messages.MainWin_33);
525                         
526                         animation.setVisible(false);
527                         animation.setPlaying(false);
528
529
530                         btnConnect.setText(Messages.MainWin_34);
531                         btnConnect.setVisible(true);
532                         
533                         //Should we hide?
534                         if( prefs.getBoolean(PREF_AUTOHIDE, false) == true)
535                         {
536                                 hideToTray();
537                         }
538                         
539                         addAccountsTab();
540                         
541                         
542                         tabFolder.setSelection(1);
543                         
544                         updateAccountList();
545
546                         //Update icons for systray and window
547                         shell.setImage( iconProgramOnline );
548                         trayIcon.setImage( iconSystrayOnline );
549
550                         int numAccounts=FkManager.getInstance().getList().size();
551                         if( numAccounts>0 )
552                         {
553                                 trayIcon.displayMessage("FinalKey", numAccounts +" "+ (( numAccounts >1)?Messages.MainWin_1:Messages.MainWin_2), //$NON-NLS-1$ //$NON-NLS-2$
554                                                 TrayIcon.MessageType.INFO);
555                         }
556
557                         
558                         log(Messages.MainWin_40);
559                         break;
560                 case Connecting:
561                         shell.setText(Messages.MainWin_41);
562                         animation.setVisible(true);
563                         animation.setPlaying(true);
564
565                         txtPsw.setVisible(false);
566                         txtDev.setVisible(false);
567                         btnConnect.setVisible(false);
568                         lblPort.setVisible(false);
569                         lblPassword.setVisible(false);
570                         chkAutoHide.setVisible(false);
571                         break;
572                 case Disconnected:
573                         fkSerial=null;
574                         animation.setVisible(false);
575                         animation.setPlaying(false);
576
577                         remAccountsTab();
578                         
579                         tabFolder.setSelection(0);
580
581                         shell.setText(Messages.MainWin_42);
582                         txtPsw.setVisible(true);
583                         txtDev.setVisible(true);
584                         btnConnect.setText(Messages.MainWin_43);
585                         btnConnect.setVisible(true);
586                         lblPort.setVisible(true);
587                         lblPassword.setVisible(true);
588                         chkAutoHide.setVisible(true);
589                         clearSystray();
590
591                         //Update icons for systray and window
592                         shell.setImage( iconProgramOffline );
593                         trayIcon.setImage( iconSystrayOffline );
594
595                         if(lastState != state)
596                         {
597                                 log(Messages.MainWin_44);
598                         }
599                         break;
600                 
601                 case Working:
602                         animation.setPlaying(false);
603                         animation.setVisible(false);
604                         break;
605                         
606                 default:
607                         break;
608                 }
609                 lastState=state;
610                 cmpConnect.layout();
611         }
612
613         private void remAccountsTab() {
614                 if( tabFolder.getItemCount() > 1 )
615                 {
616                         tabFolder.getItem(1).dispose();
617                 }
618                 
619         }
620
621         private void addAccountsTab() {
622                 TabItem tbtmAccounts = new TabItem(tabFolder, SWT.NONE);
623                 tbtmAccounts.setText(Messages.MainWin_45);
624                 
625                 cmpAccounts = new Composite(tabFolder, SWT.BORDER);
626                 tbtmAccounts.setControl(cmpAccounts);
627                 cmpAccounts.setLayout(new FormLayout());
628                 
629                 btnNewAccoount = new Button(cmpAccounts, SWT.NONE);
630                 btnNewAccoount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/new.png")); //$NON-NLS-1$
631                 btnNewAccoount.addSelectionListener(new SelectionAdapter() {
632                         @Override
633                         public void widgetSelected(SelectionEvent e) {
634                                 NewAccountDialog dialog = new NewAccountDialog(shell, shell.getStyle() );
635                                 shell.setEnabled(false);
636                                 dialog.open();
637                                 shell.setEnabled(true);
638                                 updateAccountList();
639                         }
640                 });
641                 FormData fd_btnNewAccoount = new FormData();
642                 fd_btnNewAccoount.right = new FormAttachment(100, -10);
643                 fd_btnNewAccoount.top = new FormAttachment(0, 549);
644                 fd_btnNewAccoount.bottom = new FormAttachment(100, -6);
645                 btnNewAccoount.setLayoutData(fd_btnNewAccoount);
646                 btnNewAccoount.setText(Messages.MainWin_47);
647                 
648                 lblNumFree = new Label(cmpAccounts, SWT.NONE);
649                 lblNumFree.setText("Hello World!"); //$NON-NLS-1$
650                 
651                 FormData fd_lblNumFree = new FormData();
652                 fd_lblNumFree.bottom = new FormAttachment(100, -10);
653                 lblNumFree.setLayoutData(fd_lblNumFree);
654                 
655                 
656                 lstAccounts = new ListViewer(cmpAccounts, SWT.BORDER | SWT.V_SCROLL);
657                 List list = lstAccounts.getList();
658                 fd_lblNumFree.left = new FormAttachment(list, 0, SWT.LEFT);
659                 list.setLayoutData(new FormData());
660                 FormData fd_lstAccounts = new FormData();
661                 fd_lstAccounts.bottom = new FormAttachment(btnNewAccoount, -6);
662                 
663                 btnActivateAccount = new Button(cmpAccounts, SWT.NONE);
664                 fd_btnNewAccoount.left = new FormAttachment(btnActivateAccount, 6);
665                 btnActivateAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightbulb.png")); //$NON-NLS-1$
666                 FormData fd_btnActivateAccount = new FormData();
667                 fd_btnActivateAccount.right = new FormAttachment(100, -169);
668                 fd_btnActivateAccount.bottom = new FormAttachment(btnNewAccoount, 0, SWT.BOTTOM);
669                 fd_btnActivateAccount.top = new FormAttachment(btnNewAccoount, 0, SWT.TOP);
670                 btnActivateAccount.setLayoutData(fd_btnActivateAccount);
671                 fd_lstAccounts.top = new FormAttachment(0, 10);
672                 fd_lstAccounts.left = new FormAttachment(0, 10);
673                 fd_lstAccounts.right = new FormAttachment(100, -10);
674                 btnActivateAccount.setVisible(false);
675                 
676                 Button btnByAccountId = new Button(cmpAccounts, SWT.CHECK);
677                 fd_btnActivateAccount.left = new FormAttachment(btnByAccountId, 6);
678                 fd_lblNumFree.right = new FormAttachment(btnByAccountId, -6);
679                 btnByAccountId.addSelectionListener(new SelectionAdapter() {
680                         @Override
681                         public void widgetSelected(SelectionEvent e) {
682                                 Button btnAccId = (Button)e.getSource();
683                                 FkManager.getInstance().sortById( btnAccId.getSelection() );
684                                 prefs.putBoolean( PREF_SORT_BY_ID_KEY, btnAccId.getSelection() );
685                                 updateAccountList();
686                         }
687                 });
688                 FormData fd_btnByAccountId = new FormData();
689                 fd_btnByAccountId.left = new FormAttachment(0, 139);
690                 fd_btnByAccountId.right = new FormAttachment(100, -398);
691                 fd_btnByAccountId.bottom = new FormAttachment(btnNewAccoount, 0, SWT.BOTTOM);
692                 btnByAccountId.setLayoutData(fd_btnByAccountId);
693                 btnByAccountId.setText(Messages.MainWin_btnByAccountId_text);
694                 btnByAccountId.setSelection( prefs.getBoolean( PREF_SORT_BY_ID_KEY, false));
695                 
696                 
697                 //lstAccounts.setLayoutData(fd_lstAccounts);
698                 lstAccounts.getControl().setLayoutData(fd_lstAccounts);
699                 lstAccounts.addDoubleClickListener( new IDoubleClickListener() {
700                         
701                         @Override
702                         public void doubleClick(DoubleClickEvent arg0) {
703                                 // TODO Auto-generated method stub
704                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
705                                 if( !selection.isEmpty() )
706                                 {
707                                         Account acc = (Account)selection.getFirstElement();
708                                         showTrigDialog(acc);
709                                 } else {
710                                         System.out.println("Selected nothing."); //$NON-NLS-1$
711                                 }
712                                 
713                                 
714                         }
715
716
717                 });
718                 
719                 lstAccounts.addSelectionChangedListener( new ISelectionChangedListener() {
720                         
721                         @Override
722                         public void selectionChanged(SelectionChangedEvent arg0) {
723                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
724                                 if( !selection.isEmpty() )
725                                 {
726                                         Account acc = (Account)selection.getFirstElement();
727                                         btnActivateAccount.setVisible(true);
728                                         btnActivateAccount.setText(acc.name);
729                                         
730                                         //Remove any listeners
731                                         for( Listener s :btnActivateAccount.getListeners(SWT.Selection) )
732                                         {
733                                                 btnActivateAccount.removeListener(SWT.Selection, s);
734                                         }
735                                         
736                                         btnActivateAccount.addSelectionListener( new SelectionListener() {
737                                                 
738                                                 @Override
739                                                 public void widgetSelected(SelectionEvent arg0) {
740                                                         
741                                                         StructuredSelection selection = (StructuredSelection) lstAccounts.getSelection();
742                                                         Account acc = (Account)selection.getFirstElement();
743                                                         showTrigDialog(acc);
744                                                 }
745                                                 
746                                                 @Override
747                                                 public void widgetDefaultSelected(SelectionEvent arg0) {
748                                                         // TODO Auto-generated method stub
749                                                         
750                                                 }
751                                         });
752                                         
753                                 }
754                         }
755                 });
756                 
757                                 
758         }
759
760         private void showTrigDialog(Account acc) {
761                 TriggerDialog diag = new TriggerDialog(shell, shell.getStyle(), acc, mySelf );
762
763                 //shell.setMinimized(true);
764                 shell.setEnabled(false);
765                 if( !((Boolean)diag.open()) )
766                 {
767                         //shell.setMinimized(false);
768                 }
769                 
770                 shell.setEnabled(true);
771                 
772         }
773
774         @Override
775         public void updateCheckFinished(UpdateResultEvent event) {
776                 switch(event.result)
777                 {
778                 case CHECK_FAILED:
779                         System.out.println("Trouble checking for updates."); //$NON-NLS-1$
780                         break;
781                 case NO_UPDATE:
782                         System.out.println("No update avaiable at this time."); //$NON-NLS-1$
783                         break;
784                 case UPDATE_AVAILABLE:
785                         MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
786                         dialog.setText(Messages.MainWin_52+event.version+Messages.MainWin_53);
787                         dialog.setMessage(Messages.MainWin_54+event.message);
788                         dialog.open();  
789                         break;
790                 }
791                 
792         }
793 }