]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/MultiLineInputDialog.java
Fixed SWT font resource leak from MultiLineInputDialog.
[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             }
142             texts[i] = new Text(composite, SWT.SINGLE | SWT.BORDER);
143             texts[i].setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
144             texts[i].setData(INDEX_KEY, new Integer(i));
145             texts[i].addModifyListener(textModifyListener);
146             errorMessageTexts[i] = new Text(composite, SWT.READ_ONLY);
147             errorMessageTexts[i].setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
148             errorMessageTexts[i].setBackground(errorMessageTexts[i].getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
149             errorMessageTexts[i].setForeground(errorMessageTexts[i].getDisplay().getSystemColor(SWT.COLOR_RED));
150         }        
151         
152         for (int i=0; i<count; i++) {
153             texts[i].setText(values[i]);            
154         }
155
156         // Fix tab order
157         ArrayList<Control> tabs = new ArrayList<Control>();
158         tabs.add(getText());
159         for (Text t : texts)
160             tabs.add(t);
161         //tabs.add(getButton(IDialogConstants.CANCEL_ID));
162         //tabs.add(getButton(IDialogConstants.OK_ID));
163         composite.setTabList(tabs.toArray(new Control[0]));
164         
165         applyDialogFont(composite);
166         return composite;
167     }
168
169     @Override
170     protected void createButtonsForButtonBar(Composite parent) {
171         super.createButtonsForButtonBar(parent);
172         boolean ok = getValidator().isValid(getText().getText())==null;
173         for (int i = 0; i < count; i++) {
174             String txt = texts[i].getText();
175             if (txt==null) txt="";
176             String error = validators[i].isValid(txt);
177             boolean valid = error==null;
178             ok = ok & valid;
179         }
180         getOkButton().setEnabled(ok);
181     }
182     
183     protected boolean allFieldsValid() {
184         if (!(getValidator().isValid(getText().getText())==null)) return false;
185         
186         for (int i = 0; i < count; i++) {
187                 
188             // Eclipse 3.2R3 seems to initialize code in new order
189             // Some hubba here to omit initialization problems
190                 if (texts[i]==null) return true;
191                 
192             String txt = texts[i].getText();
193             if (txt==null) txt="";
194             boolean valid = validators[i].isValid(txt)==null;
195             if (!valid) return false;
196         }
197         return true;
198     }
199     
200     @Override
201     public void setErrorMessage(String errorMessage) {
202 //        this.firstErrorMessage = errorMessage;
203         super.setErrorMessage(errorMessage);
204         
205         // Eclipse 3.2R3 seems to initialize code in new order
206         // Some hubba here to omit initialization problems
207         if (getOkButton()==null) return;
208         
209         getOkButton().setEnabled(allFieldsValid());
210     }
211
212     @Override
213     protected void buttonPressed(int buttonId) {
214         if (buttonId == IDialogConstants.OK_ID) {
215             for (int i=0; i<values.length; i++)
216                 values[i] = texts[i].getText();
217         } else {
218             for (int i=0; i<values.length; i++)
219                 values[i] = null;
220         }
221         super.buttonPressed(buttonId);
222     }    
223     
224 }