editor now updatable via updateSite
[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.IPropertyDescriptor;
19 import org.eclipse.ui.views.properties.IPropertySource;
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.propertysheet.reference.IReferenceSearch;
31 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource;
32 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferenceSearchDescriptor;
33 import eu.etaxonomy.taxeditor.propertysheet.type.TypeCollectionPropertySource;
34 import eu.etaxonomy.taxeditor.propertysheet.type.TypePropertyDescriptor;
35 import eu.etaxonomy.taxeditor.store.model.NameUtil;
36
37 /**
38 * @author p.ciardelli
39 * @created 11.11.2008
40 * @version 1.0
41 */
42 public class TaxonBasePropertySource implements IPropertySource {
43 private static final Logger logger = Logger
44 .getLogger(TaxonBasePropertySource.class);
45
46 private TaxonBase<?> taxonBase;
47
48 // Property unique keys
49 public static final String P_ID_TAXONNAME = "P_ID_TAXONNAME";
50 public static final String P_ID_TAXONSEC = "P_ID_TAXONSEC";
51 public static final String P_ID_TYPES = "P_ID_TYPES";
52
53 // Property display keys
54 public String P_TAXONNAME;
55 public static final String P_TAXONSEC = "Secundum";
56 public String P_TYPES = "Name Types";
57
58 public TaxonBasePropertySource(TaxonBase<?> taxon, String nameTitle) {
59 this.taxonBase = taxon;
60
61 this.P_TAXONNAME = nameTitle;
62
63 if (taxon != null && taxon.getName() != null &&
64 !NameUtil.isNameSupraSpecific(taxon.getName())) {
65 P_TYPES = "Specimen Types";
66 }
67
68 addDescriptor(P_ID_TAXONNAME);
69 addDescriptor(P_ID_TAXONSEC);
70 addDescriptor(P_ID_TYPES);
71 }
72
73 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
74
75 protected void addDescriptor(String id) {
76 if (id.equals(P_ID_TAXONNAME)) {
77 descriptors.addElement(
78 new PropertyDescriptor(P_ID_TAXONNAME, P_TAXONNAME));
79 }
80 if (id.equals(P_ID_TAXONSEC)) {
81 descriptors.addElement(new ReferenceSearchDescriptor(P_ID_TAXONSEC, P_TAXONSEC, IReferenceSearch.BIBREF) {
82 protected void saveReference(ReferenceBase reference) {
83 setPropertyValue(P_ID_TAXONSEC, reference);
84 }
85 });
86 }
87 if (id.equals(P_ID_TYPES)) {
88 descriptors.addElement(
89 new TypePropertyDescriptor(P_ID_TYPES, P_TYPES, taxonBase.getName()) {
90 protected void saveTypes(Set set) {
91 setPropertyValue(P_ID_TYPES, set);
92 }
93 }
94 );
95 };
96 }
97
98 /* (non-Javadoc)
99 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
100 */
101 public Object getEditableValue() {
102 return null;
103 }
104
105 /* (non-Javadoc)
106 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
107 */
108 public IPropertyDescriptor[] getPropertyDescriptors() {
109 return (IPropertyDescriptor[]) descriptors.toArray(
110 new IPropertyDescriptor[descriptors.size()]);
111 }
112
113 /* (non-Javadoc)
114 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
115 */
116 public Object getPropertyValue(Object id) {
117
118 // Edit taxon's name
119 if (id.equals(P_ID_TAXONNAME)) {
120
121 if (taxonBase == null) {
122 logger.warn("no taxon");
123 return null;
124 }
125 TaxonNameBase<?, ?> name = taxonBase.getName();
126
127 // Create taxon name as necessary
128 if (name == null) {
129 name = NonViralName.NewInstance(Rank.SPECIES());
130 }
131 name = CdmBase.deproxy(name, TaxonNameBase.class);
132 // Send taxon name to appropriate property source for submenu
133 if (name instanceof BotanicalName) {
134 return new BotanicalNamePropertySource((BotanicalName) name);
135 }
136 if (name instanceof ZoologicalName) {
137 return new ZoologicalNamePropertySource((ZoologicalName) name);
138 }
139 if (name instanceof NonViralName) {
140 return new NonViralNamePropertySource((NonViralName<?>) name);
141 }
142
143 }
144
145 // Edit taxon's sec. reference
146 if (id.equals(P_ID_TAXONSEC)) {
147
148 if (taxonBase == null) {
149 return null;
150 }
151 ReferenceBase<?> sec = taxonBase.getSec();
152
153 // Create property source for submenu
154 ReferencePropertySource secPropertySource = new ReferencePropertySource(sec);
155
156 // Add listener to notify taxon of all changes to sec
157 secPropertySource.addPropertyChangeListener(new PropertyChangeListener() {
158 public void propertyChange(PropertyChangeEvent evt) {
159 if (evt.getNewValue() instanceof ReferenceBase) {
160 taxonBase.setSec((ReferenceBase<?>) evt.getNewValue());
161 }
162 }
163 });
164 return secPropertySource;
165 }
166
167 if (id.equals(P_ID_TYPES)) {
168 if (taxonBase.getName() != null) {
169 // TODO return NameTypeDesignations
170 TaxonNameBase<?, ?> name = taxonBase.getName();
171 Set<?> typeDesignations = null;
172 if (NameUtil.isNameSupraSpecific(name)) {
173 typeDesignations = name.getNameTypeDesignations();
174 } else {
175 typeDesignations = name.getSpecimenTypeDesignations();
176 }
177 return new TypeCollectionPropertySource(name, typeDesignations);
178 }
179 }
180
181 return null;
182 }
183
184 /* (non-Javadoc)
185 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
186 */
187 public boolean isPropertySet(Object id) {
188 return false;
189 }
190
191 /* (non-Javadoc)
192 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
193 */
194 public void resetPropertyValue(Object id) {}
195
196 /* (non-Javadoc)
197 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
198 */
199 public void setPropertyValue(Object id, Object value) {
200 if (id.equals(P_ID_TAXONSEC)) {
201 if (value instanceof ReferenceBase) {
202 taxonBase.setSec((ReferenceBase<?>) value);
203 }
204 }
205 }
206
207 /**
208 * @return the taxonBase
209 */
210 public TaxonBase<?> getTaxonBase() {
211 return taxonBase;
212 }
213 }