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