1c04c73ec3e9a0b0877cd7408a7f174ad845e3d1
[FinalKeyGui.git] / src / fkgui / MainWin.java
1 package fkgui;
2
3
4 import java.util.Map;
5 import java.util.Vector;
6 import  java.util.prefs.*;
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10
11 import javax.swing.UIManager;
12
13 import org.eclipse.jface.viewers.DoubleClickEvent;
14 import org.eclipse.jface.viewers.IDoubleClickListener;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ListViewer;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Listener;
21 import org.eclipse.swt.widgets.MessageBox;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.FocusListener;
27 import org.eclipse.swt.events.KeyEvent;
28 import org.eclipse.swt.events.KeyListener;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.events.ShellEvent;
33 import org.eclipse.swt.events.ShellListener;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.Device;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.wb.swt.SWTResourceManager;
40
41 import fkgui.FkManager.Account;
42 import fkgui.SerialWorker.SerialState;
43 import fkgui.UpdateChecker.UpdateCheckResultListener;
44
45 import org.eclipse.swt.widgets.TabFolder;
46 import org.eclipse.swt.widgets.TabItem;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.layout.FormLayout;
49 import org.eclipse.swt.layout.FormData;
50 import org.eclipse.swt.layout.FormAttachment;
51 import org.eclipse.swt.layout.FillLayout;
52 import org.eclipse.swt.widgets.List;
53
54
55 public class MainWin implements ConsoleMsg, UpdateCheckResultListener {
56
57         protected Shell shell;
58         private Text txtPsw;
59         public TrayIcon trayIcon;
60         public PopupMenu popup;
61         public MenuItem showMain;
62         public MenuItem hideMain;
63
64         public Text txtLog;
65         public Button btnConnect;
66         public Label lblPort;
67         public Label lblPassword;
68         
69         public Animation animation;
70         
71         public TabFolder tabFolder;
72
73         private Text txtDev;
74         Preferences prefs;
75         MainWin mySelf;
76         private SerialState lastState = SerialState.Disconnected;
77         
78         SerialWorker fkSerial;
79         private boolean sysTrayIconVisible;
80         
81         Button btnOpenAccount;
82         private Color blackColor;
83         private Color grayColor;
84         private Color defaultBgColor;
85         private Color greenColor;
86         private Color redColor;
87         
88         private static final String PREF_PORT ="lastUsedPortPref"; //$NON-NLS-1$
89         private static final String PREF_DEFAULT_DEVICE = "/dev/FinalKey"; //$NON-NLS-1$
90         private static final String PREF_AUTOHIDE = "hideMainWinAfterConnect"; //$NON-NLS-1$
91         private static final String PREF_SORT_BY_ID_KEY = "sortAccountListById"; //$NON-NLS-1$
92         private static final String PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY = "showAccountIdInName"; //$NON-NLS-1$
93         private static final String PREF_ALLOW_UPDATE_CHECK = "allowUpdateCheck"; //$NON-NLS-1$
94         private static final String PREF_SHOW_ACCOUNTS_READY_NOTICE = "showAccountsReadyNotice"; //$NON-NLS-1$
95
96         public Composite cmpConnect;
97         private Composite cmpAccounts;
98         ListViewer lstAccounts;
99         private List lstAccountsControl;
100         Label lblNumFree;
101         Button btnNewAccount;
102         Button chkCheckForUpdates;
103         Boolean hiddenAtStart=false;
104         Button btnShowaccountsReady;
105  
106         //Icons
107         private org.eclipse.swt.graphics.Image iconProgramOnline  = SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/finalkey.png");  //$NON-NLS-1$
108         private org.eclipse.swt.graphics.Image iconProgramOffline  = SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/finalkey-big-offline.png");  //$NON-NLS-1$
109         private Image iconSystrayOnline = Toolkit.getDefaultToolkit().createImage(getClass().getResource("gfx/systray-color.png"));  //$NON-NLS-1$
110         private Image iconSystrayOffline= Toolkit.getDefaultToolkit().createImage(getClass().getResource("gfx/systray-offline.png"));  //$NON-NLS-1$
111
112         private TabItem tbtmAccounts;
113         private TabItem tbtmSettings;
114         private Composite cmpSettings;
115         private Button chkAutoHide;
116         private Button chkSortByAccountId;
117         private Button chkShowAccountId;
118         private Thread updateCheckThread;
119         private Text txtFilter;
120
121         
122         
123         /**
124          * Launch the application.
125          * @param args
126          */
127         public static void main(String[] args) {
128
129                 try {
130                         MainWin window = new MainWin();
131
132                         for(String s : args )
133                         {
134                                 if( s.compareTo("--hide") == 0 ) //$NON-NLS-1$
135                                 {
136                                         window.hiddenAtStart=true;
137                                 }
138                         }
139
140                         window.open();
141                 } catch (Exception e) {
142                         e.printStackTrace();
143                 }
144                 
145         }
146
147         /**
148          * Open the window.
149          */
150         public void open() {
151                 Display display = Display.getDefault();
152                 createContents();
153                 addAccountsTab();
154                 addSettingsTab();
155                 createSysTrayIcon();
156
157                 serialEvent(SerialState.Disconnected);
158
159
160                 checkForUpdates();
161
162                 if( hiddenAtStart )
163                 {
164                         hideToTray();
165                 } else {
166                         shell.open();
167                         shell.layout();
168                 }
169
170                 while (!shell.isDisposed()) {
171                         if (!display.readAndDispatch()) {
172                                 display.sleep();                                
173                         }
174                 }
175         }
176         
177         private void checkForUpdates() {
178                 if( prefs.getBoolean(PREF_ALLOW_UPDATE_CHECK, true) )
179                 {
180                         if( updateCheckThread == null )
181                         {
182                                 updateCheckThread = new Thread(new UpdateChecker(this));
183                                 updateCheckThread.start();
184                         } else {
185                                 System.out.println(Messages.MainWin_0);
186                         }
187                 } else {
188                         System.out.println(Messages.MainWin_3);
189                 }
190         }
191
192         public void log( String str )
193         {
194                 txtLog.append(str+"\n"); //$NON-NLS-1$
195                 if( tabFolder.getSelectionIndex() == 0 )
196                 {
197                         txtLog.redraw();
198                         shell.redraw();
199                 }
200                 System.out.println(str);
201
202         }
203
204         /**
205          * Create a systemTray icon
206          */
207         private void createSysTrayIcon()
208         {
209                 sysTrayIconVisible=true;
210                 //Check the SystemTray is supported
211                 if (!SystemTray.isSupported()) {
212                         log("SystemTray is not supported, app is useless"); //$NON-NLS-1$
213                         return;
214                 }
215
216                 try {
217                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
218                 } catch (Exception e1) {
219                         e1.printStackTrace();
220                 } 
221
222                 popup = new PopupMenu();
223                 trayIcon = new TrayIcon( iconSystrayOffline ); //$NON-NLS-1$
224                 trayIcon.setToolTip("The Final Key - Hardware password manager"); //$NON-NLS-1$
225                 trayIcon.setImageAutoSize(true);
226                 final SystemTray tray = SystemTray.getSystemTray();
227
228                 // Create a pop-up menu components
229                 showMain = new MenuItem(Messages.MainWin_7);
230                 hideMain = new MenuItem(Messages.MainWin_8);
231
232                 showMain.addActionListener(new ActionListener() {
233                         @Override
234                         public void actionPerformed(ActionEvent e) {
235                                 Display.getDefault().syncExec( new Runnable(){
236
237                                         public void run()
238                                         {
239                                                 showFromTray();
240                                         }
241                                 } );
242                         }
243
244                 });
245
246                 hideMain.addActionListener(new ActionListener() {
247                         @Override
248                         public void actionPerformed(ActionEvent e) {
249                                 Display.getDefault().syncExec( new Runnable(){
250
251                                         public void run()
252                                         {
253                                                 hideToTray();
254                                         }
255                                 } );
256                         }
257                 });
258
259                 clearSystray();
260
261
262                 trayIcon.setPopupMenu(popup);
263
264                 try {
265                         tray.add(trayIcon);
266                 } catch (AWTException e) {
267                         log("TrayIcon could not be added."); //$NON-NLS-1$
268                 }
269
270         }
271
272         private void hideToTray()
273         {
274                 popup.remove(0);
275                 popup.insert(showMain, 0);
276                 shell.setVisible(false);
277         }
278
279         private void showFromTray()
280         {
281                 popup.remove(0);
282                 popup.insert(hideMain, 0);
283                 shell.setVisible(true);
284                 shell.forceActive();
285         }
286
287         private void destroySysTrayIcon()
288         {
289                 sysTrayIconVisible=false;
290                 SystemTray.getSystemTray().remove(trayIcon);
291         }
292
293         private void clearSystray() {
294                         popup.removeAll();
295                         //Add components to pop-up menu
296                         if(shell.isVisible()==true)
297                         {
298                                 popup.add(hideMain);
299                         } else {
300                                 popup.add(showMain);
301                         }
302                         popup.addSeparator();
303         }
304
305         public void shutDownApp()
306         {
307                 if(fkSerial != null)
308                 {
309                         fkSerial.disconnect();
310                 }
311                 if(sysTrayIconVisible == true)
312                 {
313                         destroySysTrayIcon();
314                 }
315                 System.exit(0);
316         }
317
318         /**
319          * Create contents of the window.
320          */
321         protected void createContents() {
322
323
324                 shell = new Shell();
325                 shell.setImage( iconProgramOffline ); //$NON-NLS-1$
326                 shell.setSize(711, 655);
327
328
329                 prefs = Preferences.userNodeForPackage(this.getClass());
330
331                 FkManager.getInstance().sortById( prefs.getBoolean(PREF_SORT_BY_ID_KEY, false) );
332
333                 mySelf = this;
334                 shell.setLayout(new FillLayout(SWT.HORIZONTAL));
335
336                 tabFolder = new TabFolder(shell, SWT.NONE);
337                 tabFolder.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
338
339                 TabItem tbtmConnection = new TabItem(tabFolder, SWT.NONE);
340                 tbtmConnection.setText(Messages.MainWin_12);
341
342                 cmpConnect = new Composite(tabFolder, SWT.BORDER);
343                 tbtmConnection.setControl(cmpConnect);
344                 cmpConnect.setLayout(new FormLayout());
345
346                 btnConnect = new Button(cmpConnect, SWT.CENTER);
347                 btnConnect.setImage( SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightning.png") ); //$NON-NLS-1$
348                 FormData fd_btnConnect = new FormData();
349                 fd_btnConnect.top = new FormAttachment(0, 8);
350                 fd_btnConnect.left = new FormAttachment(100, -125);
351                 fd_btnConnect.right = new FormAttachment(100, -10);
352
353                 btnConnect.setLayoutData(fd_btnConnect);
354
355                 btnConnect.addSelectionListener(new SelectionAdapter() {
356                         public void widgetSelected(SelectionEvent e) {
357
358                                 if( fkSerial!=null && fkSerial.state == SerialState.Connected )
359                                 {
360                                         fkSerial.disconnect();
361                                 } else {
362                                         connect();
363                                 }
364                         }
365
366                 });
367                 btnConnect.setText(Messages.MainWin_15);
368                 txtPsw = new Text(cmpConnect, SWT.BORDER | SWT.PASSWORD);
369                 FormData fd_txtPsw = new FormData();
370
371                 fd_txtPsw.left = new FormAttachment(0, 102);
372                 txtPsw.setLayoutData(fd_txtPsw);
373                 txtPsw.setFocus();
374                 txtPsw.addKeyListener( new KeyListener() {
375                         @Override
376                         public void keyReleased(KeyEvent arg0) { }
377
378                         @Override
379                         public void keyPressed(KeyEvent arg0) {
380                                 if( arg0.character==SWT.CR )
381                                 {
382                                         connect();
383                                 }
384                         }
385                 });
386
387                 txtLog = new Text(cmpConnect, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
388                 fd_btnConnect.bottom = new FormAttachment(txtPsw, 0, SWT.BOTTOM);
389                 FormData fd_txtLog = new FormData();
390                 fd_txtLog.bottom = new FormAttachment(100, -8);
391                 fd_txtLog.top = new FormAttachment(btnConnect, 8, SWT.BOTTOM);
392                 fd_txtLog.left = new FormAttachment(0, 8);
393                 fd_txtLog.right = new FormAttachment(btnConnect,0, SWT.RIGHT);
394                 txtLog.setLayoutData(fd_txtLog);
395                 txtLog.setEditable(false);
396
397                 lblPort = new Label(cmpConnect, SWT.NONE);
398                 lblPort.setAlignment(SWT.RIGHT);
399                 FormData fd_lblPort = new FormData();
400                 fd_lblPort.left = new FormAttachment(0, 10);
401                 lblPort.setLayoutData(fd_lblPort);
402                 lblPort.setText(Messages.MainWin_4);
403
404                 txtDev = new Text(cmpConnect, SWT.BORDER);
405                 fd_txtPsw.top = new FormAttachment(txtDev,4, SWT.BOTTOM);
406                 fd_lblPort.top = new FormAttachment(txtDev,0,SWT.CENTER);
407                 fd_lblPort.right = new FormAttachment(txtDev, -7);
408                 FormData fd_txtDev = new FormData();
409                 fd_txtDev.top = new FormAttachment(btnConnect,0,SWT.TOP);
410                 fd_txtDev.left = new FormAttachment(0, 102);
411                 txtDev.setLayoutData(fd_txtDev);
412                 txtDev.setText( prefs.get(PREF_PORT, PREF_DEFAULT_DEVICE));
413                 lblPassword = new Label(cmpConnect, SWT.NONE);
414                 lblPassword.setAlignment(SWT.RIGHT);
415                 FormData fd_lblPassword = new FormData();
416                 fd_lblPassword.right = new FormAttachment(0, 95);
417                 fd_lblPassword.top = new FormAttachment(0, 29);
418                 fd_lblPassword.left = new FormAttachment(0, 10);
419                 lblPassword.setLayoutData(fd_lblPassword);
420                 lblPassword.setText(Messages.MainWin_18);
421
422                 animation = new Animation(cmpConnect, SWT.NONE, 4);
423                 fd_txtDev.right = new FormAttachment(animation,-16, SWT.LEFT );
424                 fd_txtPsw.right = new FormAttachment(animation,-16, SWT.LEFT );
425
426                 FormData fd_animation = new FormData();
427                 fd_animation.right = new FormAttachment(btnConnect, -16);
428                 fd_animation.top = new FormAttachment(0, 10);
429
430                 animation.setLayoutData(fd_animation);
431
432                 animation.setVisible(false);
433                 animation.setPlaying(false);
434                 cmpConnect.setTabList(new Control[]{txtPsw, btnConnect, txtDev});
435
436                 log(Messages.MainWin_22);
437
438
439                 shell.addShellListener( new ShellListener() {
440
441                         public void shellIconified(ShellEvent e) {
442                         }
443
444                         public void shellDeiconified(ShellEvent e) {
445                         }
446
447                         public void shellDeactivated(ShellEvent e) {
448                         }
449
450                         public void shellClosed(ShellEvent e) {
451                                 shutDownApp();
452                         }
453
454                         public void shellActivated(ShellEvent e) {
455
456                         }
457                 } );
458
459
460                 if( System.getProperty("os.name").compareTo("Linux") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
461                 {
462                         Boolean gtkOk=false;
463                         Map<String, String> env = System.getenv();
464                         for (String envName : env.keySet()) {
465                                 if( envName.compareTo("SWT_GTK3") == 0 && env.get(envName).compareTo("0") == 0 ) //$NON-NLS-1$ //$NON-NLS-2$
466                                 {
467                                         gtkOk=true;
468                                 }
469                         }
470
471                         if( !gtkOk )
472                         {
473                                 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$
474                         }
475                 }
476
477                 Device display = Display.getCurrent();
478                 blackColor = display.getSystemColor(SWT.COLOR_BLACK );
479                 grayColor = display.getSystemColor(SWT.COLOR_GRAY );
480                 defaultBgColor = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND );
481                 greenColor = display.getSystemColor(SWT.COLOR_DARK_GREEN );
482                 redColor = display.getSystemColor(SWT.COLOR_RED );
483
484         }
485
486
487         private void connect() {
488                 fkSerial = new SerialWorker(mySelf);
489                 prefs.put(PREF_PORT, txtDev.getText() );
490                 fkSerial.connect(txtDev.getText(),txtPsw.getText());
491                 txtPsw.setText(""); //$NON-NLS-1$
492         }
493
494         @Override
495         public void updateAccountList()
496         {
497                 int free=256;
498                 clearSystray();
499                 lstAccounts.getList().removeAll();
500
501                 Boolean showAccountId = prefs.getBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false);
502
503                 for( FkManager.Account a : FkManager.getInstance().getList() )
504                 {
505                         free--;
506                         lstAccounts.add( a.showNumInName( showAccountId ) );
507
508                         Menu menu = new Menu(a.toString());
509                         MenuItem both = new MenuItem(Messages.MainWin_25);
510                         MenuItem usr = new MenuItem(Messages.MainWin_26);
511                         MenuItem psw = new MenuItem(Messages.MainWin_27);
512                         menu.add(both);
513                         menu.add(usr);
514                         menu.add(psw);
515
516
517                         both.addActionListener(FkManager.getInstance());
518                         both.setActionCommand( "%"+a.num ); //$NON-NLS-1$
519
520
521
522                         psw.addActionListener(FkManager.getInstance()); 
523                         psw.setActionCommand( "p"+a.num); //$NON-NLS-1$
524
525
526                         usr.addActionListener(FkManager.getInstance()); 
527                         usr.setActionCommand( "u"+a.num ); //$NON-NLS-1$
528
529                         popup.add(menu);
530                 }
531
532                 lblNumFree.setText(" "+free+Messages.MainWin_32); //$NON-NLS-1$
533                 lblNumFree.pack();
534                 lblNumFree.getParent().layout();
535
536                 if( free == 0 )
537                 {
538                         btnNewAccount.setVisible(false);
539                 } else {
540                         btnNewAccount.setVisible(true);
541                 }
542
543         }
544
545         @Override
546         public void serialEvent(SerialState state) {
547                 switch(state)
548                 {
549                 case Connected:
550                         shell.setText(Messages.MainWin_33);
551
552                         animation.setVisible(false);
553                         animation.setPlaying(false);
554
555                         btnConnect.setText(Messages.MainWin_34);
556                         btnConnect.setEnabled(true);
557
558                         //Should we hide?
559                         if( prefs.getBoolean(PREF_AUTOHIDE, false) == true)
560                         {
561                                 hideToTray();
562                         }
563
564                         addAccountsTab();
565
566                         tabFolder.setSelection(1);
567
568                         updateAccountList();
569                         txtFilter.setFocus();
570
571                         //Update icons for systray and window
572                         shell.setImage( iconProgramOnline );
573                         trayIcon.setImage( iconSystrayOnline );
574
575                         int numAccounts=FkManager.getInstance().getList().size();
576                         if( numAccounts>0 && prefs.getBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, true) )
577                         {
578                                 trayIcon.displayMessage("FinalKey", numAccounts +" "+ (( numAccounts >1)?Messages.MainWin_1:Messages.MainWin_2), //$NON-NLS-1$ //$NON-NLS-2$
579                                                 TrayIcon.MessageType.INFO);
580                         }
581
582                         log(Messages.MainWin_40);
583                         break;
584                 case Connecting:
585                         shell.setText(Messages.MainWin_41);
586                         animation.setVisible(true);
587                         animation.setPlaying(true);
588
589                         txtPsw.setEnabled(false);
590                         txtDev.setEnabled(false);
591                         btnConnect.setEnabled(false);
592
593                         lblPort.setEnabled(false);
594                         lblPassword.setEnabled(false);
595                         break;
596                 case Disconnected:
597                         fkSerial=null;
598                         animation.setVisible(false);
599                         animation.setPlaying(false);
600
601                         remAccountsTab();
602
603                         tabFolder.setSelection(0);
604
605                         shell.setText(Messages.MainWin_42);
606                         btnConnect.setText(Messages.MainWin_43);
607
608                         btnConnect.setEnabled(true);
609
610                         lblPort.setEnabled(true);
611                         lblPassword.setEnabled(true);
612                         txtPsw.setEnabled(true);
613                         txtDev.setEnabled(true);
614
615                         cmpConnect.update();
616
617                         clearSystray();
618
619                         //Update icons for systray and window
620                         shell.setImage( iconProgramOffline );
621                         trayIcon.setImage( iconSystrayOffline );
622
623                         if(lastState != state)
624                         {
625                                 log(Messages.MainWin_44);
626                         }
627                         break;
628
629                 case Working:
630                         animation.setPlaying(false);
631                         animation.setVisible(false);
632                         break;
633
634                 default:
635                         break;
636                 }
637                 lastState=state;
638                 cmpConnect.layout();
639         }
640
641         private void remAccountsTab() {
642                 if( tabFolder.getItemCount() > 1 )
643                 {
644                         tabFolder.getItem(1).dispose();
645                 }
646
647         }
648
649         void updateFilteredList(Vector<Account> source)
650         {
651                 lstAccounts.getList().removeAll();
652
653                 Boolean showAccountId = prefs.getBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false);
654
655                 for( FkManager.Account a : source )
656                 {
657                         lstAccounts.add( a.showNumInName( showAccountId ) );
658                 }
659         }
660
661         private void addAccountsTab() {
662
663                 tbtmAccounts = new TabItem(tabFolder, SWT.NONE, 1);//TabItem(tabFolder, SWT.NONE);
664                 tbtmAccounts.setText(Messages.MainWin_45);
665
666                 cmpAccounts = new Composite(tabFolder, SWT.BORDER);
667                 tbtmAccounts.setControl(cmpAccounts);
668                 cmpAccounts.setLayout(new FormLayout());
669
670                 btnNewAccount = new Button(cmpAccounts, SWT.NONE);
671                 btnNewAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/new.png")); //$NON-NLS-1$
672                 btnNewAccount.addSelectionListener(new SelectionAdapter() {
673                         @Override
674                         public void widgetSelected(SelectionEvent e) {
675                                 NewAccountDialog dialog = new NewAccountDialog(shell, shell.getStyle() );
676                                 shell.setEnabled(false);
677                                 dialog.open();
678                                 shell.setEnabled(true);
679                                 updateAccountList();
680                         }
681                 });
682                 FormData fd_btnNewAccoount = new FormData();
683                 fd_btnNewAccoount.right = new FormAttachment(100, -10);
684                 fd_btnNewAccoount.top = new FormAttachment(100, -40);
685                 fd_btnNewAccoount.bottom = new FormAttachment(100, -6);
686                 fd_btnNewAccoount.left = new FormAttachment(100, -200);
687
688                 btnNewAccount.setLayoutData(fd_btnNewAccoount);
689                 btnNewAccount.setText(Messages.MainWin_47);
690
691                 lstAccounts = new ListViewer(cmpAccounts, SWT.BORDER | SWT.V_SCROLL);
692                 lstAccountsControl = lstAccounts.getList();
693                 lstAccountsControl.setLayoutData(new FormData());
694
695                 lblNumFree = new Label(cmpAccounts, SWT.NONE);
696                 //lblNumFree.setText("Hello World!"); //$NON-NLS-1$
697                 FormData fd_lblNumFree = new FormData();
698                 fd_lblNumFree.left = new FormAttachment(lstAccountsControl,0,SWT.LEFT);
699                 fd_lblNumFree.bottom = new FormAttachment(btnNewAccount, 0, SWT.CENTER);
700                 lblNumFree.setLayoutData(fd_lblNumFree);
701
702                 btnOpenAccount = new Button(cmpAccounts, SWT.NONE);
703                 //fd_lblNumFree.right = new FormAttachment(btnOpenAccount, -224);
704                 btnOpenAccount.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/lightbulb.png")); //$NON-NLS-1$
705                 FormData fd_btnOpenAccount = new FormData();
706                 fd_btnOpenAccount.top = new FormAttachment(btnNewAccount, 0, SWT.TOP);
707                 fd_btnOpenAccount.bottom = new FormAttachment(btnNewAccount, 0, SWT.BOTTOM);
708                 fd_btnOpenAccount.right = new FormAttachment(btnNewAccount, -8, SWT.LEFT );
709                 btnOpenAccount.setLayoutData(fd_btnOpenAccount);
710                 btnOpenAccount.setText(Messages.MainWin_5);
711                 btnOpenAccount.getShell().layout();
712                 btnOpenAccount.setEnabled(false);
713
714
715                 FormData fd_lstAccounts = new FormData();
716                 fd_lstAccounts.top = new FormAttachment(0, 8);
717                 fd_lstAccounts.bottom = new FormAttachment(btnNewAccount, -6);
718
719                 txtFilter = new Text(cmpAccounts, SWT.BORDER);
720                 txtFilter.setText(Messages.MainWin_10);
721                 FormData fd_txtFilter = new FormData();
722                 fd_txtFilter.left = new FormAttachment(lblNumFree, 8, SWT.RIGHT);
723                 fd_txtFilter.bottom = new FormAttachment(btnOpenAccount,0, SWT.CENTER);
724                 fd_txtFilter.right = new FormAttachment(btnOpenAccount, -8, SWT.LEFT);
725                 txtFilter.setLayoutData(fd_txtFilter);
726                 txtFilter.setForeground( grayColor );
727                 txtFilter.addFocusListener( new FocusListener() {
728                         @Override
729                         public void focusLost(FocusEvent arg0) {
730                                 txtFilter.setText(Messages.MainWin_10);
731                                 txtFilter.setForeground( grayColor );
732                                 txtFilter.setBackground( defaultBgColor );
733                         }
734
735                         @Override
736                         public void focusGained(FocusEvent arg0) {
737                                 updateFilteredList( FkManager.getInstance().getList() );
738                                 txtFilter.setForeground( blackColor );
739                                 txtFilter.setText(""); //$NON-NLS-1$
740                                 btnOpenAccount.setEnabled(false);
741                         }
742                 });
743
744                 txtFilter.addKeyListener( new KeyListener() {
745
746                         @Override
747                         public void keyReleased(KeyEvent arg0) {
748                                 if( arg0.keyCode == SWT.ESC )
749                                 {
750                                         txtFilter.setText("");
751                                 }
752
753                                 txtFilterKeyPressEvent(arg0);
754                         }
755
756                         @Override
757                         public void keyPressed(KeyEvent arg0) {
758
759                         }
760                 });
761
762                 fd_lstAccounts.left = new FormAttachment(0, 10);
763                 fd_lstAccounts.right = new FormAttachment(100, -10);
764
765                 lstAccounts.getControl().setLayoutData(fd_lstAccounts);
766                 lstAccounts.addDoubleClickListener( new IDoubleClickListener() {
767
768                         @Override
769                         public void doubleClick(DoubleClickEvent arg0) {
770                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
771                                 if( !selection.isEmpty() )
772                                 {
773                                         Account acc = (Account)selection.getFirstElement();
774                                         showTrigDialog(acc);
775                                 }
776                         }
777
778
779                 });
780
781                 lstAccounts.addSelectionChangedListener( new ISelectionChangedListener() {
782
783                         @Override
784                         public void selectionChanged(SelectionChangedEvent arg0) {
785                                 StructuredSelection selection = (StructuredSelection) arg0.getSelection();
786                                 if( !selection.isEmpty() )
787                                 {
788                                         btnOpenAccount.setEnabled(true);
789
790                                         //Remove any listeners
791                                         for( Listener s :btnOpenAccount.getListeners(SWT.Selection) )
792                                         {
793                                                 btnOpenAccount.removeListener(SWT.Selection, s);
794                                         }
795
796                                         btnOpenAccount.addSelectionListener( new SelectionListener() {
797
798                                                 @Override
799                                                 public void widgetSelected(SelectionEvent arg0) {
800                                                         StructuredSelection selection = (StructuredSelection) lstAccounts.getSelection();
801                                                         Account acc = (Account)selection.getFirstElement();
802                                                         showTrigDialog(acc);
803                                                 }
804
805                                                 @Override
806                                                 public void widgetDefaultSelected(SelectionEvent arg0) {
807                                                         // TODO Auto-generated method stub
808                                                 }
809                                         });
810                                 }
811                         }
812                 });
813
814                 lstAccountsControl.addKeyListener( new KeyListener() {
815                         @Override
816                         public void keyReleased(KeyEvent arg0) {
817                                 if( arg0.keyCode > 31 && arg0.keyCode < 126 )
818                                 {
819                                 }
820                         }
821
822                         @Override
823                         public void keyPressed(KeyEvent arg0) {
824                                 if( arg0.keyCode > 31 && arg0.keyCode < 126 )
825                                 {
826                                         txtFilter.setFocus();
827                                         txtFilter.setText( ""+(char)arg0.keyCode );
828                                         txtFilterKeyPressEvent(arg0);
829                                         txtFilter.setSelection(1);
830                                 }
831                         }
832                 });
833                 
834                 lstAccountsControl.addFocusListener( new FocusListener() {
835                         
836                         @Override
837                         public void focusLost(FocusEvent arg0) {
838                                 System.out.println("lost");
839                                 
840                         }
841                         
842                         @Override
843                         public void focusGained(FocusEvent arg0) {
844                                 if( lstAccountsControl.getItemCount() == 0 )
845                                 {
846                                         updateFilteredList(FkManager.getInstance().getList());
847                                 }
848                                 
849                         }
850                 });
851
852                 cmpAccounts.setTabList(new Control[]{txtFilter,lstAccountsControl, btnOpenAccount, btnNewAccount});
853         }
854
855         protected void txtFilterKeyPressEvent(KeyEvent arg0) {
856
857                 if( txtFilter.getText().length() == 0 )
858                 {
859                         updateFilteredList( FkManager.getInstance().getList() );
860                         txtFilter.setBackground( defaultBgColor );
861                 } else {
862                         if( arg0.keyCode > 31 && arg0.keyCode < 126 )
863                         {
864                                 Vector<Account> res = FkManager.getInstance().getList(txtFilter.getText());
865                                 if( res.size() < 1 )
866                                 {
867                                         txtFilter.setBackground( redColor );
868                                 } else if( res.size() == 1 )
869                                 {
870                                         txtFilter.setBackground( greenColor );
871                                         if( arg0.character== SWT.CR)
872                                         {
873                                                 showTrigDialog(res.get(0));
874                                         }
875                                 } else {
876                                         txtFilter.setBackground( defaultBgColor );
877                                 }
878                                 updateFilteredList( res );
879                         }
880                 }
881
882                 if(arg0.keyCode == SWT.ARROW_UP || arg0.keyCode == SWT.ARROW_DOWN )
883                 {
884                         lstAccountsControl.forceFocus();
885                 }
886
887         }
888
889         private void addSettingsTab()
890         {
891                 tbtmSettings = new TabItem(tabFolder, SWT.NONE);
892                 tbtmSettings.setText(Messages.MainWin_tbtmSettings_text);
893
894                 cmpSettings = new Composite(tabFolder, SWT.BORDER);
895                 tbtmSettings.setControl(cmpSettings);
896                 cmpSettings.setLayout(new FormLayout());
897
898                 chkAutoHide = new Button(cmpSettings, SWT.CHECK);
899                 chkAutoHide.setText(Messages.MainWin_6);
900                 chkAutoHide.setSelection( prefs.getBoolean( PREF_AUTOHIDE, false ) );
901                 FormData fd_chkAutoHide = new FormData();
902                 fd_chkAutoHide.top = new FormAttachment(0, 10);
903                 fd_chkAutoHide.left = new FormAttachment(0, 10);
904                 chkAutoHide.setLayoutData(fd_chkAutoHide);
905                 chkAutoHide.addSelectionListener( new SelectionAdapter() {
906                         public void widgetSelected(SelectionEvent e) {
907                                 prefs.putBoolean(PREF_AUTOHIDE, chkAutoHide.getSelection() );
908                         }
909                 });
910
911                 chkSortByAccountId = new Button(cmpSettings, SWT.CHECK);
912                 chkSortByAccountId.setText(Messages.MainWin_9);
913                 chkSortByAccountId.setSelection( prefs.getBoolean( PREF_SORT_BY_ID_KEY, false) );
914                 FormData fd_btnSortByAccountId = new FormData();
915                 fd_btnSortByAccountId.top = new FormAttachment(chkAutoHide, 6);
916                 fd_btnSortByAccountId.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
917                 chkSortByAccountId.setLayoutData(fd_btnSortByAccountId);
918                 chkSortByAccountId.addSelectionListener( new SelectionAdapter() {
919                         public void widgetSelected(SelectionEvent e) {
920                                 prefs.putBoolean( PREF_SORT_BY_ID_KEY, chkSortByAccountId.getSelection() );
921                                 FkManager.getInstance().sortById( chkSortByAccountId.getSelection() );
922                                 updateAccountList();
923                         }
924                 });
925
926                 chkShowAccountId = new Button(cmpSettings, SWT.CHECK);
927                 FormData fd_chkShowAccountId = new FormData();
928                 fd_chkShowAccountId.top = new FormAttachment(chkSortByAccountId, 6);
929                 fd_chkShowAccountId.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
930                 chkShowAccountId.setLayoutData(fd_chkShowAccountId);
931                 chkShowAccountId.setText(Messages.MainWin_btnShowAccountId_text);
932                 chkShowAccountId.setSelection( prefs.getBoolean(PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, false) );
933                 chkShowAccountId.addSelectionListener( new SelectionAdapter() {
934                         public void widgetSelected(SelectionEvent e) {
935                                 prefs.putBoolean( PREF_SHOW_ACCOUNT_ID_IN_NAME_KEY, chkShowAccountId.getSelection() );
936                                 updateAccountList();
937                         }
938                 });
939
940                 chkCheckForUpdates = new Button(cmpSettings, SWT.CHECK);
941                 FormData fd_chkCheckForUpdates = new FormData();
942                 fd_chkCheckForUpdates.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
943                 chkCheckForUpdates.setLayoutData(fd_chkCheckForUpdates);
944                 chkCheckForUpdates.setText(Messages.MainWin_btnCheckForUpdates_text);
945                 chkCheckForUpdates.setSelection( prefs.getBoolean( PREF_ALLOW_UPDATE_CHECK, true) );
946                 chkCheckForUpdates.addSelectionListener( new SelectionAdapter() {
947                         public void widgetSelected(SelectionEvent e) {
948                                 prefs.putBoolean( PREF_ALLOW_UPDATE_CHECK, chkCheckForUpdates.getSelection() );
949                                 checkForUpdates();
950                         }
951                 });
952                 fd_chkCheckForUpdates.top = new FormAttachment(chkShowAccountId, 6);
953
954                 btnShowaccountsReady = new Button(cmpSettings, SWT.CHECK);
955                 btnShowaccountsReady.setSelection( prefs.getBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, true) );
956                 FormData fd_btnShowaccountsReady = new FormData();
957                 fd_btnShowaccountsReady.top = new FormAttachment(chkCheckForUpdates, 6);
958                 fd_btnShowaccountsReady.left = new FormAttachment(chkAutoHide, 0, SWT.LEFT);
959                 btnShowaccountsReady.setLayoutData(fd_btnShowaccountsReady);
960
961                 btnShowaccountsReady.setText(Messages.MainWin_btnShowaccountsReady_text);
962                 btnShowaccountsReady.addSelectionListener( new SelectionAdapter() {
963                 public void widgetSelected(SelectionEvent e) {
964                                 prefs.putBoolean( PREF_SHOW_ACCOUNTS_READY_NOTICE, btnShowaccountsReady.getSelection() );
965                                 checkForUpdates();
966                         }
967                 });
968
969         }
970
971         private void showTrigDialog(Account acc) {
972                 Account selectedAccount = acc;
973                 TriggerDialog diag = new TriggerDialog(shell, shell.getStyle(), acc,
974                                 mySelf);
975
976                 shell.setEnabled(false);
977                 diag.open();
978
979                 // Check if the account still exists
980                 Boolean deleted = true;
981                 for (Account a : FkManager.getInstance().getList()) {
982                         if (a.equals(selectedAccount)) {
983                                 deleted = false;
984                                 break;
985                         }
986                 }
987
988                 if (deleted) {
989                         btnOpenAccount.setEnabled(false);
990                 }
991
992                 txtFilter.setFocus();
993                 shell.setEnabled(true);
994         }
995
996         @Override
997         public void updateCheckFinished(UpdateResultEvent event) {
998                 updateCheckThread=null;
999                 switch(event.result)
1000                 {
1001                 case CHECK_FAILED:
1002                         System.out.println("Trouble checking for updates."); //$NON-NLS-1$
1003                         break;
1004                 case NO_UPDATE:
1005                         System.out.println("No update avaiable at this time."); //$NON-NLS-1$
1006                         break;
1007                 case UPDATE_AVAILABLE:
1008                         MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
1009                         dialog.setText(Messages.MainWin_52+event.version+Messages.MainWin_53);
1010                         dialog.setMessage(Messages.MainWin_54+event.message);
1011                         dialog.open();  
1012                         break;
1013                 }
1014
1015         }
1016 }