1 package net.sourceforge.phpeclipse.xdebug.ui;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
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;
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;
35 protected Composite panel;
37 protected List fieldList = new ArrayList();
38 protected List controlList = new ArrayList();
39 protected List validators = new ArrayList();
40 protected Map valueMap = new HashMap();
46 public MultipleInputDialog(Shell shell, String title) {
49 setShellStyle(getShellStyle() | SWT.RESIZE);
53 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
55 protected void configureShell(Shell shell) {
56 super.configureShell(shell);
64 * @see org.eclipse.jface.dialogs.Dialog#createButtonBar(org.eclipse.swt.widgets.Composite)
66 protected Control createButtonBar(Composite parent) {
67 Control bar = super.createButtonBar(parent);
73 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
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));
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));
85 for (Iterator i = fieldList.iterator(); i.hasNext();) {
86 FieldSummary field = (FieldSummary)i.next();
89 createTextField(field.name, field.initialValue, field.allowsEmpty);
92 createBrowseField(field.name, field.initialValue, field.allowsEmpty);
95 createVariablesField(field.name, field.initialValue, field.allowsEmpty);
100 fieldList = null; // allow it to be gc'd
101 Dialog.applyDialogFont(container);
105 public void addBrowseField(String labelText, String initialValue, boolean allowsEmpty) {
106 fieldList.add(new FieldSummary(BROWSE, labelText, initialValue, allowsEmpty));
108 public void addTextField(String labelText, String initialValue, boolean allowsEmpty) {
109 fieldList.add(new FieldSummary(TEXT, labelText, initialValue, allowsEmpty));
111 public void addVariablesField(String labelText, String initialValue, boolean allowsEmpty) {
112 fieldList.add(new FieldSummary(VARIABLE, labelText, initialValue, allowsEmpty));
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));
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);
124 // make sure rows are the same height on both panels.
125 label.setSize(label.getSize().x, text.getSize().y);
127 if (initialValue != null) {
128 text.setText(initialValue);
132 validators.add(new Validator() {
133 public boolean validate() {
134 return !text.getText().equals(""); //$NON-NLS-1$
137 text.addModifyListener(new ModifyListener() {
138 public void modifyText(ModifyEvent e) {
144 controlList.add(text);
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));
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));
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);
165 // make sure rows are the same height on both panels.
166 label.setSize(label.getSize().x, text.getSize().y);
168 if (initialValue != null) {
169 text.setText(initialValue);
173 validators.add(new Validator() {
174 public boolean validate() {
175 return !text.getText().equals(""); //$NON-NLS-1$
179 text.addModifyListener(new ModifyListener() {
180 public void modifyText(ModifyEvent e) {
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);
195 dialog.setFilterPath(currentWorkingDir);
199 String selectedDirectory = dialog.open();
200 if (selectedDirectory != null) {
201 text.setText(selectedDirectory);
206 controlList.add(text);
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));
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));
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);
229 // make sure rows are the same height on both panels.
230 label.setSize(label.getSize().x, text.getSize().y);
232 if (initialValue != null) {
233 text.setText(initialValue);
237 validators.add(new Validator() {
238 public boolean validate() {
239 return !text.getText().equals(""); //$NON-NLS-1$
243 text.addModifyListener(new ModifyListener() {
244 public void modifyText(ModifyEvent e) {
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);
264 controlList.add(text);
269 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
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());
284 * @see org.eclipse.jface.window.Window#open()
287 applyDialogFont(panel);
291 public Object getValue(String key) {
292 return valueMap.get(key);
295 public String getStringValue(String key) {
296 return (String) getValue(key);
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);
307 getButton(IDialogConstants.OK_ID).setEnabled(true);
311 // * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
313 // protected Point getInitialLocation(Point initialSize) {
314 // Point initialLocation= DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName());
315 // if (initialLocation != null) {
316 // return initialLocation;
318 // return super.getInitialLocation(initialSize);
322 protected String getDialogSettingsSectionName() {
323 return IDebugUIConstants.PLUGIN_ID + ".MULTIPLE_INPUT_DIALOG_2"; //$NON-NLS-1$
327 // * @see org.eclipse.jface.window.Window#getInitialSize()
329 // protected Point getInitialSize() {
330 // Point size = super.getInitialSize();
331 // return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size);
335 // * @see org.eclipse.jface.window.Window#close()
337 // public boolean close() {
338 // DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName());
339 // return super.close();
342 protected class FieldSummary {
348 public FieldSummary(int type, String name, String initialValue, boolean allowsEmpty) {
351 this.initialValue = initialValue;
352 this.allowsEmpty = allowsEmpty;
356 protected class Validator {