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