2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.propertysheet
.name
;
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
;
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
;
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
;
35 public class ScientificNamePropertySource
implements IPropertySource
{
36 private static final Logger logger
= Logger
37 .getLogger(ScientificNamePropertySource
.class);
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";
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";
63 private static final int CACHE_NOT_PROTECTED
= 0;
64 private static final int CACHE_PROTECTED
= 1;
66 public ScientificNamePropertySource(NonViralName name
) {
68 // Default type of ReferenceBase is Generic
70 name
= PreferencesController
.getInstanceOfPreferredNameClass();
77 protected void initDescriptors() {
79 List
<String
> displayFields
= new ArrayList
<String
>();
82 // displayFields.add(P_ID_SEARCH);
83 // displayFields.add(P_ID_EDITABLECACHE);
84 // displayFields.add(P_ID_PROTECT_CACHE);
87 displayFields
.add(P_ID_UNINOMIAL
);
89 if (name
.isSupraGeneric() || name
.isGenus()) { // Rank is higher than GENUS or equals GENUS
91 else if (name
.isInfraGeneric()) { // lower than GENUS and higher than SPECIES
92 displayFields
.add(P_ID_INFRAGENERICEP
);
94 else if (name
.isSpecies()) { // Rank equals SPECIES
95 displayFields
.add(P_ID_SPECIESEP
);
97 else if (name
.isInfraSpecific()) { // Rank is lower than SPECIES
98 displayFields
.add(P_ID_SPECIESEP
);
99 displayFields
.add(P_ID_INFRASPECIFICEP
);
101 displayFields
.add(P_ID_SPECIESEP
);
103 displayFields
.add(P_ID_AUTHORSHIPCACHE
);
104 displayFields
.add(P_ID_APPENDEDPHRASE
);
107 for (String field
: displayFields
) {
108 addDescriptor(field
);
112 protected Vector
<PropertyDescriptor
> descriptors
= new Vector
<PropertyDescriptor
>();
114 protected void addDescriptor(String id
) {
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);
127 if (id
.equals(P_ID_EDITABLECACHE
)) {
128 descriptors
.addElement(
129 new TextPropertyDescriptor(P_ID_EDITABLECACHE
, P_EDITABLECACHE
));
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"}));
139 // Uninomial (aka Genus)
140 if (id
.equals(P_ID_UNINOMIAL
)) {
141 descriptors
.addElement(
142 new TextPropertyDescriptor(P_ID_UNINOMIAL
, P_UNINOMIAL
));
145 // Infrageneric epithet
146 if (id
.equals(P_ID_INFRAGENERICEP
)) {
147 descriptors
.addElement(
148 new TextPropertyDescriptor(P_ID_INFRAGENERICEP
, P_INFRAGENERICEP
));
152 if (id
.equals(P_ID_SPECIESEP
)) {
153 descriptors
.addElement(
154 new TextPropertyDescriptor(P_ID_SPECIESEP
, P_SPECIESEP
));
157 // Infraspecific epithet
158 if (id
.equals(P_ID_INFRASPECIFICEP
)) {
159 descriptors
.addElement(
160 new TextPropertyDescriptor(P_ID_INFRASPECIFICEP
, P_INFRASPECIFICEP
));
164 if (id
.equals(P_ID_APPENDEDPHRASE
)) {
165 descriptors
.addElement(
166 new TextPropertyDescriptor(P_ID_APPENDEDPHRASE
, P_APPENDEDPHRASE
));
170 if (id
.equals(P_ID_AUTHORSHIPCACHE
)) {
171 descriptors
.addElement(
172 new TextPropertyDescriptor(P_ID_AUTHORSHIPCACHE
, P_AUTHORSHIPCACHE
));
177 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
179 public Object
getEditableValue() {
180 return CdmUtils
.Nz(name
.getTitleCache());
184 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
186 public IPropertyDescriptor
[] getPropertyDescriptors() {
187 return (IPropertyDescriptor
[]) descriptors
.toArray(
188 new IPropertyDescriptor
[descriptors
.size()]);
192 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
194 public Object
getPropertyValue(Object id
) {
197 if (id
.equals(P_ID_EDITABLECACHE
)) {
198 return (CdmUtils
.Nz(name
.getTitleCache()));
202 if (id
.equals(P_ID_PROTECT_CACHE
)) {
203 if (name
.isProtectedTitleCache()) {
204 return CACHE_PROTECTED
;
206 return CACHE_NOT_PROTECTED
;
210 // Uninomial (aka Genus)
211 if (id
.equals(P_ID_UNINOMIAL
)) {
212 return CdmUtils
.Nz(name
.getGenusOrUninomial());
215 // Infrageneric epithet
216 if (id
.equals(P_ID_INFRAGENERICEP
)) {
217 return CdmUtils
.Nz(name
.getInfraGenericEpithet());
221 if (id
.equals(P_ID_SPECIESEP
)) {
222 return CdmUtils
.Nz(name
.getSpecificEpithet());
225 // Infraspecific epithet
226 if (id
.equals(P_ID_INFRASPECIFICEP
)) {
227 return CdmUtils
.Nz(name
.getInfraSpecificEpithet());
232 if (id
.equals(P_ID_APPENDEDPHRASE
)) {
233 return CdmUtils
.Nz(name
.getAppendedPhrase());
237 if (id
.equals(P_ID_AUTHORSHIPCACHE
)) {
238 return CdmUtils
.Nz(name
.getAuthorshipCache());
245 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
247 public boolean isPropertySet(Object id
) {
252 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
254 public void resetPropertyValue(Object id
) {}
257 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
259 public void setPropertyValue(Object id
, Object value
) {
262 if (id
.equals(P_ID_SEARCH
)) {
263 if (value
instanceof NonViralName
) {
264 name
= (NonViralName
) value
;
269 if (id
.equals(P_ID_PROTECT_CACHE
)) {
270 if (((Integer
) value
).intValue() == CACHE_PROTECTED
) {
271 name
.setProtectedTitleCache(true);
273 name
.setProtectedTitleCache(false);
277 // Uninomial (aka Genus)
278 if (id
.equals(P_ID_UNINOMIAL
)) {
279 name
.setGenusOrUninomial((String
) value
);
282 // Infrageneric epithet
283 if (id
.equals(P_ID_INFRAGENERICEP
)) {
284 name
.setInfraGenericEpithet((String
) value
);
288 if (id
.equals(P_ID_SPECIESEP
)) {
289 name
.setSpecificEpithet((String
) value
);
292 // Infraspecific epithet
293 if (id
.equals(P_ID_INFRASPECIFICEP
)) {
294 name
.setInfraSpecificEpithet((String
) value
);
298 if (id
.equals(P_ID_APPENDEDPHRASE
)) {
299 name
.setAppendedPhrase((String
) value
);
303 if (id
.equals(P_ID_AUTHORSHIPCACHE
)) {
304 // name.setProtectedAuthorshipCache(true);
305 name
.setAuthorshipCache((String
) value
);
309 if (id
.equals(P_ID_EDITABLECACHE
)) {
310 name
.setTitleCache((String
) value
);
312 name
.setTitleCache(name
.generateTitle(), name
.isProtectedTitleCache());
315 propertyChangeSupport
.firePropertyChange(ITaxEditorConstants
.PROPERTY_SHEET_CHANGE
, null, name
);
319 public String
toString() {
320 return CdmUtils
.Nz(name
.getTitleCache());
323 private PropertyChangeSupport propertyChangeSupport
= new PropertyChangeSupport(this);
325 public void addPropertyChangeListener(
326 PropertyChangeListener listener
) {
327 propertyChangeSupport
.addPropertyChangeListener(listener
);