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
.preference
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
19 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
20 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
21 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
22 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
23 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
24 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
28 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
29 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
30 import eu
.etaxonomy
.taxeditor
.model
.Resources
;
31 import eu
.etaxonomy
.taxeditor
.store
.VocabularyStore
;
32 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
40 public class PreferencesUtil
{
41 private static final Logger logger
= Logger
42 .getLogger(PreferencesUtil
.class);
48 public static IPreferenceStore
getPreferenceStore() {
49 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
56 private static String
getNameCodePreference() {
58 return getPreferenceStore().getString(Resources
.CODE_PREFERENCE
);
59 }catch(NullPointerException e
){
60 logger
.warn("PreferenceStore was not available. That is OK if you are running a unit test. Setting ICBN as default code.");
61 return Resources
.CODE_PREFERENCE_ICBN
;
67 * @param preferredCode
69 public static void setNomenclaturalCode(String preferredCode
) {
70 getPreferenceStore().setValue(Resources
.CODE_PREFERENCE
, preferredCode
);
76 public static void setDefaultNomenclaturalCode() {
77 setNomenclaturalCode(Resources
.DEFAULT_CODE_PREFERENCE
);
84 public static String
getPreferredNomenclaturalCodeAsString() {
85 String nameCodePreference
= getNameCodePreference();
87 if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICBN
)) {
88 return "International Code of Botanical Nomenclature (ICBN)";
89 } else if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICZN
)) {
90 return "International Code of Zoological Nomenclature (ICZN)";
98 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
100 String nameCodePreference
= getNameCodePreference();
102 if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICBN
)) {
103 return NomenclaturalCode
.ICBN
;
104 } else if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICZN
)) {
105 return NomenclaturalCode
.ICZN
;
112 * Returns a new name object that conforms to the nomenclatural
113 * code specified in user preferences.
117 public static NonViralName
<?
> getInstanceOfPreferredNameClass() {
118 NomenclaturalCode code
= getPreferredNomenclaturalCode();
120 // Check whether name code preference needs to be initialized
122 setDefaultNomenclaturalCode();
125 if (code
.equals(NomenclaturalCode
.ICBN
)) {
126 return BotanicalName
.NewInstance(null);
127 } else if (code
.equals(NomenclaturalCode
.ICZN
)) {
128 return ZoologicalName
.NewInstance(null);
130 return NonViralName
.NewInstance(null);
134 * Generic method to get term preferences for a term vocabulary
137 * @param initialFeatures
140 public static <T
extends DefinedTermBase
> List
<T
> getPreferredTerms(Set
<T
> initialFeatures
){
141 List
<T
> preferredTerms
= new ArrayList
<T
>();
143 for (T term
: initialFeatures
){
144 if(getPreferenceStore().getBoolean(getPreferenceKey(term
))){
145 preferredTerms
.add(term
);
149 return preferredTerms
;
152 /** @return a <code>List</code> containing user preferred <code>Feature</code> terms */
153 public static List
<Feature
> getPreferredFeatures() { return getPreferredTerms(VocabularyStore
.getFeatures());}
155 /** @return a <code>List</code> containing user preferred <code>Rank</code> terms */
156 public static List
<Rank
> getPreferredRanks() { return getPreferredTerms(VocabularyStore
.getRanks());}
158 /** @return a <code>List</code> containing user preferred <code>PresenceAbsenceTermBase</code> terms */
159 public static List
<PresenceAbsenceTermBase
<PresenceAbsenceTermBase
<?
>>> getPreferredPresenceAbsenceTerms(){ return getPreferredTerms(VocabularyStore
.getPresenceAbsenceTerms());}
161 /** @return a <code>List</code> containing user preferred <code>NomenclaturalStatusType</code> terms */
162 public static List
<NomenclaturalStatusType
> getPreferredNomenclaturalStatusTypes(){ return getPreferredTerms(VocabularyStore
.getNomenclaturalStatusTypes());}
164 /** @return a <code>List</code> containing user preferred <code>NameRelationshipType</code> terms */
165 public static List
<NameRelationshipType
> getPreferredNameRelationshipTypes(){ return getPreferredTerms(VocabularyStore
.getNameRelationshipTypes());}
169 * Generic method to set term preferences
171 * @param preferredTerms
174 public static <T
extends DefinedTermBase
> void setPreferredTerms(List
<T
> preferredTerms
, Set
<T
> initalTerms
){
175 for(TermBase term
: initalTerms
){
176 getPreferenceStore().setValue(getPreferenceKey(term
), preferredTerms
.contains(term
));
181 * Construct a unique key using the terms uuid
186 private static String
getPreferenceKey(TermBase term
) {
187 return term
.getClass().getName()
189 . concat(term
.getUuid().toString());