Snapshot before changing swt list to comboviewer
[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.Vector;
6
7 import javax.swing.JFrame;
8 import javax.swing.JTextField;
9
10 import org.eclipse.swt.widgets.Display;
11
12 import jssc.SerialPortException;
13 import fkgui.FkActionEventListener.FkActionEvent;
14 import fkgui.FkActionEventListener.FkActionEventType;
15 import fkgui.FkManager.Account;
16 import fkgui.SerialWorker.SerialState;
17
18 public class FkManager implements ActionListener {
19         private static FkManager instance = null;
20         private SerialWorker com = null;
21
22         public class Account
23         {
24                 public String name;
25                 public String num;
26                 
27                 public Account( String acNum, String acName )
28                 {
29                         name = acName;
30                         num = acNum;
31                 }
32         }
33                 
34         private Vector<Account> list;
35         
36         protected FkManager()
37         {
38                 list = new Vector<Account>(256);
39         }
40         
41         public static FkManager getInstance()
42         {
43                 if( instance == null )
44                 {
45                         instance = new FkManager();
46                 }
47
48                 return( instance );
49         }
50         
51         public void setWorker( SerialWorker sw )
52         {
53                 com = sw;
54         }
55         
56         private class TrigTask implements Runnable
57         {
58                 private Account acc;
59                 private char action;
60                 private FkActionEventListener delegate;
61                 public TrigTask(Account ac, char act, FkActionEventListener d )
62                 {
63                         acc=ac;
64                         action=act;
65                         delegate=d;
66                 }
67
68                 private class FkActionEventMsg implements Runnable  {
69                         private FkActionEventListener delegate;
70                         private FkActionEvent event;
71
72                         private FkActionEventMsg(FkActionEventListener d, FkActionEventType t, String data, Account acc )
73                         {
74                                 delegate=d;
75                                 event=new FkActionEvent(t, data, acc) ;
76                         }
77                         @Override
78                         public void run() {
79                                 if( delegate != null )
80                                 {
81                                         delegate.fkActionEvent(event);
82                                 }
83                         }
84                 }
85                 
86                 @Override
87                 public void run() {
88                         if(action != '%' )
89                         {
90                                 try
91                                 {
92                                         com.serialPort.writeByte((byte)action);
93                                 } catch( Exception ex )
94                                 {
95                                         ex.printStackTrace();
96                                 }
97                         }
98                         
99                         try {
100                                 com.serialPort.writeBytes(acc.num.toLowerCase().getBytes());
101                 
102                                 try {
103                                         Thread.sleep(400);
104                                 } catch (InterruptedException e1) {
105                                         // TODO Auto-generated catch block
106                                         e1.printStackTrace();
107                                 }
108                                 
109                                 int t=0;
110
111                                 String msg = "";
112                                 while( t < 1200 )
113                                 {
114                                         t++;
115                                         try {
116                                                 Thread.sleep(50);
117                                         } catch (InterruptedException e) {
118                                                 // TODO Auto-generated catch block
119                                                 e.printStackTrace();
120                                         }
121                                         if( com.serialPort.getInputBufferBytesCount() > 0 )
122                                         {
123                                                 msg += com.serialPort.readString();
124                                                 //System.out.println( msg );
125                                                 if( msg.contains("[done]") )
126                                                 {
127                                                         //System.out.println("FkManager: Action OK");;
128                                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_OKAY, msg, acc) );
129                                                         return;
130                                                 } else if( msg.contains("[abort]") )
131                                                 {
132                                                         //System.out.println("FkManager: Action Abort");;
133                                                         Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, msg,acc) );
134                                                         
135                                                         return;
136                                                 }
137                                         }
138                                 }
139                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, msg,acc) );
140
141                         } catch (Exception e1) {
142                                 // TODO Auto-generated catch block
143                                 System.out.println("TrigTask Exception:");
144                                 e1.printStackTrace();
145                                 Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "EXCEPTION",acc) );
146                         }
147                         
148                 }
149                 
150         }
151         
152         public void trig(Account acc, char action, FkActionEventListener delegate)
153         {
154                 TrigTask trigTask = new TrigTask(acc,action,delegate);
155                 new Thread(trigTask).start();
156         }
157         
158         
159         public void listAddAcc(String num, String name)
160         {
161                 list.addElement( new Account(num,name) );
162         }
163         
164         public void listClear()
165         {
166                 list.clear();
167         }
168         
169         public Vector<Account> getList()
170         {
171                 return(list);
172         }
173         
174         public void getCurrentLayout()
175         {
176                 
177         }
178         
179         public void getAvailableLayouts()
180         {
181                 
182         }
183         
184         public void setLayout(int num)
185         {
186                 
187         }
188
189         @Override
190         public void actionPerformed(ActionEvent e) {
191                 Account f=null;
192                 for( Account a : list )
193                 {
194                         if( a.num.compareTo( e.getActionCommand().substring(1, 3)) == 0 )
195                         {
196                                 f=a;
197                                 break;
198                         }
199                 }
200                 if(f!=null)
201                 {
202                         trig(f, e.getActionCommand().charAt(0), null);
203                 }
204                 
205         }
206
207         public void setCurrentLayout(String str) {
208                 System.out.println("FkManager: Got current layout:" + str );
209                 
210         }
211         
212         
213 }