*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / SomeItemInputDialog.java
1 /*
2  * $Id: SomeItemInputDialog.java,v 1.2 2005-05-06 00:57:33 stefanbjarni 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         String[] inputMessages;
30         
31         IInputValidator[] validators;
32         Text[] texts;
33         Text error;
34         
35         String[] errorMsgs;
36         String[] resultValues;
37
38         public SomeItemInputDialog(Shell parentShell, String dialogTitle, String[] inputMessages, IInputValidator[] validators) {
39                 super(parentShell);
40                 if(inputMessages.length != validators.length){
41                         throw new IllegalArgumentException("Specify validator counts and input message count is not same.");
42                 }
43                 
44                 this.dialogTitle = dialogTitle;
45                 this.inputMessages = (String[])inputMessages.clone();
46                 this.validators = (IInputValidator[])validators.clone();
47                 this.errorMsgs = new String[validators.length];
48                 
49                 setShellStyle(SWT.RESIZE | getShellStyle());
50         }
51
52         protected void configureShell(Shell newShell) {
53                 super.configureShell(newShell);
54                 newShell.setText(dialogTitle);
55         }
56
57         protected Control createDialogArea(Composite parent) {
58                 Composite base = (Composite)super.createDialogArea(parent);
59                 GridLayout gl = new GridLayout(2, false);
60                 gl.marginWidth = 4;
61                 gl.marginHeight = 6;
62                 base.setLayout(gl);
63                 
64                 texts = new Text[inputMessages.length];
65                 for(int i=0; i<inputMessages.length; i++){
66                         new Label(base, SWT.NONE).setText(inputMessages[i] + ":");
67                         
68                         final int index = 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);
74                                 }
75                         });
76                         texts[i] = t;
77                 }
78                 
79                 error = new Text(base, SWT.READ_ONLY);
80                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
81                 gd.horizontalSpan = 2;
82                 error.setLayoutData(gd);
83                 
84                 return base;
85         }
86         
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);
92                 }
93                 
94                 Button okButton = getButton(IDialogConstants.OK_ID);
95                 for(int i=0; i<errorMsgs.length; i++){
96                         String msg = errorMsgs[i];
97                         if(msg != null){
98                                 error.setText(msg);
99                                 okButton.setEnabled(false);
100                                 return;
101                         }
102                 }
103                 error.setText("");
104                 okButton.setEnabled(true);
105         }
106
107         public String[] getValues(){
108                 return (String[]) resultValues.clone();
109         }
110
111         protected Point getInitialSize() {
112                 Point p = super.getInitialSize();
113                 return new Point( p.x * 2, (int)(p.y * 1.25) );
114         }
115
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();
120                 }
121                 super.okPressed();
122         }
123
124 }