plugins/org.symbian.tools.tmw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/RegexpValidator.java
author Eugene Ostroukhov <eugeneo@symbian.org>
Thu, 19 Aug 2010 17:48:04 -0700
changeset 470 d4809db37847
parent 463 org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/RegexpValidator.java@aea4c83725d8
child 485 df4f55e8569e
permissions -rw-r--r--
Changed repository layout and renamed project files. This revision is untested and may not run.

package org.symbian.tools.tmw.internal.ui.wizard;

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.symbian.tools.tmw.ui.TMWCoreUI;

public class RegexpValidator implements IValidator {
    private final String errorMessage;
    private final Pattern pattern;
    private final boolean match;

    public RegexpValidator(String pattern, String errorMessage, boolean match) {
        this.errorMessage = errorMessage;
        this.match = match;
        this.pattern = Pattern.compile(pattern);
    }

    public IStatus validate(Object value) {
        String string = value.toString();
        Matcher matcher = pattern.matcher(string);
        if (match && !matcher.matches()) {
            return new Status(IStatus.ERROR, TMWCoreUI.PLUGIN_ID, MessageFormat.format(errorMessage, string));
        } else if (!match && matcher.find()) {
            return new Status(IStatus.ERROR, TMWCoreUI.PLUGIN_ID, MessageFormat.format(errorMessage,
                    string.substring(matcher.start(), matcher.end())));
        } else {
            return Status.OK_STATUS;
        }
    }

}