Enabled keyboard navigation in editor pages and uploaded a new feature.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / description / DescriptionElementPropertySource.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.description;
11
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Vector;
17
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 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
22
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.cdm.model.description.Distribution;
27 import eu.etaxonomy.cdm.model.description.Feature;
28 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
29 import eu.etaxonomy.cdm.model.description.QuantitativeData;
30 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
31 import eu.etaxonomy.cdm.model.description.TextData;
32 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
33 import eu.etaxonomy.taxeditor.propertysheet.ICdmBasePropertySource;
34 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource;
35 import eu.etaxonomy.taxeditor.store.model.DescriptionUtil;
36
37 /**
38 * @author p.ciardelli
39 * @created 29.08.2008
40 * @version 1.0
41 */
42 public class DescriptionElementPropertySource implements ICdmBasePropertySource {
43
44 /**
45 * The DescriptionElement whose properties are being displayed
46 */
47 private DescriptionElementBase descriptionElement;
48
49 /**
50 * If this is a property with a parent, the parent's property ID
51 */
52 private String parentid;
53
54 /**
55 * Property unique keys
56 */
57 public static final String P_ID_ELEMENT_CLASS = "element_class";
58 public static final String P_ID_ELEMENT_TEXT = "text";
59 public static final String P_ID_FEATURE = "feature";
60 public static final String P_ID_BIBREF = "bibref";
61 public static final String P_ID_MICROREF = "microref";
62
63 /**
64 * Property display keys
65 */
66 public static final String P_ELEMENT_CLASS = "Save element as";
67 public static final String P_ELEMENT_TEXT = "Element";
68 public static final String P_FEATURE = "Feature";
69 public static final String P_BIBREF = "Bibliographic Reference";
70 public static final String P_MICROREF = "Reference Detail";
71
72 protected static final String[] TOP_LEVEL_PROPERTIES = new String[] {
73 // P_ID_ELEMENT_TEXT,
74 P_ID_FEATURE,
75 P_ID_BIBREF,
76 P_ID_MICROREF};
77
78 private String[] P_FEATURE_CLASS_NAME_MENU;
79
80 /**
81 * Constructor for top level property fields. All fields that are not subfields
82 * should be listed here.
83 * @param name
84 */
85 public DescriptionElementPropertySource(DescriptionElementBase descriptionElement) {
86 this(descriptionElement, null, TOP_LEVEL_PROPERTIES);
87 }
88
89 public DescriptionElementPropertySource(DescriptionElementBase descriptionElement, String parentid,
90 String[] keys) {
91 this.descriptionElement = descriptionElement;
92 this.setParentid(parentid);
93 for (String key : keys) {
94 addDescriptor(key);
95 }
96 }
97
98 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
99 /**
100 * Add descriptor for a given property.
101 *
102 * Notes on Descriptor:
103 *
104 * PropertyDescriptor - uneditable cell value
105 * ComboBoxPropertyDescriptor - dropdown list, property supplied must be integer-based
106 * TextPropertyDescriptor - editable cell value
107 *
108 * If any descriptor calls setCategory, all descriptors w/out a category are put in
109 * category "Misc".
110 *
111 * descriptor.setFilterFlags (new String[] { IPropertySheetEntry.FILTER_ID_EXPERT }) -
112 * this descriptor shown under advanced properties
113 *
114 * @param id
115 */
116 protected void addDescriptor(String id) {
117 if (id.equals(P_ID_ELEMENT_CLASS)) {
118
119 Feature feature = descriptionElement.getFeature();
120 Map<Class<?>, String> supportedClasses = new HashMap<Class<?>, String>();
121
122 // Populate feature class arrays with classes supported by this feature
123 if (feature.supportsCommonTaxonName()) {
124 supportedClasses.put(CommonTaxonName.class, "Common name");
125 }
126 if (feature.supportsDistribution()) {
127 supportedClasses.put(Distribution.class, "Distribution");
128 }
129 if (feature.supportsIndividualAssociation()) {
130 supportedClasses.put(IndividualsAssociation.class, "Individual association");
131 }
132 if (feature.supportsQuantitativeData()) {
133 supportedClasses.put(QuantitativeData.class, "Quantitative data");
134 }
135 if (feature.supportsTaxonInteraction()) {
136 supportedClasses.put(TaxonInteraction.class, "Taxon interaction");
137 }
138 if (feature.supportsTextData()) {
139 supportedClasses.put(TextData.class, "Text");
140 }
141
142 P_FEATURE_CLASS_NAME_MENU = supportedClasses.values().toArray((new String[supportedClasses.size()]));
143 supportedClasses.keySet().toArray(new Class[supportedClasses.size()]);
144
145 descriptors.addElement(
146 new ComboBoxPropertyDescriptor(P_ID_ELEMENT_CLASS, P_ELEMENT_CLASS, P_FEATURE_CLASS_NAME_MENU)
147 );
148 }
149
150 if (id.equals(P_ID_FEATURE)) {
151 descriptors.addElement(new PropertyDescriptor(P_ID_FEATURE, P_FEATURE));
152 }
153
154 if (id.equals(P_ID_BIBREF)) {
155 descriptors.addElement(new PropertyDescriptor(P_ID_BIBREF, P_BIBREF));
156 }
157
158 if (id.equals(P_ID_ELEMENT_TEXT)) {
159 descriptors.addElement(new TextPropertyDescriptor(P_ID_ELEMENT_TEXT, P_ELEMENT_TEXT));
160 }
161
162 if (id.equals(P_ID_MICROREF)) {
163 descriptors.addElement(new TextPropertyDescriptor(P_ID_MICROREF, P_MICROREF));
164 }
165 }
166
167 /* (non-Javadoc)
168 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
169 */
170 public Object getEditableValue() {
171 return this;
172 }
173
174 /* (non-Javadoc)
175 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
176 */
177 public IPropertyDescriptor[] getPropertyDescriptors() {
178 return descriptors.toArray(
179 new IPropertyDescriptor[descriptors.size()]);
180 }
181
182 /* (non-Javadoc)
183 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
184 */
185 public Object getPropertyValue(Object id) {
186 if (id.equals(P_ID_ELEMENT_CLASS)) {
187 if (descriptionElement instanceof TextData) {
188 return 0;
189 }
190 if (descriptionElement instanceof CommonTaxonName) {
191 return 1;
192 }
193 if (descriptionElement instanceof Distribution) {
194 return 2;
195 }
196 }
197
198 if (id.equals(P_ID_FEATURE)) {
199 Feature feature = descriptionElement.getFeature();
200 if (feature != null) {
201 return feature.getLabel();
202 } else {
203 return "";
204 }
205 }
206
207 if (id.equals(P_ID_BIBREF)) {
208 ReferenceBase<?> citation = descriptionElement.getCitation();
209 ReferencePropertySource bibRefPropertySource = new ReferencePropertySource(citation);
210 bibRefPropertySource.addPropertyChangeListener(new PropertyChangeListener() {
211 public void propertyChange(PropertyChangeEvent evt) {
212 descriptionElement.setCitation((ReferenceBase<?>) evt.getNewValue());
213 }
214 });
215 return bibRefPropertySource;
216 }
217
218 if (id.equals(P_ID_ELEMENT_TEXT)) {
219 return DescriptionUtil.getCache(descriptionElement);
220 }
221
222 if (id.equals(P_ID_MICROREF)) {
223 return CdmUtils.Nz(descriptionElement.getCitationMicroReference());
224 }
225
226 return null;
227 }
228
229 /* (non-Javadoc)
230 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
231 */
232 public boolean isPropertySet(Object id) {
233 return false;
234 }
235
236 /* (non-Javadoc)
237 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
238 */
239 public void resetPropertyValue(Object id) {}
240
241 /* (non-Javadoc)
242 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
243 */
244 public void setPropertyValue(Object id, Object value) {
245 if (id.equals(P_ID_ELEMENT_CLASS)) {
246 // Hmmm ...
247 }
248
249 if (id.equals(P_ID_BIBREF)) {
250 if (value instanceof ReferenceBase) {
251 descriptionElement.setCitation((ReferenceBase<?>) value);
252 }
253 }
254
255 if (id.equals(P_ID_ELEMENT_TEXT)) {
256 DescriptionUtil.setCache(descriptionElement, (String) value);
257 }
258
259 if (id.equals(P_ID_MICROREF)) {
260 descriptionElement.setCitationMicroReference((String) value);
261 }
262
263 // descriptionElement.firePropertyChange(ITaxEditorConstants.PROPERTY_SHEET_CHANGE, null, null);
264 }
265
266 /**
267 * @return
268 */
269 public DescriptionElementBase getDescriptionElement() {
270 return descriptionElement;
271 }
272
273 /**
274 * @param parentid the parentid to set
275 */
276 public void setParentid(String parentid) {
277 this.parentid = parentid;
278 }
279
280 /**
281 * @return the parentid
282 */
283 public String getParentid() {
284 return parentid;
285 }
286 }