-
Notifications
You must be signed in to change notification settings - Fork 96
/
PropertyTable.java
309 lines (279 loc) · 7.07 KB
/
PropertyTable.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*******************************************************************************
* Copyright (c) 2012 Laurent CARON
*
* 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:
* Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.propertytable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
/**
* Instances of this class are property sheets
* <p>
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>BORDER</dd>
* <dt><b>Events:</b></dt>
* <dd>PTPropertyChange</dd>
* </dl>
* </p>
*/
public class PropertyTable extends Composite {
final static int VIEW_AS_FLAT_LIST = 0;
final static int VIEW_AS_CATEGORIES = 1;
boolean showButtons;
boolean showDescription;
boolean sorted;
int styleOfView;
final List<PTProperty> properties;
private boolean hasBeenBuilt = false;
private final List<PTPropertyChangeListener> changeListeners;
private PTWidget widget;
private boolean bulkUpdateInProgress = false;
/**
* Constructs a new instance of this class given its parent and a style
* value describing its behavior and appearance.
* <p>
* The style value is either one of the style constants defined in class
* <code>SWT</code> which is applicable to instances of this class, or must
* be built by <em>bitwise OR</em>'ing together (that is, using the
* <code>int</code> "|" operator) two or more of those <code>SWT</code>
* style constants. The class description lists the style constants that are
* applicable to the class. Style bits are also inherited from superclasses.
* </p>
*
* @param parent a composite control which will be the parent of the new
* instance (cannot be null)
* @param style the style of control to construct
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the parent</li>
* </ul>
*
*/
public PropertyTable(final Composite parent, final int style) {
super(parent, style);
setLayout(new FillLayout(SWT.VERTICAL));
showButtons = true;
showDescription = true;
sorted = true;
styleOfView = VIEW_AS_CATEGORIES;
properties = new ArrayList<>();
changeListeners = new ArrayList<>();
widget = PTWidgetFactory.build(this);
addListener(SWT.Resize, event -> {
// Draw the widget on first displaying
if (!hasBeenBuilt) {
widget.build();
hasBeenBuilt = true;
}
});
}
/**
* Add a change listener (event fired when the value of a property is
* changed)
*
* @param listener
*/
public void addChangeListener(final PTPropertyChangeListener listener) {
changeListeners.add(listener);
}
/**
* Add a property in this widget
*
* @param property property to add
* @return the property
*/
public PTProperty addProperty(final PTProperty property) {
if (properties.contains(property)) {
throw new IllegalArgumentException("A property called '" + property.getName() + "' has already been declared.");
}
properties.add(property);
property.setParentTable(this);
return property;
}
/**
* Fire the event "a value of a property has changed"
*
* @param property property which value has changed
*/
public void firePTPropertyChangeListeners(final PTProperty property) {
for (final PTPropertyChangeListener listener : changeListeners) {
listener.propertyHasChanged(property);
}
}
/**
* @return the values stored in this object in a map. Keys are property's
* name, values are values stored in a the property.
*/
public Map<String, Object> getProperties() {
final Map<String, Object> map = new HashMap<>();
for (final PTProperty prop : properties) {
map.put(prop.getName(), prop.getValue());
}
return map;
}
/**
* @return the properties stored in a list
*/
public List<PTProperty> getPropertiesAsList() {
return new ArrayList<>(properties);
}
/**
* Hide all buttons
*
* @return this property table
*/
public PropertyTable hideButtons() {
showButtons = false;
return rebuild();
}
/**
* Hide description
*
* @return this property table
*/
public PropertyTable hideDescription() {
showDescription = false;
return rebuild();
}
/**
* Rebuild the whole table
*
* @return this property table
*/
PropertyTable rebuild() {
if (bulkUpdateInProgress) {
return this;
}
try {
setRedraw(false);
widget = widget.disposeAndBuild(this);
if (hasBeenBuilt) {
setLayout(new FillLayout());
widget.build();
layout();
}
} finally {
setRedraw(true);
redraw();
}
return this;
}
/**
* Update the component when some values has changed
*/
public void refreshValues() {
rebuild();
}
/**
* Remove a change listener
*
* @param listener listener to remove
*/
public void removeChangeListener(final PTPropertyChangeListener listener) {
changeListeners.remove(listener);
}
/**
* @param newValues
*/
public void setProperties(final Map<String, Object> newValues) {
for (final PTProperty prop : properties) {
if (newValues == null) {
prop.setValue(null);
} else {
final Object value = newValues.get(prop.getName());
prop.setValue(value);
}
}
rebuild();
}
/**
* Show all buttons
*
* @return this property table
*/
public PropertyTable showButtons() {
showButtons = true;
return rebuild();
}
/**
* Show description
*
* @return this property table
*/
public PropertyTable showDescription() {
showDescription = true;
return rebuild();
}
/**
* Sort the properties
*
* @return this property table
*/
public PropertyTable sort() {
sorted = true;
widget.refillData();
return this;
}
/**
* Show properties not sorted
*
* @return this property table
*/
public PropertyTable unsort() {
sorted = false;
widget.refillData();
return this;
}
/**
* View the properties as categories
*
* @return this property table
*/
public PropertyTable viewAsCategories() {
styleOfView = VIEW_AS_CATEGORIES;
return rebuild();
}
/**
* View the properties as a flat list
*
* @return this property table
*/
public PropertyTable viewAsFlatList() {
styleOfView = VIEW_AS_FLAT_LIST;
return rebuild();
}
/**
* Discard all changes that happened in that table
*
* @return this property table
*/
public PropertyTable discardChanges() {
bulkUpdateInProgress = true;
for (final PTProperty prop : properties) {
prop.discardChange();
}
bulkUpdateInProgress = false;
return rebuild();
}
}