Enabled keyboard navigation in editor pages and uploaded a new feature.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / type / TypeDesignationPropertySource.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.propertysheet.type;
12
13 import java.beans.PropertyChangeEvent;
14 import java.beans.PropertyChangeListener;
15 import java.util.Vector;
16
17 import org.eclipse.ui.views.properties.IPropertyDescriptor;
18 import org.eclipse.ui.views.properties.PropertyDescriptor;
19 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
20
21 import eu.etaxonomy.cdm.common.CdmUtils;
22 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
23 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
24 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
25 import eu.etaxonomy.cdm.model.reference.Generic;
26 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
27 import eu.etaxonomy.taxeditor.propertysheet.ICdmBasePropertySource;
28 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource;
29
30 /**
31 * @author p.ciardelli
32 * @created 28.04.2009
33 * @version 1.0
34 */
35 public class TypeDesignationPropertySource implements ICdmBasePropertySource {
36
37 private TypeDesignationBase<?> typeDesignation;
38
39 /**
40 * Property unique keys
41 */
42 public static final String P_ID_CITATION = "citation";
43 public static final String P_ID_MICROREF = "microref";
44
45 /**
46 * Property display keys
47 */
48 public static final String P_CITATION = "Citation";
49 public static final String P_MICROREF = "Microcitation";
50
51 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
52
53 TypeDesignationPropertySource(TypeDesignationBase<?> typeDesignation) {
54 this.typeDesignation = typeDesignation;
55
56 addDescriptor(P_ID_CITATION);
57 addDescriptor(P_ID_MICROREF);
58 }
59
60 public Object getEditableValue() {
61 if (typeDesignation instanceof SpecimenTypeDesignation) {
62 return ((SpecimenTypeDesignation) typeDesignation).getTypeSpecimen().getTitleCache();
63 }
64
65 if (typeDesignation instanceof NameTypeDesignation) {
66 if (((NameTypeDesignation) typeDesignation).getTypeName() != null) {
67 return ((NameTypeDesignation) typeDesignation).getTypeName().getTitleCache();
68 }
69 }
70 return null;
71 }
72
73 private void addDescriptor(String id) {
74 // Name relations, listed in custom property descriptor
75 if (id.equals(P_ID_CITATION)) {
76 descriptors.addElement(
77 new PropertyDescriptor(P_ID_CITATION, P_CITATION));
78 }
79 if (id.equals(P_ID_MICROREF)) {
80 descriptors.addElement(
81 new TextPropertyDescriptor(P_ID_MICROREF, P_MICROREF));
82 }
83 }
84
85 public IPropertyDescriptor[] getPropertyDescriptors() {
86 return (IPropertyDescriptor[]) descriptors.toArray(
87 new IPropertyDescriptor[descriptors.size()]);
88 }
89
90 public Object getPropertyValue(Object id) {
91
92 if (id.equals(P_ID_CITATION)) {
93
94 ReferenceBase<?> reference = typeDesignation.getCitation();
95 if (reference == null) {
96 reference = Generic.NewInstance();
97 }
98 ReferencePropertySource referencePropertySource = new ReferencePropertySource(reference);
99 referencePropertySource.addPropertyChangeListener(new PropertyChangeListener() {
100 public void propertyChange(PropertyChangeEvent evt) {
101 if (evt.getNewValue() instanceof ReferenceBase) {
102 typeDesignation.setCitation((ReferenceBase<?>) evt.getNewValue());
103 }
104 }
105 });
106 return referencePropertySource;
107 }
108
109 if (id.equals(P_ID_MICROREF)) {
110 return CdmUtils.Nz(typeDesignation.getCitationMicroReference());
111 }
112
113 return null;
114 }
115
116 public boolean isPropertySet(Object id) {
117 return false;
118 }
119
120 public void resetPropertyValue(Object id) {}
121
122 public void setPropertyValue(Object id, Object value) {
123 if (id.equals(P_ID_MICROREF)) {
124 typeDesignation.setCitationMicroReference((String) value);
125 }
126 }
127
128 public boolean equals(Object object) {
129 if (object == null) {
130 return false;
131 }
132 if (toString() == null) {
133 return false;
134 }
135 if (toString().equals(object.toString())) {
136 return true;
137 } else {
138 return false;
139 }
140 }
141 }