/**
- *
+ *
*/
package eu.etaxonomy.taxeditor.ui.element;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.widgets.Section;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
/**
- * <p>Abstract AbstractCdmFormElement class.</p>
- *
* @author n.hoffmann
* @version $Id: $
*/
-public abstract class AbstractCdmFormElement implements IPropertyChangeEmitter, ICdmFormElement {
+public abstract class AbstractCdmFormElement implements ICdmFormElement {
protected CdmFormFactory formFactory;
private List<IPropertyChangeListener> propertyChangeListeners;
private Composite layoutComposite;
-
- private Set<Control> controls = new HashSet<Control>();
-
- private Set<ICdmFormElement> elements = new HashSet<ICdmFormElement>();
+
+ private final Set<Control> controls = new HashSet<Control>();
+
+ private final Set<ICdmFormElement> elements = new HashSet<ICdmFormElement>();
private ICdmFormElement parentElement;
private Color persistentBackgroundColor;
-
-
- /**
- * <p>Constructor for AbstractCdmFormElement.</p>
- *
- * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
- * @param layoutComposite a {@link org.eclipse.swt.widgets.Composite} object.
- */
+
+
protected AbstractCdmFormElement(CdmFormFactory formFactory, Composite layoutComposite){
this.layoutComposite = layoutComposite;
this.formFactory = formFactory;
}
-
- /**
- * <p>Constructor for AbstractCdmFormElement.</p>
- *
- * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
- * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
- */
+
public AbstractCdmFormElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
this(formFactory, formElement.getLayoutComposite());
this.parentElement = formElement;
// addControl(layoutComposite);
}
-
- /**
- * <p>Getter for the field <code>formFactory</code>.</p>
- *
- * @return a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
- */
- public CdmFormFactory getFormFactory() {
+
+ @Override
+ public CdmFormFactory getFormFactory() {
return formFactory;
}
-
+
/**
* Delegates the focus to <code>this</code> elements main input control
*/
public void setFocus(){
- // Override in subclasses where needed
+ // Override in subclasses where needed
}
-
+
/**
* Returns all Controls that are managed by this element
*
* @return a {@link java.util.Set} object.
*/
- public Set<Control> getControls(){
+ @Override
+ public Set<Control> getControls(){
return controls;
}
-
+
/**
* adds the control to the set of controls that are managed by this element
*
protected void addControl(Control child){
controls.add(child);
}
-
- /**
- * <p>removeControl</p>
- *
- * @param child a {@link org.eclipse.swt.widgets.Control} object.
- */
+
protected void removeControl(Control child){
controls.remove(child);
}
-
+
/**
* <p>Getter for the field <code>elements</code>.</p>
*
* @return a {@link java.util.Set} object.
*/
- public Set<ICdmFormElement> getElements(){
+ @Override
+ public Set<ICdmFormElement> getElements(){
return elements;
}
-
+
/**
* <p>Getter for the field <code>parentElement</code>.</p>
*
* @return a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
*/
- public ICdmFormElement getParentElement(){
+ @Override
+ public ICdmFormElement getParentElement(){
return parentElement;
}
-
+
/** {@inheritDoc} */
- public void addElement(ICdmFormElement element){
+ @Override
+ public void addElement(ICdmFormElement element){
elements.add(element);
}
-
+
/**
- * <p>removeElement</p>
- *
- * @param element a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
+ * Remove all child {@link ICdmFormElement}s and child {@link Control}s
+ * of the given and the element itself.
+ * @param formElement The element to remove
*/
- protected void removeElement(ICdmFormElement element){
- elements.remove(element);
+ public void removeElementsAndControls(ICdmFormElement formElement){
+ for(ICdmFormElement childElement : formElement.getElements()){
+ // recursion
+ childElement.removeElements();
+
+ // unregister selection arbitrator
+ if(childElement instanceof ISelectableElement){
+ SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
+ if(selectionArbitrator != null){
+ formFactory.destroySelectionArbitrator(selectionArbitrator);
+ }
+ }
+
+ // unregister from property changes
+ formFactory.removePropertyChangeListener(childElement);
+
+ // dispose of the controls
+ removeControls(childElement);
+ }
+ removeControls(formElement);
}
-
+
/**
- * <p>removeElements</p>
+ * Removes all child {@link ICdmFormElement}s and child {@link Control}s
+ * and the element itself.
*/
- public void removeElements(){
- for(ICdmFormElement childElement : getElements()){
+ @Override
+ public void removeElements(){
+ for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
+ ICdmFormElement childElement = iterator.next();
// recursion
childElement.removeElements();
-
+
// unregister selection arbitrator
if(childElement instanceof ISelectableElement){
SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
formFactory.destroySelectionArbitrator(selectionArbitrator);
}
}
-
+
// unregister from property changes
- if(childElement instanceof IPropertyChangeListener){
- formFactory.removePropertyChangeListener((IPropertyChangeListener) childElement);
- }
-
+ formFactory.removePropertyChangeListener(childElement);
+
// dispose of the controls
removeControls(childElement);
}
+ if(this instanceof ISelectableElement){
+ SelectionArbitrator selectionArbitrator = ((ISelectableElement) this).getSelectionArbitrator();
+ if(selectionArbitrator != null){
+ formFactory.destroySelectionArbitrator(selectionArbitrator);
+ }
+ }
removeControls(this);
elements.clear();
}
-
+
private void removeControls(ICdmFormElement element){
if(element instanceof Section){
((Section) element).dispose();
}
}
}
-
+
/**
* <p>Getter for the field <code>layoutComposite</code>.</p>
*
* @return a {@link org.eclipse.swt.widgets.Composite} object.
*/
- public Composite getLayoutComposite() {
+ @Override
+ public Composite getLayoutComposite() {
return layoutComposite;
}
-
+
/**
* <p>Setter for the field <code>layoutComposite</code>.</p>
*
public void setLayoutComposite(Composite layoutComposite){
this.layoutComposite = layoutComposite;
}
-
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.ICdmFormComposite#getPropertyChangeListeners()
- */
+
/**
* <p>Getter for the field <code>propertyChangeListeners</code>.</p>
*
* @return a {@link java.util.Set} object.
*/
- public List<IPropertyChangeListener> getPropertyChangeListeners() {
+ @Override
+ public List<IPropertyChangeListener> getPropertyChangeListeners() {
return propertyChangeListeners;
}
-
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.ICdmFormComposite#setPropertyChangeListeners(org.eclipse.core.runtime.ListenerList)
- */
+
/** {@inheritDoc} */
- public void setPropertyChangeListeners(List<IPropertyChangeListener> propertyChangeListeners){
+ @Override
+ public void setPropertyChangeListeners(List<IPropertyChangeListener> propertyChangeListeners){
this.propertyChangeListeners = propertyChangeListeners;
}
-
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.IPropertyChangeEmitter#firePropertyChangeEvent()
- */
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.ICdmFormComposite#firePropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent)
- */
+
/** {@inheritDoc} */
- public void firePropertyChangeEvent(CdmPropertyChangeEvent event) {
+ @Override
+ public void firePropertyChangeEvent(CdmPropertyChangeEvent event) {
+ //TODO: replace propertyChangeListeners with formFactory.getPropertyChangeListeners() and remove member propertyChangeListeners from AbstractCdmFormElement
Assert.isNotNull(propertyChangeListeners, "Property change listeners are not present");
try{
((IPropertyChangeListener)listener).propertyChange(event);
}
}catch(ConcurrentModificationException e){
- // There are two cases that produce a CME.
+ // There are two cases that produce a CME.
// Described here: http://dev.e-taxonomy.eu/trac/ticket/2363#comment:2
// and here: http://dev.e-taxonomy.eu/trac/ticket/2438
// Ignoring the CME because nothing bad is happening
- StoreUtil.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
+ MessagingUtils.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
}
}
-
+
/**
* Fires a {@link CdmPropertyChangeEvent} with the given object as source.
- *
+ *
* @param object the object on which the property changed
*/
public void firePropertyChangeEvent(Object object){
firePropertyChangeEvent(object, null);
}
-
+
/**
* Fires a {@link CdmPropertyChangeEvent} with the given object as source also containing the
* originating event
- *
+ *
* @param object the object on which the property changed
* @param originatingEvent the originating event
*/
public void firePropertyChangeEvent(Object object, PropertyChangeEvent originatingEvent){
firePropertyChangeEvent(new CdmPropertyChangeEvent(object, originatingEvent));
}
-
+
/**
* {@inheritDoc}
* Implementations should also check for null PropertyChangeEvents and return immediately in that case.
* @see eu.etaxonomy.taxeditor.ui.element.ICdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
*/
- public void propertyChange(PropertyChangeEvent event) {
+ @Override
+ public void propertyChange(PropertyChangeEvent event) {
// implement in subclasses
}
-
+
/** {@inheritDoc} */
- public boolean containsFormElement(ICdmFormElement formElement){
+ @Override
+ public boolean containsFormElement(ICdmFormElement formElement){
if(formElement == this){
return true;
}else{
return false;
}
}
-
- /*
- * (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.ICdmFormElement#refresh()
- */
- /**
- * <p>refresh</p>
- */
- public void refresh() {
- // empty default implementation
+
+ @Override
+ public void refresh() {
+ // empty default implementation
}
-
+
/** {@inheritDoc} */
@Override
element.setBackground(color);
}
}
-
+
@Override
public void setPersistentBackground(Color color) {
persistentBackgroundColor = color;
setBackground(color);
}
-
+
@Override
public Color getPersistentBackground() {
return persistentBackgroundColor;
}
-
+
public Color getColor(String colorId){
- return StoreUtil.getColor(colorId);
+ return AbstractUtility.getColor(colorId);
}
}