Added back some missing keys. from previous cleanup
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / MultipleInputDialog.java
1 package net.sourceforge.phpeclipse.xdebug.ui;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.eclipse.debug.ui.IDebugUIConstants;
11 import org.eclipse.debug.ui.StringVariableSelectionDialog;
12 import org.eclipse.jface.dialogs.Dialog;
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.DirectoryDialog;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28
29 public class MultipleInputDialog extends Dialog {
30         protected static final String FIELD_NAME = "FIELD_NAME"; //$NON-NLS-1$
31
32         protected static final int TEXT = 100;
33
34         protected static final int BROWSE = 101;
35
36         protected static final int VARIABLE = 102;
37
38         protected Composite panel;
39
40         protected List fieldList = new ArrayList();
41
42         protected List controlList = new ArrayList();
43
44         protected List validators = new ArrayList();
45
46         protected Map valueMap = new HashMap();
47
48         private String title;
49
50         public MultipleInputDialog(Shell shell, String title) {
51                 super(shell);
52                 this.title = title;
53                 setShellStyle(getShellStyle() | SWT.RESIZE);
54         }
55
56         /*
57          * (non-Javadoc)
58          * 
59          * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
60          */
61         protected void configureShell(Shell shell) {
62                 super.configureShell(shell);
63                 if (title != null) {
64                         shell.setText(title);
65                 }
66
67         }
68
69         /*
70          * (non-Javadoc)
71          * 
72          * @see org.eclipse.jface.dialogs.Dialog#createButtonBar(org.eclipse.swt.widgets.Composite)
73          */
74         protected Control createButtonBar(Composite parent) {
75                 Control bar = super.createButtonBar(parent);
76                 validateFields();
77                 return bar;
78         }
79
80         /*
81          * (non-Javadoc)
82          * 
83          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
84          */
85         protected Control createDialogArea(Composite parent) {
86                 Composite container = (Composite) super.createDialogArea(parent);
87                 container.setLayout(new GridLayout(2, false));
88                 container.setLayoutData(new GridData(GridData.FILL_BOTH));
89
90                 panel = new Composite(container, SWT.NONE);
91                 GridLayout layout = new GridLayout(2, false);
92                 panel.setLayout(layout);
93                 panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
94
95                 for (Iterator i = fieldList.iterator(); i.hasNext();) {
96                         FieldSummary field = (FieldSummary) i.next();
97                         switch (field.type) {
98                         case TEXT:
99                                 createTextField(field.name, field.initialValue,
100                                                 field.allowsEmpty);
101                                 break;
102                         case BROWSE:
103                                 createBrowseField(field.name, field.initialValue,
104                                                 field.allowsEmpty);
105                                 break;
106                         case VARIABLE:
107                                 createVariablesField(field.name, field.initialValue,
108                                                 field.allowsEmpty);
109                                 break;
110                         }
111                 }
112
113                 fieldList = null; // allow it to be gc'd
114                 Dialog.applyDialogFont(container);
115                 return container;
116         }
117
118         public void addBrowseField(String labelText, String initialValue,
119                         boolean allowsEmpty) {
120                 fieldList.add(new FieldSummary(BROWSE, labelText, initialValue,
121                                 allowsEmpty));
122         }
123
124         public void addTextField(String labelText, String initialValue,
125                         boolean allowsEmpty) {
126                 fieldList.add(new FieldSummary(TEXT, labelText, initialValue,
127                                 allowsEmpty));
128         }
129
130         public void addVariablesField(String labelText, String initialValue,
131                         boolean allowsEmpty) {
132                 fieldList.add(new FieldSummary(VARIABLE, labelText, initialValue,
133                                 allowsEmpty));
134         }
135
136         protected void createTextField(String labelText, String initialValue,
137                         boolean allowEmpty) {
138                 Label label = new Label(panel, SWT.NONE);
139                 label.setText(labelText);
140                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
141
142                 final Text text = new Text(panel, SWT.SINGLE | SWT.BORDER);
143                 text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
144                 text.setData(FIELD_NAME, labelText);
145
146                 // make sure rows are the same height on both panels.
147                 label.setSize(label.getSize().x, text.getSize().y);
148
149                 if (initialValue != null) {
150                         text.setText(initialValue);
151                 }
152
153                 if (!allowEmpty) {
154                         validators.add(new Validator() {
155                                 public boolean validate() {
156                                         return !text.getText().equals(""); //$NON-NLS-1$
157                                 }
158                         });
159                         text.addModifyListener(new ModifyListener() {
160                                 public void modifyText(ModifyEvent e) {
161                                         validateFields();
162                                 }
163                         });
164                 }
165
166                 controlList.add(text);
167         }
168
169         protected void createBrowseField(String labelText, String initialValue,
170                         boolean allowEmpty) {
171                 Label label = new Label(panel, SWT.NONE);
172                 label.setText(labelText);
173                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
174
175                 Composite comp = new Composite(panel, SWT.NONE);
176                 GridLayout layout = new GridLayout();
177                 layout.marginHeight = 0;
178                 layout.marginWidth = 0;
179                 comp.setLayout(layout);
180                 comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
181
182                 final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER);
183                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
184                 data.widthHint = 200;
185                 text.setLayoutData(data);
186                 text.setData(FIELD_NAME, labelText);
187
188                 // make sure rows are the same height on both panels.
189                 label.setSize(label.getSize().x, text.getSize().y);
190
191                 if (initialValue != null) {
192                         text.setText(initialValue);
193                 }
194
195                 if (!allowEmpty) {
196                         validators.add(new Validator() {
197                                 public boolean validate() {
198                                         return !text.getText().equals(""); //$NON-NLS-1$
199                                 }
200                         });
201
202                         text.addModifyListener(new ModifyListener() {
203                                 public void modifyText(ModifyEvent e) {
204                                         validateFields();
205                                 }
206                         });
207                 }
208
209                 Button button = createButton(comp, IDialogConstants.IGNORE_ID,
210                                 "&Browse...", false);
211                 button.addSelectionListener(new SelectionAdapter() {
212                         public void widgetSelected(SelectionEvent e) {
213                                 DirectoryDialog dialog = new DirectoryDialog(getShell());
214                                 dialog.setMessage("Select a file:");
215                                 String currentWorkingDir = text.getText();
216                                 if (!currentWorkingDir.trim().equals("")) { //$NON-NLS-1$
217                                         File path = new File(currentWorkingDir);
218                                         if (path.exists()) {
219                                                 dialog.setFilterPath(currentWorkingDir);
220                                         }
221                                 }
222
223                                 String selectedDirectory = dialog.open();
224                                 if (selectedDirectory != null) {
225                                         text.setText(selectedDirectory);
226                                 }
227                         }
228                 });
229
230                 controlList.add(text);
231
232         }
233
234         public void createVariablesField(String labelText, String initialValue,
235                         boolean allowEmpty) {
236                 Label label = new Label(panel, SWT.NONE);
237                 label.setText(labelText);
238                 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
239
240                 Composite comp = new Composite(panel, SWT.NONE);
241                 GridLayout layout = new GridLayout();
242                 layout.marginHeight = 0;
243                 layout.marginWidth = 0;
244                 comp.setLayout(layout);
245                 comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
246
247                 final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER);
248                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
249                 data.widthHint = 200;
250                 text.setLayoutData(data);
251                 text.setData(FIELD_NAME, labelText);
252
253                 // make sure rows are the same height on both panels.
254                 label.setSize(label.getSize().x, text.getSize().y);
255
256                 if (initialValue != null) {
257                         text.setText(initialValue);
258                 }
259
260                 if (!allowEmpty) {
261                         validators.add(new Validator() {
262                                 public boolean validate() {
263                                         return !text.getText().equals(""); //$NON-NLS-1$
264                                 }
265                         });
266
267                         text.addModifyListener(new ModifyListener() {
268                                 public void modifyText(ModifyEvent e) {
269                                         validateFields();
270                                 }
271                         });
272                 }
273
274                 Button button = createButton(comp, IDialogConstants.IGNORE_ID,
275                                 "Varia&bles...", false);
276                 button.addSelectionListener(new SelectionAdapter() {
277                         public void widgetSelected(SelectionEvent e) {
278                                 StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(
279                                                 getShell());
280                                 int code = dialog.open();
281                                 if (code == IDialogConstants.OK_ID) {
282                                         String variable = dialog.getVariableExpression();
283                                         if (variable != null) {
284                                                 text.insert(variable);
285                                         }
286                                 }
287                         }
288                 });
289
290                 controlList.add(text);
291
292         }
293
294         /*
295          * (non-Javadoc)
296          * 
297          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
298          */
299         protected void okPressed() {
300                 for (Iterator i = controlList.iterator(); i.hasNext();) {
301                         Control control = (Control) i.next();
302                         if (control instanceof Text) {
303                                 valueMap.put(control.getData(FIELD_NAME), ((Text) control)
304                                                 .getText());
305                         }
306                 }
307                 controlList = null;
308                 super.okPressed();
309         }
310
311         /*
312          * (non-Javadoc)
313          * 
314          * @see org.eclipse.jface.window.Window#open()
315          */
316         public int open() {
317                 applyDialogFont(panel);
318                 return super.open();
319         }
320
321         public Object getValue(String key) {
322                 return valueMap.get(key);
323         }
324
325         public String getStringValue(String key) {
326                 return (String) getValue(key);
327         }
328
329         public void validateFields() {
330                 for (Iterator i = validators.iterator(); i.hasNext();) {
331                         Validator validator = (Validator) i.next();
332                         if (!validator.validate()) {
333                                 getButton(IDialogConstants.OK_ID).setEnabled(false);
334                                 return;
335                         }
336                 }
337                 getButton(IDialogConstants.OK_ID).setEnabled(true);
338         }
339
340         // /* (non-Javadoc)
341         // * @see
342         // org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
343         // */
344         // protected Point getInitialLocation(Point initialSize) {
345         // Point initialLocation=
346         // DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName());
347         // if (initialLocation != null) {
348         // return initialLocation;
349         // }
350         // return super.getInitialLocation(initialSize);
351         // }
352
353         protected String getDialogSettingsSectionName() {
354                 return IDebugUIConstants.PLUGIN_ID + ".MULTIPLE_INPUT_DIALOG_2"; //$NON-NLS-1$
355         }
356
357         // /* (non-Javadoc)
358         // * @see org.eclipse.jface.window.Window#getInitialSize()
359         // */
360         // protected Point getInitialSize() {
361         // Point size = super.getInitialSize();
362         // return
363         // DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(),
364         // size);
365         // }
366
367         // /* (non-Javadoc)
368         // * @see org.eclipse.jface.window.Window#close()
369         // */
370         // public boolean close() {
371         // DialogSettingsHelper.persistShellGeometry(getShell(),
372         // getDialogSettingsSectionName());
373         // return super.close();
374         // }
375
376         protected class FieldSummary {
377                 int type;
378
379                 String name;
380
381                 String initialValue;
382
383                 boolean allowsEmpty;
384
385                 public FieldSummary(int type, String name, String initialValue,
386                                 boolean allowsEmpty) {
387                         this.type = type;
388                         this.name = name;
389                         this.initialValue = initialValue;
390                         this.allowsEmpty = allowsEmpty;
391                 }
392         }
393
394         protected class Validator {
395                 boolean validate() {
396                         return true;
397                 }
398         }
399
400 }