Merge branch 'develop' into nameEditorE4
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / ProtologueSection.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.ui.section.name;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15
16 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
17 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
19 import eu.etaxonomy.cdm.model.description.Feature;
20 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
21 import eu.etaxonomy.cdm.model.description.TextData;
22 import eu.etaxonomy.cdm.model.name.TaxonName;
23 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
24 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
25 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
26 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
27 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
28
29 /**
30 * <p>ProtologueSection class.</p>
31 *
32 * @author n.hoffmann
33 * @created Nov 5, 2009
34 */
35 public class ProtologueSection extends AbstractEntityCollectionSection<TaxonName, DescriptionElementBase> implements ITaxonBaseDetailSection{
36
37 private TaxonBase taxonBase;
38
39 /**
40 * <p>Constructor for ProtologueSection.</p>
41 *
42 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
43 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
44 * @param style a int.
45 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
46 */
47 public ProtologueSection(CdmFormFactory cdmFormFactory, ConversationHolder conversation, ICdmFormElement parentElement, int style) {
48 super(cdmFormFactory, conversation, parentElement, "Protologues / Original Publications", style);
49 }
50
51 /** {@inheritDoc} */
52 @Override
53 public DescriptionElementBase createNewElement() {
54 return TextData.NewInstance(Feature.PROTOLOGUE());
55 }
56
57
58 /** {@inheritDoc} */
59 @Override
60 public Collection<DescriptionElementBase> getCollection(TaxonName entity) {
61 Collection<DescriptionElementBase> result = new ArrayList<>();
62 List<TaxonNameDescription> descriptions = getTaxonNameDescriptions(entity);
63 for (TaxonNameDescription desc : descriptions){
64 result.addAll(desc.getElements());
65 }
66 return result;
67 //OLD: return getTaxonNameDescription(entity).getElements();
68 }
69
70
71 /** {@inheritDoc} */
72 @Override
73 public String getEmptyString() {
74 return "No protologues yet.";
75 }
76
77
78 /** {@inheritDoc} */
79 @Override
80 protected String getTooltipString() {
81 return "Create a new protologue";
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public void addElement(DescriptionElementBase element) {
87 //for simplification we always use the first description to add elements here
88 getTaxonNameDescription(getEntity()).addElement(element);
89 }
90
91 /** {@inheritDoc} */
92 @Override
93 public void removeElement(DescriptionElementBase element) {
94 List<TaxonNameDescription> descriptions = getTaxonNameDescriptions(getEntity());
95 for (TaxonNameDescription desc : descriptions){
96 desc.removeElement(element);
97 }
98 }
99
100 /** {@inheritDoc} */
101 @Override
102 public void setTaxonBase(TaxonBase entity) {
103 this.taxonBase = entity;
104 TaxonName name = HibernateProxyHelper.deproxy(entity.getName());
105 setEntity(name);
106 }
107
108 /**
109 * NOTE: returns first description
110 * @param name
111 * @return
112 */
113 private TaxonNameDescription getTaxonNameDescription(TaxonName name){
114 if(name.getDescriptions().size() == 0){
115 name.addDescription(TaxonNameDescription.NewInstance(name));
116 }
117 return (TaxonNameDescription) name.getDescriptions().iterator().next();
118 }
119
120 /**
121 * Try to fix FIXME in {@link #getTaxonNameDescription(TaxonNameBase)}
122 * @param name
123 * @return
124 */
125 private List<TaxonNameDescription> getTaxonNameDescriptions(TaxonName name){
126 List<TaxonNameDescription> result = new ArrayList<>();
127 if (name == null){
128 return result;
129 }
130 for (TaxonNameDescription desc : name.getDescriptions()){
131 result.add(desc);
132 }
133 return result;
134 }
135
136 @Override
137 public TaxonBase getTaxonBase() {
138 return taxonBase;
139 }
140
141 /**
142 * {@inheritDoc}
143 */
144 @Override
145 public DescriptionElementBase addExisting() {
146 return null;
147 }
148
149 /**
150 * {@inheritDoc}
151 */
152 @Override
153 public boolean allowAddExisting() {
154 return false;
155 }
156 }