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
.HashSet
;
14 import java
.util
.SortedSet
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
19 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
20 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
21 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
22 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
23 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
24 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
25 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
26 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
27 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
28 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
35 public class PreferencesUtil
{
36 private static final Logger logger
= Logger
37 .getLogger(PreferencesUtil
.class);
40 public static void setNomenclaturalCode(String preferredCode
) {
41 getPrefStore().setValue(ITaxEditorConstants
.CODE_PREFERENCE
, preferredCode
);
44 public static void setDefaultNomenclaturalCode() {
45 setNomenclaturalCode(ITaxEditorConstants
.DEFAULT_CODE_PREFERENCE
);
48 public static String
getPreferredNomenclaturalCodeAsString() {
49 String nameCodePreference
= getNameCodePreference();
51 if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICBN
)) {
52 return "International Code of Botanical Nomenclature (ICBN)";
53 } else if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICZN
)) {
54 return "International Code of Zoological Nomenclature (ICZN)";
62 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
64 String nameCodePreference
= getNameCodePreference();
66 if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICBN
)) {
67 return NomenclaturalCode
.ICBN();
68 } else if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICZN
)) {
69 return NomenclaturalCode
.ICZN();
74 private static String
getNameCodePreference() {
75 return getPrefStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
79 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
80 * to have shown in preferences.
83 * <code>Feature</code>s are shown unless otherwise specified.
87 public static Set
<Feature
> getPreferredFeatures() {
89 // Initialize preferredFeatureSet as necessary
90 if (GlobalController
.getPreferredFeatures() == null) {
92 GlobalController
.setPreferredFeatures(new HashSet
<Feature
>());
94 for (Feature feature
: CdmSessionDataRepository
.getDefault().getFeatures()) {
96 // If the feature is set to show, add it to preferredFeatureSet
97 if (PreferencesUtil
.getFeaturePreference(feature
)) {
98 GlobalController
.addPreferredFeature(feature
);
103 return GlobalController
.getPreferredFeatures();
107 * True if <code>Feature</code> is set to "show" in preferences.
112 public static boolean getFeaturePreference(Feature feature
) {
114 String preferenceKey
= getPreferenceKey(feature
);
116 // If feature does not yet have a pref, set it to true
117 if (!PreferencesUtil
.getPrefStore().contains(preferenceKey
)) {
118 PreferencesUtil
.getPrefStore().setDefault(preferenceKey
, true);
121 return PreferencesUtil
.getPrefStore().getBoolean(preferenceKey
);
125 * Set the show state of a <code>Feature</code> in the
126 * <code>PreferenceStore</code>.
128 * Also sets <code>preferredFeatureSet</code> to null to force it be
129 * re-populated the next time {@link getPreferredFeatures} is called.
134 public static void setFeaturePreference(Feature feature
, boolean show
) {
135 GlobalController
.setPreferredFeatures(null);
136 PreferencesUtil
.getPrefStore().setValue(PreferencesUtil
.getPreferenceKey(feature
), show
);
139 public static IPreferenceStore
getPrefStore() {
140 return TaxEditorPlugin
.getDefault().getPreferenceStore();
144 * Construct a unique key using <code>Feature</code>'s <code>Uuid</code>
149 private static String
getPreferenceKey(Feature feature
) {
150 return ITaxEditorConstants
.FEATURE_PREFERENCE
152 . concat(feature
.getUuid().toString());
156 * Returns a <code>Set</code> of the <code>Rank</code>s that the user has chosen
157 * to have shown in preferences.
160 * <code>Rank</code>s are shown unless otherwise specified.
164 public static Set
<Rank
> getPreferredRanks() {
166 // Initialize preferredRankSet as necessary
167 if (GlobalController
.getPreferredRanks() == null) {
169 GlobalController
.setPreferredRanks(new HashSet
<Rank
>());
171 SortedSet
<Rank
> ranks
= CdmSessionDataRepository
.getDefault().getRanks();
174 for (Rank rank
: CdmSessionDataRepository
.getDefault().getRanks()) {
176 // If the feature is set to show, add it to preferredFeatureSet
177 if (PreferencesUtil
.getRankPreference(rank
)) {
178 GlobalController
.addPreferredRank(rank
);
181 } // else { TODO: error message
184 return GlobalController
.getPreferredRanks();
188 * True if <code>Rank</code> is set to "show" in preferences.
193 public static boolean getRankPreference(Rank rank
) {
195 String preferenceKey
= PreferencesUtil
.getPreferenceKey(rank
);
197 // If rank does not yet have a pref, set it to true
198 if (!getPrefStore().contains(preferenceKey
)) {
199 getPrefStore().setDefault(preferenceKey
, true);
202 return getPrefStore().getBoolean(preferenceKey
);
206 * Set the show state of a <code>Rank</code> in the
207 * <code>PreferenceStore</code>.
209 * Also sets <code>preferredRankSet</code> to null to force it be
210 * re-populated the next time {@link getPreferredRanks} is called.
215 public static void setRankPreference(Rank rank
, boolean show
) {
216 GlobalController
.setPreferredRanks(null);
217 getPrefStore().setValue(PreferencesUtil
.getPreferenceKey(rank
), show
);
222 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
227 private static String
getPreferenceKey(Rank rank
) {
228 return ITaxEditorConstants
.RANK_PREFERENCE
230 . concat(rank
.getUuid().toString());
234 * Returns a new name object that conforms to the nomenclatural
235 * code specified in user preferences.
239 public static NonViralName
getInstanceOfPreferredNameClass() {
240 NomenclaturalCode code
= getPreferredNomenclaturalCode();
242 // Check whether name code preference needs to be initialized
244 setDefaultNomenclaturalCode();
247 if (code
.equals(NomenclaturalCode
.ICBN())) {
248 return BotanicalName
.NewInstance(null);
249 } else if (code
.equals(NomenclaturalCode
.ICZN())) {
250 return ZoologicalName
.NewInstance(null);
252 return NonViralName
.NewInstance(null);
256 * Appends the code (i.e. ICBN or ICZN) specified in the user preferences
257 * to a string. The result is separated by a period: "<code>key.ICBN</code>".
262 public static String
concatCodeMessageSuffix(String key
) {
263 String code
= getNameCodePreference();
264 return key
+ "." + code
;