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