KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > MultipleInputDialog


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.debug.ui.StringVariableSelectionDialog;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.DirectoryDialog;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37
38 public class MultipleInputDialog extends Dialog {
39     protected static final String JavaDoc FIELD_NAME = "FIELD_NAME"; //$NON-NLS-1$
40
protected static final int TEXT = 100;
41     protected static final int BROWSE = 101;
42     protected static final int VARIABLE = 102;
43     
44     protected Composite panel;
45     
46     protected List JavaDoc fieldList = new ArrayList JavaDoc();
47     protected List JavaDoc controlList = new ArrayList JavaDoc();
48     protected List JavaDoc validators = new ArrayList JavaDoc();
49     protected Map JavaDoc valueMap = new HashMap JavaDoc();
50
51     private String JavaDoc title;
52     
53     
54     
55     public MultipleInputDialog(Shell shell, String JavaDoc title) {
56         super(shell);
57         this.title = title;
58         setShellStyle(getShellStyle() | SWT.RESIZE);
59     }
60     
61     /* (non-Javadoc)
62      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
63      */

64     protected void configureShell(Shell shell) {
65         super.configureShell(shell);
66         if (title != null) {
67             shell.setText(title);
68         }
69         
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.jface.dialogs.Dialog#createButtonBar(org.eclipse.swt.widgets.Composite)
74      */

75     protected Control createButtonBar(Composite parent) {
76         Control bar = super.createButtonBar(parent);
77         validateFields();
78         return bar;
79     }
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
83      */

84     protected Control createDialogArea(Composite parent) {
85         Composite container = (Composite)super.createDialogArea(parent);
86         container.setLayout(new GridLayout(2, false));
87         container.setLayoutData(new GridData(GridData.FILL_BOTH));
88         
89         panel = new Composite(container, SWT.NONE);
90         GridLayout layout = new GridLayout(2, false);
91         panel.setLayout(layout);
92         panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
93         
94         for (Iterator JavaDoc i = fieldList.iterator(); i.hasNext();) {
95             FieldSummary field = (FieldSummary)i.next();
96             switch(field.type) {
97                 case TEXT:
98                     createTextField(field.name, field.initialValue, field.allowsEmpty);
99                     break;
100                 case BROWSE:
101                     createBrowseField(field.name, field.initialValue, field.allowsEmpty);
102                     break;
103                 case VARIABLE:
104                     createVariablesField(field.name, field.initialValue, field.allowsEmpty);
105                     break;
106             }
107         }
108         
109         fieldList = null; // allow it to be gc'd
110
Dialog.applyDialogFont(container);
111         return container;
112     }
113     
114     public void addBrowseField(String JavaDoc labelText, String JavaDoc initialValue, boolean allowsEmpty) {
115         fieldList.add(new FieldSummary(BROWSE, labelText, initialValue, allowsEmpty));
116     }
117     public void addTextField(String JavaDoc labelText, String JavaDoc initialValue, boolean allowsEmpty) {
118         fieldList.add(new FieldSummary(TEXT, labelText, initialValue, allowsEmpty));
119     }
120     public void addVariablesField(String JavaDoc labelText, String JavaDoc initialValue, boolean allowsEmpty) {
121         fieldList.add(new FieldSummary(VARIABLE, labelText, initialValue, allowsEmpty));
122     }
123
124     protected void createTextField(String JavaDoc labelText, String JavaDoc initialValue, boolean allowEmpty) {
125         Label label = new Label(panel, SWT.NONE);
126         label.setText(labelText);
127         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
128         
129         final Text text = new Text(panel, SWT.SINGLE | SWT.BORDER);
130         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
131         text.setData(FIELD_NAME, labelText);
132         
133         // make sure rows are the same height on both panels.
134
label.setSize(label.getSize().x, text.getSize().y);
135         
136         if (initialValue != null) {
137             text.setText(initialValue);
138         }
139         
140         if (!allowEmpty) {
141             validators.add(new Validator() {
142                 public boolean validate() {
143                     return !text.getText().equals(""); //$NON-NLS-1$
144
}
145             });
146             text.addModifyListener(new ModifyListener() {
147                 public void modifyText(ModifyEvent e) {
148                     validateFields();
149                 }
150             });
151         }
152         
153         controlList.add(text);
154     }
155     
156     protected void createBrowseField(String JavaDoc labelText, String JavaDoc initialValue, boolean allowEmpty) {
157         Label label = new Label(panel, SWT.NONE);
158         label.setText(labelText);
159         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
160         
161         Composite comp = new Composite(panel, SWT.NONE);
162         GridLayout layout = new GridLayout();
163         layout.marginHeight=0;
164         layout.marginWidth=0;
165         comp.setLayout(layout);
166         comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
167         
168         final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER);
169         GridData data = new GridData(GridData.FILL_HORIZONTAL);
170         data.widthHint = 200;
171         text.setLayoutData(data);
172         text.setData(FIELD_NAME, labelText);
173
174         // make sure rows are the same height on both panels.
175
label.setSize(label.getSize().x, text.getSize().y);
176         
177         if (initialValue != null) {
178             text.setText(initialValue);
179         }
180
181         if (!allowEmpty) {
182             validators.add(new Validator() {
183                 public boolean validate() {
184                     return !text.getText().equals(""); //$NON-NLS-1$
185
}
186             });
187
188             text.addModifyListener(new ModifyListener() {
189                 public void modifyText(ModifyEvent e) {
190                     validateFields();
191                 }
192             });
193         }
194         
195         Button button = createButton(comp, IDialogConstants.IGNORE_ID, DebugUIMessages.MultipleInputDialog_6, false);
196         button.addSelectionListener(new SelectionAdapter() {
197             public void widgetSelected(SelectionEvent e) {
198                 DirectoryDialog dialog = new DirectoryDialog(getShell());
199                 dialog.setMessage(DebugUIMessages.MultipleInputDialog_7);
200                 String JavaDoc currentWorkingDir = text.getText();
201                 if (!currentWorkingDir.trim().equals("")) { //$NON-NLS-1$
202
File JavaDoc path = new File JavaDoc(currentWorkingDir);
203                     if (path.exists()) {
204                         dialog.setFilterPath(currentWorkingDir);
205                     }
206                 }
207                 
208                 String JavaDoc selectedDirectory = dialog.open();
209                 if (selectedDirectory != null) {
210                     text.setText(selectedDirectory);
211                 }
212             }
213         });
214
215         controlList.add(text);
216         
217     }
218     
219     
220     public void createVariablesField(String JavaDoc labelText, String JavaDoc initialValue, boolean allowEmpty) {
221         Label label = new Label(panel, SWT.NONE);
222         label.setText(labelText);
223         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
224         
225         Composite comp = new Composite(panel, SWT.NONE);
226         GridLayout layout = new GridLayout();
227         layout.marginHeight=0;
228         layout.marginWidth=0;
229         comp.setLayout(layout);
230         comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
231         
232         final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER);
233         GridData data = new GridData(GridData.FILL_HORIZONTAL);
234         data.widthHint = 200;
235         text.setLayoutData(data);
236         text.setData(FIELD_NAME, labelText);
237
238         // make sure rows are the same height on both panels.
239
label.setSize(label.getSize().x, text.getSize().y);
240         
241         if (initialValue != null) {
242             text.setText(initialValue);
243         }
244
245         if (!allowEmpty) {
246             validators.add(new Validator() {
247                 public boolean validate() {
248                     return !text.getText().equals(""); //$NON-NLS-1$
249
}
250             });
251
252             text.addModifyListener(new ModifyListener() {
253                 public void modifyText(ModifyEvent e) {
254                     validateFields();
255                 }
256             });
257         }
258         
259         Button button = createButton(comp, IDialogConstants.IGNORE_ID, DebugUIMessages.MultipleInputDialog_8, false);
260         button.addSelectionListener(new SelectionAdapter() {
261             public void widgetSelected(SelectionEvent e) {
262                 StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
263                 int code = dialog.open();
264                 if (code == IDialogConstants.OK_ID) {
265                     String JavaDoc variable = dialog.getVariableExpression();
266                     if (variable != null) {
267                         text.insert(variable);
268                     }
269                 }
270             }
271         });
272
273         controlList.add(text);
274                 
275     }
276     
277     /* (non-Javadoc)
278      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
279      */

280     protected void okPressed() {
281         for (Iterator JavaDoc i = controlList.iterator(); i.hasNext(); ) {
282             Control control = (Control)i.next();
283             if (control instanceof Text) {
284                 valueMap.put(control.getData(FIELD_NAME), ((Text)control).getText());
285             }
286         }
287         controlList = null;
288         super.okPressed();
289     }
290
291     
292     /* (non-Javadoc)
293      * @see org.eclipse.jface.window.Window#open()
294      */

295     public int open() {
296         applyDialogFont(panel);
297         return super.open();
298     }
299     
300     public Object JavaDoc getValue(String JavaDoc key) {
301         return valueMap.get(key);
302     }
303     
304     public String JavaDoc getStringValue(String JavaDoc key) {
305         return (String JavaDoc) getValue(key);
306     }
307     
308     public void validateFields() {
309         for(Iterator JavaDoc i = validators.iterator(); i.hasNext(); ) {
310             Validator validator = (Validator) i.next();
311             if (!validator.validate()) {
312                 getButton(IDialogConstants.OK_ID).setEnabled(false);
313                 return;
314             }
315         }
316         getButton(IDialogConstants.OK_ID).setEnabled(true);
317     }
318     
319     protected class FieldSummary {
320         int type;
321         String JavaDoc name;
322         String JavaDoc initialValue;
323         boolean allowsEmpty;
324         
325         public FieldSummary(int type, String JavaDoc name, String JavaDoc initialValue, boolean allowsEmpty) {
326             this.type = type;
327             this.name = name;
328             this.initialValue = initialValue;
329             this.allowsEmpty = allowsEmpty;
330         }
331     }
332     
333     protected class Validator {
334         boolean validate() {
335             return true;
336         }
337     }
338 }
339
Popular Tags