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