2 * $Id: SomeItemInputDialog.java,v 1.2 2005-05-06 00:57:33 stefanbjarni 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 {
29 String[] inputMessages;
31 IInputValidator[] validators;
36 String[] resultValues;
38 public SomeItemInputDialog(Shell parentShell, String dialogTitle, String[] inputMessages, IInputValidator[] validators) {
40 if(inputMessages.length != validators.length){
41 throw new IllegalArgumentException("Specify validator counts and input message count is not same.");
44 this.dialogTitle = dialogTitle;
45 this.inputMessages = (String[])inputMessages.clone();
46 this.validators = (IInputValidator[])validators.clone();
47 this.errorMsgs = new String[validators.length];
49 setShellStyle(SWT.RESIZE | getShellStyle());
52 protected void configureShell(Shell newShell) {
53 super.configureShell(newShell);
54 newShell.setText(dialogTitle);
57 protected Control createDialogArea(Composite parent) {
58 Composite base = (Composite)super.createDialogArea(parent);
59 GridLayout gl = new GridLayout(2, false);
64 texts = new Text[inputMessages.length];
65 for(int i=0; i<inputMessages.length; i++){
66 new Label(base, SWT.NONE).setText(inputMessages[i] + ":");
69 Text t = new Text(base, SWT.BORDER);
70 t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
71 t.addModifyListener(new ModifyListener() {
72 public void modifyText(ModifyEvent e) {
73 refreshValidator(index);
79 error = new Text(base, SWT.READ_ONLY);
80 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
81 gd.horizontalSpan = 2;
82 error.setLayoutData(gd);
87 void refreshValidator(int index){
88 String data = texts[index].getText();
89 IInputValidator validator = validators[index];
90 if( validator != null){
91 errorMsgs[index] = validator.isValid(data);
94 Button okButton = getButton(IDialogConstants.OK_ID);
95 for(int i=0; i<errorMsgs.length; i++){
96 String msg = errorMsgs[i];
99 okButton.setEnabled(false);
104 okButton.setEnabled(true);
107 public String[] getValues(){
108 return (String[]) resultValues.clone();
111 protected Point getInitialSize() {
112 Point p = super.getInitialSize();
113 return new Point( p.x * 2, (int)(p.y * 1.25) );
116 protected void okPressed() {
117 resultValues = new String[texts.length];
118 for (int i = 0; i < texts.length; i++) {
119 resultValues[i] = texts[i].getText();