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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document.ui.dialogs;
\r
14 import java.util.HashMap;
\r
15 import java.util.Map;
\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
27 * Base class for validated text input dialogs.
\r
29 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
32 public abstract class TextInputDialog extends Dialog{
\r
34 // cache for all used validators
\r
35 private Map<IInputValidator, String> validators = new HashMap<IInputValidator, String>();
\r
37 protected TextInputDialog(Shell parentShell) {
\r
41 protected boolean validate(Text text, IInputValidator validator) {
\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
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
55 button.setEnabled(valid);
\r
58 text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
\r
59 text.setToolTipText(err);
\r
63 text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
\r
64 text.setToolTipText(null);
\r
70 protected String updateName(String url, String name) {
\r
72 if (url.length() > 0 ) {
\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