Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0365b02b8064964e1d5ef019467dc501a2e0814f (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
 * Copyright (c) 2011 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.tm.te.ui.controls.validator;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ICellEditorValidator;
import org.eclipse.tm.te.ui.controls.nls.Messages;


/**
 * Target Explorer: Input validator and message provider.
 */
public abstract class Validator implements IMessageProvider, ICellEditorValidator {

	// message text, set in isValid(String)
	private String message = null;
	// message type, set in isValid(String)
	private int messageType = NONE;
	// map with all message texts
	private Map<String, String> messages = new HashMap<String, String>();
	// map with all message text types
	private Map<String, Integer> messageTypes = new HashMap<String, Integer>();

	// arguments (binary coded)
	public static final int NO_ATTR = 0;
	public static final int ATTR_MANDATORY = 1;
	// next attribute should start with 2^1

	// binary coded arguments
	private int attributes;

	/**
	 * Constructor
	 * @param attributes The validator attributes.
	 */
	public Validator(int attributes) {
		setAttributes(attributes);
	}

	/**
	 * Set the attributes for the validator.
	 * @param attributes The validator attributes.
	 */
	public void setAttributes(int attributes) {
		this.attributes = attributes;
	}

	/**
	 * Add an attribute.
	 * @param attribute The validator attribute to add.
	 */
	public void addAttribute(int attribute) {
		if (!isAttribute(attribute)) {
			this.attributes |= attribute;
		}
	}

	/**
	 * Remove an attribute.
	 * @param attribute The validator attribute to remove.
	 */
	public void delAttribute(int attribute) {
		if (isAttribute(attribute)) {
			this.attributes -= attribute;
		}
	}

	/**
	 * Returns the attributes.
	 * @return
	 */
	public int getAttributes() {
		return attributes;
	}

	/**
	 * Returns true if the argument is set.
	 * @param attribute The argument to ask for.
	 * @return
	 */
	public boolean isAttribute(int attribute) {
		return isAttribute(attribute, attributes);
	}

	/**
	 * Returns true is argument is set.
	 * This static method can be used in the constructor or other static methods
	 * to check attributes.
	 * @param attribute The attribute to ask for
	 * @param attributes The binary coded attribute list
	 * @return
	 */
	public static boolean isAttribute(int attribute, int attributes) {
		return ((attributes & attribute) == attribute);
	}

	/**
	 * Initialize the validator.
	 * Should always be called in isValid(String) before validation is done.
	 * Doesn't reset message texts!
	 */
	protected void init() {
		setMessage(null);
		setMessageType(NONE);
	}

	/**
	 * Validates the given text.
	 * A message text could be set even when the text is valid!
	 * @param newText text to validate
	 * @return true if text is valid
	 */
	public abstract boolean isValid(String newText);

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ICellEditorValidator#isValid(java.lang.Object)
	 */
	@Override
	public final String isValid(Object newValue) {
		String strValue = (newValue != null) ? newValue.toString() : null;
		if (!isValid(strValue)) {
			return (getMessage());
		}
		return null;
	}

	/**
	 * Sets the message text and type.
	 * @param message message
	 * @param messageType type for message
	 */
	protected final void setMessage(String message, int messageType) {
		setMessage(message);
		setMessageType(messageType);
	}

	/**
	 * Sets the message.
	 * @param message message
	 */
	protected final void setMessage(String message) {
		this.message = message;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IMessageProvider#getMessage()
	 */
	@Override
	public final String getMessage() {
		return message;
	}

	/**
	 * Sets the message type.
	 * @param messageType type for message
	 */
	protected final void setMessageType(int messageType) {
		this.messageType = messageType;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IMessageProvider#getMessageType()
	 */
	@Override
	public final int getMessageType() {
		return messageType;
	}

	/**
	 * Returns the message text for the given key.
	 * The key always points to the default text.
	 * @param key message key
	 * @return message text
	 */
	protected final String getMessageText(String key) {
		String message = messages.get(key);
		if (message == null) {
			message = getString(key);
			setMessageText(key, message);
		}
		return message;
	}

	/**
	 * Returns the message type for the given key. If not set, the
	 * proposed message type is returned.
	 *
	 * @param key The message key.
	 * @param proposedType The proposed message type.
	 *
	 * @return The message type.
	 */
	protected final int getMessageTextType(String key, int proposedType) {
		Integer type = messageTypes.get(key);
		if (type == null || type.intValue() == -1) type = Integer.valueOf(proposedType);
		return type.intValue();
	}

	/**
	 * Sets an alternate message text for an info, warning or error on isValid(String).
	 *
	 * @param key property key of default text
	 * @param text alternate message text, if null the default text for this key is taken
	 */
	public final void setMessageText(String key, String text) {
		setMessageText(key, text, -1);
	}

	/**
	 * Sets an alternate message text for an info, warning or error on isValid(String).
	 *
	 * @param key property key of default text
	 * @param text alternate message text, if null the default text for this key is taken
	 * @param type alternate message type, if -1 the default type for this key is taken
	 */
	public final void setMessageText(String key, String text, int type) {
		if (key != null) {
			if (text != null) {
				this.messages.put(key, text);
			} else {
				this.messages.put(key, getString(key));
			}
			if (type != -1) {
				this.messageTypes.put(key, Integer.valueOf(type));
			} else {
				this.messageTypes.remove(key);
			}
		}
	}

	/**
	 * Returns the externalized string value for the given key.
	 *
	 * @param key The key. Must not be <code>null</code>.
	 * @return The string value or <code>null</code>
	 */
	protected String getString(String key) {
		Assert.isNotNull(key);
		return Messages.getString(key);
	}
}

Back to the top