Cleaned up imports.
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / type / TypeCollectionPropertySource.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 package eu.etaxonomy.taxeditor.propertysheet.type;
10
11 import java.beans.PropertyChangeEvent;
12 import java.beans.PropertyChangeListener;
13 import java.util.Collection;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.ui.views.properties.IPropertyDescriptor;
17 import org.eclipse.ui.views.properties.IPropertySource;
18 import org.eclipse.ui.views.properties.PropertyDescriptor;
19 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
20
21 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
22 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
23 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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.CollectionPropertySource;
28 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource;
29
30 public class TypeCollectionPropertySource extends CollectionPropertySource {
31 private static final Logger logger = Logger
32 .getLogger(TypeCollectionPropertySource.class);
33
34 private TaxonNameBase name;
35
36 public TypeCollectionPropertySource(TaxonNameBase name, Collection collection) {
37 super(collection);
38 this.name = name;
39 }
40
41 protected void addDescriptor(Object item) {
42 String itemDisplayName = getItemDisplayName(item);
43
44 if (item instanceof NameTypeDesignation) {
45 descriptors.addElement(
46 new PropertyDescriptor(item, itemDisplayName));
47 } else {
48
49 /* Two issues here, caused by making a ProperySheet field editable while at the same
50 * time returning an IPropertySource with which its child fields are built.
51 *
52 * First, TextPropertyDescriptor expects its value to be set w/ a String and
53 * throws an Assert exception when this is not the case, i.e. when it is passed an
54 * IPropertySource; hence, the override of doSetValue().
55 *
56 * Second, the super implementation of doGetValue() was causing the equality test in
57 * PropertySheetEntry:140 to return false: it was comparing the IPropertySource with
58 * the CellEditor's text String, thereby triggering an infinite loop where the cell's
59 * previous state would never equals the cell's current state. Hence, the
60 * IPropertySource is now saved in the field editorValue for comparison.
61 */
62 descriptors.addElement(
63 new TextPropertyDescriptor(item, itemDisplayName));
64 // new TextPropertyDescriptor(item, itemDisplayName) {
65 // public CellEditor createPropertyEditor(Composite parent) {
66 // CellEditor editor = new TextCellEditor(parent) {
67 // Object editorValue;
68 // protected void doSetValue(Object value) {
69 // if (value instanceof String) {
70 // super.doSetValue(value);
71 // } else {
72 // editorValue = value;
73 // super.doSetValue(value.toString());
74 // }
75 // }
76 //// protected Object doGetValue() {
77 //// return super.doGetValue();
78 //// }
79 // };
80 // if (getValidator() != null) {
81 // editor.setValidator(getValidator());
82 // }
83 // return editor;
84 // }
85 // });
86 }
87 }
88
89 @Override
90 protected String getItemDisplayName(Object item) {
91 String str = "";
92
93 if (item instanceof SpecimenTypeDesignation) {
94
95 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) item;
96
97 if (typeDesignation.getTypeStatus() != null) {
98 str = typeDesignation.getTypeStatus().getLabel();
99 } else {
100 str = "UNDEFINED";
101 }
102 }
103
104 if (item instanceof NameTypeDesignation) {
105
106 NameTypeDesignation typeDesignation = (NameTypeDesignation) item;
107
108 if (typeDesignation.isLectoType()) {
109 str = "Lectotype";
110 } else {
111 str = "Type";
112 }
113 }
114
115 return str;
116 }
117
118 @Override
119 public Object getPropertyValue(Object id) {
120
121 if (id instanceof TypeDesignationBase) {
122 return new TypeDesignationPropertySource((TypeDesignationBase) id);
123 }
124 return null;
125 }
126
127 @Override
128 public void setPropertyValue(Object id, Object value) {
129
130 if (id instanceof SpecimenTypeDesignation && value instanceof String) {
131 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) id;
132
133 if (typeDesignation.getTypeSpecimen() != null) {
134 typeDesignation.getTypeSpecimen().setTitleCache((String) value);
135 } else {
136 logger.warn("'typeDesignation.getTypeSpecimen()' returned null.");
137 }
138 }
139 }
140
141 /* (non-Javadoc)
142 * @see eu.etaxonomy.taxeditor.propertysheet.CollectionPropertySource#toString()
143 */
144 @Override
145 public String toString() {
146 return "";
147 }
148
149 class TypeDesignationPropertySource implements IPropertySource {
150
151 private TypeDesignationBase typeDesignation;
152
153 TypeDesignationPropertySource(TypeDesignationBase typeDesignation) {
154 this.typeDesignation = typeDesignation;
155 }
156
157 @Override
158 public Object getEditableValue() {
159 if (typeDesignation instanceof SpecimenTypeDesignation) {
160 return ((SpecimenTypeDesignation) typeDesignation).getTypeSpecimen().getTitleCache();
161 }
162
163 if (typeDesignation instanceof NameTypeDesignation) {
164 if (((NameTypeDesignation) typeDesignation).getTypeName() != null) {
165 return ((NameTypeDesignation) typeDesignation).getTypeName().getTitleCache();
166 }
167 }
168
169 return null;
170 }
171
172 @Override
173 public IPropertyDescriptor[] getPropertyDescriptors() {
174 return new IPropertyDescriptor[]{
175 new PropertyDescriptor(
176 typeDesignation, "Citation")
177 };
178 }
179
180 @Override
181 public Object getPropertyValue(Object id) {
182 ReferenceBase reference = typeDesignation.getCitation();
183 if (reference == null) {
184 reference = Generic.NewInstance();
185 }
186 ReferencePropertySource referencePropertySource = new ReferencePropertySource(reference);
187 referencePropertySource.addPropertyChangeListener(new PropertyChangeListener() {
188 public void propertyChange(PropertyChangeEvent evt) {
189 if (evt.getNewValue() instanceof ReferenceBase) {
190 typeDesignation.setCitation((ReferenceBase) evt.getNewValue());
191 }
192 }
193 });
194 return referencePropertySource;
195 }
196
197 @Override
198 public boolean isPropertySet(Object id) {
199 return false;
200 }
201
202 @Override
203 public void resetPropertyValue(Object id) {}
204
205 @Override
206 public void setPropertyValue(Object id, Object value) {
207 }
208
209 // public String toString() {
210 // if (typeDesignation instanceof SpecimenTypeDesignation) {
211 // return ((SpecimenTypeDesignation) typeDesignation).getTypeSpecimen().getTitleCache();
212 // }
213 //
214 // if (typeDesignation instanceof NameTypeDesignation) {
215 // if (((NameTypeDesignation) typeDesignation).getTypeName() != null) {
216 // return ((NameTypeDesignation) typeDesignation).getTypeName().getTitleCache();
217 // }
218 // }
219 //
220 // return null;
221 // }
222
223 public boolean equals(Object object) {
224 if (object == null) {
225 return false;
226 }
227 if (toString() == null) {
228 return false;
229 }
230 if (toString().equals(object.toString())) {
231 return true;
232 } else {
233 return false;
234 }
235 }
236 }
237 }