acffe9b00e31636ab1a5ce9fd38d1564b862043c
[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                                 shell.setEnabled(false); //Fisk
101                                 permitCountdownDialog.open();
102                         }
103                 });
104                 btnUsernamePassword.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/both.png")); //$NON-NLS-1$
105                 FormData fd_btnUsernamePassword = new FormData();
106                 fd_btnUsernamePassword.bottom = new FormAttachment(0, 45);
107                 fd_btnUsernamePassword.top = new FormAttachment(0, 10);
108                 fd_btnUsernamePassword.left = new FormAttachment(0, 10);
109                 fd_btnUsernamePassword.right = new FormAttachment(0, 224);
110                 btnUsernamePassword.setLayoutData(fd_btnUsernamePassword);
111
112                 btnUsernamePassword.setText(Messages.TriggerDialog_6);
113                 
114                 Button btnUsernameOnly = new Button(grpMakeFinalKey, SWT.NONE);
115                 btnUsernameOnly.addSelectionListener(new SelectionAdapter() {
116                         @Override
117                         public void widgetSelected(SelectionEvent e) {
118                                 FkManager.getInstance().trig(account, 'u', mySelf);
119                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_7, Messages.TriggerDialog_8, 30000);
120                                 //shell.setMinimized(true);
121                                 shell.setEnabled(false); //Fisk
122                                 permitCountdownDialog.open();
123                         }
124                 });
125                 btnUsernameOnly.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/user.png")); //$NON-NLS-1$
126                 FormData fd_btnUsernameOnly = new FormData();
127                 fd_btnUsernameOnly.bottom = new FormAttachment(100, -15);
128                 fd_btnUsernameOnly.top = new FormAttachment(0, 10);
129                 fd_btnUsernameOnly.left = new FormAttachment(btnUsernamePassword);
130                 btnUsernameOnly.setLayoutData(fd_btnUsernameOnly);
131
132                 btnUsernameOnly.setText(Messages.TriggerDialog_10);
133                 
134                 Button btnPasswordOnly = new Button(grpMakeFinalKey, SWT.NONE);
135                 fd_btnUsernameOnly.right = new FormAttachment(btnPasswordOnly, -29);
136                 btnPasswordOnly.addSelectionListener(new SelectionAdapter() {
137                         @Override
138                         public void widgetSelected(SelectionEvent e) {
139                                 FkManager.getInstance().trig(account, 'p', mySelf);
140                                 
141                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_11, Messages.TriggerDialog_12, 30000);
142
143                                 //shell.setMinimized(true);
144                                 shell.setEnabled(false); //Fisk
145                                 permitCountdownDialog.open();
146                         }
147                 });
148                 btnPasswordOnly.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/key-icon.png")); //$NON-NLS-1$
149                 FormData fd_btnPasswordOnly = new FormData();
150                 fd_btnPasswordOnly.bottom = new FormAttachment(100, -15);
151                 fd_btnPasswordOnly.top = new FormAttachment(0, 10);
152                 fd_btnPasswordOnly.left = new FormAttachment(0, 431);
153                 fd_btnPasswordOnly.right = new FormAttachment(100, -21);
154                 btnPasswordOnly.setLayoutData(fd_btnPasswordOnly);
155
156                 btnPasswordOnly.setText(Messages.TriggerDialog_14);
157                                 
158                                 grpChange = new Group(shell, SWT.NONE);
159                                 grpChange.setText(Messages.TriggerDialog_15+account.name);
160                                 grpChange.setLayout(new FormLayout());
161                                 fd_grpChange = new FormData();
162                                 fd_grpChange.left = new FormAttachment(grpMakeFinalKey, 353, SWT.LEFT);
163                                 fd_grpChange.right = new FormAttachment(grpMakeFinalKey, 0, SWT.RIGHT);
164                                 fd_grpChange.top = new FormAttachment(grpMakeFinalKey, 6);
165                                 grpChange.setLayoutData(fd_grpChange);
166                                 
167                                 Button btnEdit = new Button(grpChange, SWT.NONE);
168                                 btnEdit.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/gtk_edit.png")); //$NON-NLS-1$
169                                 FormData fd_btnEdit = new FormData();
170                                 fd_btnEdit.bottom = new FormAttachment(0, 48);
171                                 fd_btnEdit.left = new FormAttachment(0, 10);
172                                 fd_btnEdit.top = new FormAttachment(0, 10);
173                                 btnEdit.setLayoutData(fd_btnEdit);
174                                 btnEdit.setText(Messages.TriggerDialog_17);
175                                 ///TODO: Inplement edit box..
176                                 btnEdit.setVisible(false);
177                                 
178                                 Button btnDelete = new Button(grpChange, SWT.NONE);
179                                 fd_btnEdit.right = new FormAttachment(100, -138);
180                                 btnDelete.addSelectionListener(new SelectionAdapter() {
181                                         @Override
182                                         public void widgetSelected(SelectionEvent e) {
183                                                 MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO );
184                                                 dialog.setText(Messages.TriggerDialog_18+account.name );
185                                                 dialog.setMessage(Messages.TriggerDialog_19+account.name+Messages.TriggerDialog_20+account.num+Messages.TriggerDialog_21);
186                                                 if( dialog.open() == SWT.YES )
187                                                 {
188                                                         permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_22, Messages.TriggerDialog_23+account.name+Messages.TriggerDialog_24, 5000);
189                                                         //shell.setMinimized(true);
190                                                         FkManager.getInstance().trig(account, 'd', mySelf);
191                                                         shell.setEnabled(false); //Fisk
192                                                         permitCountdownDialog.open();
193                                                         
194                                                         
195
196                                                 }
197                                         }
198                                 });
199                                 btnDelete.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/trashdelete.gif")); //$NON-NLS-1$
200                                 FormData fd_btnDelete = new FormData();
201                                 fd_btnDelete.bottom = new FormAttachment(100, -20);
202                                 fd_btnDelete.top = new FormAttachment(0, 10);
203                                 fd_btnDelete.right = new FormAttachment(100, -16);
204                                 fd_btnDelete.left = new FormAttachment(btnEdit, 10);
205                                 btnDelete.setLayoutData(fd_btnDelete);
206                                 btnDelete.setText(Messages.TriggerDialog_26);
207
208                                 Button btnCancel = new Button(shell, SWT.NONE);
209                                 fd_grpChange.bottom = new FormAttachment(btnCancel, -6);
210                                 btnCancel.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/Delete.png")); //$NON-NLS-1$
211                                 FormData fd_btnCancel = new FormData();
212                                 fd_btnCancel.left = new FormAttachment(grpMakeFinalKey, 0, SWT.LEFT);
213                                 fd_btnCancel.bottom = new FormAttachment(100, -10);
214                                 fd_btnCancel.right = new FormAttachment(grpMakeFinalKey, 2, SWT.RIGHT);
215                                 btnCancel.setLayoutData(fd_btnCancel);
216                                 
217                                 btnCancel.setText(Messages.TriggerDialog_28);
218                                 
219                                 btnCancel.addSelectionListener( new SelectionAdapter() {
220                                         public void widgetSelected(SelectionEvent e) {
221                                                 shell.close();
222                                         }
223                                 });                             
224                                 Group grpShow = new Group(shell, SWT.NONE);
225                                 grpShow.setText(Messages.TriggerDialog_29+account.name);
226                                 FormData fd_grpShow = new FormData();
227                                 fd_grpShow.bottom = new FormAttachment(grpChange, 0, SWT.BOTTOM);
228                                 fd_grpShow.right = new FormAttachment(grpChange, -6);
229                                 fd_grpShow.left = new FormAttachment(grpMakeFinalKey, 0, SWT.LEFT);
230                                 fd_grpShow.top = new FormAttachment(grpMakeFinalKey, 6);
231                                 grpShow.setLayoutData(fd_grpShow);
232                                 
233                                 Button btnShowUsername = new Button(grpShow, SWT.NONE);
234                                 btnShowUsername.addSelectionListener(new SelectionAdapter() {
235                                         @Override
236                                         public void widgetSelected(SelectionEvent e) {
237                                                 FkManager.getInstance().trig(account, 's', mySelf);
238                                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + Messages.TriggerDialog_30, Messages.TriggerDialog_31, 30000);
239                                                 //shell.setMinimized(true);
240                                                 shell.setEnabled(false); //Fisk
241                                                 permitCountdownDialog.open();
242                                                 
243                                         }
244                                 });
245                                 btnShowUsername.setImage(SWTResourceManager.getImage(MainWin.class, "/fkgui/gfx/both.png")); //$NON-NLS-1$
246                                 btnShowUsername.setBounds(10, 28, 327, 35);
247                                 btnShowUsername.setText(Messages.TriggerDialog_33);                             
248                                 
249
250         }
251
252         @SuppressWarnings("incomplete-switch")
253         @Override
254         public void fkActionEvent(FkActionEvent event) {
255                 MessageBox dialog;
256                 
257                 if( shell.isDisposed() )
258                 {
259                         return;
260                 }
261                 
262                 if( permitCountdownDialog != null && !permitCountdownDialog.shell.isDisposed() )
263                 {
264                         permitCountdownDialog.shell.close();
265                 }
266                 
267                 
268                 switch(event.type)
269                 {
270                 case STATE_ERROR:
271                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
272                         dialog.setText(Messages.TriggerDialog_5);
273                         dialog.setMessage(Messages.TriggerDialog_9);
274                         dialog.open();                  
275                 break;
276                 case ACTION_ABORTED:
277                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
278                         dialog.setText(event.acc.name);
279                         dialog.setMessage(Messages.TriggerDialog_34);
280                         dialog.open();
281                         break;
282                 case ACTION_ERROR:
283                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
284                         dialog.setText(event.acc.name + Messages.TriggerDialog_35);
285                         dialog.setMessage(Messages.TriggerDialog_36);
286                         dialog.open();                  
287                         break;
288                 case ACTION_OKAY:
289                         result = (Object)new Boolean(true);
290                         if( event.action == 's' )
291                         {
292                                 result = (Object)new Boolean(false); //Don't close main window after "show"
293
294                                 int begin = event.data.lastIndexOf("Account: "+ event.acc.num); //$NON-NLS-1$
295                                 if( begin == -1 )
296                                 {
297                                         begin=0;
298                                 }
299                                 int end = event.data.lastIndexOf("[done]"); //$NON-NLS-1$
300                                 String s = event.data.substring( begin,end  );
301                                 dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
302                                 dialog.setText(Messages.TriggerDialog_39);
303                                 dialog.setMessage(s);
304                                 dialog.open();
305                         }
306                         
307                         if( event.action == 'd' )
308                         {
309                                 result = (Object)new Boolean(false); //We want the mainwin to get back after deleting an account.
310                                 dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
311                                 dialog.setText(Messages.TriggerDialog_40);
312                                 dialog.setMessage(event.acc.name + Messages.TriggerDialog_41);
313                                 dialog.open();
314                                 Display.getDefault().asyncExec( new Runnable() {
315                                         @Override
316                                         public void run() {
317                                                 delegate.updateAccountList();
318                                         }
319                                         
320                                 } );
321                         }
322                         shell.close();
323                         break;
324                 } //Switch
325                 
326                 if( !shell.isDisposed() && !shell.isEnabled())
327                 {
328                         shell.setEnabled(true); //Fisk
329                 }
330
331                 
332         }
333 }