1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
13 * Created on 13.10.2005
14 * @author Toni Kalajainen
16 package org.simantics.utils.ui.dialogs;
18 import java.util.ArrayList;
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;
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.
39 public class MultiLineInputDialog extends InputDialog {
41 private String values[];
43 private IInputValidator validators[];
45 private String messages[];
47 /** Error message text boxes */
48 private Text errorMessageTexts[];
55 /** Error message of first line */
56 // private String firstErrorMessage;
58 private static String INDEX_KEY = "index";
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.
69 * @param dialogMessage
72 * first line initial value
74 * first line validator
76 * more lines, each line must have {message, initialvalue,
79 public MultiLineInputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, IInputValidator validator, Object... moreLines) {
80 super(parentShell, dialogTitle, dialogMessage, initialValue, validator);
83 if (moreLines.length % 3 != 0)
84 throw new IllegalArgumentException("bad argument count");
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];
99 public String getValue(int index) {
101 return this.getValue();
102 return values[index - 1];
105 protected Control createDialogArea(Composite parent) {
106 Composite composite = (Composite) super.createDialogArea(parent);
108 // Text box modify listener
109 ModifyListener textModifyListener = new ModifyListener() {
110 public void modifyText(ModifyEvent e) {
111 if (getOkButton()==null) return;
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());
120 errorMessageTexts[index].setText(errorMessage == null ? "" : errorMessage);
121 errorMessageTexts[index].getParent().update();
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(""));
128 getOkButton().setEnabled(ok);
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);
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));
152 for (int i=0; i<count; i++) {
153 texts[i].setText(values[i]);
157 ArrayList<Control> tabs = new ArrayList<Control>();
161 //tabs.add(getButton(IDialogConstants.CANCEL_ID));
162 //tabs.add(getButton(IDialogConstants.OK_ID));
163 composite.setTabList(tabs.toArray(new Control[0]));
165 applyDialogFont(composite);
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;
180 getOkButton().setEnabled(ok);
183 protected boolean allFieldsValid() {
184 if (!(getValidator().isValid(getText().getText())==null)) return false;
186 for (int i = 0; i < count; i++) {
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;
192 String txt = texts[i].getText();
193 if (txt==null) txt="";
194 boolean valid = validators[i].isValid(txt)==null;
195 if (!valid) return false;
201 public void setErrorMessage(String errorMessage) {
202 // this.firstErrorMessage = errorMessage;
203 super.setErrorMessage(errorMessage);
205 // Eclipse 3.2R3 seems to initialize code in new order
206 // Some hubba here to omit initialization problems
207 if (getOkButton()==null) return;
209 getOkButton().setEnabled(allFieldsValid());
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();
218 for (int i=0; i<values.length; i++)
221 super.buttonPressed(buttonId);