--- /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
+package org.simantics.document.ui.dialogs;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.dialogs.IDialogConstants;\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+\r
+/**\r
+ * Base class for validated text input dialogs.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public abstract class TextInputDialog extends Dialog{\r
+\r
+ // cache for all used validators\r
+ private Map<IInputValidator, String> validators = new HashMap<IInputValidator, String>();\r
+ \r
+ protected TextInputDialog(Shell parentShell) {\r
+ super(parentShell);\r
+ }\r
+ \r
+ protected boolean validate(Text text, IInputValidator validator) {\r
+ String err = null;\r
+ if (validator != null)\r
+ err = validator.isValid(text.getText());\r
+ // add validator to the cache\r
+ validators.put(validator, err);\r
+ Control button = getButton(IDialogConstants.OK_ID);\r
+ \r
+ // disable ok button if any of the validators are reporting errors. \r
+ if (button != null) {\r
+ boolean valid = true;\r
+ for (String s : validators.values())\r
+ if (s != null)\r
+ valid = false;\r
+ button.setEnabled(valid);\r
+ }\r
+ if (err != null) {\r
+ text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));\r
+ text.setToolTipText(err);\r
+ \r
+ return false;\r
+ } else {\r
+ text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));\r
+ text.setToolTipText(null);\r
+ return true;\r
+ }\r
+ \r
+ }\r
+ \r
+ protected String updateName(String url, String name) {\r
+ \r
+ if (url.length() > 0 ) {\r
+ if (name == null) {\r
+ return new String(url);\r
+ } else if (Math.abs(url.length() - name.length()) <= 1) {\r
+ int common = Math.min(name.length(), url.length());\r
+ if (name.regionMatches(0, url, 0, common)) {\r
+ return new String(url);\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ \r
+ }\r
+\r
+}\r