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