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
.PropertyDescriptor
;
22 import org
.eclipse
.ui
.views
.properties
.TextPropertyDescriptor
;
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
;
34 public class ScientificNamePropertySource
implements ICdmBasePropertySource
{
35 private static final Logger logger
= Logger
36 .getLogger(ScientificNamePropertySource
.class);
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";
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";
62 private static final int CACHE_NOT_PROTECTED
= 0;
63 private static final int CACHE_PROTECTED
= 1;
65 public ScientificNamePropertySource(NonViralName name
) {
67 // Default type of ReferenceBase is Generic
69 // if (name == null) {
70 // name = PreferencesUtil.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 boolean isProtectedCache
= name
.isProtectedTitleCache();
140 // boolean isProtectedCache = false;
142 // Uninomial (aka Genus)
143 if (id
.equals(P_ID_UNINOMIAL
)) {
144 descriptors
.addElement(
146 new PropertyDescriptor(P_ID_UNINOMIAL
, P_UNINOMIAL
) :
147 new TextPropertyDescriptor(P_ID_UNINOMIAL
, P_UNINOMIAL
));
150 // Infrageneric epithet
151 if (id
.equals(P_ID_INFRAGENERICEP
)) {
152 descriptors
.addElement(
154 new PropertyDescriptor(P_ID_INFRAGENERICEP
, P_INFRAGENERICEP
) :
155 new TextPropertyDescriptor(P_ID_INFRAGENERICEP
, P_INFRAGENERICEP
));
159 if (id
.equals(P_ID_SPECIESEP
)) {
160 descriptors
.addElement(
162 new PropertyDescriptor(P_ID_SPECIESEP
, P_SPECIESEP
) :
163 new TextPropertyDescriptor(P_ID_SPECIESEP
, P_SPECIESEP
));
166 // Infraspecific epithet
167 if (id
.equals(P_ID_INFRASPECIFICEP
)) {
168 descriptors
.addElement(
170 new PropertyDescriptor(P_ID_INFRASPECIFICEP
, P_INFRASPECIFICEP
) :
171 new TextPropertyDescriptor(P_ID_INFRASPECIFICEP
, P_INFRASPECIFICEP
));
175 if (id
.equals(P_ID_APPENDEDPHRASE
)) {
176 descriptors
.addElement(
178 new PropertyDescriptor(P_ID_APPENDEDPHRASE
, P_APPENDEDPHRASE
) :
179 new TextPropertyDescriptor(P_ID_APPENDEDPHRASE
, P_APPENDEDPHRASE
));
183 if (id
.equals(P_ID_AUTHORSHIPCACHE
)) {
184 descriptors
.addElement(
185 new PropertyDescriptor(P_ID_AUTHORSHIPCACHE
, P_AUTHORSHIPCACHE
));
190 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
192 public Object
getEditableValue() {
193 return CdmUtils
.Nz(name
.getTitleCache());
197 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
199 public IPropertyDescriptor
[] getPropertyDescriptors() {
200 return (IPropertyDescriptor
[]) descriptors
.toArray(
201 new IPropertyDescriptor
[descriptors
.size()]);
205 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
207 public Object
getPropertyValue(Object id
) {
210 if (id
.equals(P_ID_EDITABLECACHE
)) {
211 return (CdmUtils
.Nz(name
.getTitleCache()));
215 if (id
.equals(P_ID_PROTECT_CACHE
)) {
216 if (name
.isProtectedTitleCache()) {
217 return CACHE_PROTECTED
;
219 return CACHE_NOT_PROTECTED
;
223 // Uninomial (aka Genus)
224 if (id
.equals(P_ID_UNINOMIAL
)) {
225 return CdmUtils
.Nz(name
.getGenusOrUninomial());
228 // Infrageneric epithet
229 if (id
.equals(P_ID_INFRAGENERICEP
)) {
230 return CdmUtils
.Nz(name
.getInfraGenericEpithet());
234 if (id
.equals(P_ID_SPECIESEP
)) {
235 return CdmUtils
.Nz(name
.getSpecificEpithet());
238 // Infraspecific epithet
239 if (id
.equals(P_ID_INFRASPECIFICEP
)) {
240 return CdmUtils
.Nz(name
.getInfraSpecificEpithet());
245 if (id
.equals(P_ID_APPENDEDPHRASE
)) {
246 return CdmUtils
.Nz(name
.getAppendedPhrase());
250 if (id
.equals(P_ID_AUTHORSHIPCACHE
)) {
251 if (name
.isProtectedTitleCache()) {
252 return CdmUtils
.Nz(name
.getAuthorshipCache());
254 return new NomenclaturalAuthorshipPropertySource(name
);
262 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
264 public boolean isPropertySet(Object id
) {
269 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
271 public void resetPropertyValue(Object id
) {}
274 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
276 public void setPropertyValue(Object id
, Object value
) {
279 if (id
.equals(P_ID_SEARCH
)) {
280 if (value
instanceof NonViralName
) {
281 name
= (NonViralName
) value
;
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
);
292 // Uninomial (aka Genus)
293 if (id
.equals(P_ID_UNINOMIAL
)) {
294 name
.setGenusOrUninomial((String
) value
);
297 // Infrageneric epithet
298 if (id
.equals(P_ID_INFRAGENERICEP
)) {
299 name
.setInfraGenericEpithet((String
) value
);
303 if (id
.equals(P_ID_SPECIESEP
)) {
304 name
.setSpecificEpithet((String
) value
);
307 // Infraspecific epithet
308 if (id
.equals(P_ID_INFRASPECIFICEP
)) {
309 name
.setInfraSpecificEpithet((String
) value
);
313 if (id
.equals(P_ID_APPENDEDPHRASE
)) {
314 name
.setAppendedPhrase((String
) value
);
318 // if (id.equals(P_ID_AUTHORSHIPCACHE)) {
319 //// name.setProtectedAuthorshipCache(true);
320 // name.setAuthorshipCache((String) value);
324 if (id
.equals(P_ID_EDITABLECACHE
)) {
325 name
.setTitleCache((String
) value
);
327 // name.setTitleCache(name.generateTitle(), name.isProtectedTitleCache());
330 propertyChangeSupport
.firePropertyChange(Resources
.PROPERTY_SHEET_CHANGE
, null, name
);
334 public String
toString() {
335 return CdmUtils
.Nz(name
.getTitleCache());
338 private PropertyChangeSupport propertyChangeSupport
= new PropertyChangeSupport(this);
340 public void addPropertyChangeListener(
341 PropertyChangeListener listener
) {
342 propertyChangeSupport
.addPropertyChangeListener(listener
);