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