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