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.List;
|
9
|
import java.util.Set;
|
10
|
|
11
|
import org.eclipse.core.runtime.Assert;
|
12
|
import org.eclipse.jface.util.IPropertyChangeListener;
|
13
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
14
|
import org.eclipse.swt.graphics.Color;
|
15
|
import org.eclipse.swt.widgets.Composite;
|
16
|
import org.eclipse.swt.widgets.Control;
|
17
|
import org.eclipse.ui.forms.widgets.ScrolledForm;
|
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
|
protected void removeElement(ICdmFormElement element){
|
116
|
elements.remove(element);
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Remove all child {@link ICdmFormElement}s and child {@link Control}s
|
121
|
* of the given and the element itself.
|
122
|
* @param formElement The element to remove
|
123
|
*/
|
124
|
public void removeElementsAndControls(ICdmFormElement formElement){
|
125
|
for(ICdmFormElement childElement : formElement.getElements()){
|
126
|
// recursion
|
127
|
childElement.removeElements();
|
128
|
|
129
|
// unregister selection arbitrator
|
130
|
if(childElement instanceof ISelectableElement){
|
131
|
SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
|
132
|
if(selectionArbitrator != null){
|
133
|
formFactory.destroySelectionArbitrator(selectionArbitrator);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
// unregister from property changes
|
138
|
formFactory.removePropertyChangeListener(childElement);
|
139
|
|
140
|
// dispose of the controls
|
141
|
removeControls(childElement);
|
142
|
}
|
143
|
removeControls(formElement);
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Removes all child {@link ICdmFormElement}s and child {@link Control}s
|
148
|
* and the element itself.
|
149
|
*/
|
150
|
@Override
|
151
|
public void removeElements(){
|
152
|
for(ICdmFormElement childElement : getElements()){
|
153
|
// recursion
|
154
|
childElement.removeElements();
|
155
|
|
156
|
// unregister selection arbitrator
|
157
|
if(childElement instanceof ISelectableElement){
|
158
|
SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
|
159
|
if(selectionArbitrator != null){
|
160
|
formFactory.destroySelectionArbitrator(selectionArbitrator);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
// unregister from property changes
|
165
|
formFactory.removePropertyChangeListener(childElement);
|
166
|
|
167
|
// dispose of the controls
|
168
|
removeControls(childElement);
|
169
|
}
|
170
|
removeControls(this);
|
171
|
elements.clear();
|
172
|
}
|
173
|
|
174
|
private void removeControls(ICdmFormElement element){
|
175
|
if(element instanceof Section){
|
176
|
((Section) element).dispose();
|
177
|
element = null;
|
178
|
}else{
|
179
|
for(Control control : element.getControls()){
|
180
|
// we added the layoutComposite of the parental element as the layout composite to this formElement
|
181
|
// but we do not want to destroy it.
|
182
|
if(control.equals(element.getLayoutComposite())){
|
183
|
continue;
|
184
|
}else{
|
185
|
control.dispose();
|
186
|
control = null;
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* <p>Getter for the field <code>layoutComposite</code>.</p>
|
194
|
*
|
195
|
* @return a {@link org.eclipse.swt.widgets.Composite} object.
|
196
|
*/
|
197
|
@Override
|
198
|
public Composite getLayoutComposite() {
|
199
|
return layoutComposite;
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* <p>Setter for the field <code>layoutComposite</code>.</p>
|
204
|
*
|
205
|
* @param layoutComposite a {@link org.eclipse.swt.widgets.Composite} object.
|
206
|
*/
|
207
|
public void setLayoutComposite(Composite layoutComposite){
|
208
|
this.layoutComposite = layoutComposite;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* <p>Getter for the field <code>propertyChangeListeners</code>.</p>
|
213
|
*
|
214
|
* @return a {@link java.util.Set} object.
|
215
|
*/
|
216
|
@Override
|
217
|
public List<IPropertyChangeListener> getPropertyChangeListeners() {
|
218
|
return propertyChangeListeners;
|
219
|
}
|
220
|
|
221
|
/** {@inheritDoc} */
|
222
|
@Override
|
223
|
public void setPropertyChangeListeners(List<IPropertyChangeListener> propertyChangeListeners){
|
224
|
this.propertyChangeListeners = propertyChangeListeners;
|
225
|
}
|
226
|
|
227
|
/** {@inheritDoc} */
|
228
|
@Override
|
229
|
public void firePropertyChangeEvent(CdmPropertyChangeEvent event) {
|
230
|
//TODO: replace propertyChangeListeners with formFactory.getPropertyChangeListeners() and remove member propertyChangeListeners from AbstractCdmFormElement
|
231
|
Assert.isNotNull(propertyChangeListeners, "Property change listeners are not present");
|
232
|
|
233
|
try{
|
234
|
for(Object listener : propertyChangeListeners){
|
235
|
((IPropertyChangeListener)listener).propertyChange(event);
|
236
|
}
|
237
|
}catch(ConcurrentModificationException e){
|
238
|
// There are two cases that produce a CME.
|
239
|
// Described here: http://dev.e-taxonomy.eu/trac/ticket/2363#comment:2
|
240
|
// and here: http://dev.e-taxonomy.eu/trac/ticket/2438
|
241
|
// Ignoring the CME because nothing bad is happening
|
242
|
MessagingUtils.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
|
243
|
}
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Fires a {@link CdmPropertyChangeEvent} with the given object as source.
|
248
|
*
|
249
|
* @param object the object on which the property changed
|
250
|
*/
|
251
|
public void firePropertyChangeEvent(Object object){
|
252
|
firePropertyChangeEvent(object, null);
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Fires a {@link CdmPropertyChangeEvent} with the given object as source also containing the
|
257
|
* originating event
|
258
|
*
|
259
|
* @param object the object on which the property changed
|
260
|
* @param originatingEvent the originating event
|
261
|
*/
|
262
|
public void firePropertyChangeEvent(Object object, PropertyChangeEvent originatingEvent){
|
263
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(object, originatingEvent));
|
264
|
}
|
265
|
|
266
|
|
267
|
/**
|
268
|
* {@inheritDoc}
|
269
|
*
|
270
|
* This method gets called whenever the toolkit this composite was created with gets a property change notification.
|
271
|
*
|
272
|
* It is good advice to check whether the PropertyChangeEvent is destined for the implementing composite.
|
273
|
* Implementations should also check for null PropertyChangeEvents and return immediately in that case.
|
274
|
* @see eu.etaxonomy.taxeditor.ui.element.ICdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
|
275
|
*/
|
276
|
@Override
|
277
|
public void propertyChange(PropertyChangeEvent event) {
|
278
|
// implement in subclasses
|
279
|
}
|
280
|
|
281
|
/** {@inheritDoc} */
|
282
|
@Override
|
283
|
public boolean containsFormElement(ICdmFormElement formElement){
|
284
|
if(formElement == this){
|
285
|
return true;
|
286
|
}else{
|
287
|
for(ICdmFormElement element : getElements()){
|
288
|
boolean contains = element.containsFormElement(formElement);
|
289
|
if(contains == true){
|
290
|
return true;
|
291
|
}
|
292
|
}
|
293
|
return false;
|
294
|
}
|
295
|
}
|
296
|
|
297
|
protected void reflowParentScrolledForm(boolean flushCashes){
|
298
|
ScrolledForm scrolledForm = null;
|
299
|
Composite parent = getLayoutComposite().getParent();
|
300
|
while(parent!=null && !(parent instanceof ScrolledForm)){
|
301
|
parent = parent.getParent();
|
302
|
}
|
303
|
scrolledForm = (ScrolledForm)parent;
|
304
|
if(scrolledForm!=null){
|
305
|
scrolledForm.reflow(flushCashes);
|
306
|
}
|
307
|
}
|
308
|
|
309
|
@Override
|
310
|
public void refresh() {
|
311
|
// empty default implementation
|
312
|
}
|
313
|
|
314
|
|
315
|
/** {@inheritDoc} */
|
316
|
@Override
|
317
|
public void setBackground(Color color) {
|
318
|
for(ICdmFormElement element : getElements()){
|
319
|
element.setBackground(color);
|
320
|
}
|
321
|
}
|
322
|
|
323
|
@Override
|
324
|
public void setPersistentBackground(Color color) {
|
325
|
persistentBackgroundColor = color;
|
326
|
setBackground(color);
|
327
|
}
|
328
|
|
329
|
@Override
|
330
|
public Color getPersistentBackground() {
|
331
|
return persistentBackgroundColor;
|
332
|
}
|
333
|
|
334
|
public Color getColor(String colorId){
|
335
|
return AbstractUtility.getColor(colorId);
|
336
|
}
|
337
|
}
|