merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractEntityCollectionSection.java
index 9ee5eb3937efd71d202565ca44c66c1a80244e6d..ac94a5a830b07ce1861ffa76a2e4998efe2edcb3 100644 (file)
@@ -1,12 +1,12 @@
 /**
- * 
+ *
  */
 package eu.etaxonomy.taxeditor.ui.section;
 
 import java.util.Collection;
 
-import org.apache.commons.collections.CollectionUtils;
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.ToolBarManager;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.swt.SWT;
@@ -20,58 +20,62 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.ui.forms.events.ExpansionEvent;
 import org.eclipse.ui.forms.events.IExpansionListener;
-import org.eclipse.ui.forms.widgets.Section;
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.ImageResources;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.preference.Resources;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
-import eu.etaxonomy.taxeditor.ui.forms.AbstractFormSection;
-import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
-import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
-import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
+import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 
 /**
- * <p>Abstract AbstractEntityCollectionSection class.</p>
+ * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
+ * other elements of type ELEMENT to them.
+ *
+ * @param <ENTITY> A CDM entity which should be visualized by this section.
+ * @param <ELEMENT> An element that can be added (multiple times) to this entity.
  *
  * @author n.hoffmann
  * @version $Id: $
  */
+
 public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener{
-               
+
        protected Composite container;
-       
+
        private Label label_empty;
 
        private String title;
-       
+
        /**
         * <p>Constructor for AbstractEntityCollectionSection.</p>
         *
-        * @param conversation 
-        * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
+        * @param conversation
+        * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
         * @param style a int.
-        * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
+        * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
         * @param title a {@link java.lang.String} object.
         * @param <ENTITY> a ENTITY object.
         * @param <ELEMENT> a ELEMENT object.
         */
        public AbstractEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
-               super(formFactory, conversation, parentElement, Section.CLIENT_INDENT | style);
+               super(formFactory, parentElement, ExpandableComposite.CLIENT_INDENT | style);
                this.title = title;
                this.setText(getTitleString());
                showToolbar();
-               
+
                addExpansionListener(this);
        }
-       
+
        protected Control createToolbar() {
                ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
-               
-               Action addAction = new Action("add", Action.AS_PUSH_BUTTON){
+
+               Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
                        /* (non-Javadoc)
                         * @see org.eclipse.jface.action.Action#run()
                         */
@@ -80,34 +84,35 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                                ELEMENT element = createNewElement();
                                if(element != null){
                                        addElement(element);
-                                       if(! getSection().isExpanded())
-                                               getSection().setExpanded(true);
+                                       if(! getSection().isExpanded()) {
+                        getSection().setExpanded(true);
+                    }
                                        internalUpdateSection(true);
                                }
                        }
                };
                addAction.setImageDescriptor(new ImageDescriptor() {
-                       
+
                        @Override
                        public ImageData getImageData() {
                                return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
                        }
                });
                addAction.setToolTipText(getTooltipString());
-               
+
                toolBarManager.add(addAction);
-               
+
                return toolBarManager.createControl(this);
        }
-       
+
        public void showToolbar(){
                setTextClient(createToolbar());
        }
-       
+
        public void removeToolbar(){
                setTextClient(null);
        }
-       
+
        /**
         * <p>setEntity</p>
         *
@@ -115,14 +120,14 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         */
        @Override
        public void setEntity(ENTITY entity) {
-               if(entity != null && hasCollectionChanged(entity)){
+               if(entity != null){
                        super.setEntity(entity);
                        internalUpdateSection(false);
                }
                setSectionTitle();
                layout();
-       };
-               
+       }
+
        /**
         * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
         * Override in subclasses if you want to have a different behaviour.
@@ -134,7 +139,7 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                        this.setText(getTitleString());
                }
        }
-       
+
        /**
         * Removes all content from the container
         */
@@ -145,7 +150,7 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                }
                removeElements();
        }
-       
+
        /**
         * Call this method after dynamically changing the client area.
         * If the options changed is set to true, will also fire a state changed
@@ -155,87 +160,59 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         */
        protected void internalUpdateSection(boolean changed){
                destroyDynamicContent();
-               if(isExpanded() || expandSectionWhenContentAvailable())
-                       renderContent(isExpanded());
-               if(changed)
-                       firePropertyChangeEvent(this);
-       }
-
-       /**
-        * Whether the entities specific collection changed
-        * 
-        * @param newEntity
-        * @return
-        */
-       private boolean hasCollectionChanged(ENTITY newEntity){
-
-               // return true on null
-               if(getEntity() == null || newEntity == null)    return true;    
-               
-               // if the entities differ the collection has changed
-               if(! getEntity().equals(newEntity)) return true;
-               
-               Collection<ELEMENT> oldCollection = getCollection(getEntity());
-               Collection<ELEMENT> newCollection = getCollection(newEntity);
-               
-               // return true on null
-               if(oldCollection == null || newCollection == null) return true;
-               
-               // if the collections are object equal, check if the content is equal, too
-               if(oldCollection.equals(newCollection)){
-               
-                       boolean equal = CollectionUtils.isEqualCollection(oldCollection, newCollection);
-                       // return true when collections are not equal
-                       return equal ? false : true;
-               }
-               return true;
+               if(isExpanded() || expandSectionWhenContentAvailable()) {
+            renderContent(isExpanded());
+        }
+               if(changed) {
+            firePropertyChangeEvent(this);
+        }
        }
 
        /**
-        * Create the elements to be shown in this seciton client area 
+        * Create the elements to be shown in this section client area
         */
        private void renderContent(boolean forceExpansion)
        {
                Collection<ELEMENT> elements = getCollection(getEntity());
-               
+
                if(elements == null || elements.isEmpty()){
                        createEmptyContent();
                }else{
                        createDynamicContents(elements);
                        forceExpansion = true;
                }
-               
+
                this.setExpanded(forceExpansion);
-               
+
                reflow();
        }
-       
+
        /**
         * <p>createEmptyContent</p>
         */
        protected void createEmptyContent(){
                label_empty = formFactory.createLabel(getLayoutComposite(), getEmptyString());
        }
-       
+
        /**
         * Creates the widgets for the collection
         *
         * @param elements a {@link java.util.Collection} object.
         */
        protected void createDynamicContents(Collection<ELEMENT> elements)
-       {               
+       {
                int i = 0;
                for(final ELEMENT element : elements){
                        SelectionAdapter removeListener = new SelectionAdapter(){
                                @Override
                                public void widgetSelected(SelectionEvent e) {
-                                       removeElement(element);                                 
+                                       removeElement(element);
                                        internalUpdateSection(true);
                                }
                        };
                        boolean modulo = i++%2 == 0;
                        String colorResource = modulo ? Resources.COLOR_LIST_EVEN : Resources.COLOR_LIST_ODD;
-                       createElementComposite(element, removeListener, StoreUtil.getColor(colorResource));
+                       createElementComposite(element, removeListener, AbstractUtility.getColor(colorResource));
                }
        }
 
@@ -247,9 +224,9 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
         */
        protected void createElementComposite(ELEMENT element, SelectionListener removeListener, Color backgroundColor){
-               AbstractEntityCollectionElement formElement = formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
+               formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
        }
-       
+
        /* (non-Javadoc)
         * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
         */
@@ -270,7 +247,7 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
        public String getTitleString() {
                return CdmUtils.Nz(title);
        }
-       
+
        /**
         * <p>setTitleString</p>
         *
@@ -281,25 +258,37 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                setSectionTitle();
                layout();
        }
-       
+
        /** {@inheritDoc} */
-       public void expansionStateChanging(ExpansionEvent e) {
+       @Override
+    public void expansionStateChanging(ExpansionEvent e) {
 //             logger.warn("Expansion State Changing");
        }
-       
+
        /** {@inheritDoc} */
-       public void expansionStateChanged(ExpansionEvent e) {
+       @Override
+    public void expansionStateChanged(ExpansionEvent e) {
                if(isExpanded()){
                        renderContent(isExpanded());
                }else{
                        destroyDynamicContent();
                }
        }
-       
+
        private boolean expandSectionWhenContentAvailable(){
                return PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE);
        }
-       
+
+       /**
+        * Remove an element from the entities collection and update the section
+        *
+        * @param element a ELEMENT object.
+        */
+       public void removeElementAndUpdate(ELEMENT element) {
+               removeElement(element);
+               internalUpdateSection(true);
+       }
+
        /**
         * Get the specific collection of this entity
         *
@@ -307,39 +296,39 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         * @return a {@link java.util.Collection} object.
         */
        public abstract Collection<ELEMENT> getCollection(ENTITY entity);
-       
+
        /**
         * Create a new Element for this collection
         *
         * @return a ELEMENT object.
         */
        public abstract ELEMENT createNewElement();
-       
+
        /**
         * Add an element to the entities collection
         *
         * @param element a ELEMENT object.
         */
        public abstract void addElement(ELEMENT element);
-       
+
        /**
         * Remove an element from the entities collection
         *
         * @param element a ELEMENT object.
         */
        public abstract void removeElement(ELEMENT element);
-       
+
        /**
         * String to display when the collection is empty
         *
         * @return a {@link java.lang.String} object.
         */
        public abstract String getEmptyString();
-       
+
        /**
         * <p>getTooltipString</p>
         *
         * @return String to display when hovering the add button
         */
-       protected abstract String getTooltipString();   
+       protected abstract String getTooltipString();
 }