2 * $Id: SomeItemInputDialog.java,v 1.3 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import org.eclipse.jface.dialogs.Dialog;
8 import org.eclipse.jface.dialogs.IDialogConstants;
9 import org.eclipse.jface.dialogs.IInputValidator;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.ModifyEvent;
12 import org.eclipse.swt.events.ModifyListener;
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.swt.widgets.Text;
26 public class SomeItemInputDialog extends Dialog {
30 String[] inputMessages;
32 IInputValidator[] validators;
40 String[] resultValues;
42 public SomeItemInputDialog(Shell parentShell, String dialogTitle,
43 String[] inputMessages, IInputValidator[] validators) {
45 if (inputMessages.length != validators.length) {
46 throw new IllegalArgumentException(
47 "Specify validator counts and input message count is not same.");
50 this.dialogTitle = dialogTitle;
51 this.inputMessages = (String[]) inputMessages.clone();
52 this.validators = (IInputValidator[]) validators.clone();
53 this.errorMsgs = new String[validators.length];
55 setShellStyle(SWT.RESIZE | getShellStyle());
58 protected void configureShell(Shell newShell) {
59 super.configureShell(newShell);
60 newShell.setText(dialogTitle);
63 protected Control createDialogArea(Composite parent) {
64 Composite base = (Composite) super.createDialogArea(parent);
65 GridLayout gl = new GridLayout(2, false);
70 texts = new Text[inputMessages.length];
71 for (int i = 0; i < inputMessages.length; i++) {
72 new Label(base, SWT.NONE).setText(inputMessages[i] + ":");
75 Text t = new Text(base, SWT.BORDER);
76 t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
77 t.addModifyListener(new ModifyListener() {
78 public void modifyText(ModifyEvent e) {
79 refreshValidator(index);
85 error = new Text(base, SWT.READ_ONLY);
86 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
87 gd.horizontalSpan = 2;
88 error.setLayoutData(gd);
93 void refreshValidator(int index) {
94 String data = texts[index].getText();
95 IInputValidator validator = validators[index];
96 if (validator != null) {
97 errorMsgs[index] = validator.isValid(data);
100 Button okButton = getButton(IDialogConstants.OK_ID);
101 for (int i = 0; i < errorMsgs.length; i++) {
102 String msg = errorMsgs[i];
105 okButton.setEnabled(false);
110 okButton.setEnabled(true);
113 public String[] getValues() {
114 return (String[]) resultValues.clone();
117 protected Point getInitialSize() {
118 Point p = super.getInitialSize();
119 return new Point(p.x * 2, (int) (p.y * 1.25));
122 protected void okPressed() {
123 resultValues = new String[texts.length];
124 for (int i = 0; i < texts.length; i++) {
125 resultValues[i] = texts[i].getText();