]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/MultiLineInputDialog.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / MultiLineInputDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  * Created on 13.10.2005\r
14  * @author Toni Kalajainen \r
15  */\r
16 package org.simantics.utils.ui.dialogs;\r
17 \r
18 import java.util.ArrayList;\r
19 \r
20 import org.eclipse.jface.dialogs.IDialogConstants;\r
21 import org.eclipse.jface.dialogs.IInputValidator;\r
22 import org.eclipse.jface.dialogs.InputDialog;\r
23 import org.eclipse.swt.SWT;\r
24 import org.eclipse.swt.events.ModifyEvent;\r
25 import org.eclipse.swt.events.ModifyListener;\r
26 import org.eclipse.swt.layout.GridData;\r
27 import org.eclipse.swt.widgets.Composite;\r
28 import org.eclipse.swt.widgets.Control;\r
29 import org.eclipse.swt.widgets.Label;\r
30 import org.eclipse.swt.widgets.Shell;\r
31 import org.eclipse.swt.widgets.Text;\r
32 \r
33 /**\r
34  * MultiLineInputDialog is a input dialog that can contain multiple input lines.\r
35  * The minimum number of input lines is one. Each line must have message,\r
36  * initial value and validator.\r
37  * \r
38  */\r
39 public class MultiLineInputDialog extends InputDialog {\r
40 \r
41     private String values[];\r
42 \r
43     private IInputValidator validators[];\r
44 \r
45     private String messages[];\r
46 \r
47     /** Error message text boxes */\r
48     private Text errorMessageTexts[];\r
49 \r
50     /** Text boxes */\r
51     private Text texts[];\r
52 \r
53     private int count;\r
54     \r
55     /** Error message of first line */\r
56 //    private String firstErrorMessage;\r
57 \r
58     private static String INDEX_KEY = "index";\r
59 \r
60     /**\r
61      * MultiLineInputDialog is a input dialog that can contain multiple input\r
62      * lines. The minimum number of input lines is one. Each line must have\r
63      * message, initial value and validator.\r
64      * \r
65      * @param parentShell\r
66      *            parent shell\r
67      * @param dialogTitle\r
68      *            window title\r
69      * @param dialogMessage\r
70      *            first line message\r
71      * @param initialValue\r
72      *            first line initial value\r
73      * @param validator\r
74      *            first line validator\r
75      * @param moreLines\r
76      *            more lines, each line must have {message, initialvalue,\r
77      *            validator}\r
78      */\r
79     public MultiLineInputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, IInputValidator validator, Object... moreLines) {\r
80         super(parentShell, dialogTitle, dialogMessage, initialValue, validator);\r
81 \r
82         // Parse more lines\r
83         if (moreLines.length % 3 != 0)\r
84             throw new IllegalArgumentException("bad argument count");\r
85 \r
86         count = moreLines.length / 3;\r
87         values = new String[count];\r
88         messages = new String[count];\r
89         validators = new IInputValidator[count];\r
90         errorMessageTexts = new Text[count];\r
91         texts = new Text[count];\r
92         for (int i = 0; i < count; i++) {\r
93             values[i] = (String) moreLines[i * 3 + 1];\r
94             messages[i] = (String) moreLines[i * 3];\r
95             validators[i] = (IInputValidator) moreLines[i * 3 + 2];\r
96         }\r
97     }\r
98 \r
99     public String getValue(int index) {\r
100         if (index == 0)\r
101             return this.getValue();\r
102         return values[index - 1];\r
103     }\r
104     \r
105     protected Control createDialogArea(Composite parent) {\r
106         Composite composite = (Composite) super.createDialogArea(parent);\r
107 \r
108         // Text box modify listener\r
109         ModifyListener textModifyListener = new ModifyListener() {\r
110             public void modifyText(ModifyEvent e) {\r
111                 if (getOkButton()==null) return;\r
112                 \r
113                 int index = (Integer) e.widget.getData(INDEX_KEY);\r
114                 Text text = (Text) e.widget;\r
115                 IInputValidator validator = validators[index];\r
116                 String errorMessage = null;\r
117                 if (validator != null)\r
118                     errorMessage = validator.isValid(text.getText());\r
119 \r
120                 errorMessageTexts[index].setText(errorMessage == null ? "" : errorMessage);\r
121                 errorMessageTexts[index].getParent().update();\r
122 \r
123                                 \r
124                 boolean ok = MultiLineInputDialog.this.getValidator().isValid(MultiLineInputDialog.this.getText().getText())==null;\r
125                 for (int i = 0; i < count; i++)\r
126                     ok = ok & (errorMessageTexts[i].getText().equals(""));\r
127                     \r
128                 getOkButton().setEnabled(ok);     \r
129             }\r
130         };\r
131 \r
132         // Add some more lines, text boxes\r
133         for (int i = 0; i < count; i++) {\r
134             String message = messages[i];\r
135             if (message != null) {\r
136                 Label label = new Label(composite, SWT.WRAP);\r
137                 label.setText(message);\r
138                 GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);\r
139                 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
140                 label.setLayoutData(data);\r
141                 label.setFont(parent.getFont());\r
142             }\r
143             texts[i] = new Text(composite, SWT.SINGLE | SWT.BORDER);\r
144             texts[i].setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
145             texts[i].setData(INDEX_KEY, new Integer(i));\r
146             texts[i].addModifyListener(textModifyListener);\r
147             errorMessageTexts[i] = new Text(composite, SWT.READ_ONLY);\r
148             errorMessageTexts[i].setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
149             errorMessageTexts[i].setBackground(errorMessageTexts[i].getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));\r
150             errorMessageTexts[i].setForeground(errorMessageTexts[i].getDisplay().getSystemColor(SWT.COLOR_RED));\r
151         }        \r
152         \r
153         for (int i=0; i<count; i++) {\r
154             texts[i].setText(values[i]);            \r
155         }\r
156 \r
157         // Fix tab order\r
158         ArrayList<Control> tabs = new ArrayList<Control>();\r
159         tabs.add(getText());\r
160         for (Text t : texts)\r
161             tabs.add(t);\r
162         //tabs.add(getButton(IDialogConstants.CANCEL_ID));\r
163         //tabs.add(getButton(IDialogConstants.OK_ID));\r
164         composite.setTabList(tabs.toArray(new Control[0]));\r
165         \r
166         applyDialogFont(composite);\r
167         return composite;\r
168     }\r
169 \r
170     @Override\r
171     protected void createButtonsForButtonBar(Composite parent) {\r
172         super.createButtonsForButtonBar(parent);\r
173         boolean ok = getValidator().isValid(getText().getText())==null;\r
174         for (int i = 0; i < count; i++) {\r
175             String txt = texts[i].getText();\r
176             if (txt==null) txt="";\r
177             String error = validators[i].isValid(txt);\r
178             boolean valid = error==null;\r
179             ok = ok & valid;\r
180         }\r
181         getOkButton().setEnabled(ok);\r
182     }\r
183     \r
184     protected boolean allFieldsValid() {\r
185         if (!(getValidator().isValid(getText().getText())==null)) return false;\r
186         \r
187         for (int i = 0; i < count; i++) {\r
188                 \r
189             // Eclipse 3.2R3 seems to initialize code in new order\r
190             // Some hubba here to omit initialization problems\r
191                 if (texts[i]==null) return true;\r
192                 \r
193             String txt = texts[i].getText();\r
194             if (txt==null) txt="";\r
195             boolean valid = validators[i].isValid(txt)==null;\r
196             if (!valid) return false;\r
197         }\r
198         return true;\r
199     }\r
200     \r
201     @Override\r
202     public void setErrorMessage(String errorMessage) {\r
203 //        this.firstErrorMessage = errorMessage;\r
204         super.setErrorMessage(errorMessage);\r
205         \r
206         // Eclipse 3.2R3 seems to initialize code in new order\r
207         // Some hubba here to omit initialization problems\r
208         if (getOkButton()==null) return;\r
209         \r
210         getOkButton().setEnabled(allFieldsValid());\r
211     }\r
212 \r
213     @Override\r
214     protected void buttonPressed(int buttonId) {\r
215         if (buttonId == IDialogConstants.OK_ID) {\r
216             for (int i=0; i<values.length; i++)\r
217                 values[i] = texts[i].getText();\r
218         } else {\r
219             for (int i=0; i<values.length; i++)\r
220                 values[i] = null;\r
221         }\r
222         super.buttonPressed(buttonId);\r
223     }    \r
224     \r
225 }\r