1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.ui.element;
|
5
|
|
6
|
import java.util.ConcurrentModificationException;
|
7
|
import java.util.HashSet;
|
8
|
import java.util.Iterator;
|
9
|
import java.util.List;
|
10
|
import java.util.Set;
|
11
|
|
12
|
import org.eclipse.core.runtime.Assert;
|
13
|
import org.eclipse.jface.util.IPropertyChangeListener;
|
14
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
15
|
import org.eclipse.swt.graphics.Color;
|
16
|
import org.eclipse.swt.widgets.Composite;
|
17
|
import org.eclipse.swt.widgets.Control;
|
18
|
import org.eclipse.ui.forms.widgets.Section;
|
19
|
|
20
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
21
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
22
|
|
23
|
/**
|
24
|
* @author n.hoffmann
|
25
|
* @version $Id: $
|
26
|
*/
|
27
|
public abstract class AbstractCdmFormElement implements ICdmFormElement {
|
28
|
|
29
|
protected CdmFormFactory formFactory;
|
30
|
|
31
|
private List<IPropertyChangeListener> propertyChangeListeners;
|
32
|
|
33
|
private Composite layoutComposite;
|
34
|
|
35
|
private final Set<Control> controls = new HashSet<Control>();
|
36
|
|
37
|
private final Set<ICdmFormElement> elements = new HashSet<ICdmFormElement>();
|
38
|
private ICdmFormElement parentElement;
|
39
|
|
40
|
private Color persistentBackgroundColor;
|
41
|
|
42
|
|
43
|
protected AbstractCdmFormElement(CdmFormFactory formFactory, Composite layoutComposite){
|
44
|
this.layoutComposite = layoutComposite;
|
45
|
this.formFactory = formFactory;
|
46
|
}
|
47
|
|
48
|
public AbstractCdmFormElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
|
49
|
this(formFactory, formElement.getLayoutComposite());
|
50
|
this.parentElement = formElement;
|
51
|
// addControl(layoutComposite);
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public CdmFormFactory getFormFactory() {
|
56
|
return formFactory;
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Delegates the focus to <code>this</code> elements main input control
|
61
|
*/
|
62
|
public void setFocus(){
|
63
|
// Override in subclasses where needed
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Returns all Controls that are managed by this element
|
68
|
*
|
69
|
* @return a {@link java.util.Set} object.
|
70
|
*/
|
71
|
@Override
|
72
|
public Set<Control> getControls(){
|
73
|
return controls;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* adds the control to the set of controls that are managed by this element
|
78
|
*
|
79
|
* @param child a {@link org.eclipse.swt.widgets.Control} object.
|
80
|
*/
|
81
|
protected void addControl(Control child){
|
82
|
controls.add(child);
|
83
|
}
|
84
|
|
85
|
protected void removeControl(Control child){
|
86
|
controls.remove(child);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* <p>Getter for the field <code>elements</code>.</p>
|
91
|
*
|
92
|
* @return a {@link java.util.Set} object.
|
93
|
*/
|
94
|
@Override
|
95
|
public Set<ICdmFormElement> getElements(){
|
96
|
return elements;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* <p>Getter for the field <code>parentElement</code>.</p>
|
101
|
*
|
102
|
* @return a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
103
|
*/
|
104
|
@Override
|
105
|
public ICdmFormElement getParentElement(){
|
106
|
return parentElement;
|
107
|
}
|
108
|
|
109
|
/** {@inheritDoc} */
|
110
|
@Override
|
111
|
public void addElement(ICdmFormElement element){
|
112
|
elements.add(element);
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Remove all child {@link ICdmFormElement}s and child {@link Control}s
|
117
|
* of the given and the element itself.
|
118
|
* @param formElement The element to remove
|
119
|
*/
|
120
|
public void removeElementsAndControls(ICdmFormElement formElement){
|
121
|
for(ICdmFormElement childElement : formElement.getElements()){
|
122
|
// recursion
|
123
|
childElement.removeElements();
|
124
|
|
125
|
// unregister selection arbitrator
|
126
|
if(childElement instanceof ISelectableElement){
|
127
|
SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
|
128
|
if(selectionArbitrator != null){
|
129
|
formFactory.destroySelectionArbitrator(selectionArbitrator);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
// unregister from property changes
|
134
|
formFactory.removePropertyChangeListener(childElement);
|
135
|
|
136
|
// dispose of the controls
|
137
|
removeControls(childElement);
|
138
|
}
|
139
|
removeControls(formElement);
|
140
|
this.elements.remove(formElement);
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Removes all child {@link ICdmFormElement}s and child {@link Control}s
|
145
|
* and the element itself.
|
146
|
*/
|
147
|
@Override
|
148
|
public void removeElements(){
|
149
|
for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
|
150
|
ICdmFormElement childElement = iterator.next();
|
151
|
// recursion
|
152
|
childElement.removeElements();
|
153
|
|
154
|
// unregister selection arbitrator
|
155
|
if(childElement instanceof ISelectableElement){
|
156
|
SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
|
157
|
if(selectionArbitrator != null){
|
158
|
formFactory.destroySelectionArbitrator(selectionArbitrator);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
// unregister from property changes
|
163
|
formFactory.removePropertyChangeListener(childElement);
|
164
|
|
165
|
// dispose of the controls
|
166
|
removeControls(childElement);
|
167
|
}
|
168
|
if(this instanceof ISelectableElement){
|
169
|
SelectionArbitrator selectionArbitrator = ((ISelectableElement) this).getSelectionArbitrator();
|
170
|
if(selectionArbitrator != null){
|
171
|
formFactory.destroySelectionArbitrator(selectionArbitrator);
|
172
|
}
|
173
|
}
|
174
|
removeControls(this);
|
175
|
elements.clear();
|
176
|
}
|
177
|
|
178
|
private void removeControls(ICdmFormElement element){
|
179
|
if(element instanceof Section){
|
180
|
((Section) element).dispose();
|
181
|
element = null;
|
182
|
}else{
|
183
|
for(Control control : element.getControls()){
|
184
|
// we added the layoutComposite of the parental element as the layout composite to this formElement
|
185
|
// but we do not want to destroy it.
|
186
|
if(control.equals(element.getLayoutComposite())){
|
187
|
continue;
|
188
|
}else{
|
189
|
control.dispose();
|
190
|
control = null;
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* <p>Getter for the field <code>layoutComposite</code>.</p>
|
198
|
*
|
199
|
* @return a {@link org.eclipse.swt.widgets.Composite} object.
|
200
|
*/
|
201
|
@Override
|
202
|
public Composite getLayoutComposite() {
|
203
|
return layoutComposite;
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* <p>Setter for the field <code>layoutComposite</code>.</p>
|
208
|
*
|
209
|
* @param layoutComposite a {@link org.eclipse.swt.widgets.Composite} object.
|
210
|
*/
|
211
|
public void setLayoutComposite(Composite layoutComposite){
|
212
|
this.layoutComposite = layoutComposite;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* <p>Getter for the field <code>propertyChangeListeners</code>.</p>
|
217
|
*
|
218
|
* @return a {@link java.util.Set} object.
|
219
|
*/
|
220
|
@Override
|
221
|
public List<IPropertyChangeListener> getPropertyChangeListeners() {
|
222
|
return propertyChangeListeners;
|
223
|
}
|
224
|
|
225
|
/** {@inheritDoc} */
|
226
|
@Override
|
227
|
public void setPropertyChangeListeners(List<IPropertyChangeListener> propertyChangeListeners){
|
228
|
this.propertyChangeListeners = propertyChangeListeners;
|
229
|
}
|
230
|
|
231
|
/** {@inheritDoc} */
|
232
|
@Override
|
233
|
public void firePropertyChangeEvent(CdmPropertyChangeEvent event) {
|
234
|
//TODO: replace propertyChangeListeners with formFactory.getPropertyChangeListeners() and remove member propertyChangeListeners from AbstractCdmFormElement
|
235
|
Assert.isNotNull(propertyChangeListeners, "Property change listeners are not present");
|
236
|
|
237
|
try{
|
238
|
for(Object listener : propertyChangeListeners){
|
239
|
((IPropertyChangeListener)listener).propertyChange(event);
|
240
|
}
|
241
|
}catch(ConcurrentModificationException e){
|
242
|
// There are two cases that produce a CME.
|
243
|
// Described here: http://dev.e-taxonomy.eu/trac/ticket/2363#comment:2
|
244
|
// and here: http://dev.e-taxonomy.eu/trac/ticket/2438
|
245
|
// Ignoring the CME because nothing bad is happening
|
246
|
MessagingUtils.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
|
247
|
}
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* Fires a {@link CdmPropertyChangeEvent} with the given object as source.
|
252
|
*
|
253
|
* @param object the object on which the property changed
|
254
|
*/
|
255
|
public void firePropertyChangeEvent(Object object){
|
256
|
firePropertyChangeEvent(object, null);
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Fires a {@link CdmPropertyChangeEvent} with the given object as source also containing the
|
261
|
* originating event
|
262
|
*
|
263
|
* @param object the object on which the property changed
|
264
|
* @param originatingEvent the originating event
|
265
|
*/
|
266
|
public void firePropertyChangeEvent(Object object, PropertyChangeEvent originatingEvent){
|
267
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(object, originatingEvent));
|
268
|
}
|
269
|
|
270
|
|
271
|
/**
|
272
|
* {@inheritDoc}
|
273
|
*
|
274
|
* This method gets called whenever the toolkit this composite was created with gets a property change notification.
|
275
|
*
|
276
|
* It is good advice to check whether the PropertyChangeEvent is destined for the implementing composite.
|
277
|
* Implementations should also check for null PropertyChangeEvents and return immediately in that case.
|
278
|
* @see eu.etaxonomy.taxeditor.ui.element.ICdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
|
279
|
*/
|
280
|
@Override
|
281
|
public void propertyChange(PropertyChangeEvent event) {
|
282
|
// implement in subclasses
|
283
|
}
|
284
|
|
285
|
/** {@inheritDoc} */
|
286
|
@Override
|
287
|
public boolean containsFormElement(ICdmFormElement formElement){
|
288
|
if(formElement == this){
|
289
|
return true;
|
290
|
}else{
|
291
|
for(ICdmFormElement element : getElements()){
|
292
|
boolean contains = element.containsFormElement(formElement);
|
293
|
if(contains == true){
|
294
|
return true;
|
295
|
}
|
296
|
}
|
297
|
return false;
|
298
|
}
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
public void refresh() {
|
303
|
// empty default implementation
|
304
|
}
|
305
|
|
306
|
|
307
|
/** {@inheritDoc} */
|
308
|
@Override
|
309
|
public void setBackground(Color color) {
|
310
|
for(ICdmFormElement element : getElements()){
|
311
|
element.setBackground(color);
|
312
|
}
|
313
|
}
|
314
|
|
315
|
@Override
|
316
|
public void setPersistentBackground(Color color) {
|
317
|
persistentBackgroundColor = color;
|
318
|
setBackground(color);
|
319
|
}
|
320
|
|
321
|
@Override
|
322
|
public Color getPersistentBackground() {
|
323
|
return persistentBackgroundColor;
|
324
|
}
|
325
|
|
326
|
public Color getColor(String colorId){
|
327
|
return AbstractUtility.getColor(colorId);
|
328
|
}
|
329
|
}
|