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