plugins/org.symbian.tools.tmw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/RegexpValidator.java
changeset 470 d4809db37847
parent 463 aea4c83725d8
child 485 df4f55e8569e
equal deleted inserted replaced
469:4d198a32ac7d 470:d4809db37847
       
     1 package org.symbian.tools.tmw.internal.ui.wizard;
       
     2 
       
     3 import java.text.MessageFormat;
       
     4 import java.util.regex.Matcher;
       
     5 import java.util.regex.Pattern;
       
     6 
       
     7 import org.eclipse.core.databinding.validation.IValidator;
       
     8 import org.eclipse.core.runtime.IStatus;
       
     9 import org.eclipse.core.runtime.Status;
       
    10 import org.symbian.tools.tmw.ui.TMWCoreUI;
       
    11 
       
    12 public class RegexpValidator implements IValidator {
       
    13     private final String errorMessage;
       
    14     private final Pattern pattern;
       
    15     private final boolean match;
       
    16 
       
    17     public RegexpValidator(String pattern, String errorMessage, boolean match) {
       
    18         this.errorMessage = errorMessage;
       
    19         this.match = match;
       
    20         this.pattern = Pattern.compile(pattern);
       
    21     }
       
    22 
       
    23     public IStatus validate(Object value) {
       
    24         String string = value.toString();
       
    25         Matcher matcher = pattern.matcher(string);
       
    26         if (match && !matcher.matches()) {
       
    27             return new Status(IStatus.ERROR, TMWCoreUI.PLUGIN_ID, MessageFormat.format(errorMessage, string));
       
    28         } else if (!match && matcher.find()) {
       
    29             return new Status(IStatus.ERROR, TMWCoreUI.PLUGIN_ID, MessageFormat.format(errorMessage,
       
    30                     string.substring(matcher.start(), matcher.end())));
       
    31         } else {
       
    32             return Status.OK_STATUS;
       
    33         }
       
    34     }
       
    35 
       
    36 }