8f47bf02098ea8f83329df3ac2e32314d3cb953c
[FinalKeyGui.git] / src / fkgui / TriggerDialog.java
1 package fkgui;
2
3 import org.eclipse.swt.graphics.Point;
4 import org.eclipse.swt.widgets.Dialog;
5 import org.eclipse.swt.widgets.Display;
6 import org.eclipse.swt.widgets.MessageBox;
7 import org.eclipse.swt.widgets.Shell;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.layout.FormLayout;
12 import org.eclipse.swt.layout.FormData;
13 import org.eclipse.swt.layout.FormAttachment;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Group;
16 import org.eclipse.wb.swt.SWTResourceManager;
17
18 import fkgui.FkManager.Account;
19
20 public class TriggerDialog extends Dialog implements FkActionEventListener {
21
22         protected Object result;
23         protected Shell shell;
24         private Account account;
25         public TriggerDialog mySelf;
26         private FormData fd_grpChange;
27         private Group grpMakeFinalKey;
28         private Group grpChange;
29         ConsoleMsg delegate;
30         PermitCountDownDialog permitCountdownDialog = null;
31         /**
32          * Create the dialog.
33          * @param parent
34          * @param style
35          */
36         public TriggerDialog(Shell parent, int style, Account a, ConsoleMsg d) {
37                 super(parent, style);
38                 account = a;
39                 delegate = d;
40                 setText(Messages.TriggerDialog_0+ account.name);
41         }
42
43         /**
44          * Open the dialog.
45          * @return the result
46          */
47         public Object open() {
48                 result = (Object)new Boolean(false);
49                 createContents();
50
51                 Point p = getParent().getSize();
52                 p.x /= 2;
53                 p.y /= 2;
54                 p.x -= shell.getSize().x/2;
55                 p.y -= shell.getSize().y/2;
56                 p.x += getParent().getLocation().x;
57                 p.y += getParent().getLocation().y;
58                 shell.setLocation( p );
59
60                 shell.open();
61                 shell.layout();
62                 Display display = getParent().getDisplay();
63                 
64                 
65                 while (!shell.isDisposed()) {
66                         if (!display.readAndDispatch()) {
67                                 display.sleep();
68                         }
69                 }
70                 System.out.println(Messages.TriggerDialog_1 + (Boolean)result);
71                 return result;
72         }
73
74         /**
75          * Create contents of the dialog.
76          */
77         private void createContents() {
78                 shell = new Shell(getParent(), SWT.BORDER | SWT.CLOSE);
79                 shell.setSize(677, 250);
80                 shell.setText(getText());
81                 shell.setLayout(new FormLayout());
82                 mySelf = this;
83                 grpMakeFinalKey = new Group(shell, SWT.NONE);
84                 grpMakeFinalKey.setLayout(new FormLayout());
85                 FormData fd_grpMakeFinalKey = new FormData();
86                 fd_grpMakeFinalKey.top = new FormAttachment(0, 10);
87                 fd_grpMakeFinalKey.bottom = new FormAttachment(0, 88);
88                 fd_grpMakeFinalKey.left = new FormAttachment(0, 10);
89                 fd_grpMakeFinalKey.right = new FormAttachment(100, -10);
90                 grpMakeFinalKey.setLayoutData(fd_grpMakeFinalKey);
91                 grpMakeFinalKey.setText(Messages.TriggerDialog_2 + account.name);
92                 
93                 Button btnUsernamePassword = new Button(grpMakeFinalKey, SWT.NONE);
94                 btnUsernamePassword.addSelectionListener(new SelectionAdapter() {
95                         @Override
96                         public void widgetSelected(SelectionEvent e) {
97                                 FkManager.getInstance().trig(account, '%', mySelf);
98                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_3, Messages.TriggerDialog_4, 30000);
99                                 shell.setMinimized(true);
100                                 permitCountdownDialog.open();
101                         }
102                 });
103                 btnUsernamePassword.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/both.png")); //$NON-NLS-1$
104                 FormData fd_btnUsernamePassword = new FormData();
105                 fd_btnUsernamePassword.top = new FormAttachment(0, 10);
106                 fd_btnUsernamePassword.left = new FormAttachment(0, 10);
107                 fd_btnUsernamePassword.right = new FormAttachment(0, 224);
108                 btnUsernamePassword.setLayoutData(fd_btnUsernamePassword);
109
110                 btnUsernamePassword.setText(Messages.TriggerDialog_6);
111                 
112                 Button btnUsernameOnly = new Button(grpMakeFinalKey, SWT.NONE);
113                 btnUsernameOnly.addSelectionListener(new SelectionAdapter() {
114                         @Override
115                         public void widgetSelected(SelectionEvent e) {
116                                 FkManager.getInstance().trig(account, 'u', mySelf);
117                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_7, Messages.TriggerDialog_8, 30000);
118                                 shell.setMinimized(true);
119                                 permitCountdownDialog.open();
120                         }
121                 });
122                 btnUsernameOnly.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/user.png")); //$NON-NLS-1$
123                 FormData fd_btnUsernameOnly = new FormData();
124                 fd_btnUsernameOnly.top = new FormAttachment(btnUsernamePassword, -35);
125                 fd_btnUsernameOnly.bottom = new FormAttachment(btnUsernamePassword, 0, SWT.BOTTOM);
126                 fd_btnUsernameOnly.left = new FormAttachment(btnUsernamePassword, 6);
127                 fd_btnUsernameOnly.right = new FormAttachment(0, 405);
128                 btnUsernameOnly.setLayoutData(fd_btnUsernameOnly);
129
130                 btnUsernameOnly.setText(Messages.TriggerDialog_10);
131                 
132                 Button btnPasswordOnly = new Button(grpMakeFinalKey, SWT.NONE);
133                 btnPasswordOnly.addSelectionListener(new SelectionAdapter() {
134                         @Override
135                         public void widgetSelected(SelectionEvent e) {
136                                 FkManager.getInstance().trig(account, 'p', mySelf);
137                                 
138                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_11, Messages.TriggerDialog_12, 30000);
139
140                                 shell.setMinimized(true);
141                                 permitCountdownDialog.open();
142                         }
143                 });
144                 btnPasswordOnly.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/key-icon.png")); //$NON-NLS-1$
145                 FormData fd_btnPasswordOnly = new FormData();
146                 fd_btnPasswordOnly.left = new FormAttachment(btnUsernameOnly, 29);
147                 fd_btnPasswordOnly.top = new FormAttachment(btnUsernamePassword, 0, SWT.TOP);
148                 fd_btnPasswordOnly.right = new FormAttachment(100, -9);
149                 fd_btnPasswordOnly.bottom = new FormAttachment(0, 45);
150                 btnPasswordOnly.setLayoutData(fd_btnPasswordOnly);
151
152                 btnPasswordOnly.setText(Messages.TriggerDialog_14);
153                                 
154                                 grpChange = new Group(shell, SWT.NONE);
155                                 grpChange.setText(Messages.TriggerDialog_15+account.name);
156                                 grpChange.setLayout(new FormLayout());
157                                 fd_grpChange = new FormData();
158                                 fd_grpChange.left = new FormAttachment(grpMakeFinalKey, 353, SWT.LEFT);
159                                 fd_grpChange.right = new FormAttachment(grpMakeFinalKey, 0, SWT.RIGHT);
160                                 fd_grpChange.top = new FormAttachment(grpMakeFinalKey, 6);
161                                 grpChange.setLayoutData(fd_grpChange);
162                                 
163                                 Button btnEdit = new Button(grpChange, SWT.NONE);
164                                 btnEdit.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/gtk_edit.png")); //$NON-NLS-1$
165                                 FormData fd_btnEdit = new FormData();
166                                 fd_btnEdit.top = new FormAttachment(0, 10);
167                                 fd_btnEdit.left = new FormAttachment(0, 10);
168                                 btnEdit.setLayoutData(fd_btnEdit);
169                                 btnEdit.setText(Messages.TriggerDialog_17);
170                                 ///TODO: Inplement edit box..
171                                 btnEdit.setVisible(false);
172                                 
173                                 Button btnDelete = new Button(grpChange, SWT.NONE);
174                                 btnDelete.addSelectionListener(new SelectionAdapter() {
175                                         @Override
176                                         public void widgetSelected(SelectionEvent e) {
177                                                 MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO );
178                                                 dialog.setText(Messages.TriggerDialog_18+account.name );
179                                                 dialog.setMessage(Messages.TriggerDialog_19+account.name+Messages.TriggerDialog_20+account.num+Messages.TriggerDialog_21);
180                                                 if( dialog.open() == SWT.YES )
181                                                 {
182                                                         permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_22, Messages.TriggerDialog_23+account.name+Messages.TriggerDialog_24, 5000);
183                                                         shell.setMinimized(true);
184                                                         FkManager.getInstance().trig(account, 'd', mySelf);
185                                                         permitCountdownDialog.open();
186                                                         
187
188                                                 }
189                                         }
190                                 });
191                                 fd_btnEdit.right = new FormAttachment(btnDelete, -6);
192                                 btnDelete.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/trashdelete.gif")); //$NON-NLS-1$
193                                 FormData fd_btnDelete = new FormData();
194                                 fd_btnDelete.top = new FormAttachment(0, 10);
195                                 fd_btnDelete.left = new FormAttachment(0, 166);
196                                 fd_btnDelete.right = new FormAttachment(100, -11);
197                                 btnDelete.setLayoutData(fd_btnDelete);
198                                 btnDelete.setText(Messages.TriggerDialog_26);
199
200                                 Button btnCancel = new Button(shell, SWT.NONE);
201                                 fd_grpChange.bottom = new FormAttachment(btnCancel, -6);
202                                 btnCancel.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/Delete.png")); //$NON-NLS-1$
203                                 FormData fd_btnCancel = new FormData();
204                                 fd_btnCancel.left = new FormAttachment(grpMakeFinalKey, 0, SWT.LEFT);
205                                 fd_btnCancel.bottom = new FormAttachment(100, -10);
206                                 fd_btnCancel.right = new FormAttachment(grpMakeFinalKey, 2, SWT.RIGHT);
207                                 btnCancel.setLayoutData(fd_btnCancel);
208                                 
209                                 btnCancel.setText(Messages.TriggerDialog_28);
210                                 
211                                 btnCancel.addSelectionListener( new SelectionAdapter() {
212                                         public void widgetSelected(SelectionEvent e) {
213                                                 shell.close();
214                                         }
215                                 });                             
216                                 Group grpShow = new Group(shell, SWT.NONE);
217                                 grpShow.setText(Messages.TriggerDialog_29+account.name);
218                                 FormData fd_grpShow = new FormData();
219                                 fd_grpShow.bottom = new FormAttachment(grpChange, 0, SWT.BOTTOM);
220                                 fd_grpShow.right = new FormAttachment(grpChange, -6);
221                                 fd_grpShow.left = new FormAttachment(grpMakeFinalKey, 0, SWT.LEFT);
222                                 fd_grpShow.top = new FormAttachment(grpMakeFinalKey, 6);
223                                 grpShow.setLayoutData(fd_grpShow);
224                                 
225                                 Button btnShowUsername = new Button(grpShow, SWT.NONE);
226                                 btnShowUsername.addSelectionListener(new SelectionAdapter() {
227                                         @Override
228                                         public void widgetSelected(SelectionEvent e) {
229                                                 FkManager.getInstance().trig(account, 's', mySelf);
230                                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_30, Messages.TriggerDialog_31, 30000);
231                                                 shell.setMinimized(true);
232                                                 permitCountdownDialog.open();
233                                         }
234                                 });
235                                 btnShowUsername.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/both.png")); //$NON-NLS-1$
236                                 btnShowUsername.setBounds(10, 36, 327, 35);
237                                 btnShowUsername.setText(Messages.TriggerDialog_33);                             
238                                 
239
240         }
241
242         @SuppressWarnings("incomplete-switch")
243         @Override
244         public void fkActionEvent(FkActionEvent event) {
245                 MessageBox dialog;
246                 
247                 if( permitCountdownDialog != null && !permitCountdownDialog.shell.isDisposed() )
248                 {
249                         permitCountdownDialog.shell.close();
250                 }
251                 
252                 switch(event.type)
253                 {
254                 case ACTION_ABORTED:
255                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
256                         dialog.setText(event.acc.name);
257                         dialog.setMessage(Messages.TriggerDialog_34);
258                         dialog.open();
259                         break;
260                 case ACTION_ERROR:
261                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
262                         dialog.setText(event.acc.name + Messages.TriggerDialog_35);
263                         dialog.setMessage(Messages.TriggerDialog_36);
264                         dialog.open();                  
265                         break;
266                 case ACTION_OKAY:
267                         result = (Object)new Boolean(true);
268                         if( event.action == 's' )
269                         {
270                                 result = (Object)new Boolean(false); //Don't close main window after "show"
271
272                                 int begin = event.data.lastIndexOf("Account: "+ event.acc.num); //$NON-NLS-1$
273                                 if( begin == -1 )
274                                 {
275                                         begin=0;
276                                 }
277                                 int end = event.data.lastIndexOf("[done]"); //$NON-NLS-1$
278                                 String s = event.data.substring( begin,end  );
279                                 dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
280                                 dialog.setText(Messages.TriggerDialog_39);
281                                 dialog.setMessage(s);
282                                 dialog.open();
283                         }
284                         
285                         if( event.action == 'd' )
286                         {
287                                 result = (Object)new Boolean(false); //We want the mainwin to get back after deleting an account.
288                                 dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
289                                 dialog.setText(Messages.TriggerDialog_40);
290                                 dialog.setMessage(event.acc.name + Messages.TriggerDialog_41);
291                                 dialog.open();
292                                 Display.getDefault().asyncExec( new Runnable() {
293                                         @Override
294                                         public void run() {
295                                                 delegate.updateAccountList();
296                                         }
297                                         
298                                 } );
299                         }
300                         
301                         if( !shell.isDisposed() )
302                         {
303                                 shell.close();
304                         }                       
305                         break;
306                 }
307
308
309                 
310         }
311 }