ref #10138: after parsing send event to update the details view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / reference / OriginalSourceElement.java
1 /**
2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.ui.section.reference;
10
11 import java.awt.Component;
12
13 import org.apache.commons.lang3.StringUtils;
14 import org.eclipse.jface.util.IPropertyChangeListener;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.ui.forms.widgets.TableWrapData;
17 import org.hibernate.EntityMode;
18
19 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20 import eu.etaxonomy.cdm.model.common.CdmBase;
21 import eu.etaxonomy.cdm.model.common.TimePeriod;
22 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
23 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
24 import eu.etaxonomy.cdm.model.reference.NamedSourceBase;
25 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
26 import eu.etaxonomy.cdm.model.reference.Reference;
27 import eu.etaxonomy.cdm.model.reference.ReferenceType;
28 import eu.etaxonomy.taxeditor.model.AbstractUtility;
29 import eu.etaxonomy.taxeditor.preference.Resources;
30 import eu.etaxonomy.taxeditor.store.StoreUtil;
31 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
32 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
33 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
34 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
35 import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
37 import eu.etaxonomy.taxeditor.ui.section.common.ExternalLinksSection;
38 import eu.etaxonomy.taxeditor.ui.section.taxon.TaxonBaseDetailSection;
39 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
40
41 /**
42 * @author k.luther
43 * @since Jul 1, 2020
44 */
45 public class OriginalSourceElement<T extends OriginalSourceBase>
46 extends AbstractCdmDetailElement<T>
47 implements IPropertyChangeListener{
48
49 protected ICdmFormElement composite;
50 protected EntitySelectionElement<Reference> selection_Ref;
51 protected TextWithLabelElement microReference;
52 protected TimePeriodElement accessed;
53 protected OriginalSourceAdvancedSection advancedSection;
54 protected String label = "Source";
55 protected CdmBase cdmEntity;
56 protected ExternalLinksSection externalLinks;
57 protected ReferenceType refType;
58
59 public OriginalSourceElement(CdmFormFactory formFactory, ICdmFormElement formElement, CdmBase cdmEntity, String label) {
60 super(formFactory, formElement);
61 this.cdmEntity = cdmEntity;
62 this.label = label;
63 }
64
65 @Override
66 protected void createControls(ICdmFormElement formElement, T entity, int style) {
67
68 selection_Ref = formFactory.createSelectionElement(Reference.class,
69 formElement, label, null,
70 EntitySelectionElement.ALL, style, 100);
71
72 if (entity != null){
73 selection_Ref.setEntity(entity.getCitation());
74 if (entity.getCitation() != null) {
75 refType = entity.getCitation().getType();
76 }
77
78 }
79 selection_Ref.setBackground(this.getPersistentBackground());
80 microReference = formFactory.createTextWithLabelElement(formElement, "Details", entity != null? entity.getCitationMicroReference(): "", style);
81 microReference.setIndent(10);
82 for (ICdmFormElement element: selection_Ref.getElements()){
83 element.setBackground(getPersistentBackground());
84 }
85
86 //TODO preliminary #10057
87 if (entity != null && entity.getCitation() != null && entity.getCitation().isDynamic()){
88 TimePeriod acc =entity.getAccessed(); // (entity != null? entity.getAccessed(): null);
89 accessed = formFactory.createTimePeriodElement(formElement, "Accessed", acc, style);
90 }
91
92 externalLinks = formFactory.createExternalLinksSection(formElement, "Link(s)", StoreUtil.getSectionStyle(ExternalLinksSection.class, DescriptionElementSource.class.getCanonicalName()));
93 externalLinks.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 3));
94 externalLinks.setEntity(HibernateProxyHelper.deproxy(entity));
95 externalLinks.setFont(AbstractUtility.getFont(Resources.COLOR_FONT_DEFAULT));
96 externalLinks.setEmptySectionString("No links yet.");
97
98 addControl(externalLinks);
99 addElement(externalLinks);
100 advancedSection = formFactory.createOriginalSourceAdvancedSection(formElement, null, StoreUtil.getSectionStyle(OriginalSourceAdvancedSection.class, INomenclaturalReference.class.getCanonicalName()));
101 TableWrapData layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
102 advancedSection.setLayoutData(layoutData);
103 advancedSection.setFont(AbstractUtility.getFont(Resources.COLOR_FONT_DEFAULT));
104 addControl(advancedSection);
105 addElement(advancedSection);
106 advancedSection.setBackground(this.getPersistentBackground());
107 advancedSection.setEntity(entity);
108 if (entity == null){
109 advancedSection.setExpanded(false);
110 }else if (StringUtils.isNotBlank(entity.getIdInSource()) || StringUtils.isNotBlank(entity.getIdNamespace()) || StringUtils.isNotBlank(entity.getOriginalInfo()) || (entity.getLinks() != null && !entity.getLinks().isEmpty()) || (entity instanceof DescriptionElementSource && ((NamedSourceBase)entity).getNameUsedInSource() != null)){
111 advancedSection.setExpanded(true);
112 }else{
113 advancedSection.setExpanded(false);
114 }
115 }
116
117 @Override
118 public void handleEvent(Object eventSource) {
119 if (eventSource.equals(selection_Ref)){
120
121 getEntity().setCitation(selection_Ref.getEntity());
122 ReferenceType newRefType = getEntity().getCitation().getType();
123 if (refType != null && refType.isDynamic() ^ getEntity().getCitation().getType().isDynamic()) {
124 this.refresh();
125 // this.getParentElement().refresh();
126 }
127 refType = newRefType;
128 }else if (eventSource.equals(microReference)){
129 getEntity().setCitationMicroReference(microReference.getText());
130 }else if(eventSource == accessed){
131 getEntity().setAccessed(accessed.getTimePeriod());
132 }
133 }
134
135 public void setCdmEntity(CdmBase cdmEntity) {
136 this.cdmEntity = cdmEntity;
137 }
138
139 public EntitySelectionElement<Reference> getReferenceSelectionElement(){
140 return selection_Ref;
141 }
142
143 public void setIndent(int indent){
144 selection_Ref.setIndent(indent);
145 microReference.setIndent(indent+10);
146 externalLinks.setIndent(indent+10);
147 advancedSection.setIndent(indent+10);
148 }
149
150 @Override
151 public void refresh() {
152 updateContent();
153 this.getParentElement().refresh();
154 }
155
156 @Override
157 public void fillFields() {
158 if (getEntity() != null) {
159 selection_Ref.setEntity(getEntity().getCitation()!= null? getEntity().getCitation(): null);
160 microReference.setText(getEntity().getCitationMicroReference());
161 if (accessed != null) {
162 accessed.setEntity(getEntity().getAccessed()!= null? getEntity().getAccessed(): null);
163 }
164 if (advancedSection != null) {
165 advancedSection.setEntity(getEntity());
166 }
167
168 externalLinks.setEntity(getEntity());
169
170 }
171
172 }
173
174
175
176
177 }