]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/TextInputDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / dialogs / TextInputDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.document.ui.dialogs;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.jface.dialogs.Dialog;\r
18 import org.eclipse.jface.dialogs.IDialogConstants;\r
19 import org.eclipse.jface.dialogs.IInputValidator;\r
20 import org.eclipse.swt.SWT;\r
21 import org.eclipse.swt.widgets.Control;\r
22 import org.eclipse.swt.widgets.Display;\r
23 import org.eclipse.swt.widgets.Shell;\r
24 import org.eclipse.swt.widgets.Text;\r
25 \r
26 /**\r
27  * Base class for validated text input dialogs.\r
28  * \r
29  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
30  *\r
31  */\r
32 public abstract class TextInputDialog extends Dialog{\r
33 \r
34         // cache for all used validators\r
35         private Map<IInputValidator, String> validators = new HashMap<IInputValidator, String>();\r
36         \r
37         protected TextInputDialog(Shell parentShell) {\r
38                 super(parentShell);\r
39         }\r
40         \r
41         protected boolean validate(Text text, IInputValidator validator) {\r
42                 String err = null;\r
43                 if (validator != null)\r
44                         err = validator.isValid(text.getText());\r
45                 // add validator to the cache\r
46                 validators.put(validator, err);\r
47                 Control button = getButton(IDialogConstants.OK_ID);\r
48                 \r
49                 // disable ok button if any of the validators are reporting errors. \r
50                 if (button != null) {\r
51                         boolean valid = true;\r
52                         for (String s : validators.values())\r
53                                 if (s != null)\r
54                                         valid = false;\r
55                         button.setEnabled(valid);\r
56                 }\r
57                 if (err != null) {\r
58                         text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));\r
59                         text.setToolTipText(err);\r
60                         \r
61                         return false;\r
62                 } else {\r
63                         text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));\r
64                         text.setToolTipText(null);\r
65                         return true;\r
66                 }\r
67                 \r
68         }\r
69         \r
70         protected String updateName(String url, String name) {\r
71                 \r
72                 if (url.length() > 0 ) {\r
73                         if (name == null) {\r
74                                 return new String(url);\r
75                         } else if (Math.abs(url.length() - name.length()) <=  1) {\r
76                                 int common = Math.min(name.length(), url.length());\r
77                                 if (name.regionMatches(0, url, 0, common)) {\r
78                                         return new String(url);\r
79                                 }\r
80                         }\r
81                 }\r
82                 return null;\r
83                 \r
84         }\r
85 \r
86 }\r