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
.controller
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collections
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import java
.util
.SortedSet
;
20 import java
.util
.TreeMap
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
25 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
26 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
29 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
30 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
31 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
32 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
33 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
40 public class PreferencesController
{
41 private static final Logger logger
= Logger
42 .getLogger(PreferencesController
.class);
45 public static void setNomenclaturalCode(String preferredCode
) {
46 getPrefStore().setValue(ITaxEditorConstants
.CODE_PREFERENCE
, preferredCode
);
49 public static void setDefaultNomenclaturalCode() {
50 setNomenclaturalCode(ITaxEditorConstants
.DEFAULT_CODE_PREFERENCE
);
53 public static String
getPreferredNomenclaturalCodeAsString() {
54 String nameCodePreference
= getNameCodePreference();
56 if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICBN
)) {
57 return "International Code of Botanical Nomenclature (ICBN)";
58 } else if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICZN
)) {
59 return "International Code of Zoological Nomenclature (ICZN)";
67 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
69 String nameCodePreference
= getNameCodePreference();
71 if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICBN
)) {
72 return NomenclaturalCode
.ICBN
;
73 } else if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICZN
)) {
74 return NomenclaturalCode
.ICZN
;
79 private static String
getNameCodePreference() {
80 return getPrefStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
84 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
85 * to have shown in preferences.
88 * <code>Feature</code>s are shown unless otherwise specified.
92 public static List
<Feature
> getPreferredFeatures() {
94 // Initialize preferredFeatureSet as necessary
95 if (GlobalController
.getPreferredFeatures() == null) {
97 GlobalController
.setPreferredFeatures(new ArrayList
<Feature
>());
98 for (Feature feature
: CdmSessionDataRepository
.getDefault().getFeatures()) {
100 // If the feature is set to show, add it to preferredFeatureSet
101 if (PreferencesController
.getFeaturePreference(feature
)) {
102 GlobalController
.addPreferredFeature(feature
);
107 return GlobalController
.getPreferredFeatures();
111 * True if <code>Feature</code> is set to "show" in preferences.
116 public static boolean getFeaturePreference(Feature feature
) {
118 String preferenceKey
= getPreferenceKey(feature
);
120 // If feature does not yet have a pref, set it to true
121 if (!PreferencesController
.getPrefStore().contains(preferenceKey
)) {
122 PreferencesController
.getPrefStore().setDefault(preferenceKey
, true);
125 return PreferencesController
.getPrefStore().getBoolean(preferenceKey
);
129 * Set the show state of a <code>Feature</code> in the
130 * <code>PreferenceStore</code>.
132 * Also sets <code>preferredFeatureSet</code> to null to force it be
133 * re-populated the next time {@link getPreferredFeatures} is called.
138 public static void setFeaturePreference(Feature feature
, boolean show
) {
139 GlobalController
.setPreferredFeatures(null);
140 PreferencesController
.getPrefStore().setValue(PreferencesController
.getPreferenceKey(feature
), show
);
143 public static IPreferenceStore
getPrefStore() {
144 return TaxEditorPlugin
.getDefault().getPreferenceStore();
148 * Construct a unique key using <code>Feature</code>'s <code>Uuid</code>
153 private static String
getPreferenceKey(Feature feature
) {
154 return ITaxEditorConstants
.FEATURE_PREFERENCE
156 . concat(feature
.getUuid().toString());
160 * Returns a <code>Set</code> of the <code>Rank</code>s that the user has chosen
161 * to have shown in preferences.
164 * <code>Rank</code>s are shown unless otherwise specified.
168 public static Set
<Rank
> getPreferredRanks() {
170 // Initialize preferredRankSet as necessary
171 if (GlobalController
.getPreferredRanks() == null) {
173 GlobalController
.setPreferredRanks(new HashSet
<Rank
>());
175 SortedSet
<Rank
> ranks
= CdmSessionDataRepository
.getDefault().getRanks();
178 for (Rank rank
: CdmSessionDataRepository
.getDefault().getRanks()) {
180 // If the feature is set to show, add it to preferredFeatureSet
181 if (PreferencesController
.getRankPreference(rank
)) {
182 GlobalController
.addPreferredRank(rank
);
185 } // else { TODO: error message
188 return GlobalController
.getPreferredRanks();
192 * True if <code>Rank</code> is set to "show" in preferences.
197 public static boolean getRankPreference(Rank rank
) {
199 String preferenceKey
= PreferencesController
.getPreferenceKey(rank
);
201 // If rank does not yet have a pref, set it to true
202 if (!getPrefStore().contains(preferenceKey
)) {
203 getPrefStore().setDefault(preferenceKey
, true);
206 return getPrefStore().getBoolean(preferenceKey
);
210 * Set the show state of a <code>Rank</code> in the
211 * <code>PreferenceStore</code>.
213 * Also sets <code>preferredRankSet</code> to null to force it be
214 * re-populated the next time {@link getPreferredRanks} is called.
219 public static void setRankPreference(Rank rank
, boolean show
) {
220 GlobalController
.setPreferredRanks(null);
221 getPrefStore().setValue(PreferencesController
.getPreferenceKey(rank
), show
);
226 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
231 private static String
getPreferenceKey(Rank rank
) {
232 return ITaxEditorConstants
.RANK_PREFERENCE
234 . concat(rank
.getUuid().toString());
238 * Returns a new name object that conforms to the nomenclatural
239 * code specified in user preferences.
243 public static NonViralName
getInstanceOfPreferredNameClass() {
244 NomenclaturalCode code
= getPreferredNomenclaturalCode();
246 // Check whether name code preference needs to be initialized
248 setDefaultNomenclaturalCode();
251 if (code
.equals(NomenclaturalCode
.ICBN
)) {
252 return BotanicalName
.NewInstance(null);
253 } else if (code
.equals(NomenclaturalCode
.ICZN
)) {
254 return ZoologicalName
.NewInstance(null);
256 return NonViralName
.NewInstance(null);
260 * Appends the code (i.e. ICBN or ICZN) specified in the user preferences
261 * to a string. The result is separated by a period: "<code>key.ICBN</code>".
266 public static String
concatCodeMessageSuffix(String key
) {
267 String code
= getNameCodePreference();
268 return key
+ "." + code
;