inital plugin from webtools project
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / SomeItemInputDialog.java
1 /*
2  * $Id: SomeItemInputDialog.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
7 import org.eclipse.jface.dialogs.*;
8 import org.eclipse.jface.dialogs.Dialog;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.*;
11 import org.eclipse.swt.graphics.Point;
12 import org.eclipse.swt.layout.*;
13 import org.eclipse.swt.widgets.*;
14
15 /**
16  * 
17  */
18 public class SomeItemInputDialog extends Dialog {
19
20         String dialogTitle;
21         String[] inputMessages;
22         
23         IInputValidator[] validators;
24         Text[] texts;
25         Text error;
26         
27         String[] errorMsgs;
28         String[] resultValues;
29
30         public SomeItemInputDialog(Shell parentShell, String dialogTitle, String[] inputMessages, IInputValidator[] validators) {
31                 super(parentShell);
32                 if(inputMessages.length != validators.length){
33                         throw new IllegalArgumentException("Specify validator counts and input message count is not same.");
34                 }
35                 
36                 this.dialogTitle = dialogTitle;
37                 this.inputMessages = (String[])inputMessages.clone();
38                 this.validators = (IInputValidator[])validators.clone();
39                 this.errorMsgs = new String[validators.length];
40                 
41                 setShellStyle(SWT.RESIZE | getShellStyle());
42         }
43
44         protected void configureShell(Shell newShell) {
45                 super.configureShell(newShell);
46                 newShell.setText(dialogTitle);
47         }
48
49         protected Control createDialogArea(Composite parent) {
50                 Composite base = (Composite)super.createDialogArea(parent);
51                 GridLayout gl = new GridLayout(2, false);
52                 gl.marginWidth = 4;
53                 gl.marginHeight = 6;
54                 base.setLayout(gl);
55                 
56                 texts = new Text[inputMessages.length];
57                 for(int i=0; i<inputMessages.length; i++){
58                         new Label(base, SWT.NONE).setText(inputMessages[i] + ":");
59                         
60                         final int index = i;
61                         Text t = new Text(base, SWT.BORDER);
62                         t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
63                         t.addModifyListener(new ModifyListener() {
64                                 public void modifyText(ModifyEvent e) {
65                                         refreshValidator(index);
66                                 }
67                         });
68                         texts[i] = t;
69                 }
70                 
71                 error = new Text(base, SWT.READ_ONLY);
72                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
73                 gd.horizontalSpan = 2;
74                 error.setLayoutData(gd);
75                 
76                 return base;
77         }
78         
79         void refreshValidator(int index){
80                 String data = texts[index].getText();
81                 IInputValidator validator = validators[index];
82                 if( validator != null){
83                         errorMsgs[index] = validator.isValid(data);
84                 }
85                 
86                 Button okButton = getButton(IDialogConstants.OK_ID);
87                 for(int i=0; i<errorMsgs.length; i++){
88                         String msg = errorMsgs[i];
89                         if(msg != null){
90                                 error.setText(msg);
91                                 okButton.setEnabled(false);
92                                 return;
93                         }
94                 }
95                 error.setText("");
96                 okButton.setEnabled(true);
97         }
98
99         public String[] getValues(){
100                 return (String[]) resultValues.clone();
101         }
102
103         protected Point getInitialSize() {
104                 Point p = super.getInitialSize();
105                 return new Point( p.x * 2, (int)(p.y * 1.25) );
106         }
107
108         protected void okPressed() {
109                 resultValues = new String[texts.length];
110                 for (int i = 0; i < texts.length; i++) {
111                         resultValues[i] = texts[i].getText();
112                 }
113                 super.okPressed();
114         }
115
116 }