More gui work, more testing to do.
[FinalKeyGui.git] / src / fkgui / FkManager.java
1 package fkgui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collection;
8 import java.util.Comparator;
9 import java.util.Vector;
10 import java.util.concurrent.ExecutionException;
11
12 import javax.swing.JFrame;
13 import javax.swing.JTextField;
14
15 import org.eclipse.jface.viewers.IContentProvider;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.swt.widgets.Display;
18
19 import jssc.SerialPortException;
20 import fkgui.FkActionEventListener.FkActionEvent;
21 import fkgui.FkActionEventListener.FkActionEventType;
22 import fkgui.FkManager.Account;
23 import fkgui.SerialWorker.SerialState;
24
25 public class FkManager implements ActionListener {
26         private static FkManager instance = null;
27         private SerialWorker com = null;
28
29         private Comparator<Account> sortMethod = null;
30         
31         public class Account
32         {
33                 public String name;
34                 public String num;
35                 
36                 public Account( String acNum, String acName )
37                 {
38                         name = acName;
39                         num = acNum;
40                 }
41                 
42                 public String toString()
43                 {
44                         return(name);
45                 }
46         }
47                 
48         private Vector<Account> list;
49         
50         protected FkManager()
51         {
52                 list = new Vector<Account>(256);
53         }
54         
55         public static FkManager getInstance()
56         {
57                 if( instance == null )
58                 {
59                         instance = new FkManager();
60                 }
61
62                 return( instance );
63         }
64         
65         public void setWorker( SerialWorker sw )
66         {
67                 com = sw;
68         }
69         
70
71         private class FkActionEventMsg implements Runnable  {
72                 private FkActionEventListener delegate;
73                 private FkActionEvent event;
74
75                 private FkActionEventMsg(FkActionEventListener d, FkActionEventType t, String data, Account acc, char act )
76                 {
77                         delegate=d;
78                         event=new FkActionEvent(t, data, acc,act) ;
79                 }
80                 @Override
81                 public void run() {
82                         if( delegate != null )
83                         {
84                                 delegate.fkActionEvent(event);
85                         }
86                 }
87         }
88         
89         private class TrigTask implements Runnable
90         {
91                 private Account acc;
92                 private char action;
93                 private FkActionEventListener delegate;
94                 public TrigTask(Account ac, char act, FkActionEventListener d )
95                 {
96                         acc=ac;
97                         action=act;
98                         delegate=d;
99                 }
100                 
101                 @Override
102                 public void run() {
103                         
104                         flushSerial();
105                         
106                         if(action != '%' )
107                         {
108                                 try
109                                 {
110                                         
111                                         if( action == 'd' || action == 'o' )
112                                         {
113                                                 com.serialPort.writeByte((byte)'x');
114                                         }
115                                         com.serialPort.writeByte((byte)action);
116                                 } catch( Exception ex )
117                                 {
118                                         ex.printStackTrace();
119                                 }
120                         }
121                         
122                         try {
123                                 com.serialPort.writeBytes(acc.num.toLowerCase().getBytes());
124                 
125                                 if( action == 'd' || action == 'o' )
126                                 {
127                                         com.serialPort.writeByte((byte)'y');
128                                 }                               
129                                 
130                                 try {
131                                         Thread.sleep(200);
132                                 } catch (InterruptedException e1) {
133                                         // TODO Auto-generated catch block
134                                         e1.printStackTrace();
135                                 }
136                                 
137                                 int t=0;
138
139                                 String msg = "";
140                                 while( t < 1200 )
141                                 {
142                                         t++;
143                                         try {
144                                                 Thread.sleep(50);
145                                         } catch (InterruptedException e) {
146                                                 // TODO Auto-generated catch block
147                                                 e.printStackTrace();
148                                         }
149                                         if( com.serialPort.getInputBufferBytesCount() > 0 )
150                                         {
151                                                 msg += com.serialPort.readString();
152                                                 //System.out.println( msg );
153                                                 if( msg.contains("[done]") || msg.contains("[deleted]") )
154                                                 {
155                                                         if( action == 'd' )
156                                                         {
157                                                                 list.remove( acc );
158                                                         }
159                                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_OKAY, msg, acc, action) );
160                                                         return;
161                                                 } else if( msg.contains("[abort]") )
162                                                 {
163                                                         //System.out.println("FkManager: Action Abort");;
164                                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, msg,acc,action) );
165                                                         return;
166                                                 }
167                                         }
168                                 }
169                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, msg,acc,action) );
170
171                         } catch (Exception e1) {
172                                 // TODO Auto-generated catch block
173                                 System.out.println("TrigTask Exception:");
174                                 e1.printStackTrace();
175                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "EXCEPTION",acc,action) );
176                         }
177                         
178                 }
179                 
180         }
181         
182         public void trig(Account acc, char action, FkActionEventListener delegate)
183         {
184                 TrigTask trigTask = new TrigTask(acc,action,delegate);
185                 new Thread(trigTask).start();
186         }
187         
188         
189         
190         private class SortByName implements Comparator<Account>
191         {
192                 @Override
193                 public int compare(Account o1, Account o2) {
194                         return o1.name.compareTo(o2.name);
195                 }
196         }
197         
198         private class SortById implements Comparator<Account>
199         {
200                 @Override
201                 public int compare(Account o1, Account o2) {
202                         int a = Integer.valueOf(o1.num, 16);
203                         int b = Integer.valueOf(o2.num, 16);
204                         return( a - b );
205                 }
206         }
207         
208         
209         
210         public void listAddAcc(String num, String name)
211         {
212                 list.addElement( new Account(num,name) );
213                 
214                 list.sort( sortMethod );
215         }
216         
217         public void listClear()
218         {
219                 list.clear();
220         }
221         
222         public Vector<Account> getList()
223         {
224                 return(list);
225         }
226         
227         public void getCurrentLayout()
228         {
229                 
230         }
231         
232         public void getAvailableLayouts()
233         {
234                 
235         }
236         
237         public void setLayout(int num)
238         {
239                 
240         }
241
242         @Override
243         public void actionPerformed(ActionEvent e) {
244                 Account f=null;
245                 for( Account a : list )
246                 {
247                         if( a.num.compareTo( e.getActionCommand().substring(1, 3)) == 0 )
248                         {
249                                 f=a;
250                                 break;
251                         }
252                 }
253                 if(f!=null)
254                 {
255                         trig(f, e.getActionCommand().charAt(0), null);
256                 }
257                 
258         }
259
260         public void setCurrentLayout(String str) {
261                 System.out.println("FkManager: Got current layout:" + str );
262                 
263         }
264         
265         private class NewAccountTask implements Runnable
266         {
267                 private String seq;
268                 private Account acc;
269                 FkActionEventListener delegate;
270                 public NewAccountTask(String sequence, FkActionEventListener d, String accountName)
271                 {
272                         seq=sequence;
273                         delegate=d;
274                         acc = new Account( "00", accountName );
275                         System.out.println("NewAccountTask ctor");
276                 }
277                 @Override
278                 public void run() {
279                         System.out.println("NewAccountTask run");
280
281                         flushSerial();
282                         String data="";
283                         int timeOut;
284                         try {                   
285                         //First type x a and wait for account title
286                                 
287                                 com.serialPort.writeByte((byte)'x');
288                                 com.serialPort.writeByte((byte)'a');
289                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_WAITING, "WAITING",acc,'A') );
290
291                                 timeOut = 6000;
292                                 while(timeOut > 0)
293                                 {
294                                         if( com.serialPort.getInputBufferBytesCount() > 0 )
295                                         {
296                                                 String in = com.serialPort.readString(); 
297                                                 data += in;
298                                                 System.out.println("Datain:" +in);
299                                                 if( data.contains("Account Title, (0-31):") )
300                                                 {
301                                                         System.out.println("Found");
302                                                         break;
303                                                 }
304                                         } else {
305                                                 timeOut -= 50;
306                                                 Thread.sleep(50);
307                                         }
308                                 }
309                                 
310                                 if(timeOut > 0 )
311                                 {
312                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_WORKING, "WORKING",acc,'A') );
313
314                                         for(int p=0; p < seq.length();p++)
315                                         {
316                                                 
317                                                 if( com.serialPort.getOutputBufferBytesCount() > 1 )
318                                                 {
319                                                         Thread.sleep(50);
320                                                 } else {
321                                                         com.serialPort.writeByte( (byte)seq.charAt(p) );
322                                                         Thread.sleep(5);
323                                                         
324                                                 }
325                                                 System.out.println( "Type:" + seq.charAt(p));
326                                                 while( com.serialPort.getInputBufferBytesCount() > 0 )
327                                                 {
328                                                         String in = com.serialPort.readString();
329                                                         data += in;
330                                                         System.out.println("Read:" + in);
331                                                 }
332                                         }
333                                         System.out.println("All chars typed, waiting for [done]");
334                                         
335                                         timeOut = 30000;
336                                         while( timeOut > 0 )
337                                         {
338                                                 timeOut -= 50;
339                                                 Thread.sleep(50);
340                                                 if( com.serialPort.getInputBufferBytesCount() > 0 )
341                                                 {
342                                                         String in = com.serialPort.readString();
343                                                         System.out.println("Read>"+in);
344                                                         data += in;
345                                                         if( data.contains("[done]") )
346                                                         {
347                                                                 int begin = data.lastIndexOf("[save entry ")+12;
348                                                                 String subStr = data.substring(begin);
349                                                                 int end = subStr.indexOf("]");
350                                                                 subStr = subStr.substring(0,end);
351                                                                 if( subStr.length()==1)
352                                                                 {
353                                                                         subStr = "0"+subStr;
354                                                                 }
355                                                                 acc.num = subStr;
356                                                                 listAddAcc( acc.num, acc.name);
357                                                                 System.out.println("Account: "+acc.num+" " + acc.name);
358                                                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_OKAY, "DONE:"+data,acc,'A') );
359                                                                 break;
360                                                         } else if( data.contains("[abort]") )
361                                                         {
362                                                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "ERROR:"+data,null,'A') );
363                                                                 break;
364                                                         }
365                                                 }
366                                         }
367                                         if(timeOut < 1)
368                                         {
369                                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "TIMEOUT1:"+data,null,'A') );
370                                         }
371                                 } else {
372                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "TIMEOUT2:"+data,null,'A') );
373                                 }
374                                 
375                                 
376                         } catch (Exception e) {
377                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "EXCEPTION",null,'A') );
378                         }
379                         
380                 }
381
382                 
383         }
384
385         private void flushSerial() {
386                 try {
387                 Thread.sleep(10);
388                 
389                 while( com.serialPort.getInputBufferBytesCount()!=0 )
390                 {
391                         System.out.println("Flushed "+com.serialPort.getInputBufferBytesCount()+" bytes >>>" + com.serialPort.readString() + "<<<");
392                 }
393                 } catch(Exception e)
394                 {
395                         
396                 }
397                 
398         }       
399         
400         public void createAccount(String strAccountName, String strUserName,
401                         Boolean autoPassword, int autoPassLen, Boolean autoPassAllSpecials,
402                         String autoPassSpecials, String strPassword, Boolean seperatorTab,
403                         FkActionEventListener delegate) {
404                         
405                         String seq = new String();
406                         
407                         seq += strAccountName;
408                         seq += (char)13;
409                         seq     += strUserName;
410                         seq += (char)13;
411                         
412                         if( autoPassword )
413                         {
414                                 seq += '2';
415                                 seq += autoPassLen;
416                                 seq += (char)13;
417                                 if( autoPassAllSpecials )
418                                 {
419                                         seq += '1';
420                                 } else {
421                                         if( autoPassSpecials.length() > 0 )
422                                         {
423                                                 seq += '2';
424                                                 seq += autoPassSpecials;
425                                                 seq += (char)13;
426                                         } else {
427                                                 seq += '3';
428                                         }
429                                 }
430                         } else {
431                                 //Manual entered password
432                                 seq += '1';
433                                 seq += strPassword;
434                                 seq += (char)13;
435                         }
436                         
437                         //Tab/Enter sep
438                         if( seperatorTab )
439                         {
440                                 seq += '1';
441                         } else {
442                                 seq += '2';
443                         }
444                         
445                         //System.out.println("Seq ["+seq+"]");
446                         
447                         NewAccountTask newTask = new NewAccountTask(seq, delegate, strAccountName);
448                         new Thread(newTask).start();                    
449                 
450         }
451
452         public void sortById(boolean byId) {
453                 if( byId )
454                 {
455                         sortMethod = new SortById();
456                 } else {
457                         sortMethod = new SortByName();
458                 }
459                 if(list != null )
460                 {
461                         list.sort(sortMethod);
462                 }
463         }
464
465
466 }