Removed "Citation*" and "Microcitation" from labels. Removed librarycentric helper...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / name / TaxonBasePropertySource.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.propertysheet.name;
11
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.util.Set;
15 import java.util.Vector;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
19 import org.eclipse.ui.views.properties.IPropertyDescriptor;
20 import org.eclipse.ui.views.properties.PropertyDescriptor;
21
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.name.BotanicalName;
24 import eu.etaxonomy.cdm.model.name.NonViralName;
25 import eu.etaxonomy.cdm.model.name.Rank;
26 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.name.ZoologicalName;
28 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.taxeditor.model.NameHelper;
31 import eu.etaxonomy.taxeditor.propertysheet.AnnotationPropertySource;
32 import eu.etaxonomy.taxeditor.propertysheet.AnnotationsPropertyDescriptor;
33 import eu.etaxonomy.taxeditor.propertysheet.ExtensionPropertySource;
34 import eu.etaxonomy.taxeditor.propertysheet.ExtensionsPropertyDescriptor;
35 import eu.etaxonomy.taxeditor.propertysheet.ICdmBasePropertySource;
36 import eu.etaxonomy.taxeditor.propertysheet.MarkerPropertySource;
37 import eu.etaxonomy.taxeditor.propertysheet.MarkersPropertyDescriptor;
38 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource;
39 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferenceSearchDescriptor;
40 import eu.etaxonomy.taxeditor.propertysheet.type.TypeCollectionPropertySource;
41 import eu.etaxonomy.taxeditor.propertysheet.type.TypePropertyDescriptor;
42
43 /**
44 * @author p.ciardelli
45 * @created 11.11.2008
46 * @version 1.0
47 */
48 public class TaxonBasePropertySource implements ICdmBasePropertySource {
49 private static final Logger logger = Logger
50 .getLogger(TaxonBasePropertySource.class);
51
52 private TaxonBase<?> taxonBase;
53
54 // Property unique keys
55 public static final String P_ID_TAXONNAME = "P_ID_TAXONNAME";
56 public static final String P_ID_TAXONSEC = "P_ID_TAXONSEC";
57 public static final String P_ID_TYPES = "P_ID_TYPES";
58 public static final String P_ID_DOUBTFUL = "P_ID_DOUBTFUL";
59 public static final String P_ID_TAXON_MARKERS = "P_ID_TAXON_MARKERS";
60 public static final String P_ID_EXTENSIONS = "P_ID_EXTENSIONS";
61 public static final String P_ID_TAXON_ANNOTATIONS = "P_ID_TAXON_ANNOTATIONS";
62
63 // Property display keys
64 public String P_TAXONNAME;
65 public static final String P_TAXONSEC = "Secundum";
66 public String P_TYPES = "Name Types";
67 public static final String P_DOUBTFUL = "Doubtful?";
68 public static final String P_TAXON_MARKERS = "Markers";
69 public static final String P_EXTENSIONS = "Extensions";
70 public static final String P_TAXON_ANNOTATIONS = "Annotations";
71
72 private static final int NOT_DOUBTFUL = 0;
73 private static final int DOUBTFUL = 1;
74
75 public TaxonBasePropertySource(TaxonBase<?> taxon, String nameTitle) {
76 this.taxonBase = taxon;
77
78 this.P_TAXONNAME = nameTitle;
79
80 if (taxon != null && taxon.getName() != null &&
81 !taxon.getName().isSupraSpecific()) {
82 P_TYPES = "Specimen Types";
83 }
84
85 addDescriptor(P_ID_TAXONNAME);
86 addDescriptor(P_ID_TAXONSEC);
87 addDescriptor(P_ID_TYPES);
88 addDescriptor(P_ID_DOUBTFUL);
89 addDescriptor(P_ID_TAXON_ANNOTATIONS);
90 addDescriptor(P_ID_TAXON_MARKERS);
91 addDescriptor(P_ID_EXTENSIONS);
92 }
93
94 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
95
96 protected void addDescriptor(String id) {
97 if (id.equals(P_ID_TAXONNAME)) {
98 descriptors.addElement(
99 new PropertyDescriptor(P_ID_TAXONNAME, P_TAXONNAME));
100 }
101 if (id.equals(P_ID_TAXONSEC)) {
102 descriptors.addElement(new ReferenceSearchDescriptor(P_ID_TAXONSEC, P_TAXONSEC, taxonBase.getSec()) {
103 protected void saveReference(ReferenceBase reference) {
104 setPropertyValue(P_ID_TAXONSEC, reference);
105 }
106 });
107 }
108 if (id.equals(P_ID_TYPES)) {
109 descriptors.addElement(
110 new TypePropertyDescriptor(P_ID_TYPES, P_TYPES, taxonBase.getName()) {
111 protected void saveTypes(Set set) {
112 setPropertyValue(P_ID_TYPES, set);
113 }
114 }
115 );
116 };
117
118 if (id.equals(P_ID_DOUBTFUL)) {
119 descriptors.addElement(
120 new ComboBoxPropertyDescriptor(P_ID_DOUBTFUL, P_DOUBTFUL,
121 new String[] {"no", "yes"}));
122 }
123
124 if (id.equals(P_ID_TAXON_MARKERS)) {
125 descriptors.addElement(
126 new MarkersPropertyDescriptor(P_ID_TAXON_MARKERS, P_TAXON_MARKERS, taxonBase));
127 }
128
129 if (id.equals(P_ID_EXTENSIONS)) {
130 descriptors.addElement(
131 new ExtensionsPropertyDescriptor(P_ID_EXTENSIONS, P_EXTENSIONS, taxonBase));
132 }
133
134 // Annotations, listed in custom property descriptor
135 if (id.equals(P_ID_TAXON_ANNOTATIONS)) {
136 descriptors.addElement(
137 new AnnotationsPropertyDescriptor(P_ID_TAXON_ANNOTATIONS, P_TAXON_ANNOTATIONS, taxonBase) {
138 protected void saveAnnotations(Set set) {
139 setPropertyValue(P_ID_TAXON_ANNOTATIONS, set);
140 }
141 }
142 );
143 };
144 }
145
146 /* (non-Javadoc)
147 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
148 */
149 public Object getEditableValue() {
150 return null;
151 }
152
153 /* (non-Javadoc)
154 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
155 */
156 public IPropertyDescriptor[] getPropertyDescriptors() {
157 return (IPropertyDescriptor[]) descriptors.toArray(
158 new IPropertyDescriptor[descriptors.size()]);
159 }
160
161 /* (non-Javadoc)
162 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
163 */
164 public Object getPropertyValue(Object id) {
165
166 // Edit taxon's name
167 if (id.equals(P_ID_TAXONNAME)) {
168
169 if (taxonBase == null) {
170 logger.warn("no taxon");
171 return null;
172 }
173 TaxonNameBase<?, ?> name = taxonBase.getName();
174
175 // Create taxon name as necessary
176 if (name == null) {
177 name = NonViralName.NewInstance(Rank.SPECIES());
178 }
179 name = CdmBase.deproxy(name, TaxonNameBase.class);
180 // Send taxon name to appropriate property source for submenu
181 if (name instanceof BotanicalName) {
182 return new BotanicalNamePropertySource((BotanicalName) name);
183 }
184 if (name instanceof ZoologicalName) {
185 return new ZoologicalNamePropertySource((ZoologicalName) name);
186 }
187 if (name instanceof NonViralName) {
188 return new NonViralNamePropertySource((NonViralName<?>) name);
189 }
190
191 }
192
193 // Edit taxon's sec. reference
194 if (id.equals(P_ID_TAXONSEC)) {
195
196 if (taxonBase == null) {
197 return null;
198 }
199 ReferenceBase<?> sec = taxonBase.getSec();
200
201 // Create property source for submenu
202 ReferencePropertySource secPropertySource = new ReferencePropertySource(sec);
203
204 // Add listener to notify taxon of all changes to sec
205 secPropertySource.addPropertyChangeListener(new PropertyChangeListener() {
206 public void propertyChange(PropertyChangeEvent evt) {
207 if (evt.getNewValue() instanceof ReferenceBase) {
208 taxonBase.setSec((ReferenceBase<?>) evt.getNewValue());
209 }
210 }
211 });
212 return secPropertySource;
213 }
214
215 if (id.equals(P_ID_TYPES)) {
216 if (taxonBase.getName() != null) {
217 // TODO return NameTypeDesignations
218 TaxonNameBase<?, ?> name = taxonBase.getName();
219 Set<?> typeDesignations = null;
220 if (name.isSupraSpecific()) {
221 typeDesignations = name.getNameTypeDesignations();
222 } else {
223 typeDesignations = name.getSpecimenTypeDesignations();
224 }
225 return new TypeCollectionPropertySource(name, typeDesignations);
226 }
227 }
228
229 // Edit doubtful flag
230 if (id.equals(P_ID_DOUBTFUL)) {
231 if (taxonBase.isDoubtful()) {
232 return DOUBTFUL;
233 } else {
234 return NOT_DOUBTFUL;
235 }
236 }
237
238 if (id.equals(P_ID_TAXON_MARKERS)) {
239 return new MarkerPropertySource(taxonBase);
240 }
241
242 if (id.equals(P_ID_EXTENSIONS)) {
243 return new ExtensionPropertySource(taxonBase);
244 }
245
246 if (id.equals(P_ID_TAXON_ANNOTATIONS)) {
247 return new AnnotationPropertySource(taxonBase);
248 }
249
250 return null;
251 }
252
253 /* (non-Javadoc)
254 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
255 */
256 public boolean isPropertySet(Object id) {
257 return false;
258 }
259
260 /* (non-Javadoc)
261 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
262 */
263 public void resetPropertyValue(Object id) {}
264
265 /* (non-Javadoc)
266 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
267 */
268 public void setPropertyValue(Object id, Object value) {
269 if (id.equals(P_ID_TAXONSEC)) {
270 if (value instanceof ReferenceBase) {
271 taxonBase.setSec((ReferenceBase<?>) value);
272 }
273 }
274
275 if (id.equals(P_ID_DOUBTFUL)) {
276 if (((Integer) value).intValue() == DOUBTFUL) {
277 taxonBase.setDoubtful(true);
278 } else {
279 taxonBase.setDoubtful(false);
280 }
281 }
282 }
283
284 /**
285 * @return the taxonBase
286 */
287 public TaxonBase<?> getTaxonBase() {
288 return taxonBase;
289 }
290 }