fix #6437 Generate title caches to avoid outdated information
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / selection / EntitySelectionElement.java
index b7555cf7b173cfb59509d4ad7a5f9b001c00d413..30ebf05424735d0362dbfb1c6fc614e09cd73e48 100644 (file)
@@ -3,64 +3,87 @@
  */
 package eu.etaxonomy.taxeditor.ui.selection;
 
+import java.util.EnumSet;
+import java.util.Observable;
+import java.util.Observer;
+
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
 import org.springframework.security.core.GrantedAuthority;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.api.service.IService;
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.Group;
-import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
-import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.cdm.model.common.User;
+import eu.etaxonomy.cdm.model.molecular.Amplification;
+import eu.etaxonomy.cdm.model.molecular.Primer;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
 import eu.etaxonomy.taxeditor.model.ImageResources;
 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.campanula.compatibility.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.IEntitySelectionElement;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
+import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
+import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
+import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
+import eu.etaxonomy.taxeditor.ui.section.grantedAuthority.GrantedAuthorityLabelTextProvider;
 
 /**
- * <p>
- * Abstract AbstractSelectionElement class.
- * </p>
- *
  * @author n.hoffmann
  * @created Nov 17, 2009
  * @version 1.0
  * @param <T>
  */
-public class EntitySelectionElement<T extends ICdmBase> extends
-               AbstractCdmFormElement implements IEntitySelectionElement<T>{
+public class EntitySelectionElement<T extends CdmBase> extends
+               AbstractCdmFormElement implements  SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, //IConversationEnabled,
+               Observer {
+
+       private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
+       private static final EnumSet<CRUD> DELETE = EnumSet.of(CRUD.DELETE);
+       private static final EnumSet<CRUD> CREATE = EnumSet.of(CRUD.CREATE);
 
        /**
         * Bitmask for configuring functionality of selection element
         */
-       public static final int NOTHING = 0; // 000
-       public static final int EDITABLE = 1 << 0; // 001
-       public static final int DELETABLE = 1 << 1; // 010
-       public static final int SELECTABLE = 1 << 2; // 100
-       public static final int ALL = EDITABLE | DELETABLE | SELECTABLE; // 111
-
+       public static final int NOTHING = 0; // 0000
+       public static final int EDITABLE = 1 << 0; // 0001
+       public static final int DELETABLE = 1 << 1; // 0010
+       public static final int SELECTABLE = 1 << 2; // 0100
+       public static final int EXTERNAL = 1 << 3; // 1000
+       public static final int ALL = EDITABLE | DELETABLE | SELECTABLE ; // 0111
+       public static final int ALL_WITH_EXT = EDITABLE | DELETABLE | SELECTABLE | EXTERNAL ; // 1111
        protected T entity;
+       protected T filteredEntity;
 
        protected Label label;
-       protected Label text;
+       protected Text text;
        protected Button button_selection;
 
+       protected Button button_selectionExt;
+
        private SelectionArbitrator selectionArbitrator;
 
        protected Button button_edit;
@@ -74,70 +97,54 @@ public class EntitySelectionElement<T extends ICdmBase> extends
        private final boolean isEditable;
 
        private final boolean isDeletable;
+       private final boolean isExternal;
 
-       private final ConversationHolder conversation;
+//     private final ConversationHolder conversation;
        private Class<T> clazz;
 
-       /**
-        * <p>
-        * Constructor for AbstractSelectionElement.
-        * </p>
-        *
-        * @param formFactory
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
-        *            object.
-        * @param conversation
-        *            TODO
-        * @param parentElement
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
-        *            object.
-        * @param labelString
-        *            a {@link java.lang.String} object.
-        * @param entity
-        *            a T object.
-        * @param isEditable
-        *            a boolean.
-        * @param isSelectable
-        *            a boolean.
-        * @param isDeletable
-        *            a boolean.
-        * @param style
-        *            a int.
-        * @param <T>
-        *            a T object.
-        */
        public EntitySelectionElement(CdmFormFactory formFactory,
-                       ConversationHolder conversation, ICdmFormElement parentElement,
-                       String labelString, T entity, int mode, int style) {
+//                     ConversationHolder conversation,
+               ICdmFormElement parentElement, Class<T> clazz,
+                       String labelString, T entity, int mode, int style, boolean filterElement) {
                super(formFactory, parentElement);
 
+               this.clazz = clazz;
                this.isEditable = (mode & EDITABLE) == EDITABLE;
                this.isDeletable = (mode & DELETABLE) == DELETABLE;
+               this.isExternal= (mode & EXTERNAL) == EXTERNAL;
                boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
 
-               this.labelString = labelString;
+               this.labelString = (labelString == null || labelString.equals("")) ? "" : labelString;
 
-               this.conversation = conversation;
+//             this.conversation = conversation;
 
                if (isSelectable && formFactory.getSelectionProvider() != null) {
                        selectionArbitrator = formFactory.createSelectionArbitrator(this);
                }
 
                createControls(getLayoutComposite(), SWT.NULL);
-
-               setEntity(entity);
+               if (filterElement){
+                   setFilteredEntity(entity);
+               }else{
+                   setEntity(entity);
+               }
        }
 
-       public EntitySelectionElement(CdmFormFactory formFactory,
-                       ConversationHolder conversation, ICdmFormElement parentElement, Class<T> clazz,
-                       String labelString, T entity, int mode, int style) {
-               this(formFactory, conversation, parentElement, labelString, entity, mode, style);
-               this.clazz = clazz;
-       }
+    private void setFilteredEntity(T filterEntity) {
+        this.filteredEntity =filterEntity;
+
+    }
+
+    public EntitySelectionElement(CdmFormFactory formFactory,//ConversationHolder conversation,
+             ICdmFormElement parentElement, Class<T> clazz,
+            String labelString, T entity, int mode, int style){
+        this(formFactory, //conversation,
+                parentElement, clazz, labelString, entity, mode, style, false);
+    }
 
        private void createControls(Composite parent, int style) {
 
-               label = formFactory.createLabel(getLayoutComposite(), labelString + " : ",
+               label = formFactory.createLabel(getLayoutComposite(), labelString,
                                SWT.NULL);
 
                addControl(label);
@@ -151,13 +158,17 @@ public class EntitySelectionElement<T extends ICdmBase> extends
                if (isDeletable) {
                        columns += 1;
                }
+               if (isExternal) {
+            columns += 1;
+        }
 
                selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
                selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
 
                addControl(selectableComposite);
 
-               text = formFactory.createLabel(selectableComposite, null, SWT.WRAP);
+               text = formFactory.createText(selectableComposite, null, SWT.WRAP);
+               text.setEditable(false);
                addControl(text);
 
                text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
@@ -172,7 +183,16 @@ public class EntitySelectionElement<T extends ICdmBase> extends
 
                addControl(button_selection);
                button_selection.addSelectionListener(this);
-
+               if (isExternal){
+               button_selectionExt = formFactory.createButton(selectableComposite, null,
+                    SWT.PUSH);
+               button_selectionExt.setImage(ImageResources
+                    .getImage(ImageResources.BROWSE_ICON));
+               button_selectionExt.setToolTipText("Browse existing from external cdm store");
+               button_selectionExt.setText("Ext");
+            addControl(button_selectionExt);
+            button_selectionExt.addSelectionListener(this);
+               }
                if (isEditable) {
                        button_edit = formFactory.createButton(selectableComposite, null,
                                        SWT.PUSH);
@@ -196,11 +216,27 @@ public class EntitySelectionElement<T extends ICdmBase> extends
 
        @Override
     public void widgetSelected(SelectionEvent e) {
-               T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), getConversationHolder(), getEntity());
-               setSelectionInternal(selection);
+           if (e.getSource().equals(button_selection) ){
+               T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), //getConversationHolder(),
+                       getFilteredEntity(), getParentElement());
+            setSelectionInternal(selection);
+           }else{
+               Reference selection = SelectionDialogFactory.getSelectionFromExtDialog(Reference.class, getShell(),//null,
+                       getParentElement());
+            setSelectionInternal((T)selection);
+           }
+
        }
 
        /**
+     * @return
+     */
+    public T getFilteredEntity() {
+
+        return this.filteredEntity;
+    }
+
+    /**
         * Return the selected object
         *
         * @return a T object.
@@ -218,10 +254,23 @@ public class EntitySelectionElement<T extends ICdmBase> extends
        /** {@inheritDoc} */
        @Override
        public void setEnabled(boolean enabled) {
+
                button_selection.setEnabled(enabled);
+               if (isDeletable){
+                   button_remove.setEnabled(enabled);
+               }
                if (isEditable) {
-                       button_edit.setEnabled(enabled && entity != null);
+                       updateButtonStates();
                }
+
+       }
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
+        */
+       @Override
+       public boolean isEnabled() {
+           return button_selection.isEnabled();
        }
 
        /**
@@ -257,16 +306,12 @@ public class EntitySelectionElement<T extends ICdmBase> extends
         */
        protected void updateElement() {
                String title = CdmUtils.Nz(getTitle());
-               // we have to duplicate ampersands otherwise they are treated as
-               // mnenomic (see Label.setText() documentation)
-               title = title.replace("&", "&&");
                text.setText(title); // title can be null
                if (isEditable) {
-                       button_edit.setEnabled(entity != null);
+                       updateButtonStates();
                }
        }
 
-       @Override
     public void updateFromWizard() {
                updateElement();
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
@@ -281,13 +326,21 @@ public class EntitySelectionElement<T extends ICdmBase> extends
         */
        protected String getTitle() {
                if (entity != null){
-                       if(entity instanceof IIdentifiableEntity) {
-                               return ((IIdentifiableEntity) entity).getTitleCache();
-                       } else if(entity instanceof Group){
+                       if(entity instanceof Group){
                                return ((Group) entity).getName();
                        } else if(entity instanceof GrantedAuthority){
-                               return ((GrantedAuthority) entity).getAuthority();
-                       }
+                               return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
+                       } else if(entity instanceof User){
+                return ((User) entity).getUsername();
+            } else if (entity instanceof Primer){
+                return ((Primer) entity).getLabel();
+            } else if (entity instanceof Amplification){
+                return ((Amplification) entity).getLabelCache();
+            }
+            else if(entity instanceof IIdentifiableEntity) {
+                return ((IIdentifiableEntity) entity).getTitleCache();
+            }
+
                }
                return "";
        }
@@ -339,7 +392,6 @@ public class EntitySelectionElement<T extends ICdmBase> extends
         *
         * @return a {@link org.eclipse.swt.widgets.Shell} object.
         */
-       @Override
     public Shell getShell() {
                return getLayoutComposite().getShell();
        }
@@ -372,7 +424,7 @@ public class EntitySelectionElement<T extends ICdmBase> extends
 
        private class EditListener extends SelectionAdapter {
 
-               private final EntitySelectionElement<T> selectionElement;
+        private final EntitySelectionElement<T> selectionElement;
 
                public EditListener(EntitySelectionElement<T> selectionElement) {
                        this.selectionElement = selectionElement;
@@ -385,6 +437,16 @@ public class EntitySelectionElement<T extends ICdmBase> extends
                                        new EditFromSelectionWizard(selectionElement));
                        if (dialog.open() == IStatus.OK) {
                                selectionElement.updateFromWizard();
+                               //if the edited entity has already been persisted
+                               //but the transient entity is still set in this
+                               //EntitySelectionElement, re-load it and set it
+                               IService<T> service = CdmStore.getService(entity);
+                               if(entity.getId()==0){
+                                       T loadedEntity = service.load(entity.getUuid());
+                                       if(loadedEntity!=null){
+                                               setEntity(loadedEntity);
+                                       }
+                               }
                        }
                }
        }
@@ -403,15 +465,20 @@ public class EntitySelectionElement<T extends ICdmBase> extends
         * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
         *         object.
         */
-       @Override
+//     @Override
        public ConversationHolder getConversationHolder() {
-               return conversation;
+           if(getParentElement() instanceof IConversationEnabled) {
+            return ((IConversationEnabled)getParentElement()).getConversationHolder();
+        }
+           return null;
        }
 
        /** {@inheritDoc} */
        @Override
        public void setBackground(Color color) {
-               label.setBackground(color);
+           if(!label.isDisposed()){
+               label.setBackground(color);
+           }
        }
 
        /** {@inheritDoc} */
@@ -438,7 +505,30 @@ public class EntitySelectionElement<T extends ICdmBase> extends
        }
 
        /** {@inheritDoc} */
+//     @Override
+//     public void update(CdmDataChangeMap changeEvents) {
+//     }
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
+        */
        @Override
-       public void update(CdmDataChangeMap changeEvents) {
+    public void removeElements(){
+               super.removeElements();
+               LoginManager loginManager = CdmStore.getLoginManager();
+               loginManager.addObserver(this);
+       }
+
+       @Override
+       public void update(Observable o, Object arg) {
+               if(o instanceof LoginManager){
+                       updateButtonStates();
+               }
+       }
+
+       private void updateButtonStates() {
+           if(button_edit != null && !button_selection.isDisposed()){
+               button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null  && CdmStore.currentAuthentiationHasPermission(getEntity(), UPDATE));
+           }
        }
 }