Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 441e326f9a49e9de7bb74458f48a900695894a8c (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
88
89
90
91
92
93
94
95
96
97
98
99
100
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Modification to match IValidator
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.validator;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.papyrus.infra.widgets.messages.Messages;

/**
 * Validator for the UnlimitedNaturalEditor. It accepts "-1", "*" and all integer >=0
 */
public class UnlimitedNaturalValidator extends AbstractValidator {


	public static final String INFINITE_STAR = "*"; //$NON-NLS-1$

	public static final String INFINITE_MINUS_ONE = "-1"; //$NON-NLS-1$


	/**
	 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
	 *
	 * @param newText
	 * @return <code>null</code> if the newText is valid an error message when newText is
	 *         invalid
	 */
	public String isValid(String newText) {
		if(INFINITE_STAR.equals(newText) || INFINITE_MINUS_ONE.equals(newText)) {
			return null;
		}
		boolean isValid = true;
		try {
			Integer myUnlimitedNatural = Integer.valueOf(newText);
			if(myUnlimitedNatural < -1) {
				isValid = false;
			}
		} catch (NumberFormatException e) {
			isValid = false;
		}

		if(!isValid) {
			return Messages.UnlimitedNaturalInputValidator_NotAnUnlimitedNaturalMessage;
		}
		return null;
	}

	/**
	 *
	 * @param newValue
	 * @return {@link Status#OK_STATUS} if the newValue is valid and {@link IStatus#ERROR} when newValue is
	 *         invalid
	 */
	@Override
	public IStatus validate(Object newValue) {
		if(newValue instanceof Integer) {
			int value = (Integer)newValue;
			if(value == -1 || value >= 0) {
				return Status.OK_STATUS;
			}
		}

		if(newValue instanceof String) {
			String newText = (String)newValue;

			if(INFINITE_STAR.equals(newText) || INFINITE_MINUS_ONE.equals(newText)) {
				return Status.OK_STATUS;
			}

			boolean isValid = true;
			try {
				Integer myUnlimitedNatural = Integer.parseInt(newText);
				if(myUnlimitedNatural < -1) {
					isValid = false;
				}
			} catch (NumberFormatException e) {
				isValid = false;
			}

			if(isValid) {
				return Status.OK_STATUS;
			}
		}

		return error(Messages.UnlimitedNaturalInputValidator_NotAnUnlimitedNaturalMessage);
	}


}

Back to the top