minor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / AbstractEntityCollectionSection.java
index ecc7c9107e8a6ed102df8893b5465e78a04a5525..7ea27db1ea146b2ce95020f1f9c33c6b631a440e 100644 (file)
@@ -3,13 +3,22 @@
  */
 package eu.etaxonomy.taxeditor.ui.section;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Observable;
+import java.util.Observer;
 
 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;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
@@ -24,11 +33,15 @@ import org.eclipse.ui.forms.widgets.ExpandableComposite;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.model.permission.CRUD;
 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.CdmStore;
+import eu.etaxonomy.taxeditor.store.LoginManager;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
@@ -44,51 +57,49 @@ import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
  * @version $Id: $
  */
 
-public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener{
+public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener, Observer {
 
-       protected Composite container;
+    private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
+
+    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.element.ICdmFormElement} object.
-        * @param style a int.
-        * @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.
-        */
+    private AbstractEntityCollectionElement<ENTITY> entityCollectionElement;
+
        public AbstractEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
                super(formFactory, parentElement, ExpandableComposite.CLIENT_INDENT | style);
                this.title = title;
                this.setText(getTitleString());
-               showToolbar();
+               updateToolbar();
 
                addExpansionListener(this);
+
+               CdmStore.getLoginManager().addObserver(this);
+               addDisposeListener(new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                CdmStore.getLoginManager().deleteObserver(AbstractEntityCollectionSection.this);
+            }
+        });
        }
 
        protected Control createToolbar() {
                ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
 
-               Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
-                       /* (non-Javadoc)
-                        * @see org.eclipse.jface.action.Action#run()
-                        */
+               Action addAction = new Action("Add", IAction.AS_PUSH_BUTTON){
                        @Override
                        public void run() {
-                               ELEMENT element = createNewElement();
-                               if(element != null){
-                                       addElement(element);
-                                       if(! getSection().isExpanded()) {
-                        getSection().setExpanded(true);
-                    }
-                                       internalUpdateSection(true);
-                               }
+                           ELEMENT element = createNewElement();
+                           if(element != null){
+                               addElement(element);
+                               if(! getSection().isExpanded()) {
+                                   getSection().setExpanded(true);
+                               }
+                               internalUpdateSection(true);
+                           }
                        }
                };
                addAction.setImageDescriptor(new ImageDescriptor() {
@@ -100,24 +111,59 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                });
                addAction.setToolTipText(getTooltipString());
 
+               Action browseAction = null;
+               if(allowAddExisting()){
+                   browseAction = new Action("Browse", IAction.AS_PUSH_BUTTON){
+                       @Override
+                       public void run() {
+                           ELEMENT element = addExisting();
+                           if(element != null){
+                               addElement(element);
+                               if(! getSection().isExpanded()) {
+                                   getSection().setExpanded(true);
+                               }
+                               internalUpdateSection(true);
+                           }
+                       }
+                   };
+                   browseAction.setImageDescriptor(new ImageDescriptor() {
+
+                       @Override
+                       public ImageData getImageData() {
+                           return ImageResources.getImage(ImageResources.BROWSE_ICON).getImageData();
+                       }
+                   });
+                   browseAction.setToolTipText("Browse");
+               }
+
                toolBarManager.add(addAction);
+               if(browseAction!=null){
+                   toolBarManager.add(browseAction);
+               }
+
+               addAction(toolBarManager);
 
                return toolBarManager.createControl(this);
        }
 
+    protected void addAction(ToolBarManager toolBarManager) {
+        // default implementation empty
+    }
+
+    /**
+        * using this method is discouraged, use updateToolBar() instead
+        */
        public void showToolbar(){
                setTextClient(createToolbar());
        }
 
+    /**
+     * using this method is discouraged, use updateToolBar() instead
+     */
        public void removeToolbar(){
                setTextClient(null);
        }
 
-       /**
-        * <p>setEntity</p>
-        *
-        * @param entity a ENTITY object.
-        */
        @Override
        public void setEntity(ENTITY entity) {
                if(entity != null){
@@ -125,6 +171,7 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                        internalUpdateSection(false);
                }
                setSectionTitle();
+               updateToolbar();
                layout();
        }
 
@@ -133,7 +180,8 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         * Override in subclasses if you want to have a different behaviour.
         */
        protected void setSectionTitle() {
-               if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
+               Collection<ELEMENT> collection = getCollection(getEntity());
+               if(collection != null && collection.size() > 0){
                        this.setText(getTitleString() + " +");
                }else{
                        this.setText(getTitleString());
@@ -153,12 +201,13 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
 
        /**
         * Call this method after dynamically changing the client area.
-        * If the options changed is set to true, will also fire a state changed
+        * If the options changed is set to <code>true</code>, will also fire a state changed
         * event to inform the user of unsaved changes.
         *
         * @param changed a boolean.
         */
        protected void internalUpdateSection(boolean changed){
+           setSectionTitle();
                destroyDynamicContent();
                if(isExpanded() || expandSectionWhenContentAvailable()) {
             renderContent(isExpanded());
@@ -173,11 +222,13 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         */
        private void renderContent(boolean forceExpansion)
        {
-               Collection<ELEMENT> elements = getCollection(getEntity());
+               Collection<ELEMENT> collection = getCollection(getEntity());
 
-               if(elements == null || elements.isEmpty()){
+               if(collection == null || collection.isEmpty()){
                        createEmptyContent();
                }else{
+                   List<ELEMENT> elements = new ArrayList<>(collection);
+                   Collections.sort(elements, getComparator());
                        createDynamicContents(elements);
                        forceExpansion = true;
                }
@@ -187,9 +238,6 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
                reflow();
        }
 
-       /**
-        * <p>createEmptyContent</p>
-        */
        protected void createEmptyContent(){
                label_empty = formFactory.createLabel(getLayoutComposite(), getEmptyString());
        }
@@ -224,12 +272,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){
-               formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
+               entityCollectionElement = formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
-        */
        /** {@inheritDoc} */
        @Override
        public void setBackground(Color color) {
@@ -276,9 +321,45 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
        }
 
        private boolean expandSectionWhenContentAvailable(){
-               return PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE);
+               return PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE, true);
        }
 
+       /**
+        * 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);
+       }
+
+       @Override
+    public void update(Observable o, Object arg){
+           if(o instanceof LoginManager){
+               updateToolbar();
+           }
+       }
+
+    protected void updateToolbar() {
+        if(getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), UPDATE)){
+            showToolbar();
+        } else {
+            removeToolbar();
+        }
+    }
+
+    public AbstractEntityCollectionElement<ENTITY> getEntityCollectionElement() {
+        return entityCollectionElement;
+    }
+
+    /**
+     * Returns the {@link Comparator} specific for the ELEMENTs
+     * which is used to sort the elements
+     * @return the comparator for ELEMENT
+     */
+    public abstract Comparator<ELEMENT> getComparator();
+
        /**
         * Get the specific collection of this entity
         *
@@ -287,6 +368,7 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         */
        public abstract Collection<ELEMENT> getCollection(ENTITY entity);
 
+
        /**
         * Create a new Element for this collection
         *
@@ -301,6 +383,23 @@ public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends A
         */
        public abstract void addElement(ELEMENT element);
 
+       /**
+        * Add an existing element to the entities collection.
+        * @return the existing element
+        */
+       public abstract ELEMENT addExisting();
+
+       /**
+        * If <code>true</code> the section will also display
+        * a browse icon to choose from existing elements.
+        * <br>
+        * <b>Note:</b> when returning true you have to make sure
+        * to implement the {@link #addExisting()} method
+        * @return true if existing entities can be added;
+        * false otherwise
+        */
+       public abstract boolean allowAddExisting();
+
        /**
         * Remove an element from the entities collection
         *