Snapshot before changing swt list to comboviewer
[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         private Boolean askPermitShow = false;
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) {
44                 super(parent, style);
45                 account = a;
46                 setText("Use "+ account.name);
47         }
48
49         /**
50          * Open the dialog.
51          * @return the result
52          */
53         public Object open() {
54                 result = (Object)new Boolean(false);
55                 createContents();
56
57                 Point p = getParent().getSize();
58                 p.x /= 2;
59                 p.y /= 2;
60                 p.x -= shell.getSize().x/2;
61                 p.y -= shell.getSize().y/2;
62                 p.x += getParent().getLocation().x;
63                 p.y += getParent().getLocation().y;
64                 shell.setLocation( p );
65
66                 shell.open();
67                 shell.layout();
68                 Display display = getParent().getDisplay();
69                 
70                 
71                 while (!shell.isDisposed()) {
72                         if (!display.readAndDispatch()) {
73                                 display.sleep();
74                         }
75                 }
76                 System.out.println("Returning result:" + (Boolean)result);
77                 return result;
78         }
79
80         /**
81          * Create contents of the dialog.
82          */
83         private void createContents() {
84                 shell = new Shell(getParent(), SWT.BORDER | SWT.CLOSE);
85                 shell.setSize(677, 250);
86                 shell.setText(getText());
87                 shell.setLayout(new FormLayout());
88                 mySelf = this;
89                 grpMakeFinalKey = new Group(shell, SWT.NONE);
90                 grpMakeFinalKey.setLayout(new FormLayout());
91                 FormData fd_grpMakeFinalKey = new FormData();
92                 fd_grpMakeFinalKey.top = new FormAttachment(0, 10);
93                 fd_grpMakeFinalKey.bottom = new FormAttachment(0, 88);
94                 fd_grpMakeFinalKey.left = new FormAttachment(0, 10);
95                 fd_grpMakeFinalKey.right = new FormAttachment(100, -10);
96                 grpMakeFinalKey.setLayoutData(fd_grpMakeFinalKey);
97                 grpMakeFinalKey.setText("Use " + account.name);
98                 
99                 Button btnUsernamePassword = new Button(grpMakeFinalKey, SWT.NONE);
100                 btnUsernamePassword.addSelectionListener(new SelectionAdapter() {
101                         @Override
102                         public void widgetSelected(SelectionEvent e) {
103                                 FkManager.getInstance().trig(account, '%', mySelf);
104                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + ": Ready with username and password", "Focus username field and press button.\nPress and hold to cancel.", 30000);
105                                 shell.setMinimized(true);
106                                 permitCountdownDialog.open();
107                         }
108                 });
109                 btnUsernamePassword.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/both.png"));
110                 FormData fd_btnUsernamePassword = new FormData();
111                 fd_btnUsernamePassword.top = new FormAttachment(0, 10);
112                 fd_btnUsernamePassword.left = new FormAttachment(0, 10);
113                 fd_btnUsernamePassword.right = new FormAttachment(0, 224);
114                 btnUsernamePassword.setLayoutData(fd_btnUsernamePassword);
115
116                 btnUsernamePassword.setText("Username + Password");
117                 
118                 Button btnUsernameOnly = new Button(grpMakeFinalKey, SWT.NONE);
119                 btnUsernameOnly.addSelectionListener(new SelectionAdapter() {
120                         @Override
121                         public void widgetSelected(SelectionEvent e) {
122                                 FkManager.getInstance().trig(account, 'u', mySelf);
123                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + ": Ready with username", "Focus username field and press button.\nPress and hold to cancel.", 30000);
124                                 shell.setMinimized(true);
125                                 permitCountdownDialog.open();
126                         }
127                 });
128                 btnUsernameOnly.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/Deep_User.png"));
129                 FormData fd_btnUsernameOnly = new FormData();
130                 fd_btnUsernameOnly.top = new FormAttachment(btnUsernamePassword, -35);
131                 fd_btnUsernameOnly.bottom = new FormAttachment(btnUsernamePassword, 0, SWT.BOTTOM);
132                 fd_btnUsernameOnly.left = new FormAttachment(btnUsernamePassword, 6);
133                 fd_btnUsernameOnly.right = new FormAttachment(0, 405);
134                 btnUsernameOnly.setLayoutData(fd_btnUsernameOnly);
135
136                 btnUsernameOnly.setText("Username");
137                 
138                 Button btnPasswordOnly = new Button(grpMakeFinalKey, SWT.NONE);
139                 btnPasswordOnly.addSelectionListener(new SelectionAdapter() {
140                         @Override
141                         public void widgetSelected(SelectionEvent e) {
142                                 FkManager.getInstance().trig(account, 'p', mySelf);
143                                 
144                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + ": Ready with password", "Focus username field and press button.\nPress and hold to cancel.", 30000);
145
146                                 shell.setMinimized(true);
147                                 permitCountdownDialog.open();
148                         }
149                 });
150                 btnPasswordOnly.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/key-icon.png"));
151                 FormData fd_btnPasswordOnly = new FormData();
152                 fd_btnPasswordOnly.left = new FormAttachment(btnUsernameOnly, 29);
153                 fd_btnPasswordOnly.top = new FormAttachment(btnUsernamePassword, 0, SWT.TOP);
154                 fd_btnPasswordOnly.right = new FormAttachment(100, -9);
155                 fd_btnPasswordOnly.bottom = new FormAttachment(0, 45);
156                 btnPasswordOnly.setLayoutData(fd_btnPasswordOnly);
157
158                 btnPasswordOnly.setText("Password");
159                                 
160                                 grpChange = new Group(shell, SWT.NONE);
161                                 grpChange.setText("Change "+account.name);
162                                 grpChange.setLayout(new FormLayout());
163                                 fd_grpChange = new FormData();
164                                 fd_grpChange.left = new FormAttachment(grpMakeFinalKey, 353, SWT.LEFT);
165                                 fd_grpChange.right = new FormAttachment(grpMakeFinalKey, 0, SWT.RIGHT);
166                                 fd_grpChange.top = new FormAttachment(grpMakeFinalKey, 6);
167                                 grpChange.setLayoutData(fd_grpChange);
168                                 
169                                 Button btnEdit = new Button(grpChange, SWT.NONE);
170                                 btnEdit.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/gtk_edit.png"));
171                                 FormData fd_btnEdit = new FormData();
172                                 fd_btnEdit.top = new FormAttachment(0, 10);
173                                 fd_btnEdit.left = new FormAttachment(0, 10);
174                                 btnEdit.setLayoutData(fd_btnEdit);
175                                 btnEdit.setText("Edit");
176                                 ///TODO: Inplement edit box..
177                                 btnEdit.setVisible(false);
178                                 
179                                 Button btnDelete = new Button(grpChange, SWT.NONE);
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("WARNING! Confirm delete "+account.name );
185                                                 dialog.setMessage("Are you sure you want to delete account "+account.name+" ?\nAccount id: "+account.num+"\nIf you remove the account, it can not be recovered!");
186                                                 if( dialog.open() == SWT.YES )
187                                                 {
188                                                         permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + ": Confirm deletion", "Press button to show delete "+account.name+".\nPress and hold to cancel.", 5000);
189                                                         shell.setMinimized(true);
190                                                         permitCountdownDialog.open();
191
192                                                 }
193                                         }
194                                 });
195                                 fd_btnEdit.right = new FormAttachment(btnDelete, -6);
196                                 btnDelete.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/trashdelete.gif"));
197                                 FormData fd_btnDelete = new FormData();
198                                 fd_btnDelete.top = new FormAttachment(0, 10);
199                                 fd_btnDelete.left = new FormAttachment(0, 166);
200                                 fd_btnDelete.right = new FormAttachment(100, -11);
201                                 btnDelete.setLayoutData(fd_btnDelete);
202                                 btnDelete.setText("Delete");
203
204                                 Button btnCancel = new Button(shell, SWT.NONE);
205                                 fd_grpChange.bottom = new FormAttachment(btnCancel, -6);
206                                 btnCancel.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/Delete.png"));
207                                 FormData fd_btnCancel = new FormData();
208                                 fd_btnCancel.left = new FormAttachment(grpMakeFinalKey, 0, SWT.LEFT);
209                                 fd_btnCancel.bottom = new FormAttachment(100, -10);
210                                 fd_btnCancel.right = new FormAttachment(grpMakeFinalKey, 2, SWT.RIGHT);
211                                 btnCancel.setLayoutData(fd_btnCancel);
212                                 
213                                 btnCancel.setText("Do nothing");
214                                 
215                                 btnCancel.addSelectionListener( new SelectionAdapter() {
216                                         public void widgetSelected(SelectionEvent e) {
217                                                 shell.close();
218                                         }
219                                 });                             
220                                 Group grpShow = new Group(shell, SWT.NONE);
221                                 grpShow.setText("Show "+account.name);
222                                 FormData fd_grpShow = new FormData();
223                                 fd_grpShow.bottom = new FormAttachment(grpChange, 0, SWT.BOTTOM);
224                                 fd_grpShow.right = new FormAttachment(grpChange, -6);
225                                 fd_grpShow.left = new FormAttachment(grpMakeFinalKey, 0, SWT.LEFT);
226                                 fd_grpShow.top = new FormAttachment(grpMakeFinalKey, 6);
227                                 grpShow.setLayoutData(fd_grpShow);
228                                 
229                                 Button btnShowUsername = new Button(grpShow, SWT.NONE);
230                                 btnShowUsername.addSelectionListener(new SelectionAdapter() {
231                                         @Override
232                                         public void widgetSelected(SelectionEvent e) {
233                                                 FkManager.getInstance().trig(account, 's', mySelf);
234                                                 permitCountdownDialog = new PermitCountDownDialog(shell,SWT.SHELL_TRIM, account.name + ": Ready for display", "Press button to show username and password.\nPress and hold to cancel.", 30000);
235                                                 askPermitShow=true;
236                                                 shell.setMinimized(true);
237                                                 permitCountdownDialog.open();
238                                         }
239                                 });
240                                 btnShowUsername.setImage(SWTResourceManager.getImage("/home/dusted/Downloads/both.png"));
241                                 btnShowUsername.setBounds(10, 36, 327, 35);
242                                 btnShowUsername.setText("Show Username + Password");                            
243                                 
244
245         }
246
247         @Override
248         public void fkActionEvent(FkActionEvent event) {
249                 MessageBox dialog;
250                 
251                 if( permitCountdownDialog != null && !permitCountdownDialog.shell.isDisposed() )
252                 {
253                         permitCountdownDialog.shell.close();
254                 }
255                 
256                 switch(event.type)
257                 {
258                 case ACTION_ABORTED:
259                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
260                         dialog.setText(event.acc.name);
261                         dialog.setMessage("Action aborted or timed out.");
262                         dialog.open();
263                         break;
264                 case ACTION_ERROR:
265                         dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
266                         dialog.setText(event.acc.name + " error");
267                         dialog.setMessage("An error ocurred.");
268                         dialog.open();                  
269                         break;
270                 case ACTION_OKAY:
271                         result = (Object)new Boolean(true);
272                         if( askPermitShow )
273                         {
274                                 String s = event.data.substring( event.data.indexOf( "Account: "+ event.acc.num), event.data.lastIndexOf("[done]") );
275                                 dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
276                                 dialog.setText("Account information");
277                                 dialog.setMessage(s);
278                                 dialog.open();
279                         }
280                         if( !shell.isDisposed() )
281                         {
282                                 shell.close();
283                         }                       
284                         break;
285                 }
286
287
288                 
289         }
290 }