3a6191518c54e2b76266056bd5766ccd2e41cf6e
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.preference;
11
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.SortedSet;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.jface.preference.IPreferenceStore;
18
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.UiUtil;
28 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
29
30 /**
31 * @author p.ciardelli
32 * @created 05.12.2008
33 * @version 1.0
34 */
35 public class PreferencesUtil {
36 private static final Logger logger = Logger
37 .getLogger(PreferencesUtil.class);
38
39
40 public static void setNomenclaturalCode(String preferredCode) {
41 getPrefStore().setValue(ITaxEditorConstants.CODE_PREFERENCE, preferredCode);
42 }
43
44 public static void setDefaultNomenclaturalCode() {
45 setNomenclaturalCode(ITaxEditorConstants.DEFAULT_CODE_PREFERENCE);
46 }
47
48 public static String getPreferredNomenclaturalCodeAsString() {
49 String nameCodePreference = getNameCodePreference();
50
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)";
55 }
56 return null;
57 }
58
59 /**
60 * @return
61 */
62 public static NomenclaturalCode getPreferredNomenclaturalCode() {
63
64 String nameCodePreference = getNameCodePreference();
65
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();
70 }
71 return null;
72 }
73
74 private static String getNameCodePreference() {
75 return getPrefStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
76 }
77
78 /**
79 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
80 * to have shown in preferences.
81 * </p>
82 * <p>
83 * <code>Feature</code>s are shown unless otherwise specified.
84 * </p>
85 * @return
86 */
87 public static Set<Feature> getPreferredFeatures() {
88
89 // Initialize preferredFeatureSet as necessary
90 if (UiUtil.preferredFeatureSet == null) {
91
92 UiUtil.preferredFeatureSet = new HashSet<Feature>();
93
94 for (Feature feature : CdmSessionDataRepository.getDefault().getFeatures()) {
95
96 // If the feature is set to show, add it to preferredFeatureSet
97 if (PreferencesUtil.getFeaturePreference(feature)) {
98 UiUtil.preferredFeatureSet.add(feature);
99 }
100 }
101
102 }
103 return UiUtil.preferredFeatureSet;
104 }
105
106 /**
107 * True if <code>Feature</code> is set to "show" in preferences.
108 *
109 * @param feature
110 * @return
111 */
112 public static boolean getFeaturePreference(Feature feature) {
113
114 String preferenceKey = getPreferenceKey(feature);
115
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);
119 }
120
121 return PreferencesUtil.getPrefStore().getBoolean(preferenceKey);
122 }
123
124 /**
125 * Set the show state of a <code>Feature</code> in the
126 * <code>PreferenceStore</code>.
127 * <p>
128 * Also sets <code>preferredFeatureSet</code> to null to force it be
129 * re-populated the next time {@link getPreferredFeatures} is called.
130 *
131 * @param feature
132 * @param show
133 */
134 public static void setFeaturePreference(Feature feature, boolean show) {
135 UiUtil.preferredFeatureSet = null;
136 PreferencesUtil.getPrefStore().setValue(PreferencesUtil.getPreferenceKey(feature), show);
137 }
138
139 public static IPreferenceStore getPrefStore() {
140 return TaxEditorPlugin.getDefault().getPreferenceStore();
141 }
142
143 /**
144 * Construct a unique key using <code>Feature</code>'s <code>Uuid</code>
145 *
146 * @param feature
147 * @return
148 */
149 private static String getPreferenceKey(Feature feature) {
150 return ITaxEditorConstants.FEATURE_PREFERENCE
151 . concat(".")
152 . concat(feature.getUuid().toString());
153 }
154
155 /**
156 * Returns a <code>Set</code> of the <code>Rank</code>s that the user has chosen
157 * to have shown in preferences.
158 * </p>
159 * <p>
160 * <code>Rank</code>s are shown unless otherwise specified.
161 * </p>
162 * @return
163 */
164 public static Set<Rank> getPreferredRanks() {
165
166 // Initialize preferredFeatureSet as necessary
167 if (UiUtil.preferredRankSet == null) {
168
169 UiUtil.preferredRankSet = new HashSet<Rank>();
170
171 SortedSet<Rank> ranks = CdmSessionDataRepository.getDefault().getRanks();
172 if (ranks != null) {
173
174 for (Rank rank : CdmSessionDataRepository.getDefault().getRanks()) {
175
176 // If the feature is set to show, add it to preferredFeatureSet
177 if (PreferencesUtil.getRankPreference(rank)) {
178 UiUtil.preferredRankSet.add(rank);
179 }
180 }
181 } // else { TODO: error message
182
183 }
184 return UiUtil.preferredRankSet;
185 }
186
187 /**
188 * True if <code>Rank</code> is set to "show" in preferences.
189 *
190 * @param rank
191 * @return
192 */
193 public static boolean getRankPreference(Rank rank) {
194
195 String preferenceKey = PreferencesUtil.getPreferenceKey(rank);
196
197 // If rank does not yet have a pref, set it to true
198 if (!getPrefStore().contains(preferenceKey)) {
199 getPrefStore().setDefault(preferenceKey, true);
200 }
201
202 return getPrefStore().getBoolean(preferenceKey);
203 }
204
205 /**
206 * Set the show state of a <code>Rank</code> in the
207 * <code>PreferenceStore</code>.
208 * <p>
209 * Also sets <code>preferredRankSet</code> to null to force it be
210 * re-populated the next time {@link getPreferredRanks} is called.
211 *
212 * @param rank
213 * @param show
214 */
215 public static void setRankPreference(Rank rank, boolean show) {
216 UiUtil.preferredRankSet = null;
217 getPrefStore().setValue(PreferencesUtil.getPreferenceKey(rank), show);
218
219 }
220
221 /**
222 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
223 *
224 * @param feature
225 * @return
226 */
227 private static String getPreferenceKey(Rank rank) {
228 return ITaxEditorConstants.RANK_PREFERENCE
229 . concat(".")
230 . concat(rank.getUuid().toString());
231 }
232
233 /**
234 * Returns a new name object that conforms to the nomenclatural
235 * code specified in user preferences.
236 *
237 * @return
238 */
239 public static NonViralName getInstanceOfPreferredNameClass() {
240 NomenclaturalCode code = getPreferredNomenclaturalCode();
241
242 // Check whether name code preference needs to be initialized
243 if (code == null) {
244 setDefaultNomenclaturalCode();
245 }
246
247 if (code.equals(NomenclaturalCode.ICBN())) {
248 return BotanicalName.NewInstance(null);
249 } else if (code.equals(NomenclaturalCode.ICZN())) {
250 return ZoologicalName.NewInstance(null);
251 }
252 return NonViralName.NewInstance(null);
253 }
254
255 /**
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>".
258 *
259 * @param key
260 * @return
261 */
262 public static String concatCodeMessageSuffix(String key) {
263 String code = getNameCodePreference();
264 return key + "." + code;
265 }
266 }