-
Notifications
You must be signed in to change notification settings - Fork 96
/
StringFormatter.java
123 lines (114 loc) · 3.66 KB
/
StringFormatter.java
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
/*******************************************************************************
* Copyright (c) 2005, 2009 Eric Wuillai.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eric Wuillai (eric@wdev91.com) - initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.formattedtext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
/**
* Default formatter for the String class.<br>
* This formatter is a kind of NullFormatter and do no formatting, allowing
* the edit of simple String values, without constraints.<br>
* It is registered in the DefaultFormatterFactory as the default formatter
* for String values.
*/
public class StringFormatter extends AbstractFormatter {
/**
* Returns the current value formatted for display.<p>
* There is no difference in this formatter between edit and display values.
* So this method returns the edit string.
*
* @return display string
* @see ITextFormatter#getDisplayString()
*/
public String getDisplayString() {
return getEditString();
}
/**
* Returns the current value formatted for editing.
* This method is called by <code>FormattedText</code> when the <code>Text</code>
* widget gains focus.<br>
* This formatter has no formatting features. So it simply return the Text
* widget content.
*
* @return edit string
* @see ITextFormatter#getEditString()
*/
public String getEditString() {
return text.getText();
}
/**
* Returns the current value.<br>
* This formatter has no formatting features. So it simply return the Text
* widget content.
*
* @return current string value
* @see ITextFormatter#getValue()
*/
public Object getValue() {
return text.getText();
}
/**
* Returns the type of value this {@link ITextFormatter} handles,
* i.e. returns in {@link #getValue()}.<br>
* A StringFormatter always returns a String value.
*
* @return The value type.
*/
public Class<String> getValueType() {
return String.class;
}
/**
* Returns <code>true</code> if current edited value is empty, else returns
* <code>false</code>.<br>
* A string is empty if its length is 0.
*
* @return true if empty, else false
*/
public boolean isEmpty() {
return text.getText().length() == 0;
}
/**
* Returns <code>true</code> if current edited value is valid, else returns
* <code>false</code>.<br>
* StringFormatter always return true.
*
* @return true
* @see ITextFormatter#isValid()
*/
public boolean isValid() {
return true;
}
/**
* Sets the value to edit. The value provided must be a <code>String</code>.
* The Text widget is simply updated with the value.
*
* @param value string value
* @throws IllegalArgumentException if not a string
* @see ITextFormatter#setValue(java.lang.Object)
*/
public void setValue(Object value) {
if ( ! (value instanceof String) ) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
text.setText((String) value);
}
/**
* Handles a <code>VerifyEvent</code> sent when the text is about to be modified.
* This method is the entry point of all operations of formatting.<br>
* This formatter has no formatting features. So this method do nothing.
*
* @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
*/
public void verifyText(VerifyEvent e) {
}
}