4 package eu
.etaxonomy
.taxeditor
.ui
.element
;
6 import java
.util
.ConcurrentModificationException
;
7 import java
.util
.HashSet
;
8 import java
.util
.Iterator
;
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
.layout
.GridData
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Control
;
19 import org
.eclipse
.ui
.forms
.widgets
.Section
;
20 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
22 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
23 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
29 public abstract class AbstractCdmFormElement
implements ICdmFormElement
{
31 protected CdmFormFactory formFactory
;
33 private List
<IPropertyChangeListener
> propertyChangeListeners
;
35 private Composite layoutComposite
;
37 private final Set
<Control
> controls
= new HashSet
<Control
>();
39 private final Set
<ICdmFormElement
> elements
= new HashSet
<ICdmFormElement
>();
40 private ICdmFormElement parentElement
;
42 private Color persistentBackgroundColor
;
45 protected AbstractCdmFormElement(CdmFormFactory formFactory
, Composite layoutComposite
){
46 this.layoutComposite
= layoutComposite
;
47 this.formFactory
= formFactory
;
50 public AbstractCdmFormElement(CdmFormFactory formFactory
, ICdmFormElement formElement
) {
51 this(formFactory
, formElement
.getLayoutComposite());
52 this.parentElement
= formElement
;
53 // addControl(layoutComposite);
57 public CdmFormFactory
getFormFactory() {
62 * Delegates the focus to <code>this</code> elements main input control
64 public void setFocus(){
65 // Override in subclasses where needed
69 * Returns all Controls that are managed by this element
71 * @return a {@link java.util.Set} object.
74 public Set
<Control
> getControls(){
79 * adds the control to the set of controls that are managed by this element
81 * @param child a {@link org.eclipse.swt.widgets.Control} object.
83 protected void addControl(Control child
){
87 protected void removeControl(Control child
){
88 controls
.remove(child
);
92 * <p>Getter for the field <code>elements</code>.</p>
94 * @return a {@link java.util.Set} object.
97 public Set
<ICdmFormElement
> getElements(){
102 * <p>Getter for the field <code>parentElement</code>.</p>
104 * @return a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
107 public ICdmFormElement
getParentElement(){
108 return parentElement
;
113 public void addElement(ICdmFormElement element
){
114 elements
.add(element
);
118 * Remove all child {@link ICdmFormElement}s and child {@link Control}s
119 * of the given and the element itself.
120 * @param formElement The element to remove
122 public void removeElementsAndControls(ICdmFormElement formElement
){
123 for(ICdmFormElement childElement
: formElement
.getElements()){
125 childElement
.removeElements();
127 // unregister selection arbitrator
128 if(childElement
instanceof ISelectableElement
){
129 SelectionArbitrator selectionArbitrator
= ((ISelectableElement
) childElement
).getSelectionArbitrator();
130 if(selectionArbitrator
!= null){
131 formFactory
.destroySelectionArbitrator(selectionArbitrator
);
135 // unregister from property changes
136 formFactory
.removePropertyChangeListener(childElement
);
138 // dispose of the controls
139 removeControls(childElement
);
141 removeControls(formElement
);
142 this.elements
.remove(formElement
);
146 * Removes all child {@link ICdmFormElement}s and child {@link Control}s
147 * and the element itself.
150 public void removeElements(){
151 for (Iterator
<ICdmFormElement
> iterator
= getElements().iterator();iterator
.hasNext();) {
152 ICdmFormElement childElement
= iterator
.next();
154 childElement
.removeElements();
156 // unregister selection arbitrator
157 if(childElement
instanceof ISelectableElement
){
158 SelectionArbitrator selectionArbitrator
= ((ISelectableElement
) childElement
).getSelectionArbitrator();
159 if(selectionArbitrator
!= null){
160 formFactory
.destroySelectionArbitrator(selectionArbitrator
);
164 // unregister from property changes
165 formFactory
.removePropertyChangeListener(childElement
);
167 // dispose of the controls
168 removeControls(childElement
);
170 if(this instanceof ISelectableElement
){
171 SelectionArbitrator selectionArbitrator
= ((ISelectableElement
) this).getSelectionArbitrator();
172 if(selectionArbitrator
!= null){
173 formFactory
.destroySelectionArbitrator(selectionArbitrator
);
176 removeControls(this);
180 private void removeControls(ICdmFormElement element
){
181 if(element
instanceof Section
){
182 ((Section
) element
).dispose();
185 for(Control control
: element
.getControls()){
186 // we added the layoutComposite of the parental element as the layout composite to this formElement
187 // but we do not want to destroy it.
188 if(control
.equals(element
.getLayoutComposite())){
199 * <p>Getter for the field <code>layoutComposite</code>.</p>
201 * @return a {@link org.eclipse.swt.widgets.Composite} object.
204 public Composite
getLayoutComposite() {
205 return layoutComposite
;
209 * <p>Setter for the field <code>layoutComposite</code>.</p>
211 * @param layoutComposite a {@link org.eclipse.swt.widgets.Composite} object.
213 public void setLayoutComposite(Composite layoutComposite
){
214 this.layoutComposite
= layoutComposite
;
217 public void addIndent(int indent
)
218 { this.layoutComposite
= formFactory
.createComposite(layoutComposite
);
220 if (this.layoutComposite
.getParent().getLayoutData() instanceof TableWrapData
){
221 TableWrapData tableWrap
= (TableWrapData
)this.layoutComposite
.getParent().getLayoutData();
222 tableWrap
.indent
= indent
;
224 this.layoutComposite
.setLayoutData(tableWrap
);
226 if (this.layoutComposite
.getLayoutData() instanceof GridData
){
227 GridData gridData
= new GridData();
228 gridData
.verticalIndent
= indent
;
229 this.layoutComposite
.setLayoutData(gridData
);
234 * <p>Getter for the field <code>propertyChangeListeners</code>.</p>
236 * @return a {@link java.util.Set} object.
239 public List
<IPropertyChangeListener
> getPropertyChangeListeners() {
240 return propertyChangeListeners
;
245 public void setPropertyChangeListeners(List
<IPropertyChangeListener
> propertyChangeListeners
){
246 this.propertyChangeListeners
= propertyChangeListeners
;
251 public void firePropertyChangeEvent(CdmPropertyChangeEvent event
) {
252 //TODO: replace propertyChangeListeners with formFactory.getPropertyChangeListeners() and remove member propertyChangeListeners from AbstractCdmFormElement
253 Assert
.isNotNull(propertyChangeListeners
, "Property change listeners are not present");
256 for(Object listener
: propertyChangeListeners
){
257 ((IPropertyChangeListener
)listener
).propertyChange(event
);
259 }catch(ConcurrentModificationException e
){
260 // There are two cases that produce a CME.
261 // Described here: http://dev.e-taxonomy.eu/trac/ticket/2363#comment:2
262 // and here: http://dev.e-taxonomy.eu/trac/ticket/2438
263 // Ignoring the CME because nothing bad is happening
264 MessagingUtils
.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
269 * Fires a {@link CdmPropertyChangeEvent} with the given object as source.
271 * @param object the object on which the property changed
273 public void firePropertyChangeEvent(Object object
){
274 firePropertyChangeEvent(object
, null);
278 * Fires a {@link CdmPropertyChangeEvent} with the given object as source also containing the
281 * @param object the object on which the property changed
282 * @param originatingEvent the originating event
284 public void firePropertyChangeEvent(Object object
, PropertyChangeEvent originatingEvent
){
285 firePropertyChangeEvent(new CdmPropertyChangeEvent(object
, originatingEvent
));
292 * This method gets called whenever the toolkit this composite was created with gets a property change notification.
294 * It is good advice to check whether the PropertyChangeEvent is destined for the implementing composite.
295 * Implementations should also check for null PropertyChangeEvents and return immediately in that case.
296 * @see eu.etaxonomy.taxeditor.ui.element.ICdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
299 public void propertyChange(PropertyChangeEvent event
) {
300 // implement in subclasses
305 public boolean containsFormElement(ICdmFormElement formElement
){
306 if(formElement
== this){
309 for(ICdmFormElement element
: getElements()){
310 boolean contains
= element
.containsFormElement(formElement
);
311 if(contains
== true){
320 public void refresh() {
321 // empty default implementation
327 public void setBackground(Color color
) {
328 for(ICdmFormElement element
: getElements()){
329 element
.setBackground(color
);
334 public void setPersistentBackground(Color color
) {
335 persistentBackgroundColor
= color
;
336 setBackground(color
);
340 public Color
getPersistentBackground() {
341 return persistentBackgroundColor
;
344 public Color
getColor(String colorId
){
345 return AbstractUtility
.getColor(colorId
);