2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.ui
.element
;
11 import java
.util
.ConcurrentModificationException
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
16 import org
.eclipse
.core
.runtime
.Assert
;
17 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
18 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
19 import org
.eclipse
.swt
.graphics
.Color
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Control
;
23 import org
.eclipse
.ui
.forms
.widgets
.Section
;
24 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
26 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
27 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
32 public abstract class AbstractCdmFormElement
implements ICdmFormElement
{
34 protected CdmFormFactory formFactory
;
36 private List
<IPropertyChangeListener
> propertyChangeListeners
;
38 private Composite layoutComposite
;
40 private final Set
<Control
> controls
= new HashSet
<>();
42 private final Set
<ICdmFormElement
> elements
= new HashSet
<>();
43 private ICdmFormElement parentElement
;
45 private Color persistentBackgroundColor
;
47 protected AbstractCdmFormElement(CdmFormFactory formFactory
, Composite layoutComposite
){
48 this.layoutComposite
= layoutComposite
;
49 this.formFactory
= formFactory
;
52 public AbstractCdmFormElement(CdmFormFactory formFactory
, ICdmFormElement formElement
) {
53 this(formFactory
, formElement
.getLayoutComposite());
54 this.parentElement
= formElement
;
55 // addControl(layoutComposite);
59 public CdmFormFactory
getFormFactory() {
64 * Delegates the focus to <code>this</code> elements main input control
66 public void setFocus(){
67 // Override in subclasses where needed
71 * Returns all Controls that are managed by this element
74 public Set
<Control
> getControls(){
79 * Adds the control to the set of controls that are managed by this element
81 protected void addControl(Control child
){
85 protected void removeControl(Control child
){
86 controls
.remove(child
);
90 * <p>Getter for the field <code>elements</code>.</p>
92 * @return a {@link java.util.Set} object.
95 public Set
<ICdmFormElement
> getElements(){
100 public ICdmFormElement
getParentElement(){
101 return parentElement
;
105 public void addElement(ICdmFormElement element
){
106 elements
.add(element
);
110 * Remove all child {@link ICdmFormElement}s and child {@link Control}s
111 * of the given and the element itself.
112 * @param formElement The element to remove
114 public void removeElementsAndControls(ICdmFormElement formElement
){
115 for(ICdmFormElement childElement
: formElement
.getElements()){
117 childElement
.removeElements();
119 // unregister selection arbitrator
120 if(childElement
instanceof ISelectableElement
){
121 SelectionArbitrator selectionArbitrator
= ((ISelectableElement
) childElement
).getSelectionArbitrator();
122 if(selectionArbitrator
!= null){
123 formFactory
.destroySelectionArbitrator(selectionArbitrator
);
127 // unregister from property changes
128 formFactory
.removePropertyChangeListener(childElement
);
130 // dispose of the controls
131 removeControls(childElement
);
133 removeControls(formElement
);
134 this.elements
.remove(formElement
);
138 * Removes all child {@link ICdmFormElement}s and child {@link Control}s
139 * and the element itself.
142 public void removeElements(){
143 Set
<ICdmFormElement
> tmpElements
= new HashSet
<>(getElements());
144 for (ICdmFormElement childElement
: tmpElements
) {
146 childElement
.removeElements();
148 // unregister selection arbitrator
149 if(childElement
instanceof ISelectableElement
){
150 SelectionArbitrator selectionArbitrator
= ((ISelectableElement
) childElement
).getSelectionArbitrator();
151 if(selectionArbitrator
!= null){
152 formFactory
.destroySelectionArbitrator(selectionArbitrator
);
156 // unregister from property changes
157 formFactory
.removePropertyChangeListener(childElement
);
159 // dispose of the controls
160 removeControls(childElement
);
162 if(this instanceof ISelectableElement
){
163 SelectionArbitrator selectionArbitrator
= ((ISelectableElement
) this).getSelectionArbitrator();
164 if(selectionArbitrator
!= null){
165 formFactory
.destroySelectionArbitrator(selectionArbitrator
);
168 removeControls(this);
172 private void removeControls(ICdmFormElement element
){
173 if(element
instanceof Section
){
174 ((Section
) element
).dispose();
177 for(Control control
: element
.getControls()){
178 // we added the layoutComposite of the parental element as the layout composite to this formElement
179 // but we do not want to destroy it.
180 if(control
.equals(element
.getLayoutComposite())){
191 public Composite
getLayoutComposite() {
192 return layoutComposite
;
194 public void setLayoutComposite(Composite layoutComposite
){
195 this.layoutComposite
= layoutComposite
;
198 public void addIndent(int indent
)
199 { this.layoutComposite
= formFactory
.createComposite(layoutComposite
);
201 if (this.layoutComposite
.getParent().getLayoutData() instanceof TableWrapData
){
202 TableWrapData tableWrap
= (TableWrapData
)this.layoutComposite
.getParent().getLayoutData();
203 tableWrap
.indent
= indent
;
205 this.layoutComposite
.setLayoutData(tableWrap
);
207 if (this.layoutComposite
.getLayoutData() instanceof GridData
){
208 GridData gridData
= new GridData();
209 gridData
.verticalIndent
= indent
;
210 this.layoutComposite
.setLayoutData(gridData
);
215 public List
<IPropertyChangeListener
> getPropertyChangeListeners() {
216 return propertyChangeListeners
;
219 public void setPropertyChangeListeners(List
<IPropertyChangeListener
> propertyChangeListeners
){
220 this.propertyChangeListeners
= propertyChangeListeners
;
224 public void firePropertyChangeEvent(CdmPropertyChangeEvent event
) {
225 //TODO: replace propertyChangeListeners with formFactory.getPropertyChangeListeners() and remove member propertyChangeListeners from AbstractCdmFormElement
226 Assert
.isNotNull(propertyChangeListeners
, "Property change listeners are not present");
229 for(Object listener
: propertyChangeListeners
){
230 ((IPropertyChangeListener
)listener
).propertyChange(event
);
232 }catch(ConcurrentModificationException e
){
233 // There are two cases that produce a CME.
234 // Described here: http://dev.e-taxonomy.eu/trac/ticket/2363#comment:2
235 // and here: http://dev.e-taxonomy.eu/trac/ticket/2438
236 // Ignoring the CME because nothing bad is happening
237 MessagingUtils
.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
242 * Fires a {@link CdmPropertyChangeEvent} with the given object as source.
244 * @param object the object on which the property changed
246 public void firePropertyChangeEvent(Object object
){
247 firePropertyChangeEvent(object
, null);
251 * Fires a {@link CdmPropertyChangeEvent} with the given object as source also containing the
254 * @param object the object on which the property changed
255 * @param originatingEvent the originating event
257 public void firePropertyChangeEvent(Object object
, PropertyChangeEvent originatingEvent
){
258 firePropertyChangeEvent(new CdmPropertyChangeEvent(object
, originatingEvent
));
264 * This method gets called whenever the toolkit this composite was created with gets a property change notification.
266 * It is good advice to check whether the PropertyChangeEvent is destined for the implementing composite.
267 * Implementations should also check for null PropertyChangeEvents and return immediately in that case.
268 * @see eu.etaxonomy.taxeditor.ui.element.ICdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
271 public void propertyChange(PropertyChangeEvent event
) {
272 // implement in subclasses
276 public boolean containsFormElement(ICdmFormElement formElement
){
277 if(formElement
== this){
280 for(ICdmFormElement element
: getElements()){
281 boolean contains
= element
.containsFormElement(formElement
);
282 if(contains
== true){
291 public void refresh() {
292 // empty default implementation
296 public void setBackground(Color color
) {
297 for(ICdmFormElement element
: getElements()){
298 element
.setBackground(color
);
303 public void setPersistentBackground(Color color
) {
304 persistentBackgroundColor
= color
;
305 setBackground(color
);
309 public Color
getPersistentBackground() {
310 return persistentBackgroundColor
;
313 public Color
getColor(String colorId
){
314 return AbstractUtility
.getColor(colorId
);