1 11 package org.eclipse.debug.internal.ui; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 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 FIELD_NAME = "FIELD_NAME"; 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 fieldList = new ArrayList (); 47 protected List controlList = new ArrayList (); 48 protected List validators = new ArrayList (); 49 protected Map valueMap = new HashMap (); 50 51 private String title; 52 53 54 55 public MultipleInputDialog(Shell shell, String title) { 56 super(shell); 57 this.title = title; 58 setShellStyle(getShellStyle() | SWT.RESIZE); 59 } 60 61 64 protected void configureShell(Shell shell) { 65 super.configureShell(shell); 66 if (title != null) { 67 shell.setText(title); 68 } 69 70 } 71 72 75 protected Control createButtonBar(Composite parent) { 76 Control bar = super.createButtonBar(parent); 77 validateFields(); 78 return bar; 79 } 80 81 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 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; Dialog.applyDialogFont(container); 111 return container; 112 } 113 114 public void addBrowseField(String labelText, String initialValue, boolean allowsEmpty) { 115 fieldList.add(new FieldSummary(BROWSE, labelText, initialValue, allowsEmpty)); 116 } 117 public void addTextField(String labelText, String initialValue, boolean allowsEmpty) { 118 fieldList.add(new FieldSummary(TEXT, labelText, initialValue, allowsEmpty)); 119 } 120 public void addVariablesField(String labelText, String initialValue, boolean allowsEmpty) { 121 fieldList.add(new FieldSummary(VARIABLE, labelText, initialValue, allowsEmpty)); 122 } 123 124 protected void createTextField(String labelText, String 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 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(""); } 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 labelText, String 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 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(""); } 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 currentWorkingDir = text.getText(); 201 if (!currentWorkingDir.trim().equals("")) { File path = new File (currentWorkingDir); 203 if (path.exists()) { 204 dialog.setFilterPath(currentWorkingDir); 205 } 206 } 207 208 String 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 labelText, String 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 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(""); } 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 variable = dialog.getVariableExpression(); 266 if (variable != null) { 267 text.insert(variable); 268 } 269 } 270 } 271 }); 272 273 controlList.add(text); 274 275 } 276 277 280 protected void okPressed() { 281 for (Iterator 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 295 public int open() { 296 applyDialogFont(panel); 297 return super.open(); 298 } 299 300 public Object getValue(String key) { 301 return valueMap.get(key); 302 } 303 304 public String getStringValue(String key) { 305 return (String ) getValue(key); 306 } 307 308 public void validateFields() { 309 for(Iterator 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 name; 322 String initialValue; 323 boolean allowsEmpty; 324 325 public FieldSummary(int type, String name, String 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 |