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
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
17 import org
.eclipse
.ui
.editors
.text
.EditorsUI
;
18 import org
.eclipse
.ui
.texteditor
.SourceViewerDecorationSupport
;
20 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
21 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
22 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
23 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
24 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
25 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
26 import eu
.etaxonomy
.taxeditor
.model
.Resources
;
27 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
28 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
35 public class PreferencesUtil
{
36 private static final Logger logger
= Logger
37 .getLogger(PreferencesUtil
.class);
40 private static final String FEATURE_PREFERENCE
= "FEATURE_PREFERENCE";
43 private static SourceViewerDecorationSupport decorationSupport
;
49 public static IPreferenceStore
getPrefStore() {
50 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
55 * @param preferredCode
57 public static void setNomenclaturalCode(String preferredCode
) {
58 getPrefStore().setValue(Resources
.CODE_PREFERENCE
, preferredCode
);
64 public static void setDefaultNomenclaturalCode() {
65 setNomenclaturalCode(Resources
.DEFAULT_CODE_PREFERENCE
);
72 public static String
getPreferredNomenclaturalCodeAsString() {
73 String nameCodePreference
= getNameCodePreference();
75 if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICBN
)) {
76 return "International Code of Botanical Nomenclature (ICBN)";
77 } else if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICZN
)) {
78 return "International Code of Zoological Nomenclature (ICZN)";
86 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
88 String nameCodePreference
= getNameCodePreference();
90 if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICBN
)) {
91 return NomenclaturalCode
.ICBN
;
92 } else if (nameCodePreference
.equals(Resources
.CODE_PREFERENCE_ICZN
)) {
93 return NomenclaturalCode
.ICZN
;
102 private static String
getNameCodePreference() {
104 return getPrefStore().getString(Resources
.CODE_PREFERENCE
);
105 }catch(NullPointerException e
){
106 logger
.warn("PreferenceStore was not available. That is OK if you are running a unit test. Setting ICBN as default code.");
107 return Resources
.CODE_PREFERENCE_ICBN
;
112 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
113 * to have shown in preferences.
116 * <code>Feature</code>s are shown unless otherwise specified.
120 public static List
<Feature
> getPreferredFeatures() {
122 ArrayList
<Feature
> preferedFeatures
= new ArrayList
<Feature
>();
124 for (Feature feature
: CdmStore
.getFeatures()) {
125 if(getPrefStore().getBoolean(getPreferenceKey(feature
))){
126 preferedFeatures
.add(feature
);
130 return preferedFeatures
;
135 * @param preferredFeatureList
137 public static void setPreferredFeatures(List
<Feature
> preferredFeatureList
) {
139 for (Feature feature
: CdmStore
.getFeatures()) {
140 getPrefStore().setValue(getPreferenceKey(feature
), preferredFeatureList
.contains(feature
));
145 * Construct a unique key using <code>Feature</code>'s <code>Uuid</code>
150 private static String
getPreferenceKey(Feature feature
) {
151 return FEATURE_PREFERENCE
153 . concat(feature
.getUuid().toString());
157 * Returns a <code>Set</code> of the <code>Rank</code>s that the user has chosen
158 * to have shown in preferences.
161 * <code>Rank</code>s are shown unless otherwise specified.
165 public static List
<Rank
> getPreferredRanks() {
167 ArrayList
<Rank
> preferedRanks
= new ArrayList
<Rank
>();
169 for (Rank rank
: CdmStore
.getRanks()) {
170 if(getPrefStore().getBoolean(getPreferenceKey(rank
))){
171 preferedRanks
.add(rank
);
175 return preferedRanks
;
180 * @param preferedRankList
182 public static void setPreferredRanks(List
<Rank
> preferedRankList
) {
183 for (Rank rank
: CdmStore
.getRanks()) {
184 getPrefStore().setValue(getPreferenceKey(rank
), preferedRankList
.contains(rank
));
191 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
196 private static String
getPreferenceKey(Rank rank
) {
197 return Resources
.RANK_PREFERENCE
199 . concat(rank
.getUuid().toString());
203 * Returns a new name object that conforms to the nomenclatural
204 * code specified in user preferences.
208 public static NonViralName
<?
> getInstanceOfPreferredNameClass() {
209 NomenclaturalCode code
= getPreferredNomenclaturalCode();
211 // Check whether name code preference needs to be initialized
213 setDefaultNomenclaturalCode();
216 if (code
.equals(NomenclaturalCode
.ICBN
)) {
217 return BotanicalName
.NewInstance(null);
218 } else if (code
.equals(NomenclaturalCode
.ICZN
)) {
219 return ZoologicalName
.NewInstance(null);
221 return NonViralName
.NewInstance(null);
225 * Appends the code (i.e. ICBN or ICZN) specified in the user preferences
226 * to a string. The result is separated by a period: "<code>key.ICBN</code>".
231 public static String
concatCodeMessageSuffix(String key
) {
232 String code
= getNameCodePreference();
233 return key
+ "." + code
;
240 public static SourceViewerDecorationSupport
getDecorationSupport() {
241 if (decorationSupport
== null) {
242 decorationSupport
= new SourceViewerDecorationSupport(null, null, null, EditorsUI
.getSharedTextColors());
244 return decorationSupport
;