cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / supplemental / AbstractOriginalSourceElement.java
index e004ba3f3ba908682ffb4d47da0599c8f97d9c8f..1fd7c9d296495316d6a404574a56719b724015c8 100644 (file)
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * 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 org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.widgets.Text;
 
-import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
+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;
 
 /**
- * <p>Abstract AbstractOriginalSourceElement class.</p>
- *
  * @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_originalInfo;
+    protected TextWithLabelElement text_cdmsource;
+
+    protected ExternalLinksSection externalLinks;
 
-       /**
-        * <p>Constructor for AbstractOriginalSourceElement.</p>
-        *
-        * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
-        * @param section a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection} object.
-        * @param element a T object.
-        * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
-        * @param style a int.
-        * @param <T> a T object.
-        */
        public AbstractOriginalSourceElement(CdmFormFactory formFactory,
                        AbstractFormSection section,
                        T element, SelectionListener removeListener,
-                       int style) {
-               super(formFactory, section, element, removeListener, style);
+                       int style, boolean isCommonNameReference) {
+           super(formFactory, section, element, removeListener, null, style);
+               if(formFactory.getSelectionProvider() != null){
+            selectionArbitrator = formFactory.createSelectionArbitrator(this);
+        }
+        this.isCommonNameReference = isCommonNameReference;
        }
-       
-       /** {@inheritDoc} */
+
+       public AbstractOriginalSourceElement(CdmFormFactory formFactory,
+            AbstractFormSection section,
+            T element, SelectionListener removeListener,
+            int style) {
+           this(formFactory, section, element, removeListener, style, false);
+    }
+
        @Override
-       public void init() {
-               super.init();
-               setHasOriginalNameString(true);
+       public void setEntity(T 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) {
-               text_idInSource = formFactory.createTextWithLabelElement(formElement, "Id In Source", null, style);
-               text_idNamespace = formFactory.createTextWithLabelElement(formElement, "ID Namespace", null, style);
-               super.createControls(formElement, 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, 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);
+               }
+               }
        }
-}
+
+    @Override
+    public void handleEvent(Object eventSource) {
+        if(eventSource == text_idInSource){
+            getEntity().setIdInSource(text_idInSource.getText());
+        }
+        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