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