1) Added import / export dummy functionality (i.e. CDM library not yet functional...
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / name / NonViralNamePropertySource.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.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.beans.PropertyChangeSupport;
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.Vector;
20
21 import org.apache.log4j.Logger;
22 import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
23 import org.eclipse.ui.views.properties.IPropertyDescriptor;
24 import org.eclipse.ui.views.properties.IPropertySource;
25 import org.eclipse.ui.views.properties.PropertyDescriptor;
26 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
27
28 import eu.etaxonomy.cdm.common.CdmUtils;
29 import eu.etaxonomy.cdm.model.common.Annotation;
30 import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 import eu.etaxonomy.cdm.model.common.TimePeriod;
32 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
33 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
34 import eu.etaxonomy.cdm.model.name.NonViralName;
35 import eu.etaxonomy.cdm.model.name.Rank;
36 import eu.etaxonomy.cdm.model.reference.Article;
37 import eu.etaxonomy.cdm.model.reference.BibtexReference;
38 import eu.etaxonomy.cdm.model.reference.Book;
39 import eu.etaxonomy.cdm.model.reference.BookSection;
40 import eu.etaxonomy.cdm.model.reference.Generic;
41 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
42 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
43 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
44 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
45 import eu.etaxonomy.taxeditor.UiUtil;
46 import eu.etaxonomy.taxeditor.model.CdmUtil;
47 import eu.etaxonomy.taxeditor.propertysheet.AnnotationPropertySource;
48 import eu.etaxonomy.taxeditor.propertysheet.AnnotationsPropertyDescriptor;
49 import eu.etaxonomy.taxeditor.propertysheet.MarkersPropertySource;
50 import eu.etaxonomy.taxeditor.propertysheet.reference.NomenclaturalReferencePropertySource;
51 import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource;
52
53 /**
54 * @author p.ciardelli
55 * @created 06.05.2008
56 * @version 1.0
57 */
58 public class NonViralNamePropertySource implements IPropertySource {
59 private static final Logger logger = Logger
60 .getLogger(NonViralNamePropertySource.class);
61
62 // The name whose properties are being displayed
63 NonViralName name;
64
65 // If this is a property with a parent, the parent's property ID
66 private String parentid = "";
67
68 PropertyChangeSupport propertyChangeSupport;
69
70 // Property unique keys
71 public static final String P_ID_PARSED = "parsed";
72 public static final String P_ID_RANK = "rank";
73 public static final String P_ID_TITLECACHE = "titlecache";
74 public static final String P_ID_UNINOMIAL = "uninomial";
75 public static final String P_ID_INFRAGENERICEP = "infragenericep";
76 public static final String P_ID_SPECIESEP = "speciesep";
77 public static final String P_ID_INFRASPECIFICEP = "infraspecificep";
78 public static final String P_ID_APPENDEDPHRASE = "appendedphrase";
79 public static final String P_ID_HOMOTYPICALGROUP = "homogroup";
80 public static final String P_ID_AUTHORSHIPCACHE = "authorshipcache";
81 public static final String P_ID_NAMERELATIONS = "namerelations";
82 public static final String P_ID_TYPE = "type";
83 public static final String P_ID_ANNOTATIONS = "annotations";
84 public static final String P_ID_NOMSTATUS = "nomstatus";
85 public static final String P_ID_UUID = "uuid";
86 public static final String P_ID_NUM_OF_BASES = "numberofbases";
87 public static final String P_ID_MARKERS = "markers";
88 public static final String P_ID_CREATED = "created";
89 public static final String P_ID_CREATEDBY = "createdby";
90 public static final String P_ID_NOMENCLATURAL_CODE = "nomenclaturalcode";
91 public static final String P_ID_NOMENCLATURAL_REF = "nomenclaturalref";
92 public static final String P_ID_NOMENCLATURAL_MICROREF = "nomenclaturalmicroref";
93 public static final String P_ID_REFERENCEYEAR = "referenceyear";
94 public static final String P_ID_NOMREF_TYPE = "nomreftype";
95 public static final String P_ID_NOMREF_YEAR = "nomrefyear";
96 public static final String P_ID_NOMREF_TITLE = "nomreftitle";
97
98 // Property display keys
99 // Note: for an explanation of the sorting prefixes ("04:"),
100 // @see eu.etaxonomy.taxeditor.propertysheet.CustomSortPropertySheetEntry
101 public static final String P_PARSED = "00:Parsing Status";
102 public static final String P_RANK = "02:Rank";
103 public static final String P_TITLECACHE = "03:Name Cache";
104 public static final String P_UNINOMIAL = "04:Uninomial";
105 public static final String P_INFRAGENERICEP = "05:Infrageneric Epithet";
106 public static final String P_SPECIESEP = "06:Specific Epithet";
107 public static final String P_INFRASPECIFICEP = "07:Infraspecific Epithet";
108 public static final String P_APPENDEDPHRASE = "09:Appended Phrase";
109 public static final String P_HOMOTYPICALGROUP = "09:Homotypical Group";
110 public static final String P_AUTHORSHIPCACHE = "08:Authorship";
111 public static final String P_NAMERELATIONS = "11:Name Relations";
112 public static final String P_TYPE = "12:Type";
113 public static final String P_ANNOTATIONS = "14:Annotations";
114 public static final String P_NOMSTATUS = "10:Nomenclatural Status";
115 public static final String P_UUID = "01:UUID";
116 public static final String P_NUM_OF_BASES = "12:Number of Bases";
117 public static final String P_MARKERS = "13:Markers";
118 public static final String P_CREATED = "15:Created";
119 public static final String P_CREATEDBY = "16:Created By";
120 public static final String P_NOMENCLATURAL_CODE = "02:Nomenclatural Code";
121 public static final String P_NOMENCLATURAL_REF = "09:Nomenclatural Reference";
122 public static final String P_NOMENCLATURAL_MICROREF = "10:Micro Reference";
123 public static final String P_NOMREF_TYPE = "00:Reference Type";
124 public static final String P_NOMREF_YEAR = "02:Year";
125 public static final String P_NOMREF_TITLE = "01:Title";
126
127 protected static final String[] TOP_LEVEL_PROPERTIES = new String[] {P_ID_PARSED, P_ID_RANK, P_ID_TITLECACHE,
128 P_ID_ANNOTATIONS, P_ID_NOMSTATUS, P_ID_NAMERELATIONS, P_ID_UUID, P_ID_NOMENCLATURAL_REF,
129 P_ID_TYPE, P_ID_CREATED, P_ID_CREATEDBY, P_ID_NOMENCLATURAL_CODE };
130
131 // ***********************************************************
132 // RANKS
133 // ***********************************************************
134 Rank[] ranks = null;
135 String[] P_RANK_MENU = null;
136 private void populateRanks() {
137
138 // Get terms from rank vocabulary
139 List<Rank> ranksList = new ArrayList<Rank>();
140 ranksList.addAll(UiUtil.getPreferredRanks());
141
142 // Populate ranks menu labels
143 List<String> ranksMenuList = new ArrayList<String>();
144 for (Rank rank : ranksList) {
145 ranksMenuList.add(rank.getLabel());
146 }
147
148 // Add an empty element for "nothing selected" in the rank drop-down
149 ranksList.add(0, null);
150 ranksMenuList.add(0, "");
151
152 // Convert rank lists to array
153 ranks = ranksList.toArray(new Rank[ranksList.size()]);
154 P_RANK_MENU = ranksMenuList.toArray(new String[ranksMenuList.size()]);
155 }
156
157 static String[] P_NOMENCLATURALCODE_MENU = new String[] { "ICZN", "ICBN" };
158 static String[] P_NOMREF_TYPE_MENU = new String[] { "Article", "Bibtex", "Book", "Book Section", "Generic" };
159
160 // ***********************************************************
161 // NOMENCLATURAL STATUS
162 // ***********************************************************
163 static TermVocabulary<NomenclaturalStatusType> nomStatusVocabulary =
164 TaxEditorPlugin.getDefault().getNomStatusVocabulary();
165 static NomenclaturalStatusType[] nomStatusTypes = null;
166 static String[] P_NOMSTATUS_MENU = null;
167 private static void populateNomStatusTypes() {
168
169 // Get terms from nom status vocabulary
170 List<NomenclaturalStatusType> nomStatusTypesList = new ArrayList<NomenclaturalStatusType>();
171 nomStatusTypesList.addAll(nomStatusVocabulary.getTerms());
172
173 // Populate nom status type menu labels
174 List<String> nomStatusTypesMenuList = new ArrayList<String>();
175 for (NomenclaturalStatusType nomStatusType : nomStatusTypesList) {
176 nomStatusTypesMenuList.add(nomStatusType.getLabel());
177 }
178
179 // Add an empty element for "nothing selected" in the rank drop-down
180 nomStatusTypesList.add(0, null);
181 nomStatusTypesMenuList.add(0, "");
182
183 // Convert rank lists to array
184 nomStatusTypes = nomStatusTypesList.toArray(new NomenclaturalStatusType[nomStatusTypesList.size()]);
185 P_NOMSTATUS_MENU = nomStatusTypesMenuList.toArray(new String[nomStatusTypesMenuList.size()]);
186 }
187
188 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
189 /**
190 * Add descriptor for a given property.
191 * <p>
192 * Notes on Descriptor:
193 * </p>
194 * <ul>
195 * <li>PropertyDescriptor - uneditable cell value</li>
196 * <li>ComboBoxPropertyDescriptor - dropdown list, property supplied must be integer-based</li>
197 * <li>TextPropertyDescriptor - editable cell value</li>
198 * </ul>
199 * <p>
200 * If any descriptor calls setCategory, all descriptors w/out a category are put in
201 * category "Misc".
202 * <p>
203 * <code>descriptor.setFilterFlags (new String[] { IPropertySheetEntry.FILTER_ID_EXPERT })</code> -
204 * this descriptor shown under advanced properties
205 *
206 * @param id
207 */
208 protected void addDescriptor(String id) {
209 if (id.equals(P_ID_PARSED)) {
210 descriptors.addElement(
211 new PropertyDescriptor(P_ID_PARSED, P_PARSED));
212 }
213 if (id.equals(P_ID_RANK)) {
214 if (ranks == null) {
215 populateRanks();
216 }
217 descriptors.addElement(
218 new ComboBoxPropertyDescriptor(P_ID_RANK, P_RANK, P_RANK_MENU));
219 }
220 if (id.equals(P_ID_TITLECACHE)) {
221 descriptors.addElement(
222 new PropertyDescriptor(P_ID_TITLECACHE, P_TITLECACHE));
223 }
224 if (id.equals(P_ID_UNINOMIAL)) {
225 descriptors.addElement(
226 new TextPropertyDescriptor(P_ID_UNINOMIAL, P_UNINOMIAL));
227 }
228 if (id.equals(P_ID_INFRAGENERICEP)) {
229 descriptors.addElement(
230 new TextPropertyDescriptor(P_ID_INFRAGENERICEP, P_INFRAGENERICEP));
231 }
232 if (id.equals(P_ID_SPECIESEP)) {
233 descriptors.addElement(
234 new TextPropertyDescriptor(P_ID_SPECIESEP, P_SPECIESEP));
235 }
236 if (id.equals(P_ID_INFRASPECIFICEP)) {
237 descriptors.addElement(
238 new TextPropertyDescriptor(P_ID_INFRASPECIFICEP, P_INFRASPECIFICEP));
239 }
240 if (id.equals(P_ID_APPENDEDPHRASE)) {
241 descriptors.addElement(
242 new TextPropertyDescriptor(P_ID_APPENDEDPHRASE, P_APPENDEDPHRASE));
243 }
244 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
245 descriptors.addElement(
246 new TextPropertyDescriptor(P_ID_AUTHORSHIPCACHE, P_AUTHORSHIPCACHE));
247 }
248 if (id.equals(P_ID_HOMOTYPICALGROUP)) {
249 descriptors.addElement(
250 new PropertyDescriptor(P_ID_HOMOTYPICALGROUP, P_HOMOTYPICALGROUP));
251 }
252 if (id.equals(P_ID_NAMERELATIONS)) {
253 descriptors.addElement(
254 new NameRelationsPropertyDescriptor(P_ID_NAMERELATIONS, P_NAMERELATIONS, name) {
255 protected void saveNameRelations(Set set) {
256 setPropertyValue(P_ID_NAMERELATIONS, set);
257 }
258 }
259 );
260 };
261
262 if (id.equals(P_ID_TYPE)) {
263 descriptors.addElement(
264 new PropertyDescriptor(P_ID_TYPE, P_TYPE));
265 }
266 if (id.equals(P_ID_ANNOTATIONS)) {
267 descriptors.addElement(
268 new AnnotationsPropertyDescriptor(P_ID_ANNOTATIONS, P_ANNOTATIONS, name) {
269 protected void saveAnnotations(Set set) {
270 setPropertyValue(P_ID_ANNOTATIONS, set);
271 }
272 }
273 );
274 };
275 if (id.equals(P_ID_NOMSTATUS)) {
276 if (nomStatusTypes == null) {
277 populateNomStatusTypes();
278 }
279 descriptors.addElement(
280 new ComboBoxPropertyDescriptor(P_ID_NOMSTATUS, P_NOMSTATUS, P_NOMSTATUS_MENU));
281 }
282 if (id.equals(P_ID_UUID)) {
283 descriptors.addElement(
284 new PropertyDescriptor(P_ID_UUID, P_UUID));
285 }
286 if (id.equals(P_ID_MARKERS)) {
287 descriptors.addElement(
288 new PropertyDescriptor(P_ID_MARKERS, P_MARKERS));
289 }
290 if (id.equals(P_ID_NUM_OF_BASES)) {
291 descriptors.addElement(
292 new PropertyDescriptor(P_ID_NUM_OF_BASES, P_NUM_OF_BASES));
293 }
294 if (id.equals(P_ID_CREATED)) {
295 descriptors.addElement(
296 new PropertyDescriptor(P_ID_CREATED, P_CREATED));
297 }
298 if (id.equals(P_ID_CREATEDBY)) {
299 descriptors.addElement(
300 new PropertyDescriptor(P_ID_CREATEDBY, P_CREATEDBY));
301 }
302 if (id.equals(P_ID_NOMENCLATURAL_CODE)) {
303 descriptors.addElement(
304 new PropertyDescriptor(P_ID_NOMENCLATURAL_CODE, P_NOMENCLATURAL_CODE));
305 // Assuming the ability to change a name's code is not a burning priority ...
306 // new ComboBoxPropertyDescriptor(P_ID_NOMENCLATURAL_CODE, P_NOMENCLATURAL_CODE, P_NOMENCLATURALCODE_MENU));
307 }
308 if (id.equals(P_ID_NOMENCLATURAL_REF)) {
309 descriptors.addElement(
310 // new TextPropertyDescriptor(P_ID_NOMENCLATURAL_REF, P_NOMENCLATURAL_REF));
311 new PropertyDescriptor(P_ID_NOMENCLATURAL_REF, P_NOMENCLATURAL_REF));
312 }
313 if (id.equals(P_ID_NOMENCLATURAL_MICROREF)) {
314 descriptors.addElement(
315 new TextPropertyDescriptor(P_ID_NOMENCLATURAL_MICROREF,P_NOMENCLATURAL_MICROREF));
316 }
317 if (id.equals(P_ID_NOMREF_TYPE)) {
318 descriptors.addElement(
319 new PropertyDescriptor(P_ID_NOMREF_TYPE, P_NOMREF_TYPE));
320 // new ComboBoxPropertyDescriptor(P_ID_NOMREF_TYPE, P_NOMREF_TYPE, P_NOMREF_TYPE_MENU));
321 }
322 if (id.equals(P_ID_NOMREF_YEAR)) {
323 descriptors.addElement(
324 new TextPropertyDescriptor(P_ID_NOMREF_YEAR, P_NOMREF_YEAR));
325 }
326 if (id.equals(P_ID_NOMREF_TITLE)) {
327 descriptors.addElement(
328 new TextPropertyDescriptor(P_ID_NOMREF_TITLE, P_NOMREF_TITLE));
329 }
330 }
331
332 /**
333 * Constructor for top level property fields. All fields that are not subfields
334 * should be listed here.
335 * @param name
336 */
337 public NonViralNamePropertySource(NonViralName name) {
338 this(name, null, TOP_LEVEL_PROPERTIES);
339 }
340
341 public NonViralNamePropertySource(NonViralName name,
342 String parentid, String[] keys) {
343 this.name = name;
344 this.parentid = parentid;
345 for (String key : keys) {
346 addDescriptor(key);
347 }
348 }
349
350 public Object getEditableValue() {
351 return this;
352 }
353
354 public IPropertyDescriptor[] getPropertyDescriptors() {
355 return (IPropertyDescriptor[]) descriptors.toArray(
356 new IPropertyDescriptor[descriptors.size()]);
357 }
358
359 public Object getPropertyValue(Object id) {
360 if (id.equals(P_ID_PARSED)) {
361 return name.getHasProblem() == true ? "problem" : "parsed" ;
362 }
363 if (id.equals(P_ID_RANK)) {
364 if (this.name.getRank() == null) {
365 return 0;
366 }
367 Rank rank = this.name.getRank();
368 for (int i = 0; i < ranks.length; i++) {
369 if (ranks[i] == null) continue;
370 if (rank.getUuid().equals(ranks[i].getUuid()))
371 return i;
372 }
373 return 0;
374 }
375 if (id.equals(P_ID_TITLECACHE)) {
376
377 // If the name has not been parsed, only show the title cache
378 // if (name.getHasProblem()) {
379 if (name.getRank() == null) {
380 return CdmUtils.Nz(name.getTitleCache());
381 } else {
382 return new NonViralNamePropertySource(name, P_ID_TITLECACHE, getNameFields());
383 }
384 }
385 if (id.equals(P_ID_UNINOMIAL)) {
386 return CdmUtils.Nz(name.getGenusOrUninomial());
387 }
388 if (id.equals(P_ID_INFRAGENERICEP)) {
389 return CdmUtils.Nz(name.getInfraGenericEpithet());
390 }
391 if (id.equals(P_ID_SPECIESEP)) {
392 return CdmUtils.Nz(name.getSpecificEpithet());
393 }
394 if (id.equals(P_ID_INFRASPECIFICEP)) {
395 return CdmUtils.Nz(name.getInfraSpecificEpithet());
396 }
397 if (id.equals(P_ID_APPENDEDPHRASE)) {
398 return CdmUtils.Nz(name.getAppendedPhrase());
399 }
400 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
401 return CdmUtils.Nz(name.getAuthorshipCache());
402 }
403 if (id.equals(P_ID_HOMOTYPICALGROUP)) {
404 return CdmUtils.Nz(name.getHomotypicalGroup().getUuid().toString());
405 }
406 if (id.equals(P_ID_ANNOTATIONS)) {
407 return new AnnotationPropertySource(name.getAnnotations());
408 }
409 if (id.equals(P_ID_NOMSTATUS)) {
410 // return new NomStatusPropertySource(name.getStatus());
411 // if (this.name.getRank() == null) return 0;
412 Set<NomenclaturalStatus> nomStatusSet = this.name.getStatus();
413
414 if (nomStatusSet == null || nomStatusSet.size() == 0) {
415 return 0;
416 }
417 // TODO for now, only showing first nom status - change!
418 NomenclaturalStatus nomStatus = (NomenclaturalStatus) nomStatusSet.toArray()[0];
419 for (int i = 0; i < nomStatusTypes.length; i++) {
420 if (nomStatusTypes[i] == null) continue;
421 if (nomStatus.getType().getUuid().equals(nomStatusTypes[i].getUuid()))
422 return i;
423 }
424 return 0;
425 }
426 if (id.equals(P_ID_NAMERELATIONS)) {
427 return new NameRelationsPropertySource(name, name.getNameRelations());
428 }
429 // TODO Button to link to type module
430 if (id.equals(P_ID_TYPE)) {
431 return "";
432 }
433 if (id.equals(P_ID_UUID)) {
434 return CdmUtils.Nz(name.getUuid().toString());
435 }
436 if (id.equals(P_ID_MARKERS)) {
437 return new MarkersPropertySource(name.getMarkers());
438 }
439 if (id.equals(P_ID_NUM_OF_BASES)) {
440 return name.getTaxonBases().size();
441 }
442 if (id.equals(P_ID_CREATED)) {
443 if (name.getCreated() == null) {
444 return "";
445 } else {
446 return formatDate(name.getCreated().toDate());
447 }
448 }
449 if (id.equals(P_ID_CREATEDBY)) {
450 if (name.getCreatedBy() == null) {
451 return "";
452 } else {
453 return CdmUtils.Nz(name.getCreatedBy().generateTitle());
454 }
455 }
456 if (id.equals(P_ID_NOMENCLATURAL_CODE)) {
457 // Only shown if extending classes don't handle P_ID_NOMENCLATURAL_CODE
458 return "none (nonviral name)";
459 }
460 if (id.equals(P_ID_NOMENCLATURAL_REF)) {
461
462 // INomenclaturalReference nomenclaturalReference = (INomenclaturalReference) name.getNomenclaturalReference();
463 // if (name.getNomenclaturalReference() == null) {
464 // return "";
465 // } else {
466 // return new NonViralNamePropertySource(name, P_ID_NOMENCLATURAL_REF, getReferenceFields());
467 // }
468
469 ReferenceBase nomRef = (ReferenceBase) name.getNomenclaturalReference();
470
471 // Create nom. reference as necessary
472 if (nomRef == null) {
473 nomRef = Generic.NewInstance();
474 }
475
476 // Create property source for submenu
477 ReferencePropertySource nomRefPropertySource = new NomenclaturalReferencePropertySource(nomRef);
478
479 // Add listener to notify name of all changes to nom. reference
480 nomRefPropertySource.addPropertyChangeListener(new PropertyChangeListener() {
481 public void propertyChange(PropertyChangeEvent evt) {
482 if (evt.getNewValue() instanceof INomenclaturalReference) {
483 name.setNomenclaturalReference((INomenclaturalReference) evt.getNewValue());
484 }
485 }
486 });
487 return nomRefPropertySource;
488
489 }
490 if (id.equals(P_ID_NOMENCLATURAL_MICROREF)) {
491 return CdmUtils.Nz(name.getNomenclaturalMicroReference());
492 }
493 INomenclaturalReference nomenclaturalReference = (INomenclaturalReference) name.getNomenclaturalReference();
494 if (id.equals(P_ID_NOMREF_TYPE)) {
495 if (nomenclaturalReference instanceof Article) {
496 return "Article";
497 }
498 if (nomenclaturalReference instanceof BibtexReference) {
499 return "Bibtex";
500 }
501 if (nomenclaturalReference instanceof Book) {
502 return "Book";
503 }
504 if (nomenclaturalReference instanceof BookSection) {
505 return "Book Section";
506 }
507 if (nomenclaturalReference instanceof Generic) {
508 return "Generic";
509 }
510 return "none";
511 }
512 if (id.equals(P_ID_NOMREF_YEAR)) {
513 return CdmUtils.Nz(nomenclaturalReference.getYear());
514 }
515 if (id.equals(P_ID_NOMREF_TITLE)) {
516 if (nomenclaturalReference instanceof Article) {
517 return CdmUtils.Nz(((Article) nomenclaturalReference).getTitle());
518 }
519 if (nomenclaturalReference instanceof BibtexReference) {
520 return CdmUtils.Nz(((BibtexReference) nomenclaturalReference).getTitle());
521 }
522 if (nomenclaturalReference instanceof Book) {
523 return CdmUtils.Nz(((Book) nomenclaturalReference).getTitle());
524 }
525 if (nomenclaturalReference instanceof BookSection) {
526 return CdmUtils.Nz(((BookSection) nomenclaturalReference).getTitle());
527 }
528 if (nomenclaturalReference instanceof Generic) {
529 return CdmUtils.Nz(((Generic) nomenclaturalReference).getTitle());
530 }
531 }
532 return null;
533 }
534
535 /**
536 * The fields displayed under the title cache are dependent on rank.
537 *
538 * @return
539 */
540 private String[] getNameFields() {
541
542 // An unparsed name has no name fields, only a cache.
543 // Therefore, if rank is null, the name is unparsed.
544 if (name.getRank() == null) {
545 return null;
546 }
547
548 List<String> nameFields = new ArrayList<String>();
549 nameFields.add(P_ID_UNINOMIAL);
550 if (name.isSupraGeneric() || name.isGenus()) { // Rank is higher than GENUS or equals GENUS
551 }
552 else if (name.isInfraGeneric()) { // lower than GENUS and higher than SPECIES
553 nameFields.add(P_ID_INFRAGENERICEP);
554 }
555 else if (name.isSpecies()) { // Rank equals SPECIES
556 nameFields.add(P_ID_SPECIESEP);
557 }
558 else if (name.isInfraSpecific()) { // Rank is lower than SPECIES
559 nameFields.add(P_ID_SPECIESEP);
560 nameFields.add(P_ID_INFRASPECIFICEP);
561 } else {
562 nameFields.add(P_ID_SPECIESEP);
563 }
564 nameFields.add(P_ID_AUTHORSHIPCACHE);
565 nameFields.add(P_ID_APPENDEDPHRASE);
566 return nameFields.toArray(new String[nameFields.size()]);
567 }
568
569 private String[] getReferenceFields() {
570 List<String> referenceFields = new ArrayList<String>();
571
572 INomenclaturalReference reference = (INomenclaturalReference) name.getNomenclaturalReference();
573 if (reference == null) {
574 return null;
575 }
576
577 referenceFields.add(P_ID_NOMREF_TYPE);
578 referenceFields.add(P_ID_NOMREF_YEAR);
579 referenceFields.add(P_ID_NOMENCLATURAL_MICROREF);
580 referenceFields.add(P_ID_NOMREF_TITLE);
581
582 if (reference instanceof Article) {
583
584 }
585 if (reference instanceof BibtexReference) {
586
587 }
588 if (reference instanceof Book) {
589
590 }
591 if (reference instanceof BookSection) {
592
593 }
594 if (reference instanceof Generic) {
595
596 }
597
598 return referenceFields.toArray(new String[referenceFields.size()]);
599 }
600
601 private Object formatDate(Date date) {
602 return String.format("%1$tm-%1$td-%1$tY %1$tH:%1$tM:%1$tS", date);
603 }
604
605 public boolean isPropertySet(Object id) {
606 return false;
607 }
608
609 public void resetPropertyValue(Object id) {}
610
611 /**
612 * Any editable fields are set in the object here.
613 *
614 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
615 */
616 public void setPropertyValue(Object id, Object value) {
617
618 // Since this is only called when the property value changes,
619 // it is a reliable place to fire the name's property change
620 // listener.
621 name.firePropertyChange(ITaxEditorConstants.PROPERTY_SHEET_CHANGE, null, null);
622
623 if (id.equals(P_ID_RANK)) {
624 int index = ((Integer) value).intValue();
625 name.setRank(ranks[index]);
626 }
627 if (id.equals(P_ID_UNINOMIAL)) {
628 name.setGenusOrUninomial((String) value);
629 }
630 if (id.equals(P_ID_INFRAGENERICEP)) {
631 name.setInfraGenericEpithet((String) value);
632 }
633 if (id.equals(P_ID_SPECIESEP)) {
634 name.setSpecificEpithet((String) value);
635 }
636 if (id.equals(P_ID_INFRASPECIFICEP)) {
637 name.setInfraSpecificEpithet((String) value);
638 }
639 if (id.equals(P_ID_APPENDEDPHRASE)) {
640 name.setAppendedPhrase((String) value);
641 }
642 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
643 name.setAuthorshipCache((String) value);
644 }
645 if (id.equals(P_ID_ANNOTATIONS)) {
646 Set annotations = name.getAnnotations();
647 if (value instanceof Set) {
648 for (Object object: (Set) value) {
649 if (object instanceof Annotation && !annotations.contains(object)) {
650 name.addAnnotation((Annotation) object);
651 }
652 }
653 }
654 }
655 if (id.equals(P_ID_NAMERELATIONS)) {
656 // TODO set name relations
657 }
658 if (id.equals(P_ID_NOMSTATUS)) {
659 NomenclaturalStatus nomStatus;
660 int index = ((Integer) value).intValue();
661 Set<NomenclaturalStatus> nomStatusSet = name.getStatus();
662
663 if (nomStatusSet.size() > 0) {
664 nomStatus = (NomenclaturalStatus) nomStatusSet.toArray()[0];
665
666 if (index == 0) {
667 nomStatusSet.remove(nomStatus);
668 } else {
669 nomStatus.setType(nomStatusTypes[index]);
670 }
671
672 } else {
673 nomStatus = NomenclaturalStatus.NewInstance(nomStatusTypes[index]);
674 name.addStatus(nomStatus);
675 }
676 }
677 if (id.equals(P_ID_MARKERS)) {
678 // TODO set markers
679 }
680 if (id.equals(P_ID_NOMENCLATURAL_REF)) {
681 // TODO ask Andreas M. how best to set nom. ref
682 }
683 if (id.equals(P_ID_NOMENCLATURAL_MICROREF)) {
684 name.setNomenclaturalMicroReference((String) value);
685 }
686 if (id.equals(P_ID_NOMREF_YEAR)) {
687 INomenclaturalReference reference = name.getNomenclaturalReference();
688
689 if (reference instanceof BibtexReference) {
690 ((BibtexReference) reference).setYear((String) value);
691 }
692 TimePeriod datePublished = CdmUtil.getDatePublished((String) value);
693 if (reference instanceof Article) {
694 ((Article) reference).setDatePublished(datePublished);
695 }
696 if (reference instanceof Book) {
697 ((Book) reference).setDatePublished(datePublished);
698 }
699 if (reference instanceof BookSection) {
700 ((BookSection) reference).setDatePublished(datePublished);
701 }
702 if (reference instanceof Generic) {
703 ((Generic) reference).setDatePublished(datePublished);
704 }
705 }
706 if (id.equals(P_ID_NOMREF_TITLE)) {
707 INomenclaturalReference reference = name.getNomenclaturalReference();
708
709 if (reference instanceof BibtexReference) {
710 ((BibtexReference) reference).setTitle((String) value);
711 }
712 if (reference instanceof Article) {
713 ((Article) reference).setTitle((String) value);
714 }
715 if (reference instanceof Book) {
716 ((Book) reference).setTitle((String) value);
717 }
718 if (reference instanceof BookSection) {
719 ((BookSection) reference).setTitle((String) value);
720 }
721 if (reference instanceof Generic) {
722 ((Generic) reference).setTitle((String) value);
723 }
724 }
725
726 // Reset both caches to reflect property sheet changes
727 name.setTitleCache(name.generateTitle(), false);
728 name.setFullTitleCache(name.generateFullTitle(), false);
729
730 name.firePropertyChange(ITaxEditorConstants.REFRESH_NAMEVIEWER, null, null);
731 }
732
733 /**
734 * Any node with children must override {@link java.lang.Object#toString()}
735 * to display its name correctly
736 *
737 * @see java.lang.Object#toString()
738 */
739 public String toString() {
740 if (parentid == null || parentid.equals("")) {
741 return "";
742 }
743 if (parentid.equals(P_ID_TITLECACHE)) {
744 return name.getTitleCache();
745 }
746 if (parentid.equals(P_ID_NOMENCLATURAL_REF)) {
747 INomenclaturalReference nomenclaturalReference = name.getNomenclaturalReference();
748 if (nomenclaturalReference == null) {
749 return "";
750 }
751 String microReference = name.getNomenclaturalMicroReference();
752 return CdmUtils.Nz(nomenclaturalReference.getNomenclaturalCitation(microReference));
753 }
754 return super.toString();
755 }
756
757 public void addPropertyChangeListener(
758 PropertyChangeListener listener) {
759 this.propertyChangeSupport.addPropertyChangeListener(listener);
760 }
761 }