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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
13 * Created on 13.10.2005
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.utils.ui.dialogs;
\r
18 import java.util.ArrayList;
\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
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
39 public class MultiLineInputDialog extends InputDialog {
\r
41 private String values[];
\r
43 private IInputValidator validators[];
\r
45 private String messages[];
\r
47 /** Error message text boxes */
\r
48 private Text errorMessageTexts[];
\r
51 private Text texts[];
\r
55 /** Error message of first line */
\r
56 // private String firstErrorMessage;
\r
58 private static String INDEX_KEY = "index";
\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
65 * @param parentShell
\r
67 * @param dialogTitle
\r
69 * @param dialogMessage
\r
70 * first line message
\r
71 * @param initialValue
\r
72 * first line initial value
\r
74 * first line validator
\r
76 * more lines, each line must have {message, initialvalue,
\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
83 if (moreLines.length % 3 != 0)
\r
84 throw new IllegalArgumentException("bad argument count");
\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
99 public String getValue(int index) {
\r
101 return this.getValue();
\r
102 return values[index - 1];
\r
105 protected Control createDialogArea(Composite parent) {
\r
106 Composite composite = (Composite) super.createDialogArea(parent);
\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
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
120 errorMessageTexts[index].setText(errorMessage == null ? "" : errorMessage);
\r
121 errorMessageTexts[index].getParent().update();
\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
128 getOkButton().setEnabled(ok);
\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
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
153 for (int i=0; i<count; i++) {
\r
154 texts[i].setText(values[i]);
\r
158 ArrayList<Control> tabs = new ArrayList<Control>();
\r
159 tabs.add(getText());
\r
160 for (Text t : texts)
\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
166 applyDialogFont(composite);
\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
181 getOkButton().setEnabled(ok);
\r
184 protected boolean allFieldsValid() {
\r
185 if (!(getValidator().isValid(getText().getText())==null)) return false;
\r
187 for (int i = 0; i < count; i++) {
\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
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
202 public void setErrorMessage(String errorMessage) {
\r
203 // this.firstErrorMessage = errorMessage;
\r
204 super.setErrorMessage(errorMessage);
\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
210 getOkButton().setEnabled(allFieldsValid());
\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
219 for (int i=0; i<values.length; i++)
\r
222 super.buttonPressed(buttonId);
\r