X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2Fdialogs%2FTextInputDialog.java;fp=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2Fdialogs%2FTextInputDialog.java;h=c55d5190d9760035097ed7d55be85ae27ebb0f23;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=7e37a1185124af3c87321a951bc82e293a8fc9b0;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/TextInputDialog.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/TextInputDialog.java index 7e37a1185..c55d5190d 100644 --- a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/TextInputDialog.java +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/TextInputDialog.java @@ -1,86 +1,86 @@ -/******************************************************************************* - * 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 - * - */ -public abstract class TextInputDialog extends Dialog{ - - // cache for all used validators - private Map validators = new HashMap(); - - 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; - - } - -} +/******************************************************************************* + * 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 + * + */ +public abstract class TextInputDialog extends Dialog{ + + // cache for all used validators + private Map validators = new HashMap(); + + 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; + + } + +}