cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / supplemental / AbstractOriginalSourceElement.java
index 33a8c366b51a8172ab2c3cac7d191eedfdc3ea88..1fd7c9d296495316d6a404574a56719b724015c8 100644 (file)
@@ -6,33 +6,52 @@
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.taxeditor.ui.section.supplemental;
 
-import java.util.Comparator;
-
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionListener;
-
-import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
-import eu.etaxonomy.cdm.model.common.OriginalSourceType;
+import org.eclipse.swt.widgets.Text;
+
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+import eu.etaxonomy.cdm.model.common.TimePeriod;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.reference.ICdmTarget;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
+import eu.etaxonomy.taxeditor.ui.combo.OriginalSourceTypeComparator;
 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
+import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
+import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionElement;
 import eu.etaxonomy.taxeditor.ui.section.common.ExternalLinksSection;
+import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 
 /**
  * @author n.hoffmann
  * @created Mar 16, 2010
- * @version 1.0
  */
-public abstract class AbstractOriginalSourceElement<T extends OriginalSourceBase> extends AbstractReferencedEntityElement<T> {
+public abstract class AbstractOriginalSourceElement<T extends OriginalSourceBase>
+        extends AbstractEntityCollectionElement<T> {
+
+    private SelectionArbitrator selectionArbitrator;
+
+    protected boolean isCommonNameReference = false;
+    protected EntitySelectionElement<Reference> selection_reference;
+    protected TextWithLabelElement text_referenceDetail;
+    protected TimePeriodElement accessed;
        protected EnumComboElement<OriginalSourceType> combo_origsourcetype;
        protected TextWithLabelElement text_idInSource;
        protected TextWithLabelElement text_idNamespace;
-    protected TextWithLabelElement text_originaleNameString;
+    protected TextWithLabelElement text_originalInfo;
+    protected TextWithLabelElement text_cdmsource;
 
     protected ExternalLinksSection externalLinks;
 
@@ -40,123 +59,138 @@ public abstract class AbstractOriginalSourceElement<T extends OriginalSourceBase
                        AbstractFormSection section,
                        T element, SelectionListener removeListener,
                        int style, boolean isCommonNameReference) {
-               super(formFactory, section, element, removeListener, style, isCommonNameReference);
+           super(formFactory, section, element, removeListener, null, style);
+               if(formFactory.getSelectionProvider() != null){
+            selectionArbitrator = formFactory.createSelectionArbitrator(this);
+        }
+        this.isCommonNameReference = isCommonNameReference;
        }
 
        public AbstractOriginalSourceElement(CdmFormFactory formFactory,
             AbstractFormSection section,
             T element, SelectionListener removeListener,
             int style) {
-        super(formFactory, section, element, removeListener, style);
+           this(formFactory, section, element, removeListener, style, false);
     }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public void setEntity(T entity) {
-           super.setEntity(entity);
-        text_originaleNameString.setText(entity.getOriginalNameString());
-        externalLinks.setEntity(entity);
+               this.entity = entity;
+               if (selection_reference != null){
+                   selection_reference.setEntity(entity.getCitation());
+               text_referenceDetail.setText(entity.getCitationMicroReference());
+               text_originalInfo.setText(entity.getOriginalInfo());
+
+               ICdmTarget cdmSource = entity.getCdmSource();
+               if (cdmSource != null){
+               String source = "";
+               if(cdmSource instanceof IdentifiableEntity) {
+                   source = ((IdentifiableEntity) cdmSource).getTitleCache();
+               } else if(cdmSource instanceof CdmBase) {
+                   source += CdmUtils.isNotBlank(source)?"\nUUID: ":"";
+                   source += ((CdmBase) cdmSource).getUuid().toString();
+               } else {
+                   source = cdmSource.toString();
+               }
+               text_cdmsource.setText(source);
+               }
+               if (externalLinks != null){
+                   externalLinks.setEntity(entity);
+               }
+               }
        }
 
-       /** {@inheritDoc}
+       /**
         * @wbp.parser.entryPoint*/
        @Override
        public void createControls(ICdmFormElement formElement, int style) {
+           if (isCommonNameReference && PreferencesUtil.getFilterCommonNameReferences()){
+          selection_reference = formFactory
+                    .createCommonNameReferenceSelectionElement(formElement, "Reference",
+                            null, EntitySelectionElement.ALL, style);
+        }else{
+           selection_reference = formFactory
+                      .createSelectionElement(Reference.class,
+                               formElement, "Reference",
+                              null, EntitySelectionElement.ALL, style);
+        }
+
+        text_referenceDetail = formFactory.createTextWithLabelElement(
+                  formElement, "Reference Detail", null, SWT.NULL);
+
+        //TODO preliminary #10057
+        if (entity != null && entity.getCitation() != null && entity.getCitation().isDynamic()){
+            TimePeriod acc =entity.getAccessed(); // (entity != null? entity.getAccessed(): null);
+            accessed = formFactory.createTimePeriodElement(formElement, "Accessed", acc, style);
+        }
+
                combo_origsourcetype = formFactory
                                .createEnumComboElement(OriginalSourceType.class,
-                                               formElement, new OriginalSourceTypeComparator(getEntity()), style);
-               super.createControls(formElement, style);
-               text_idInSource = formFactory.createTextWithLabelElement(formElement, "ID in Source", null, style);
-               text_idNamespace = formFactory.createTextWithLabelElement(formElement, "ID Namespace", null, style);
-               text_originaleNameString = formFactory.createTextWithLabelElement(
+                                               formElement, new OriginalSourceTypeComparator(getEntity()), style, false);
+
+               if (PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSource.getKey())){
+                   text_idInSource = formFactory.createTextWithLabelElement(formElement, "ID in Source", null, style);
+               }
+               if (PreferencesUtil.getBooleanValue(PreferencePredicate.ShowNamespaceInSource.getKey())){
+                   text_idNamespace = formFactory.createTextWithLabelElement(formElement, "ID Namespace", null, style);
+               }
+               text_originalInfo = formFactory.createTextWithLabelElement(
                        formElement, "Original Information", null, SWT.NULL);
 
+               //we only show the cdm source if it exist.
+               //It can't be set during editing as it is read only.
+               if (this.entity != null && this.entity.getCdmSource() != null) {
+                   text_cdmsource = formFactory.createTextWithLabelElement(
+                    formElement, "CDM Source", null, SWT.NULL);
+                   ((Text) text_cdmsource.getMainControl()).setEditable(false);
+        }
 
-
+               if (entity != null){
+                       selection_reference.setEntity(entity.getCitation());
+               text_referenceDetail.setText(entity.getCitationMicroReference());
+               text_originalInfo.setText(entity.getOriginalInfo());
+
+               ICdmTarget cdmSource = entity.getCdmSource();
+               if (cdmSource != null){
+                   String source = "";
+                   if(cdmSource instanceof IdentifiableEntity) {
+                       source = ((IdentifiableEntity) cdmSource).getTitleCache();
+                   } else if(cdmSource instanceof CdmBase) {
+                       source += CdmUtils.isNotBlank(source)?"\nUUID: ":"";
+                       source += ((CdmBase) cdmSource).getUuid().toString();
+                   } else {
+                       source = cdmSource.toString();
+                   }
+                   text_cdmsource.setText(source);
+               }
+               if (externalLinks != null){
+                       externalLinks.setEntity(entity);
+               }
+               }
        }
 
-       /**
-        * Sorts source type combo alphabetically <b>except</b> for PrimaryTaxonomicSource resp.
-        * PrimaryMediaSource which are always on top depending on the IdentifiableSource;
-        * <br>
-        * The last two are always "Other" followed by "Unknown"
-        * @author pplitzner
-        * @date Jan 6, 2017
-        *
-        */
-       private class OriginalSourceTypeComparator implements Comparator<OriginalSourceType>{
-
-           private OriginalSourceBase entity;
-
-        public OriginalSourceTypeComparator(OriginalSourceBase entity) {
-            this.entity = entity;
+    @Override
+    public void handleEvent(Object eventSource) {
+        if(eventSource == text_idInSource){
+            getEntity().setIdInSource(text_idInSource.getText());
         }
-
-        /**
-         * {@inheritDoc}
-         */
-        @Override
-        public int compare(OriginalSourceType o1, OriginalSourceType o2) {
-            if(o1==null){
-                if(o2==null){
-                    return 0;
-                }
-                else{
-                    return -1;
-                }
-            }
-            if(o2==null){
-                return 1;
-            }
-            //both are either taxonomic or media
-            else if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)
-                    && o2.equals(OriginalSourceType.PrimaryMediaSource)){
-                if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){
-                    return 1;
-                }
-                else{
-                    return -1;
-                }
-            }
-            else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)
-                    && o1.equals(OriginalSourceType.PrimaryMediaSource)){
-                if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){
-                    return -1;
-                }
-                else{
-                    return 1;
-                }
-            }
-            //one is not taxonomic or media
-            else if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)
-                    ||o1.equals(OriginalSourceType.PrimaryMediaSource)){
-                return -1;
-
-            }
-            else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)
-                    || o2.equals(OriginalSourceType.PrimaryMediaSource)){
-                return 1;
-            }
-            //The last two are always "Other" followed by "Unknown"
-            else if(o1.equals(OriginalSourceType.Other)){
-                if(o2.equals(OriginalSourceType.Unknown)){
-                    return -11;
-                }
-                else{
-                    return 1;
-                }
-            }
-            if(o2.equals(OriginalSourceType.Other)){
-                return 1;
-            }
-            else{
-                String message1 = o1.getKey();
-                String message2 = o2.getKey();
-                return message1.compareTo(message2);
-            }
+        else if(eventSource == text_idNamespace){
+            getEntity().setIdNamespace(text_idNamespace.getText());
         }
-
-       }
-}
+        else if(eventSource == selection_reference){
+            getEntity().setCitation(selection_reference.getSelection());
+        }
+        else if(eventSource == text_referenceDetail){
+            getEntity().setCitationMicroReference(text_referenceDetail.getText());
+        }
+        else if(eventSource == text_originalInfo){
+            getEntity().setOriginalInfo(text_originalInfo.getText());
+        }
+        else if(eventSource == combo_origsourcetype){
+            getEntity().setType(combo_origsourcetype.getSelection());
+        }
+        else if(eventSource == accessed){
+            getEntity().setAccessed(accessed.getTimePeriod());
+        }
+    }
+}
\ No newline at end of file