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