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