refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / wizards / html / SomeItemInputDialog.java
1 /*
2  * $Id: SomeItemInputDialog.java,v 1.3 2006-10-21 23:18:43 pombredanne Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
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;
22
23 /**
24  * 
25  */
26 public class SomeItemInputDialog extends Dialog {
27
28         String dialogTitle;
29
30         String[] inputMessages;
31
32         IInputValidator[] validators;
33
34         Text[] texts;
35
36         Text error;
37
38         String[] errorMsgs;
39
40         String[] resultValues;
41
42         public SomeItemInputDialog(Shell parentShell, String dialogTitle,
43                         String[] inputMessages, IInputValidator[] validators) {
44                 super(parentShell);
45                 if (inputMessages.length != validators.length) {
46                         throw new IllegalArgumentException(
47                                         "Specify validator counts and input message count is not same.");
48                 }
49
50                 this.dialogTitle = dialogTitle;
51                 this.inputMessages = (String[]) inputMessages.clone();
52                 this.validators = (IInputValidator[]) validators.clone();
53                 this.errorMsgs = new String[validators.length];
54
55                 setShellStyle(SWT.RESIZE | getShellStyle());
56         }
57
58         protected void configureShell(Shell newShell) {
59                 super.configureShell(newShell);
60                 newShell.setText(dialogTitle);
61         }
62
63         protected Control createDialogArea(Composite parent) {
64                 Composite base = (Composite) super.createDialogArea(parent);
65                 GridLayout gl = new GridLayout(2, false);
66                 gl.marginWidth = 4;
67                 gl.marginHeight = 6;
68                 base.setLayout(gl);
69
70                 texts = new Text[inputMessages.length];
71                 for (int i = 0; i < inputMessages.length; i++) {
72                         new Label(base, SWT.NONE).setText(inputMessages[i] + ":");
73
74                         final int index = 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);
80                                 }
81                         });
82                         texts[i] = t;
83                 }
84
85                 error = new Text(base, SWT.READ_ONLY);
86                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
87                 gd.horizontalSpan = 2;
88                 error.setLayoutData(gd);
89
90                 return base;
91         }
92
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);
98                 }
99
100                 Button okButton = getButton(IDialogConstants.OK_ID);
101                 for (int i = 0; i < errorMsgs.length; i++) {
102                         String msg = errorMsgs[i];
103                         if (msg != null) {
104                                 error.setText(msg);
105                                 okButton.setEnabled(false);
106                                 return;
107                         }
108                 }
109                 error.setText("");
110                 okButton.setEnabled(true);
111         }
112
113         public String[] getValues() {
114                 return (String[]) resultValues.clone();
115         }
116
117         protected Point getInitialSize() {
118                 Point p = super.getInitialSize();
119                 return new Point(p.x * 2, (int) (p.y * 1.25));
120         }
121
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();
126                 }
127                 super.okPressed();
128         }
129
130 }