--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * Created on 13.10.2005\r
+ * @author Toni Kalajainen \r
+ */\r
+package org.simantics.utils.ui.dialogs;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.jface.dialogs.IDialogConstants;\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.eclipse.jface.dialogs.InputDialog;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+\r
+/**\r
+ * MultiLineInputDialog is a input dialog that can contain multiple input lines.\r
+ * The minimum number of input lines is one. Each line must have message,\r
+ * initial value and validator.\r
+ * \r
+ */\r
+public class MultiLineInputDialog extends InputDialog {\r
+\r
+ private String values[];\r
+\r
+ private IInputValidator validators[];\r
+\r
+ private String messages[];\r
+\r
+ /** Error message text boxes */\r
+ private Text errorMessageTexts[];\r
+\r
+ /** Text boxes */\r
+ private Text texts[];\r
+\r
+ private int count;\r
+ \r
+ /** Error message of first line */\r
+// private String firstErrorMessage;\r
+\r
+ private static String INDEX_KEY = "index";\r
+\r
+ /**\r
+ * MultiLineInputDialog is a input dialog that can contain multiple input\r
+ * lines. The minimum number of input lines is one. Each line must have\r
+ * message, initial value and validator.\r
+ * \r
+ * @param parentShell\r
+ * parent shell\r
+ * @param dialogTitle\r
+ * window title\r
+ * @param dialogMessage\r
+ * first line message\r
+ * @param initialValue\r
+ * first line initial value\r
+ * @param validator\r
+ * first line validator\r
+ * @param moreLines\r
+ * more lines, each line must have {message, initialvalue,\r
+ * validator}\r
+ */\r
+ public MultiLineInputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, IInputValidator validator, Object... moreLines) {\r
+ super(parentShell, dialogTitle, dialogMessage, initialValue, validator);\r
+\r
+ // Parse more lines\r
+ if (moreLines.length % 3 != 0)\r
+ throw new IllegalArgumentException("bad argument count");\r
+\r
+ count = moreLines.length / 3;\r
+ values = new String[count];\r
+ messages = new String[count];\r
+ validators = new IInputValidator[count];\r
+ errorMessageTexts = new Text[count];\r
+ texts = new Text[count];\r
+ for (int i = 0; i < count; i++) {\r
+ values[i] = (String) moreLines[i * 3 + 1];\r
+ messages[i] = (String) moreLines[i * 3];\r
+ validators[i] = (IInputValidator) moreLines[i * 3 + 2];\r
+ }\r
+ }\r
+\r
+ public String getValue(int index) {\r
+ if (index == 0)\r
+ return this.getValue();\r
+ return values[index - 1];\r
+ }\r
+ \r
+ protected Control createDialogArea(Composite parent) {\r
+ Composite composite = (Composite) super.createDialogArea(parent);\r
+\r
+ // Text box modify listener\r
+ ModifyListener textModifyListener = new ModifyListener() {\r
+ public void modifyText(ModifyEvent e) {\r
+ if (getOkButton()==null) return;\r
+ \r
+ int index = (Integer) e.widget.getData(INDEX_KEY);\r
+ Text text = (Text) e.widget;\r
+ IInputValidator validator = validators[index];\r
+ String errorMessage = null;\r
+ if (validator != null)\r
+ errorMessage = validator.isValid(text.getText());\r
+\r
+ errorMessageTexts[index].setText(errorMessage == null ? "" : errorMessage);\r
+ errorMessageTexts[index].getParent().update();\r
+\r
+ \r
+ boolean ok = MultiLineInputDialog.this.getValidator().isValid(MultiLineInputDialog.this.getText().getText())==null;\r
+ for (int i = 0; i < count; i++)\r
+ ok = ok & (errorMessageTexts[i].getText().equals(""));\r
+ \r
+ getOkButton().setEnabled(ok); \r
+ }\r
+ };\r
+\r
+ // Add some more lines, text boxes\r
+ for (int i = 0; i < count; i++) {\r
+ String message = messages[i];\r
+ if (message != null) {\r
+ Label label = new Label(composite, SWT.WRAP);\r
+ label.setText(message);\r
+ GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);\r
+ data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
+ label.setLayoutData(data);\r
+ label.setFont(parent.getFont());\r
+ }\r
+ texts[i] = new Text(composite, SWT.SINGLE | SWT.BORDER);\r
+ texts[i].setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
+ texts[i].setData(INDEX_KEY, new Integer(i));\r
+ texts[i].addModifyListener(textModifyListener);\r
+ errorMessageTexts[i] = new Text(composite, SWT.READ_ONLY);\r
+ errorMessageTexts[i].setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
+ errorMessageTexts[i].setBackground(errorMessageTexts[i].getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));\r
+ errorMessageTexts[i].setForeground(errorMessageTexts[i].getDisplay().getSystemColor(SWT.COLOR_RED));\r
+ } \r
+ \r
+ for (int i=0; i<count; i++) {\r
+ texts[i].setText(values[i]); \r
+ }\r
+\r
+ // Fix tab order\r
+ ArrayList<Control> tabs = new ArrayList<Control>();\r
+ tabs.add(getText());\r
+ for (Text t : texts)\r
+ tabs.add(t);\r
+ //tabs.add(getButton(IDialogConstants.CANCEL_ID));\r
+ //tabs.add(getButton(IDialogConstants.OK_ID));\r
+ composite.setTabList(tabs.toArray(new Control[0]));\r
+ \r
+ applyDialogFont(composite);\r
+ return composite;\r
+ }\r
+\r
+ @Override\r
+ protected void createButtonsForButtonBar(Composite parent) {\r
+ super.createButtonsForButtonBar(parent);\r
+ boolean ok = getValidator().isValid(getText().getText())==null;\r
+ for (int i = 0; i < count; i++) {\r
+ String txt = texts[i].getText();\r
+ if (txt==null) txt="";\r
+ String error = validators[i].isValid(txt);\r
+ boolean valid = error==null;\r
+ ok = ok & valid;\r
+ }\r
+ getOkButton().setEnabled(ok);\r
+ }\r
+ \r
+ protected boolean allFieldsValid() {\r
+ if (!(getValidator().isValid(getText().getText())==null)) return false;\r
+ \r
+ for (int i = 0; i < count; i++) {\r
+ \r
+ // Eclipse 3.2R3 seems to initialize code in new order\r
+ // Some hubba here to omit initialization problems\r
+ if (texts[i]==null) return true;\r
+ \r
+ String txt = texts[i].getText();\r
+ if (txt==null) txt="";\r
+ boolean valid = validators[i].isValid(txt)==null;\r
+ if (!valid) return false;\r
+ }\r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public void setErrorMessage(String errorMessage) {\r
+// this.firstErrorMessage = errorMessage;\r
+ super.setErrorMessage(errorMessage);\r
+ \r
+ // Eclipse 3.2R3 seems to initialize code in new order\r
+ // Some hubba here to omit initialization problems\r
+ if (getOkButton()==null) return;\r
+ \r
+ getOkButton().setEnabled(allFieldsValid());\r
+ }\r
+\r
+ @Override\r
+ protected void buttonPressed(int buttonId) {\r
+ if (buttonId == IDialogConstants.OK_ID) {\r
+ for (int i=0; i<values.length; i++)\r
+ values[i] = texts[i].getText();\r
+ } else {\r
+ for (int i=0; i<values.length; i++)\r
+ values[i] = null;\r
+ }\r
+ super.buttonPressed(buttonId);\r
+ } \r
+ \r
+}\r