Features lists now sorted alphabetically. Added microreference, cache strings to...
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / name / ScientificNamePropertySource.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.PropertyChangeListener;
13 import java.beans.PropertyChangeSupport;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Vector;
17
18 import org.apache.log4j.Logger;
19 import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
20 import org.eclipse.ui.views.properties.IPropertyDescriptor;
21 import org.eclipse.ui.views.properties.IPropertySource;
22 import org.eclipse.ui.views.properties.PropertyDescriptor;
23 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
24
25 import eu.etaxonomy.cdm.common.CdmUtils;
26 import eu.etaxonomy.cdm.model.name.NonViralName;
27 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
28 import eu.etaxonomy.taxeditor.controller.PreferencesController;
29
30 /**
31 * @author p.ciardelli
32 * @created 03.11.2008
33 * @version 1.0
34 */
35 public class ScientificNamePropertySource implements IPropertySource {
36 private static final Logger logger = Logger
37 .getLogger(ScientificNamePropertySource.class);
38
39 NonViralName name;
40
41 // Property unique keys
42 public static final String P_ID_SEARCH = "P_ID_SEARCH";
43 public static final String P_ID_EDITABLECACHE = "P_ID_EDITABLECACHE";
44 public static final String P_ID_UNINOMIAL = "P_ID_UNINOMIAL";
45 public static final String P_ID_INFRAGENERICEP = "P_ID_INFRAGENERICEP";
46 public static final String P_ID_INFRASPECIFICEP = "P_ID_INFRASPECIFICEP";
47 public static final String P_ID_SPECIESEP = "P_ID_SPECIESEP";
48 public static final String P_ID_PROTECT_CACHE = "P_ID_PROTECT_CACHE";
49 public static final String P_ID_APPENDEDPHRASE = "P_ID_APPENDEDPHRASE";
50 public static final String P_ID_AUTHORSHIPCACHE = "P_ID_AUTHORSHIPCACHE";
51
52 // Property display keys
53 public static final String P_SEARCH = "001:Search";
54 public static final String P_EDITABLECACHE = "002:Editable Cache";
55 public static final String P_PROTECT_CACHE = "0020:Protect Cache from overwriting?";
56 public static final String P_UNINOMIAL = "04:Uninomial";
57 public static final String P_INFRAGENERICEP = "05:Infrageneric Epithet";
58 public static final String P_INFRASPECIFICEP = "07:Infraspecific Epithet";
59 public static final String P_SPECIESEP = "06:Specific Epithet";
60 public static final String P_AUTHORSHIPCACHE = "08:Authorship";
61 public static final String P_APPENDEDPHRASE = "09:Appended Phrase";
62
63 private static final int CACHE_NOT_PROTECTED = 0;
64 private static final int CACHE_PROTECTED = 1;
65
66 public ScientificNamePropertySource(NonViralName name) {
67
68 // Default type of ReferenceBase is Generic
69 if (name == null) {
70 name = PreferencesController.getInstanceOfPreferredNameClass();
71 }
72 this.name = name;
73
74 initDescriptors();
75 }
76
77 protected void initDescriptors() {
78
79 List<String> displayFields = new ArrayList<String>();
80
81 // Cache fields
82 // displayFields.add(P_ID_SEARCH);
83 // displayFields.add(P_ID_EDITABLECACHE);
84 // displayFields.add(P_ID_PROTECT_CACHE);
85
86 // Uninomial
87 displayFields.add(P_ID_UNINOMIAL);
88
89 if (name.isSupraGeneric() || name.isGenus()) { // Rank is higher than GENUS or equals GENUS
90 }
91 else if (name.isInfraGeneric()) { // lower than GENUS and higher than SPECIES
92 displayFields.add(P_ID_INFRAGENERICEP);
93 }
94 else if (name.isSpecies()) { // Rank equals SPECIES
95 displayFields.add(P_ID_SPECIESEP);
96 }
97 else if (name.isInfraSpecific()) { // Rank is lower than SPECIES
98 displayFields.add(P_ID_SPECIESEP);
99 displayFields.add(P_ID_INFRASPECIFICEP);
100 } else {
101 displayFields.add(P_ID_SPECIESEP);
102 }
103 displayFields.add(P_ID_AUTHORSHIPCACHE);
104 displayFields.add(P_ID_APPENDEDPHRASE);
105
106
107 for (String field : displayFields) {
108 addDescriptor(field);
109 }
110 }
111
112 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
113
114 protected void addDescriptor(String id) {
115
116 // Reference search
117 if (id.equals(P_ID_SEARCH)) {
118 // descriptors.addElement(
119 // new ReferenceSearchDescriptor(P_ID_SEARCH, P_SEARCH, getSearchType()) {
120 // protected void saveReference(ReferenceBase reference) {
121 // setPropertyValue(P_ID_SEARCH, reference);
122 // }
123 // });
124 }
125
126 // Editable cache
127 if (id.equals(P_ID_EDITABLECACHE)) {
128 descriptors.addElement(
129 new TextPropertyDescriptor(P_ID_EDITABLECACHE, P_EDITABLECACHE));
130 }
131
132 // Protect cache?
133 if (id.equals(P_ID_PROTECT_CACHE)) {
134 descriptors.addElement(
135 new ComboBoxPropertyDescriptor(P_ID_PROTECT_CACHE, P_PROTECT_CACHE,
136 new String[] {"no", "yes"}));
137 }
138
139 // Uninomial (aka Genus)
140 if (id.equals(P_ID_UNINOMIAL)) {
141 descriptors.addElement(
142 new TextPropertyDescriptor(P_ID_UNINOMIAL, P_UNINOMIAL));
143 }
144
145 // Infrageneric epithet
146 if (id.equals(P_ID_INFRAGENERICEP)) {
147 descriptors.addElement(
148 new TextPropertyDescriptor(P_ID_INFRAGENERICEP, P_INFRAGENERICEP));
149 }
150
151 // Specific epithet
152 if (id.equals(P_ID_SPECIESEP)) {
153 descriptors.addElement(
154 new TextPropertyDescriptor(P_ID_SPECIESEP, P_SPECIESEP));
155 }
156
157 // Infraspecific epithet
158 if (id.equals(P_ID_INFRASPECIFICEP)) {
159 descriptors.addElement(
160 new TextPropertyDescriptor(P_ID_INFRASPECIFICEP, P_INFRASPECIFICEP));
161 }
162
163 // Appended phrase
164 if (id.equals(P_ID_APPENDEDPHRASE)) {
165 descriptors.addElement(
166 new TextPropertyDescriptor(P_ID_APPENDEDPHRASE, P_APPENDEDPHRASE));
167 }
168
169 // Authorship cache
170 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
171 descriptors.addElement(
172 new TextPropertyDescriptor(P_ID_AUTHORSHIPCACHE, P_AUTHORSHIPCACHE));
173 }
174 }
175
176 /* (non-Javadoc)
177 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
178 */
179 public Object getEditableValue() {
180 return CdmUtils.Nz(name.getTitleCache());
181 }
182
183 /* (non-Javadoc)
184 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
185 */
186 public IPropertyDescriptor[] getPropertyDescriptors() {
187 return (IPropertyDescriptor[]) descriptors.toArray(
188 new IPropertyDescriptor[descriptors.size()]);
189 }
190
191 /* (non-Javadoc)
192 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
193 */
194 public Object getPropertyValue(Object id) {
195
196 // Editable cache
197 if (id.equals(P_ID_EDITABLECACHE)) {
198 return (CdmUtils.Nz(name.getTitleCache()));
199 }
200
201 // Protect cache?
202 if (id.equals(P_ID_PROTECT_CACHE)) {
203 if (name.isProtectedTitleCache()) {
204 return CACHE_PROTECTED;
205 } else {
206 return CACHE_NOT_PROTECTED;
207 }
208 }
209
210 // Uninomial (aka Genus)
211 if (id.equals(P_ID_UNINOMIAL)) {
212 return CdmUtils.Nz(name.getGenusOrUninomial());
213 }
214
215 // Infrageneric epithet
216 if (id.equals(P_ID_INFRAGENERICEP)) {
217 return CdmUtils.Nz(name.getInfraGenericEpithet());
218 }
219
220 // Specific epithet
221 if (id.equals(P_ID_SPECIESEP)) {
222 return CdmUtils.Nz(name.getSpecificEpithet());
223 }
224
225 // Infraspecific epithet
226 if (id.equals(P_ID_INFRASPECIFICEP)) {
227 return CdmUtils.Nz(name.getInfraSpecificEpithet());
228 }
229
230
231 // Appended phrase
232 if (id.equals(P_ID_APPENDEDPHRASE)) {
233 return CdmUtils.Nz(name.getAppendedPhrase());
234 }
235
236 // Authorship cache
237 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
238 return CdmUtils.Nz(name.getAuthorshipCache());
239 }
240
241 return "";
242 }
243
244 /* (non-Javadoc)
245 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
246 */
247 public boolean isPropertySet(Object id) {
248 return false;
249 }
250
251 /* (non-Javadoc)
252 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
253 */
254 public void resetPropertyValue(Object id) {}
255
256 /* (non-Javadoc)
257 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
258 */
259 public void setPropertyValue(Object id, Object value) {
260
261 // Search result
262 if (id.equals(P_ID_SEARCH)) {
263 if (value instanceof NonViralName) {
264 name = (NonViralName) value;
265 }
266 }
267
268 // Protect cache?
269 if (id.equals(P_ID_PROTECT_CACHE)) {
270 if (((Integer) value).intValue() == CACHE_PROTECTED) {
271 name.setProtectedTitleCache(true);
272 } else {
273 name.setProtectedTitleCache(false);
274 }
275 }
276
277 // Uninomial (aka Genus)
278 if (id.equals(P_ID_UNINOMIAL)) {
279 name.setGenusOrUninomial((String) value);
280 }
281
282 // Infrageneric epithet
283 if (id.equals(P_ID_INFRAGENERICEP)) {
284 name.setInfraGenericEpithet((String) value);
285 }
286
287 // Specific epithet
288 if (id.equals(P_ID_SPECIESEP)) {
289 name.setSpecificEpithet((String) value);
290 }
291
292 // Infraspecific epithet
293 if (id.equals(P_ID_INFRASPECIFICEP)) {
294 name.setInfraSpecificEpithet((String) value);
295 }
296
297 // Appended phrase
298 if (id.equals(P_ID_APPENDEDPHRASE)) {
299 name.setAppendedPhrase((String) value);
300 }
301
302 // Authorship cache
303 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
304 // name.setProtectedAuthorshipCache(true);
305 name.setAuthorshipCache((String) value);
306 }
307
308 // Editable cache
309 if (id.equals(P_ID_EDITABLECACHE)) {
310 name.setTitleCache((String) value);
311 } else {
312 name.setTitleCache(name.generateTitle(), name.isProtectedTitleCache());
313 }
314
315 propertyChangeSupport.firePropertyChange(ITaxEditorConstants.PROPERTY_SHEET_CHANGE, null, name);
316 }
317
318
319 public String toString() {
320 return CdmUtils.Nz(name.getTitleCache());
321 }
322
323 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
324
325 public void addPropertyChangeListener(
326 PropertyChangeListener listener) {
327 propertyChangeSupport.addPropertyChangeListener(listener);
328 }
329 }