787280404324d87128a31212b89cecc707efa47e
[taxeditor.git] / taxeditor-editor / src / main / java / 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.PropertyDescriptor;
22 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
23
24 import eu.etaxonomy.cdm.common.CdmUtils;
25 import eu.etaxonomy.cdm.model.name.NonViralName;
26 import eu.etaxonomy.taxeditor.propertysheet.ICdmBasePropertySource;
27 import eu.etaxonomy.taxeditor.store.model.Resources;
28
29 /**
30 * @author p.ciardelli
31 * @created 03.11.2008
32 * @version 1.0
33 */
34 public class ScientificNamePropertySource implements ICdmBasePropertySource {
35 private static final Logger logger = Logger
36 .getLogger(ScientificNamePropertySource.class);
37
38 NonViralName name;
39
40 // Property unique keys
41 public static final String P_ID_SEARCH = "P_ID_SEARCH";
42 public static final String P_ID_EDITABLECACHE = "P_ID_EDITABLECACHE";
43 public static final String P_ID_UNINOMIAL = "P_ID_UNINOMIAL";
44 public static final String P_ID_INFRAGENERICEP = "P_ID_INFRAGENERICEP";
45 public static final String P_ID_INFRASPECIFICEP = "P_ID_INFRASPECIFICEP";
46 public static final String P_ID_SPECIESEP = "P_ID_SPECIESEP";
47 public static final String P_ID_PROTECT_CACHE = "P_ID_PROTECT_CACHE";
48 public static final String P_ID_APPENDEDPHRASE = "P_ID_APPENDEDPHRASE";
49 public static final String P_ID_AUTHORSHIPCACHE = "P_ID_AUTHORSHIPCACHE";
50
51 // Property display keys
52 public static final String P_SEARCH = "Search";
53 public static final String P_EDITABLECACHE = "Editable Cache";
54 public static final String P_PROTECT_CACHE = "Protect Cache from overwriting?";
55 public static final String P_UNINOMIAL = "Uninomial";
56 public static final String P_INFRAGENERICEP = "Infrageneric Epithet";
57 public static final String P_INFRASPECIFICEP = "Infraspecific Epithet";
58 public static final String P_SPECIESEP = "Specific Epithet";
59 public static final String P_AUTHORSHIPCACHE = "Authorship";
60 public static final String P_APPENDEDPHRASE = "Appended Phrase";
61
62 private static final int CACHE_NOT_PROTECTED = 0;
63 private static final int CACHE_PROTECTED = 1;
64
65 public ScientificNamePropertySource(NonViralName name) {
66
67 // Default type of ReferenceBase is Generic
68 // FIXME disabled
69 // if (name == null) {
70 // name = PreferencesUtil.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 boolean isProtectedCache = name.isProtectedTitleCache();
140 // boolean isProtectedCache = false;
141
142 // Uninomial (aka Genus)
143 if (id.equals(P_ID_UNINOMIAL)) {
144 descriptors.addElement(
145 isProtectedCache ?
146 new PropertyDescriptor(P_ID_UNINOMIAL, P_UNINOMIAL) :
147 new TextPropertyDescriptor(P_ID_UNINOMIAL, P_UNINOMIAL));
148 }
149
150 // Infrageneric epithet
151 if (id.equals(P_ID_INFRAGENERICEP)) {
152 descriptors.addElement(
153 isProtectedCache ?
154 new PropertyDescriptor(P_ID_INFRAGENERICEP, P_INFRAGENERICEP) :
155 new TextPropertyDescriptor(P_ID_INFRAGENERICEP, P_INFRAGENERICEP));
156 }
157
158 // Specific epithet
159 if (id.equals(P_ID_SPECIESEP)) {
160 descriptors.addElement(
161 isProtectedCache ?
162 new PropertyDescriptor(P_ID_SPECIESEP, P_SPECIESEP) :
163 new TextPropertyDescriptor(P_ID_SPECIESEP, P_SPECIESEP));
164 }
165
166 // Infraspecific epithet
167 if (id.equals(P_ID_INFRASPECIFICEP)) {
168 descriptors.addElement(
169 isProtectedCache ?
170 new PropertyDescriptor(P_ID_INFRASPECIFICEP, P_INFRASPECIFICEP) :
171 new TextPropertyDescriptor(P_ID_INFRASPECIFICEP, P_INFRASPECIFICEP));
172 }
173
174 // Appended phrase
175 if (id.equals(P_ID_APPENDEDPHRASE)) {
176 descriptors.addElement(
177 isProtectedCache ?
178 new PropertyDescriptor(P_ID_APPENDEDPHRASE, P_APPENDEDPHRASE) :
179 new TextPropertyDescriptor(P_ID_APPENDEDPHRASE, P_APPENDEDPHRASE));
180 }
181
182 // Authorship cache
183 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
184 descriptors.addElement(
185 new PropertyDescriptor(P_ID_AUTHORSHIPCACHE, P_AUTHORSHIPCACHE));
186 }
187 }
188
189 /* (non-Javadoc)
190 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
191 */
192 public Object getEditableValue() {
193 return CdmUtils.Nz(name.getTitleCache());
194 }
195
196 /* (non-Javadoc)
197 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
198 */
199 public IPropertyDescriptor[] getPropertyDescriptors() {
200 return (IPropertyDescriptor[]) descriptors.toArray(
201 new IPropertyDescriptor[descriptors.size()]);
202 }
203
204 /* (non-Javadoc)
205 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
206 */
207 public Object getPropertyValue(Object id) {
208
209 // Editable cache
210 if (id.equals(P_ID_EDITABLECACHE)) {
211 return (CdmUtils.Nz(name.getTitleCache()));
212 }
213
214 // Protect cache?
215 if (id.equals(P_ID_PROTECT_CACHE)) {
216 if (name.isProtectedTitleCache()) {
217 return CACHE_PROTECTED;
218 } else {
219 return CACHE_NOT_PROTECTED;
220 }
221 }
222
223 // Uninomial (aka Genus)
224 if (id.equals(P_ID_UNINOMIAL)) {
225 return CdmUtils.Nz(name.getGenusOrUninomial());
226 }
227
228 // Infrageneric epithet
229 if (id.equals(P_ID_INFRAGENERICEP)) {
230 return CdmUtils.Nz(name.getInfraGenericEpithet());
231 }
232
233 // Specific epithet
234 if (id.equals(P_ID_SPECIESEP)) {
235 return CdmUtils.Nz(name.getSpecificEpithet());
236 }
237
238 // Infraspecific epithet
239 if (id.equals(P_ID_INFRASPECIFICEP)) {
240 return CdmUtils.Nz(name.getInfraSpecificEpithet());
241 }
242
243
244 // Appended phrase
245 if (id.equals(P_ID_APPENDEDPHRASE)) {
246 return CdmUtils.Nz(name.getAppendedPhrase());
247 }
248
249 // Authorship cache
250 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
251 if (name.isProtectedTitleCache()) {
252 return CdmUtils.Nz(name.getAuthorshipCache());
253 } else {
254 return new NomenclaturalAuthorshipPropertySource(name);
255 }
256 }
257
258 return "";
259 }
260
261 /* (non-Javadoc)
262 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
263 */
264 public boolean isPropertySet(Object id) {
265 return false;
266 }
267
268 /* (non-Javadoc)
269 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
270 */
271 public void resetPropertyValue(Object id) {}
272
273 /* (non-Javadoc)
274 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
275 */
276 public void setPropertyValue(Object id, Object value) {
277
278 // Search result
279 if (id.equals(P_ID_SEARCH)) {
280 if (value instanceof NonViralName) {
281 name = (NonViralName) value;
282 }
283 }
284
285 // Protect cache?
286 if (id.equals(P_ID_PROTECT_CACHE)) {
287 logger.warn(((Integer) value).intValue() == CACHE_PROTECTED);
288 name.setProtectedTitleCache(
289 ((Integer) value).intValue() == CACHE_PROTECTED);
290 }
291
292 // Uninomial (aka Genus)
293 if (id.equals(P_ID_UNINOMIAL)) {
294 name.setGenusOrUninomial((String) value);
295 }
296
297 // Infrageneric epithet
298 if (id.equals(P_ID_INFRAGENERICEP)) {
299 name.setInfraGenericEpithet((String) value);
300 }
301
302 // Specific epithet
303 if (id.equals(P_ID_SPECIESEP)) {
304 name.setSpecificEpithet((String) value);
305 }
306
307 // Infraspecific epithet
308 if (id.equals(P_ID_INFRASPECIFICEP)) {
309 name.setInfraSpecificEpithet((String) value);
310 }
311
312 // Appended phrase
313 if (id.equals(P_ID_APPENDEDPHRASE)) {
314 name.setAppendedPhrase((String) value);
315 }
316
317 // Authorship cache
318 // if (id.equals(P_ID_AUTHORSHIPCACHE)) {
319 //// name.setProtectedAuthorshipCache(true);
320 // name.setAuthorshipCache((String) value);
321 // }
322
323 // Editable cache
324 if (id.equals(P_ID_EDITABLECACHE)) {
325 name.setTitleCache((String) value);
326 // } else {
327 // name.setTitleCache(name.generateTitle(), name.isProtectedTitleCache());
328 }
329
330 propertyChangeSupport.firePropertyChange(Resources.PROPERTY_SHEET_CHANGE, null, name);
331 }
332
333
334 public String toString() {
335 return CdmUtils.Nz(name.getTitleCache());
336 }
337
338 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
339
340 public void addPropertyChangeListener(
341 PropertyChangeListener listener) {
342 propertyChangeSupport.addPropertyChangeListener(listener);
343 }
344 }