Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0dda3a091662d4332bef86996ed7ca238b991e63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
 * Copyright (c) 2011, 2014 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.controls.validator;


/**
 * Validator using regular expression.
 */
public class RegexValidator extends Validator {

	/**
	 * Attribute to check if the text to validate does not match the given regex.
	 */
	public static final int ATTR_NOT_REGEX = 2;
	// next attribute should start with 2^2

	// keys for info messages
	public static final String INFO_MISSING_VALUE = "RegexValidator_Information_MissingValue"; //$NON-NLS-1$

	// keys for error messages
	public static final String ERROR_INVALID_VALUE = "RegexValidator_Error_InvalidValue"; //$NON-NLS-1$

	// arguments
	private String regex;

	/**
	 * Constructor.
	 * @param attributes
	 */
	public RegexValidator(int attributes, String regex) {
		super(attributes);
		this.regex = regex;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.validator.Validator#isValid(java.lang.String)
	 */
	@Override
	public boolean isValid(String newText) {
		init();

		// info message when value is empty and mandatory
		if (newText == null || newText.trim().length() == 0) {
			if (isMandatory()) {
				setMessage(getMessageText(INFO_MISSING_VALUE), getMessageTextType(INFO_MISSING_VALUE, INFORMATION));
				return false;
			}
			return true;
		}

		boolean match = newText.matches(regex);
		if ((!isAttribute(ATTR_NOT_REGEX) && !match) || (isAttribute(ATTR_NOT_REGEX) && match)) {
			setMessage(getMessageText(ERROR_INVALID_VALUE), getMessageTextType(ERROR_INVALID_VALUE, ERROR));
			return getMessageType() != ERROR;
		}

		return true;
	}

	/**
	 * Returns the regular expression.
	 * @return
	 */
	protected String getRegularExpression() {
		return regex;
	}

	/**
	 * Set the regular expression.
	 * @param regex
	 */
	protected void setRegularExpression(String regex) {
		if (regex != null && regex.length() > 0) {
			this.regex = regex;
		}
		else {
			this.regex = ".*"; //$NON-NLS-1$
		}
	}
}

Back to the top