Added restore functionality to GUI.
[FinalKeyGui.git] / src / fkgui / MainWin.java
1 package fkgui;
2
3
4 import java.net.URI;
5 import java.util.Map;
6 import java.util.Vector;
7 import  java.util.prefs.*;
8 import java.awt.*;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11
12 import javax.swing.UIManager;
13
14 import org.eclipse.jface.viewers.DoubleClickEvent;
15 import org.eclipse.jface.viewers.IDoubleClickListener;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.ListViewer;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.swt.widgets.MessageBox;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.FocusEvent;
28 import org.eclipse.swt.events.FocusListener;
29 import org.eclipse.swt.events.KeyEvent;
30 import org.eclipse.swt.events.KeyListener;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.events.SelectionListener;
36 import org.eclipse.swt.events.VerifyEvent;
37 import org.eclipse.swt.events.VerifyListener;
38 import org.eclipse.swt.graphics.Color;
39 import org.eclipse.swt.graphics.Device;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.wb.swt.SWTResourceManager;
44
45 import fkgui.FkManager.Account;
46 import fkgui.SerialWorker.SerialState;
47 import fkgui.UpdateChecker.UpdateCheckResultListener;
48
49 import org.eclipse.swt.widgets.TabFolder;
50 import org.eclipse.swt.widgets.TabItem;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.layout.FormLayout;
53 import org.eclipse.swt.layout.FormData;
54 import org.eclipse.swt.layout.FormAttachment;
55 import org.eclipse.swt.layout.FillLayout;
56 import org.eclipse.swt.widgets.List;
57 import org.eclipse.swt.widgets.Combo;
58 import org.eclipse.swt.widgets.Group;
59
60 public class MainWin implements ConsoleMsg, UpdateCheckResultListener {
61
62         protected Shell shell;
63         private Text txtPsw;
64         public TrayIcon trayIcon;
65         public PopupMenu popup;
66         public MenuItem showMain;
67         public MenuItem hideMain;
68         public MenuItem exitApp;
69
70         public Text txtLog;
71         public Button btnConnect;
72         public Label lblPort;
73         public Label lblPassword;
74         
75         public Animation animation;
76         
77         public TabFolder tabFolder;
78
79         private Text txtDev;
80         Preferences prefs;
81         MainWin mySelf;
82         
83         SerialWorker fkSerial;
84         private boolean sysTrayIconVisible;
85         
86         Button btnOpenAccount;
87         private Color blackColor;
88         private Color grayColor;
89         private Color defaultBgColor;
90         private Color greenColor;
91         private Color redColor;
92         
93         private static final String PREF_PORT ="lastUsedPortPref"; //$NON-NLS-1$
94         private static final String PREF_DEFAULT_DEVICE = "/dev/FinalKey"; //$NON-NLS-1$
95         private static final String PREF_AUTOHIDE = "hideMainWinAfterConnect"; //$NON-NLS-1$
96         private static final String PREF_SORT_BY_ID_KEY = "sortAccountListById"; //$NON-NLS-1$
97         private static final String PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY = "showAccountIdInName"; //$NON-NLS-1$
98         private static final String PREF_ALLOW_UPDATE_CHECK = "allowUpdateCheck"; //$NON-NLS-1$
99         private static final String PREF_SHOW_ACCOUNTS_READY_NOTICE = "showAccountsReadyNotice"; //$NON-NLS-1$
100         private static final String PREF_SHOW_SYSTRAY_NAME = "showSystrayName"; //$NON-NLS-1$
101         private static final String PREF_HIDE_ON_CLOSE = "hideToSysTrayOnMainWinClose"; //$NON-NLS-1$
102
103         public Composite cmpConnect;
104         private Composite cmpAccounts;
105         ListViewer lstAccounts;
106         private List lstAccountsControl;
107         Label lblNumFree;
108         Button btnNewAccount;
109         Button chkCheckForUpdates;
110         Boolean hiddenAtStart=false;
111         Button btnShowaccountsReady;
112  
113         //Icons
114         private org.eclipse.swt.graphics.Image iconProgramOnline  = SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/finalkey.png");  //$NON-NLS-1$
115         private org.eclipse.swt.graphics.Image iconProgramOffline  = SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/finalkey-big-offline.png");  //$NON-NLS-1$
116         private Image iconSystrayOnline = Toolkit.getDefaultToolkit().createImage(getClass().getResource("gfx/systray-color.png"));  //$NON-NLS-1$
117         private Image iconSystrayOffline= Toolkit.getDefaultToolkit().createImage(getClass().getResource("gfx/systray-offline.png"));  //$NON-NLS-1$
118
119         private TabItem tbtmAccounts;
120         private TabItem tbtmSettings;
121         private Composite cmpSettings;
122         private Button chkAutoHide;
123         private Button chkSortByAccountId;
124         private Button chkShowAccountId;
125         private Thread updateCheckThread;
126         private Text txtFilter;
127         
128         String systrayTipTxt;
129         private Button btnHideToTrayOnClose;
130         private TabItem tbtmThisFinalkey;
131         private Composite composite;
132         private Text txtBanner; 
133         private Combo cmbLayout;
134         private Group grpBackupAndRestore;
135         private Group group_1;
136         private Label label;
137         private Text text_1;
138         private Text text_2;
139         private Text text_3;
140         private Label label_1;
141         private Label label_2;
142         private Label label_3;
143         private Button button;
144         private Group grpFormat;
145         private Label lblNewLabel;
146         private Button btnNewButton;
147         private Button btnNewButton_1;
148         
149         private Button btnFkSettingsSave;
150         private Label lblMakeSureYou;
151
152         
153         
154         /**
155          * Launch the application.
156          * @param args
157          */
158         public static void main(String[] args) {
159
160                 try {
161                         MainWin window = new MainWin();
162
163                         for(String s : args )
164                         {
165                                 if( s.compareTo("--hide") == 0 ) //$NON-NLS-1$
166                                 {
167                                         window.hiddenAtStart=true;
168                                 }
169                         }
170
171                         window.open();
172                 } catch (Exception e) {
173                         e.printStackTrace();
174                 }
175                 
176         }
177
178         /**
179          * Open the window.
180          */
181         public void open() {
182                 Display display = Display.getDefault();
183                 createContents();
184
185                 serialEvent(SerialState.Disconnected);
186                 
187                 shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
188                         
189                         @Override
190                         public void handleEvent(Event arg0) {
191                                 if( ( (arg0.stateMask&SWT.CTRL) == SWT.CTRL) && arg0.keyCode == 'f'  )
192                                 {
193                                         if( tabFolder.getItemCount() == 3 && !txtFilter.isFocusControl() )
194                                         {
195                                                 tabFolder.setSelection(1);
196                                                 txtFilter.setFocus();
197                                         }
198                                 }
199                                 
200                         }
201                 });
202
203
204                 checkForUpdates();
205
206                 if( hiddenAtStart )
207                 {
208                         hideToTray();
209                 } else {
210                         shell.open();
211                         shell.layout();
212                 }
213
214                 clearSystray(true);
215                 
216                 while (!shell.isDisposed()) {
217                         if (!display.readAndDispatch()) {
218                                 display.sleep();
219                         }
220                 }
221         }
222         
223         private void checkForUpdates() {
224                 if( prefs.getBoolean(PREF_ALLOW_UPDATE_CHECK, true) )
225                 {
226                         if( updateCheckThread == null )
227                         {
228                                 updateCheckThread = new Thread(new UpdateChecker(this));
229                                 updateCheckThread.start();
230                         } else {
231                                 System.out.println(Messages.MainWin_0);
232                         }
233                 } else {
234                         System.out.println(Messages.MainWin_3);
235                 }
236         }
237
238         public void log( String str )
239         {
240                 txtLog.append(str+"\n"); //$NON-NLS-1$
241                 if( tabFolder.getSelectionIndex() == 0 )
242                 {
243                         txtLog.redraw();
244                         shell.redraw();
245                 }
246                 System.out.println(str);
247
248         }
249
250         /**
251          * Create a systemTray icon
252          */
253         private void createSysTrayIcon()
254         {
255                 sysTrayIconVisible=true;
256                 //Check the SystemTray is supported
257                 if (!SystemTray.isSupported()) {
258                         log("SystemTray is not supported."); //$NON-NLS-1$
259                         return;
260                 }
261
262                 try {
263                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
264                 } catch (Exception e1) {
265                         e1.printStackTrace();
266                 }
267
268                 popup = new PopupMenu();
269                 trayIcon = new TrayIcon( iconSystrayOffline ); //$NON-NLS-1$
270
271                 trayIcon.setImageAutoSize(true);
272                 final SystemTray tray = SystemTray.getSystemTray();
273
274                 // Create a pop-up menu components
275                 showMain = new MenuItem(Messages.MainWin_7);
276                 hideMain = new MenuItem(Messages.MainWin_8);
277                 exitApp  = new MenuItem("Quit");
278
279                 showMain.addActionListener(new ActionListener() {
280                         @Override
281                         public void actionPerformed(ActionEvent e) {
282                                 Display.getDefault().syncExec( new Runnable(){
283
284                                         public void run()
285                                         {
286                                                 showFromTray();
287                                         }
288                                 } );
289                         }
290
291                 });
292
293                 hideMain.addActionListener(new ActionListener() {
294                         @Override
295                         public void actionPerformed(ActionEvent e) {
296                                 Display.getDefault().syncExec( new Runnable(){
297
298                                         public void run()
299                                         {
300                                                 hideToTray();
301                                         }
302                                 } );
303                         }
304                 });
305
306                 exitApp.addActionListener(new ActionListener() {
307                         @Override
308                         public void actionPerformed(ActionEvent e) {
309                                 Display.getDefault().asyncExec( new Runnable(){
310
311                                         public void run()
312                                         {
313                                                 shutDownApp();
314                                         }
315                                 } );
316                         }
317                 });             
318                 
319                 trayIcon.setPopupMenu(popup);
320
321                 try {
322                         tray.add(trayIcon);
323                 } catch (AWTException e) {
324                         log("TrayIcon could not be added."); //$NON-NLS-1$
325                 }
326
327         }
328
329         private void setSystrayIconTip(String tip) {
330                 if( tip == null )
331                 {
332                         trayIcon.setToolTip(systrayTipTxt);
333                 } else {
334                         systrayTipTxt=tip;
335                         trayIcon.setToolTip(tip);
336                 }
337
338                 if( !prefs.getBoolean(PREF_SHOW_SYSTRAY_NAME, true) )
339                 {
340                         trayIcon.setToolTip(null);
341                 }
342
343         }
344
345         private void hideToTray()
346         {
347                 popup.remove(0);
348                 popup.insert(showMain, 0);
349                 shell.setVisible(false);
350         }
351
352         private void showFromTray()
353         {
354                 popup.remove(0);
355                 popup.insert(hideMain, 0);
356                 shell.setVisible(true);
357                 shell.forceActive();
358         }
359
360         private void destroySysTrayIcon()
361         {
362                 sysTrayIconVisible=false;
363                 SystemTray.getSystemTray().remove(trayIcon);
364         }
365
366         private void clearSystray(Boolean addExitBtn) {
367                         popup.removeAll();
368                         //Add components to pop-up menu
369                         if(shell.isVisible()==true)
370                         {
371                                 popup.add(hideMain);
372                         } else {
373                                 popup.add(showMain);
374                         }
375                         popup.addSeparator();
376
377                         if(addExitBtn)
378                         {
379                                 popup.add(exitApp);
380                         }
381         }
382
383         public void shutDownApp()
384         {
385                 if(fkSerial != null)
386                 {
387                         fkSerial.disconnect();
388                 }
389                 if(sysTrayIconVisible == true)
390                 {
391                         destroySysTrayIcon();
392                 }
393                 System.exit(0);
394         }
395
396         /**
397          * Create contents of the window.
398          */
399         
400
401         /*uint8_t EncryptedStorage::crc8(const uint8_t *addr, uint8_t len)
402         {
403                 uint8_t crc = 0;
404
405                 while (len--) {
406                         uint8_t inbyte = *addr++;
407                         for (uint8_t i = 8; i; i--) {
408                                 uint8_t mix = (crc ^ inbyte) & 0x01;
409                                 crc >>= 1;
410                                 if (mix) crc ^= 0x8C;
411                                 inbyte >>= 1;
412                         }
413                 }
414                 return crc;
415         } */
416         
417         
418         protected void createContents() {
419
420
421                 shell = new Shell();
422                 shell.setImage( iconProgramOffline ); //$NON-NLS-1$
423                 shell.setSize(711, 655);
424
425                 prefs = Preferences.userNodeForPackage(this.getClass());
426
427                 FkManager.getInstance().sortById( prefs.getBoolean(PREF_SORT_BY_ID_KEY, false) );
428
429                 mySelf = this;
430                 shell.setLayout(new FillLayout(SWT.HORIZONTAL));
431
432                 tabFolder = new TabFolder(shell, SWT.NONE);
433                 tabFolder.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
434
435                 TabItem tbtmConnection = new TabItem(tabFolder, SWT.NONE);
436                 tbtmConnection.setText(Messages.MainWin_12);
437
438                 cmpConnect = new Composite(tabFolder, SWT.BORDER);
439                 tbtmConnection.setControl(cmpConnect);
440                 cmpConnect.setLayout(new FormLayout());
441
442                 btnConnect = new Button(cmpConnect, SWT.CENTER);
443                 btnConnect.setImage( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightning.png") ); //$NON-NLS-1$
444                 FormData fd_btnConnect = new FormData();
445                 fd_btnConnect.top = new FormAttachment(0, 8);
446                 fd_btnConnect.left = new FormAttachment(100, -125);
447                 fd_btnConnect.right = new FormAttachment(100, -10);
448
449                 btnConnect.setLayoutData(fd_btnConnect);
450
451                 btnConnect.addSelectionListener(new SelectionAdapter() {
452                         public void widgetSelected(SelectionEvent e) {
453
454                                 if( fkSerial!=null && fkSerial.state == SerialState.Connected )
455                                 {
456                                         fkSerial.disconnect();
457                                 } else {
458                                         connect();
459                                 }
460                         }
461
462                 });
463                 btnConnect.setText(Messages.MainWin_15);
464                 txtPsw = new Text(cmpConnect, SWT.BORDER | SWT.PASSWORD);
465                 FormData fd_txtPsw = new FormData();
466
467                 fd_txtPsw.left = new FormAttachment(0, 102);
468                 txtPsw.setLayoutData(fd_txtPsw);
469                 txtPsw.setFocus();
470                 txtPsw.addKeyListener( new KeyListener() {
471                         @Override
472                         public void keyReleased(KeyEvent arg0) { }
473
474                         @Override
475                         public void keyPressed(KeyEvent arg0) {
476                                 if( arg0.character==SWT.CR )
477                                 {
478                                         connect();
479                                 }
480                         }
481                 });
482
483                 txtLog = new Text(cmpConnect, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
484                 fd_btnConnect.bottom = new FormAttachment(txtPsw, 0, SWT.BOTTOM);
485                 FormData fd_txtLog = new FormData();
486                 fd_txtLog.bottom = new FormAttachment(100, -8);
487                 fd_txtLog.top = new FormAttachment(btnConnect, 8, SWT.BOTTOM);
488                 fd_txtLog.left = new FormAttachment(0, 8);
489                 fd_txtLog.right = new FormAttachment(btnConnect,0, SWT.RIGHT);
490                 txtLog.setLayoutData(fd_txtLog);
491                 txtLog.setEditable(false);
492
493                 lblPort = new Label(cmpConnect, SWT.NONE);
494                 lblPort.setAlignment(SWT.RIGHT);
495                 FormData fd_lblPort = new FormData();
496                 fd_lblPort.left = new FormAttachment(0, 10);
497                 lblPort.setLayoutData(fd_lblPort);
498                 lblPort.setText(Messages.MainWin_4);
499
500                 txtDev = new Text(cmpConnect, SWT.BORDER);
501                 fd_txtPsw.top = new FormAttachment(txtDev,4, SWT.BOTTOM);
502                 fd_lblPort.top = new FormAttachment(txtDev,0,SWT.CENTER);
503                 fd_lblPort.right = new FormAttachment(txtDev, -7);
504                 FormData fd_txtDev = new FormData();
505                 fd_txtDev.top = new FormAttachment(btnConnect,0,SWT.TOP);
506                 fd_txtDev.left = new FormAttachment(0, 102);
507                 txtDev.setLayoutData(fd_txtDev);
508                 txtDev.setText( prefs.get(PREF_PORT, PREF_DEFAULT_DEVICE));
509                 lblPassword = new Label(cmpConnect, SWT.NONE);
510                 lblPassword.setAlignment(SWT.RIGHT);
511                 FormData fd_lblPassword = new FormData();
512                 fd_lblPassword.right = new FormAttachment(0, 95);
513                 fd_lblPassword.top = new FormAttachment(txtPsw, 0, SWT.CENTER);
514                 fd_lblPassword.left = new FormAttachment(0, 10);
515                 lblPassword.setLayoutData(fd_lblPassword);
516                 lblPassword.setText(Messages.MainWin_18);
517
518                 animation = new Animation(cmpConnect, SWT.NONE, 4);
519                 fd_txtDev.right = new FormAttachment(animation,-16, SWT.LEFT );
520                 fd_txtPsw.right = new FormAttachment(animation,-16, SWT.LEFT );
521
522                 FormData fd_animation = new FormData();
523                 fd_animation.right = new FormAttachment(btnConnect, -16);
524                 fd_animation.top = new FormAttachment(0, 10);
525
526                 animation.setLayoutData(fd_animation);
527
528                 animation.setVisible(false);
529                 animation.setPlaying(false);
530                 cmpConnect.setTabList(new Control[]{txtPsw, btnConnect, txtDev});
531
532                 log(Messages.MainWin_22);
533
534
535                 //
536                 shell.addListener(SWT.Close, new Listener() {
537                         @Override
538                         public void handleEvent(Event arg0) {
539                                 if( prefs.getBoolean(PREF_HIDE_ON_CLOSE, true))
540                                 {
541                                         arg0.doit=false;
542                                         hideToTray();
543                                 } else {
544                                         shutDownApp();
545                                 }
546                         }
547                 });
548
549
550                 if( System.getProperty("os.name").compareTo("Linux") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
551                 {
552                         Boolean gtkOk=false;
553                         Map<String, String> env = System.getenv();
554                         for (String envName : env.keySet()) {
555                                 if( envName.compareTo("SWT_GTK3") == 0 && env.get(envName).compareTo("0") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
556                                 {
557                                         gtkOk=true;
558                                 }
559                         }
560
561                         if( !gtkOk )
562                         {
563                                 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$
564                         }
565                 }
566
567                 Device display = Display.getCurrent();
568                 blackColor = display.getSystemColor(SWT.COLOR_BLACK );
569                 grayColor = display.getSystemColor(SWT.COLOR_GRAY );
570                 defaultBgColor = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND );
571                 greenColor = display.getSystemColor(SWT.COLOR_DARK_GREEN );
572                 redColor = display.getSystemColor(SWT.COLOR_RED );
573
574                 
575                 //
576                 addSettingsTab();
577                 addThisFkTab();
578                 addAccountsTab();
579                 createSysTrayIcon();
580         }
581
582
583         private void connect() {
584                 fkSerial = new SerialWorker(mySelf);
585                 prefs.put(PREF_PORT, txtDev.getText() );
586                 fkSerial.connect(txtDev.getText(),txtPsw.getText());
587                 txtPsw.setText(""); //$NON-NLS-1$
588         }
589
590         @Override
591         public void updateAccountList()
592         {
593                 int free=256;
594                 clearSystray(false);
595                 lstAccounts.getList().removeAll();
596
597                 Boolean showAccountId = prefs.getBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false);
598
599                 for( FkManager.Account a : FkManager.getInstance().getList() )
600                 {
601                         free--;
602                         lstAccounts.add( a.showNumInName( showAccountId ) );
603
604                         //populate the popup
605                         Menu menu = new Menu(a.toString());
606                         MenuItem both = new MenuItem(Messages.MainWin_25);
607                         MenuItem usr = new MenuItem(Messages.MainWin_26);
608                         MenuItem psw = new MenuItem(Messages.MainWin_27);
609                         menu.add(both);
610                         menu.add(usr);
611                         menu.add(psw);
612
613
614                         both.addActionListener(FkManager.getInstance());
615                         both.setActionCommand( "%"+a.num ); //$NON-NLS-1$
616
617
618
619                         psw.addActionListener(FkManager.getInstance()); 
620                         psw.setActionCommand( "p"+a.num); //$NON-NLS-1$
621
622
623                         usr.addActionListener(FkManager.getInstance()); 
624                         usr.setActionCommand( "u"+a.num ); //$NON-NLS-1$
625
626                         popup.add(menu);
627                 }
628                 
629                 //Add the exit button
630                 popup.addSeparator();
631                 popup.add(exitApp);
632
633                 lblNumFree.setText(" "+free+Messages.MainWin_32); //$NON-NLS-1$
634                 lblNumFree.pack();
635                 lblNumFree.getParent().layout();
636
637                 if( free == 0 )
638                 {
639                         btnNewAccount.setVisible(false);
640                 } else {
641                         btnNewAccount.setVisible(true);
642                 }
643
644         }
645
646         @Override
647         public void serialEvent(SerialState state) {
648                 switch(state)
649                 {
650                 case Connected:
651                         shell.setText(Messages.MainWin_33 + FkManager.getInstance().getBanner());
652                         setSystrayIconTip(Messages.MainWin_33 + FkManager.getInstance().getBanner());
653
654                         animation.setVisible(false);
655                         animation.setPlaying(false);
656
657                         btnConnect.setText(Messages.MainWin_34);
658                         btnConnect.setEnabled(true);
659
660                         //Should we hide?
661                         if( prefs.getBoolean(PREF_AUTOHIDE, false) == true)
662                         {
663                                 hideToTray();
664                         }
665
666                         addThisFkTab();
667                         addAccountsTab();
668                         
669
670                         
671                         tabFolder.setSelection(tbtmAccounts);
672
673                         updateAccountList();
674                         txtFilter.setFocus();
675
676                         //Update icons for systray and window
677                         shell.setImage( iconProgramOnline );
678                         trayIcon.setImage( iconSystrayOnline );
679
680                         int numAccounts=FkManager.getInstance().getList().size();
681                         if( numAccounts>0 && prefs.getBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, true) )
682                         {
683
684                                 class TrayIconMessageTask implements Runnable
685                                 {
686                                         private String msg;
687                                         public TrayIconMessageTask(String _msg)
688                                         {
689                                                 msg = _msg;
690                                         }
691                                         public void run()
692                                         {
693                                                 trayIcon.displayMessage("FinalKey", msg, //$NON-NLS-1$ //$NON-NLS-2$
694                                                                 TrayIcon.MessageType.INFO);
695                                         }
696                                 }
697
698                                 new Thread( new TrayIconMessageTask(numAccounts +" "+ (( numAccounts >1)?Messages.MainWin_1:Messages.MainWin_2)) ).start();
699
700                         }
701
702                 //      log(Messages.MainWin_40);
703
704                         break;
705                 case Connecting:
706                         shell.setText(Messages.MainWin_41);
707                         animation.setVisible(true);
708                         animation.setPlaying(true);
709
710                         txtPsw.setEnabled(false);
711                         txtDev.setEnabled(false);
712                         btnConnect.setEnabled(false);
713
714                         lblPort.setEnabled(false);
715                         lblPassword.setEnabled(false);
716                         break;
717                 case Disconnected:
718                         setSystrayIconTip("The Final Key - Not Connected"); //$NON-NLS-1$
719
720                         fkSerial=null;
721                         animation.setVisible(false);
722                         animation.setPlaying(false);
723
724                         remAccountsTab();
725                         remThisFkTab();
726
727                         tabFolder.setSelection(0);
728
729                         shell.setText(Messages.MainWin_42);
730                         btnConnect.setText(Messages.MainWin_43);
731
732                         btnConnect.setEnabled(true);
733
734                         lblPort.setEnabled(true);
735                         lblPassword.setEnabled(true);
736                         txtPsw.setEnabled(true);
737                         txtPsw.setFocus();
738                         txtDev.setEnabled(true);
739
740                         cmpConnect.update();
741
742                         clearSystray(true);
743
744                         //Update icons for systray and window
745                         shell.setImage( iconProgramOffline );
746                         trayIcon.setImage( iconSystrayOffline );
747                         break;
748
749                 case Working:
750                         animation.setPlaying(false);
751                         animation.setVisible(false);
752                         break;
753
754                 default:
755                         break;
756                 }
757                 cmpConnect.layout();
758         }
759
760         private void updateSettingsSaveBtnVisibility()
761         {
762                 boolean show=false;
763                 if( btnFkSettingsSave != null)
764                 {
765                         if( FkManager.getInstance().getBanner().compareTo( txtBanner.getText() ) != 0 )
766                         {
767                                 show=true;
768                         }
769
770                         if( cmbLayout.getSelectionIndex() != -1  && cmbLayout.getItem(cmbLayout.getSelectionIndex()).compareTo(FkManager.getInstance().getCurrentLayout()) != 0 )
771                         {
772                                 show=true;
773                         }
774
775                         if(show)
776                         {
777                                 btnFkSettingsSave.setEnabled(true);
778                         } else {
779                                 btnFkSettingsSave.setEnabled(false);
780                         }
781                 }
782         }
783
784         private void addThisFkTab()
785         {
786                 tbtmThisFinalkey = new TabItem(tabFolder, SWT.NONE,1);
787                 tbtmThisFinalkey.setText(Messages.MainWin_tbtmThisFinalkey_text);
788
789                 composite = new Composite(tabFolder, SWT.NONE);
790                 tbtmThisFinalkey.setControl(composite);
791                 composite.setLayout(new FormLayout());
792
793                 Group group = new Group(composite, SWT.NONE);
794                 group.setText(Messages.MainWin_group_text);
795                 group.setLayout(new FormLayout());
796                 FormData fd_group = new FormData();
797                 fd_group.bottom = new FormAttachment(0, 100);
798                 fd_group.top = new FormAttachment(0, 10);
799                 fd_group.left = new FormAttachment(0, 10);
800                 fd_group.right = new FormAttachment(100, -10);
801                 group.setLayoutData(fd_group);
802
803
804                 Label lblBannerName = new Label(group, SWT.NONE);
805                 lblBannerName.setAlignment(SWT.RIGHT);
806                 FormData fd_lblBannerName = new FormData();
807                 fd_lblBannerName.left = new FormAttachment(0, 10);
808                 fd_lblBannerName.top = new FormAttachment(0, 10);
809                 lblBannerName.setLayoutData(fd_lblBannerName);
810                 lblBannerName.setText(Messages.MainWin_lblBannerName_text);
811
812                 txtBanner = new Text(group, SWT.BORDER);
813                 fd_lblBannerName.right = new FormAttachment(txtBanner, -6);
814                 FormData fd_txtBanner = new FormData();
815                 fd_txtBanner.right = new FormAttachment(0, 455);
816                 fd_txtBanner.top = new FormAttachment(0, 10);
817                 fd_txtBanner.left = new FormAttachment(0, 149);
818                 txtBanner.setLayoutData(fd_txtBanner);
819                 txtBanner.setToolTipText(Messages.MainWin_text_1_toolTipText);
820                 txtBanner.setText(Messages.MainWin_text_1_text_1);
821                 txtBanner.addVerifyListener(new VerifyListener() {
822                         @Override
823                         public void verifyText(VerifyEvent arg0) {
824                                 System.out.println("Verify:"+arg0.text );
825                                 arg0.doit=false;
826                                 if(arg0.text.length()==0)
827                                 {
828                                         arg0.doit=true;
829                                 } else if( FkManager.getInstance().isStringValidForFk(arg0.text))
830                                 {
831                                         if(txtBanner.getText().length()+arg0.text.length() < 32) //Banner can be a max of 31 chars long.
832                                         {
833                                                 arg0.doit=true;
834                                         }
835                                 }
836                         }
837                 });
838                 txtBanner.addModifyListener( new ModifyListener() {
839                         @Override
840                         public void modifyText(ModifyEvent arg0) {
841                                 updateSettingsSaveBtnVisibility();
842                         }
843                 });
844
845                 //Populate with data from FkManager
846                 txtBanner.setText( FkManager.getInstance().getBanner() );
847
848                 Label lblKeyboardLayout = new Label(group, SWT.NONE);
849                 lblKeyboardLayout.setAlignment(SWT.RIGHT);
850                 FormData fd_lblKeyboardLayout = new FormData();
851                 fd_lblKeyboardLayout.left = new FormAttachment(0, 10);
852                 fd_lblKeyboardLayout.bottom = new FormAttachment(100, -13);
853                 lblKeyboardLayout.setLayoutData(fd_lblKeyboardLayout);
854                 lblKeyboardLayout.setText(Messages.MainWin_lblKeyboardLayout_text);
855
856                 cmbLayout = new Combo(group, SWT.READ_ONLY);
857                 fd_lblKeyboardLayout.right = new FormAttachment(cmbLayout, -6);
858                 FormData fd_cmbLayout = new FormData();
859                 fd_cmbLayout.bottom = new FormAttachment(100, -11);
860                 fd_cmbLayout.top = new FormAttachment(txtBanner, 6);
861                 fd_cmbLayout.right = new FormAttachment(0, 301);
862                 fd_cmbLayout.left = new FormAttachment(0, 149);
863                 cmbLayout.setLayoutData(fd_cmbLayout);
864                 cmbLayout.setItems( FkManager.getInstance().getAvailableLayouts() );
865                 cmbLayout.addSelectionListener( new SelectionListener() {
866                         
867                         @Override
868                         public void widgetSelected(SelectionEvent arg0) {
869                                 updateSettingsSaveBtnVisibility();
870                         }
871
872                         @Override
873                         public void widgetDefaultSelected(SelectionEvent arg0) {} 
874                 });
875
876                 cmbLayout.select( cmbLayout.indexOf(FkManager.getInstance().getCurrentLayout() ));
877
878                 grpBackupAndRestore = new Group(composite, SWT.NONE);
879                 grpBackupAndRestore.setText(Messages.MainWin_grpBackupAndRestore_text);
880                 grpBackupAndRestore.setLayout(new FormLayout());
881                 FormData fd_grpBackupAndRestore = new FormData();
882                 fd_grpBackupAndRestore.top = new FormAttachment(group, 6);
883                 fd_grpBackupAndRestore.left = new FormAttachment(0, 10);
884                 fd_grpBackupAndRestore.bottom = new FormAttachment(0, 243);
885                 fd_grpBackupAndRestore.right = new FormAttachment(100, -10);
886
887                 btnFkSettingsSave = new Button(group, SWT.NONE);
888                 btnFkSettingsSave.addSelectionListener(new SelectionAdapter() {
889                         @Override
890                         public void widgetSelected(SelectionEvent e) {
891                                 SaveSettingsDialog dialog = new SaveSettingsDialog(shell, shell.getStyle() );
892                                 shell.setEnabled(false);
893                                 
894                                 if( (FkManager.getInstance().getBanner().compareTo( txtBanner.getText() ) != 0) )
895                                 {
896                                         dialog.bannerTxt = txtBanner.getText();
897                                 }
898                                 
899                                 if( (cmbLayout.getSelectionIndex() != -1  && cmbLayout.getItem(cmbLayout.getSelectionIndex()).compareTo(FkManager.getInstance().getCurrentLayout()) != 0) )
900                                 {
901                                         dialog.Layout = cmbLayout.getSelectionIndex()+1; //FinalKey presents the layout choice between 1,2,3
902                                 }
903                                 
904                                 dialog.open();
905                                 shell.setEnabled(true);
906                                 updateSettingsSaveBtnVisibility();
907                                 
908                         }
909                 });
910                 FormData fd_btnSave = new FormData();
911                 fd_btnSave.right = new FormAttachment(txtBanner, 68, SWT.RIGHT);
912                 fd_btnSave.top = new FormAttachment(lblBannerName, 0, SWT.TOP);
913                 fd_btnSave.left = new FormAttachment(txtBanner, 6);
914                 btnFkSettingsSave.setLayoutData(fd_btnSave);
915                 btnFkSettingsSave.setText(Messages.MainWin_btnSave_text);
916                 grpBackupAndRestore.setLayoutData(fd_grpBackupAndRestore);
917                 btnFkSettingsSave.setEnabled(false);
918
919                 group_1 = new Group(composite, SWT.NONE);
920                 group_1.setText("Security");
921                 group_1.setLayout(new FormLayout());
922                 FormData fd_group_1 = new FormData();
923                 fd_group_1.bottom = new FormAttachment(grpBackupAndRestore, 197, SWT.BOTTOM);
924                 fd_group_1.top = new FormAttachment(grpBackupAndRestore, 6);
925
926                 Button btnBackup = new Button(grpBackupAndRestore, SWT.CENTER);
927                 btnBackup.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/backup.png"));
928                 FormData fd_btnBackup = new FormData();
929                 fd_btnBackup.left = new FormAttachment(0, 10);
930                 fd_btnBackup.top = new FormAttachment(0, 10);
931                 fd_btnBackup.bottom = new FormAttachment(100, -28);
932                 btnBackup.setLayoutData(fd_btnBackup);
933                 btnBackup.setText(Messages.MainWin_btnBackupYourFinalkey_text);
934                 btnBackup.addSelectionListener(new SelectionAdapter() {
935                         @Override
936                         public void widgetSelected(SelectionEvent e) {
937                                 shell.setEnabled(false);;
938                                 Backup b = new Backup(shell, shell.getStyle() );
939                                 b.open(false);
940                                 shell.setEnabled(true);
941                         }
942                 });
943
944                 btnNewButton = new Button(grpBackupAndRestore, SWT.NONE);
945                 btnNewButton.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/restore.png"));
946                 FormData fd_btnNewButton = new FormData();
947                 fd_btnNewButton.top = new FormAttachment(0, 10);
948                 fd_btnNewButton.right = new FormAttachment(100, -10);
949                 fd_btnNewButton.bottom = new FormAttachment(100, -28);
950                 btnNewButton.setLayoutData(fd_btnNewButton);
951                 btnNewButton.setText(Messages.MainWin_btnNewButton_text);
952                 btnNewButton.addSelectionListener(new SelectionAdapter() {
953                         @Override
954                         public void widgetSelected(SelectionEvent e) {
955                                 shell.setEnabled(false);;
956                                 Backup b = new Backup(shell, shell.getStyle() );
957                                 b.open(true);
958                                 shell.setEnabled(true);
959                         }
960                 });
961                 
962                 lblMakeSureYou = new Label(grpBackupAndRestore, SWT.NONE);
963                 fd_btnNewButton.left = new FormAttachment(lblMakeSureYou, 6);
964                 fd_btnBackup.right = new FormAttachment(100, -555);
965                 FormData fd_lblMakeSureYou = new FormData();
966                 fd_lblMakeSureYou.top = new FormAttachment(0, 10);
967                 fd_lblMakeSureYou.left = new FormAttachment(btnBackup, 6);
968                 fd_lblMakeSureYou.right = new FormAttachment(0, 541);
969                 fd_lblMakeSureYou.bottom = new FormAttachment(0, 92);
970                 lblMakeSureYou.setLayoutData(fd_lblMakeSureYou);
971                 lblMakeSureYou.setText(Messages.MainWin_lblMakeSureYou_text);
972                 fd_group_1.right = new FormAttachment(group, 0, SWT.RIGHT);
973                 fd_group_1.left = new FormAttachment(0, 10);
974                 group_1.setLayoutData(fd_group_1);
975
976                 label = new Label(group_1, SWT.NONE);
977                 label.setText("Always use a secure master-password, but don't forget it, you can not unlock\nyour FinalKey without it! Changing the password is risky, it can result in data-loss\nif the process is interrupted, you should take a backup before changing your\nmaster-password. Changing master-password takes a while as it re-encrypts accounts.");
978                 FormData fd_label = new FormData();
979                 fd_label.bottom = new FormAttachment(0, 79);
980                 fd_label.top = new FormAttachment(0, 10);
981                 fd_label.right = new FormAttachment(0, 675);
982                 fd_label.left = new FormAttachment(0, 10);
983                 label.setLayoutData(fd_label);
984
985                 text_1 = new Text(group_1, SWT.BORDER | SWT.PASSWORD);
986                 FormData fd_text_1 = new FormData();
987                 fd_text_1.top = new FormAttachment(label, 6);
988                 fd_text_1.right = new FormAttachment(0, 455);
989                 fd_text_1.left = new FormAttachment(0, 149);
990                 text_1.setLayoutData(fd_text_1);
991
992                 text_2 = new Text(group_1, SWT.BORDER | SWT.PASSWORD);
993                 FormData fd_text_2 = new FormData();
994                 fd_text_2.top = new FormAttachment(text_1, 6);
995                 fd_text_2.right = new FormAttachment(0, 455);
996                 fd_text_2.left = new FormAttachment(0, 149);
997                 text_2.setLayoutData(fd_text_2);
998
999                 text_3 = new Text(group_1, SWT.BORDER | SWT.PASSWORD);
1000                 FormData fd_text_3 = new FormData();
1001                 fd_text_3.top = new FormAttachment(text_2, 6);
1002                 fd_text_3.right = new FormAttachment(text_1, 0, SWT.RIGHT);
1003                 fd_text_3.left = new FormAttachment(0, 149);
1004                 text_3.setLayoutData(fd_text_3);
1005
1006                 label_1 = new Label(group_1, SWT.NONE);
1007                 label_1.setText("Current Pass");
1008                 label_1.setAlignment(SWT.RIGHT);
1009                 FormData fd_label_1 = new FormData();
1010                 fd_label_1.top = new FormAttachment(label, 12);
1011                 fd_label_1.right = new FormAttachment(text_1, -6);
1012                 fd_label_1.left = new FormAttachment(label, 0, SWT.LEFT);
1013                 label_1.setLayoutData(fd_label_1);
1014
1015                 label_2 = new Label(group_1, SWT.NONE);
1016                 label_2.setText("New Pass");
1017                 label_2.setAlignment(SWT.RIGHT);
1018                 FormData fd_label_2 = new FormData();
1019                 fd_label_2.top = new FormAttachment(label_1, 12);
1020                 fd_label_2.right = new FormAttachment(text_2, -6);
1021                 fd_label_2.left = new FormAttachment(label, 0, SWT.LEFT);
1022                 label_2.setLayoutData(fd_label_2);
1023
1024                 label_3 = new Label(group_1, SWT.NONE);
1025                 label_3.setText("Repeat");
1026                 label_3.setAlignment(SWT.RIGHT);
1027                 FormData fd_label_3 = new FormData();
1028                 fd_label_3.top = new FormAttachment(label_2, 12);
1029                 fd_label_3.right = new FormAttachment(text_3, -6);
1030                 fd_label_3.left = new FormAttachment(text_3, -139, SWT.LEFT);
1031                 label_3.setLayoutData(fd_label_3);
1032
1033                 button = new Button(group_1, SWT.NONE);
1034                 button.setText("Re-encrypt");
1035                 FormData fd_button = new FormData();
1036                 fd_button.bottom = new FormAttachment(text_3, 0, SWT.BOTTOM);
1037                 fd_button.left = new FormAttachment(text_3, 6);
1038                 button.setLayoutData(fd_button);
1039
1040                 grpFormat = new Group(composite, SWT.NONE);
1041                 grpFormat.setText(Messages.MainWin_grpFormat_text);
1042                 FormData fd_grpFormat = new FormData();
1043                 fd_grpFormat.bottom = new FormAttachment(group_1, 69, SWT.BOTTOM);
1044                 fd_grpFormat.top = new FormAttachment(group_1, 6);
1045                 fd_grpFormat.right = new FormAttachment(group, 0, SWT.RIGHT);
1046                 fd_grpFormat.left = new FormAttachment(group, 0, SWT.LEFT);
1047                 grpFormat.setLayoutData(fd_grpFormat);
1048
1049                 lblNewLabel = new Label(grpFormat, SWT.NONE);
1050                 lblNewLabel.setBounds(10, 0, 366, 40);
1051                 lblNewLabel.setText(Messages.MainWin_lblNewLabel_text_1);
1052
1053                 btnNewButton_1 = new Button(grpFormat, SWT.NONE);
1054                 btnNewButton_1.setBounds(382, 10, 91, 26);
1055                 btnNewButton_1.setText(Messages.MainWin_btnNewButton_1_text);
1056
1057         }
1058         private void remThisFkTab() {
1059                 if(tbtmThisFinalkey != null)
1060                 {
1061                         tbtmThisFinalkey.dispose();
1062                 }
1063         }
1064
1065         private void remAccountsTab() {
1066                 if( tbtmAccounts != null )
1067                 {
1068                         tbtmAccounts.dispose();
1069                 }
1070
1071         }
1072
1073         void updateFilteredList(Vector<Account> source)
1074         {
1075                 lstAccounts.getList().removeAll();
1076
1077                 Boolean showAccountId = prefs.getBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false);
1078
1079                 for( FkManager.Account a : source )
1080                 {
1081                         lstAccounts.add( a.showNumInName( showAccountId ) );
1082                 }
1083         }
1084
1085         private void addAccountsTab() {
1086
1087                 tbtmAccounts = new TabItem(tabFolder, SWT.NONE, 1);//TabItem(tabFolder, SWT.NONE);
1088                 tbtmAccounts.setText(Messages.MainWin_45);
1089
1090                 cmpAccounts = new Composite(tabFolder, SWT.BORDER);
1091                 tbtmAccounts.setControl(cmpAccounts);
1092                 cmpAccounts.setLayout(new FormLayout());
1093
1094                 btnNewAccount = new Button(cmpAccounts, SWT.NONE);
1095                 btnNewAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/new.png")); //$NON-NLS-1$
1096                 btnNewAccount.addSelectionListener(new SelectionAdapter() {
1097                         @Override
1098                         public void widgetSelected(SelectionEvent e) {
1099                                 NewAccountDialog dialog = new NewAccountDialog(shell, shell.getStyle() );
1100                                 btnOpenAccount.setEnabled(false);
1101                                 shell.setEnabled(false);
1102                                 dialog.open();
1103                                 shell.setEnabled(true);
1104                                 updateAccountList();
1105                         }
1106                 });
1107                 FormData fd_btnNewAccoount = new FormData();
1108                 fd_btnNewAccoount.right = new FormAttachment(100, -10);
1109                 fd_btnNewAccoount.top = new FormAttachment(100, -40);
1110                 fd_btnNewAccoount.bottom = new FormAttachment(100, -6);
1111                 fd_btnNewAccoount.left = new FormAttachment(100, -200);
1112
1113                 btnNewAccount.setLayoutData(fd_btnNewAccoount);
1114                 btnNewAccount.setText(Messages.MainWin_47);
1115
1116                 lstAccounts = new ListViewer(cmpAccounts, SWT.BORDER | SWT.V_SCROLL);
1117                 lstAccountsControl = lstAccounts.getList();
1118                 lstAccountsControl.setLayoutData(new FormData());
1119
1120                 lblNumFree = new Label(cmpAccounts, SWT.NONE);
1121                 //lblNumFree.setText("Hello World!"); //$NON-NLS-1$
1122                 FormData fd_lblNumFree = new FormData();
1123                 fd_lblNumFree.left = new FormAttachment(lstAccountsControl,0,SWT.LEFT);
1124                 fd_lblNumFree.bottom = new FormAttachment(btnNewAccount, 0, SWT.CENTER);
1125                 lblNumFree.setLayoutData(fd_lblNumFree);
1126
1127                 btnOpenAccount = new Button(cmpAccounts, SWT.NONE);
1128                 //fd_lblNumFree.right = new FormAttachment(btnOpenAccount, -224);
1129                 btnOpenAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightbulb.png")); //$NON-NLS-1$
1130                 FormData fd_btnOpenAccount = new FormData();
1131                 fd_btnOpenAccount.top = new FormAttachment(btnNewAccount, 0, SWT.TOP);
1132                 fd_btnOpenAccount.bottom = new FormAttachment(btnNewAccount, 0, SWT.BOTTOM);
1133                 fd_btnOpenAccount.right = new FormAttachment(btnNewAccount, -8, SWT.LEFT );
1134                 btnOpenAccount.setLayoutData(fd_btnOpenAccount);
1135                 btnOpenAccount.setText(Messages.MainWin_5);
1136                 btnOpenAccount.getShell().layout();
1137                 btnOpenAccount.setEnabled(false);
1138
1139
1140                 FormData fd_lstAccounts = new FormData();
1141                 fd_lstAccounts.top = new FormAttachment(0, 8);
1142                 fd_lstAccounts.bottom = new FormAttachment(btnNewAccount, -6);
1143
1144                 txtFilter = new Text(cmpAccounts, SWT.BORDER);
1145                 txtFilter.setText(Messages.MainWin_10);
1146                 FormData fd_txtFilter = new FormData();
1147                 fd_txtFilter.left = new FormAttachment(lblNumFree, 8, SWT.RIGHT);
1148                 fd_txtFilter.bottom = new FormAttachment(btnOpenAccount,0, SWT.CENTER);
1149                 fd_txtFilter.right = new FormAttachment(btnOpenAccount, -8, SWT.LEFT);
1150                 txtFilter.setLayoutData(fd_txtFilter);
1151                 txtFilter.setForeground( grayColor );
1152                 txtFilter.addFocusListener( new FocusListener() {
1153                         @Override
1154                         public void focusLost(FocusEvent arg0) {
1155                                 txtFilter.setText(Messages.MainWin_10);
1156                                 txtFilter.setForeground( grayColor );
1157                                 txtFilter.setBackground( defaultBgColor );
1158                         }
1159
1160                         @Override
1161                         public void focusGained(FocusEvent arg0) {
1162                                 updateFilteredList( FkManager.getInstance().getList() );
1163                                 txtFilter.setForeground( blackColor );
1164                                 txtFilter.setText(""); //$NON-NLS-1$
1165                                 btnOpenAccount.setEnabled(false);
1166                         }
1167                 });
1168
1169                 txtFilter.addKeyListener( new KeyListener() {
1170
1171                         @Override
1172                         public void keyReleased(KeyEvent arg0) {
1173                                 if( arg0.keyCode == SWT.ESC )
1174                                 {
1175                                         txtFilter.setText(""); //$NON-NLS-1$
1176                                 }
1177
1178                                 txtFilterKeyPressEvent(arg0);
1179                         }
1180
1181                         @Override
1182                         public void keyPressed(KeyEvent arg0) {
1183
1184                         }
1185                 });
1186
1187                 fd_lstAccounts.left = new FormAttachment(0, 10);
1188                 fd_lstAccounts.right = new FormAttachment(100, -10);
1189
1190                 lstAccounts.getControl().setLayoutData(fd_lstAccounts);
1191                 lstAccounts.addDoubleClickListener( new IDoubleClickListener() {
1192
1193                         @Override
1194                         public void doubleClick(DoubleClickEvent arg0) {
1195                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
1196                                 if( !selection.isEmpty() )
1197                                 {
1198                                         Account acc = (Account)selection.getFirstElement();
1199                                         showTrigDialog(acc);
1200                                 }
1201                         }
1202
1203
1204                 });
1205
1206                 lstAccounts.addSelectionChangedListener( new ISelectionChangedListener() {
1207
1208                         @Override
1209                         public void selectionChanged(SelectionChangedEvent arg0) {
1210                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
1211                                 if( !selection.isEmpty() )
1212                                 {
1213                                         btnOpenAccount.setEnabled(true);
1214
1215                                         //Remove any listeners
1216                                         for( Listener s :btnOpenAccount.getListeners(SWT.Selection) )
1217                                         {
1218                                                 btnOpenAccount.removeListener(SWT.Selection, s);
1219                                         }
1220
1221                                         btnOpenAccount.addSelectionListener( new SelectionListener() {
1222
1223                                                 @Override
1224                                                 public void widgetSelected(SelectionEvent arg0) {
1225                                                         StructuredSelection selection = (StructuredSelection) lstAccounts.getSelection();
1226                                                         Account acc = (Account)selection.getFirstElement();
1227                                                         showTrigDialog(acc);
1228                                                 }
1229
1230                                                 @Override
1231                                                 public void widgetDefaultSelected(SelectionEvent arg0) {
1232                                                         // TODO Auto-generated method stub
1233                                                 }
1234                                         });
1235                                 }
1236                         }
1237                 });
1238
1239
1240                 lstAccountsControl.addKeyListener( new KeyListener() {
1241                         @Override
1242                         public void keyReleased(KeyEvent arg0) {
1243                         }
1244
1245                         @Override
1246                         public void keyPressed(KeyEvent arg0) {
1247                                 if( arg0.keyCode > 31 && arg0.keyCode < 126 && (arg0.stateMask&SWT.CTRL)!=SWT.CTRL )
1248                                 {
1249                                         txtFilter.setFocus();
1250                                         txtFilter.setText( ""+(char)arg0.keyCode ); //$NON-NLS-1$
1251                                         txtFilterKeyPressEvent(arg0);
1252                                         txtFilter.setSelection(1);
1253                                 } else if( arg0.keyCode == SWT.ARROW_UP)
1254                                 {
1255                                         //If selectionIndex is already 0, that means that the previous event caused that, so this time, it's changing nothing
1256                                         //and we can use it to wrap
1257                                         if( lstAccountsControl.getSelectionIndex() == 0)
1258                                         {
1259                                                 lstAccountsControl.setSelection( lstAccountsControl.getItemCount()-1 );
1260                                                 arg0.doit=false;
1261                                         }
1262                                 } else if( arg0.keyCode == SWT.ARROW_DOWN)
1263                                 {
1264                                         if( lstAccountsControl.getSelectionIndex() == lstAccountsControl.getItemCount()-1 )
1265                                         {
1266                                                 lstAccountsControl.setSelection(0);
1267                                                 arg0.doit=false;
1268                                         }
1269                                 }
1270                         }
1271                 });
1272
1273                 lstAccountsControl.addFocusListener( new FocusListener() {
1274
1275                         @Override
1276                         public void focusLost(FocusEvent arg0) {
1277
1278                         }
1279
1280                         @Override
1281                         public void focusGained(FocusEvent arg0) {
1282                                 if( lstAccountsControl.getItemCount() == 0 )
1283                                 {
1284                                         updateFilteredList(FkManager.getInstance().getList());
1285                                 }
1286
1287                         }
1288                 });
1289
1290                 cmpAccounts.setTabList(new Control[]{txtFilter,lstAccountsControl, btnOpenAccount, btnNewAccount});
1291         }
1292
1293         protected void txtFilterKeyPressEvent(KeyEvent arg0) {
1294                 Vector<Account> res = null;
1295
1296                 if( txtFilter.getText().length() == 0 )
1297                 {
1298                         res=FkManager.getInstance().getList();
1299                         txtFilter.setBackground( defaultBgColor );
1300                 } else {
1301                         res = FkManager.getInstance().getList(txtFilter.getText());
1302                         if( res.size() < 1 )
1303                         {
1304                                 txtFilter.setBackground( redColor );
1305                         } else if( res.size() == 1 )
1306                         {
1307                                 txtFilter.setBackground( greenColor );
1308                                 if( arg0.character== SWT.CR)
1309                                 {
1310                                         showTrigDialog(res.get(0));
1311                                 }
1312                         } else {
1313                                 txtFilter.setBackground( defaultBgColor );
1314                         }
1315                 }
1316
1317
1318                 if( res != null )
1319                 {
1320                         updateFilteredList( res );
1321                         if( res.size() > 0 )
1322                         {
1323                                 if( arg0.keyCode == SWT.ARROW_UP )
1324                                 {
1325                                         lstAccountsControl.setFocus();
1326                                         lstAccountsControl.setSelection( res.size()-1 );
1327                                 }
1328
1329                                 if( arg0.keyCode == SWT.ARROW_DOWN )
1330                                 {
1331                                         lstAccountsControl.setFocus();
1332                                         lstAccountsControl.setSelection( 0 );
1333                                 }
1334                         }
1335                 }
1336         }
1337
1338         private void addSettingsTab()
1339         {
1340                 tbtmSettings = new TabItem(tabFolder, SWT.NONE);
1341                 tbtmSettings.setText(Messages.MainWin_tbtmSettings_text);
1342
1343                 cmpSettings = new Composite(tabFolder, SWT.BORDER);
1344                 tbtmSettings.setControl(cmpSettings);
1345                 cmpSettings.setLayout(new FormLayout());
1346
1347                 chkAutoHide = new Button(cmpSettings, SWT.CHECK);
1348                 chkAutoHide.setText(Messages.MainWin_6);
1349                 chkAutoHide.setSelection( prefs.getBoolean( PREF_AUTOHIDE, false ) );
1350                 FormData fd_chkAutoHide = new FormData();
1351                 fd_chkAutoHide.top = new FormAttachment(0, 10);
1352                 fd_chkAutoHide.left = new FormAttachment(0, 10);
1353                 chkAutoHide.setLayoutData(fd_chkAutoHide);
1354                 chkAutoHide.addSelectionListener( new SelectionAdapter() {
1355                         public void widgetSelected(SelectionEvent e) {
1356                                 prefs.putBoolean(PREF_AUTOHIDE, chkAutoHide.getSelection() );
1357                         }
1358                 });
1359
1360                 chkSortByAccountId = new Button(cmpSettings, SWT.CHECK);
1361                 fd_chkAutoHide.right = new FormAttachment(chkSortByAccountId, 0, SWT.RIGHT);
1362                 chkSortByAccountId.setText(Messages.MainWin_9);
1363                 chkSortByAccountId.setSelection( prefs.getBoolean( PREF_SORT_BY_ID_KEY, false) );
1364                 FormData fd_btnSortByAccountId = new FormData();
1365                 fd_btnSortByAccountId.top = new FormAttachment(chkAutoHide, 6);
1366                 fd_btnSortByAccountId.left = new FormAttachment(0, 10);
1367                 fd_btnSortByAccountId.right = new FormAttachment(100, -10);
1368                 chkSortByAccountId.setLayoutData(fd_btnSortByAccountId);
1369                 chkSortByAccountId.addSelectionListener( new SelectionAdapter() {
1370                         public void widgetSelected(SelectionEvent e) {
1371                                 prefs.putBoolean( PREF_SORT_BY_ID_KEY, chkSortByAccountId.getSelection() );
1372                                 FkManager.getInstance().sortById( chkSortByAccountId.getSelection() );
1373                                 updateAccountList();
1374                         }
1375                 });
1376
1377                 chkShowAccountId = new Button(cmpSettings, SWT.CHECK);
1378                 FormData fd_chkShowAccountId = new FormData();
1379                 fd_chkShowAccountId.left = new FormAttachment(0, 10);
1380                 fd_chkShowAccountId.right = new FormAttachment(100, -10);
1381                 fd_chkShowAccountId.top = new FormAttachment(chkSortByAccountId, 6);
1382                 chkShowAccountId.setLayoutData(fd_chkShowAccountId);
1383                 chkShowAccountId.setText(Messages.MainWin_btnShowAccountId_text);
1384                 chkShowAccountId.setSelection( prefs.getBoolean(PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false) );
1385                 chkShowAccountId.addSelectionListener( new SelectionAdapter() {
1386                         public void widgetSelected(SelectionEvent e) {
1387                                 prefs.putBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, chkShowAccountId.getSelection() );
1388                                 updateAccountList();
1389                         }
1390                 });
1391
1392                 chkCheckForUpdates = new Button(cmpSettings, SWT.CHECK);
1393                 FormData fd_chkCheckForUpdates = new FormData();
1394                 fd_chkCheckForUpdates.left = new FormAttachment(0, 10);
1395                 fd_chkCheckForUpdates.right = new FormAttachment(100, -10);
1396                 fd_chkCheckForUpdates.top = new FormAttachment(chkShowAccountId, 6);
1397                 chkCheckForUpdates.setLayoutData(fd_chkCheckForUpdates);
1398                 chkCheckForUpdates.setText(Messages.MainWin_btnCheckForUpdates_text);
1399                 chkCheckForUpdates.setSelection( prefs.getBoolean( PREF_ALLOW_UPDATE_CHECK, true) );
1400                 chkCheckForUpdates.addSelectionListener( new SelectionAdapter() {
1401                         public void widgetSelected(SelectionEvent e) {
1402                                 prefs.putBoolean( PREF_ALLOW_UPDATE_CHECK, chkCheckForUpdates.getSelection() );
1403                                 checkForUpdates();
1404                         }
1405                 });
1406
1407                 btnShowaccountsReady = new Button(cmpSettings, SWT.CHECK);
1408                 btnShowaccountsReady.setSelection( prefs.getBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, true) );
1409                 FormData fd_btnShowaccountsReady = new FormData();
1410                 fd_btnShowaccountsReady.left = new FormAttachment(0, 10);
1411                 fd_btnShowaccountsReady.right = new FormAttachment(100, -10);
1412                 fd_btnShowaccountsReady.top = new FormAttachment(chkCheckForUpdates, 6);
1413                 btnShowaccountsReady.setLayoutData(fd_btnShowaccountsReady);
1414
1415                 btnShowaccountsReady.setText(Messages.MainWin_btnShowaccountsReady_text);
1416                 btnShowaccountsReady.addSelectionListener( new SelectionAdapter() {
1417                 public void widgetSelected(SelectionEvent e) {
1418                                 prefs.putBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, btnShowaccountsReady.getSelection() );
1419                         }
1420                 });
1421
1422                 Button btnShowSystrayName = new Button( cmpSettings, SWT.CHECK );
1423                 btnShowSystrayName.setSelection( prefs.getBoolean( PREF_SHOW_SYSTRAY_NAME,  true) );
1424                 FormData fd_btnShowSystrayName = new FormData();
1425                 fd_btnShowSystrayName.left = new FormAttachment(0, 10);
1426                 fd_btnShowSystrayName.right = new FormAttachment(100, -10);
1427                 fd_btnShowSystrayName.top = new FormAttachment(btnShowaccountsReady, 6);
1428                 btnShowSystrayName.setLayoutData(fd_btnShowSystrayName);
1429                 btnShowSystrayName.setText( Messages.MainWin_14 );
1430                 btnShowSystrayName.addSelectionListener( new SelectionAdapter() {
1431                         public void widgetSelected(SelectionEvent e) {
1432                                 Button b = (Button)e.widget;
1433                                 prefs.putBoolean( PREF_SHOW_SYSTRAY_NAME, b.getSelection() );
1434                                 setSystrayIconTip(null);
1435                         }
1436                 });
1437
1438                 btnHideToTrayOnClose = new Button(cmpSettings, SWT.CHECK);
1439                 btnHideToTrayOnClose.setSelection( prefs.getBoolean( PREF_HIDE_ON_CLOSE, true) );
1440                 btnHideToTrayOnClose.addSelectionListener(new SelectionAdapter() {
1441                         @Override
1442                         public void widgetSelected(SelectionEvent e) {
1443                                 prefs.putBoolean( PREF_HIDE_ON_CLOSE, btnHideToTrayOnClose.getSelection() );
1444                         }
1445                 });
1446                 btnHideToTrayOnClose.setToolTipText(Messages.MainWin_btnHideToTrayOnClose_toolTipText);
1447                 FormData fd_btnHideToTrayOnClose = new FormData();
1448                 fd_btnHideToTrayOnClose.top = new FormAttachment(btnShowSystrayName, 6);
1449                 fd_btnHideToTrayOnClose.right = new FormAttachment(0, 695);
1450                 fd_btnHideToTrayOnClose.left = new FormAttachment(0, 10);
1451                 btnHideToTrayOnClose.setLayoutData(fd_btnHideToTrayOnClose);
1452                 btnHideToTrayOnClose.setText(Messages.MainWin_btnCheckButton_text);
1453
1454         }
1455
1456         private void showTrigDialog(Account acc) {
1457                 Account selectedAccount = acc;
1458                 TriggerDialog diag = new TriggerDialog(shell, shell.getStyle(), acc,
1459                                 mySelf);
1460
1461                 shell.setEnabled(false);
1462                 diag.open();
1463
1464                 // Check if the account still exists
1465                 Boolean deleted = true;
1466                 for (Account a : FkManager.getInstance().getList()) {
1467                         if (a.equals(selectedAccount)) {
1468                                 deleted = false;
1469                                 break;
1470                         }
1471                 }
1472
1473                 if (deleted) {
1474                         btnOpenAccount.setEnabled(false);
1475                 }
1476
1477                 txtFilter.setFocus();
1478                 shell.setEnabled(true);
1479         }
1480
1481         @Override
1482         public void updateCheckFinished(UpdateResultEvent event) {
1483                 updateCheckThread=null;
1484                 switch(event.result)
1485                 {
1486                 case CHECK_FAILED:
1487                         System.out.println("Trouble checking for updates."); //$NON-NLS-1$
1488                         break;
1489                 case NO_UPDATE:
1490                         System.out.println("No update avaiable at this time."); //$NON-NLS-1$
1491                         break;
1492                 case UPDATE_AVAILABLE:
1493                         String title =Messages.MainWin_52+event.version+Messages.MainWin_53;
1494                         String text = Messages.MainWin_54+event.message;
1495                         log(title);
1496                         log(text);
1497
1498                         MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION |SWT.YES | SWT.NO);
1499                         dialog.setText(title);
1500                         dialog.setMessage(text+"\n\n\n"+Messages.MainWin_13); //$NON-NLS-1$
1501                         int res = dialog.open();
1502
1503                         if( res == SWT.YES )
1504                         {
1505                                 if(Desktop.isDesktopSupported())
1506                                 {
1507                                         try {
1508                                                 URI uri = new URI("http://finalkey.net/"); //$NON-NLS-1$
1509                                                 log(Messages.MainWin_16+uri.toString());
1510                                                 //FIXME: For whichever reason, this causes busy-cursor on the application, even though ui works fine.
1511                                                 Desktop.getDesktop().browse(uri);
1512                                                 log(Messages.MainWin_17);
1513                                         } catch (Exception e) {
1514                                                 log(Messages.MainWin_20);
1515                                                 log(Messages.MainWin_21);
1516                                                 log(e.getLocalizedMessage());
1517                                         }
1518                                 }
1519                         } else {
1520                                 log(Messages.MainWin_11);
1521                         }
1522
1523                         break;
1524                 }
1525
1526         }
1527 }