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