refactored folder structure
[taxeditor.git] / taxeditor-store / src / main / java / 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.ArrayList;
13 import java.util.Collections;
14 import java.util.Comparator;
15 import java.util.List;
16 import java.util.SortedSet;
17
18 import org.apache.log4j.Logger;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.ui.editors.text.EditorsUI;
21 import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
22
23 import eu.etaxonomy.cdm.model.description.Feature;
24 import eu.etaxonomy.cdm.model.name.BotanicalName;
25 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
26 import eu.etaxonomy.cdm.model.name.NonViralName;
27 import eu.etaxonomy.cdm.model.name.Rank;
28 import eu.etaxonomy.cdm.model.name.ZoologicalName;
29 import eu.etaxonomy.taxeditor.model.Resources;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
32
33 /**
34 * @author p.ciardelli
35 * @created 05.12.2008
36 * @version 1.0
37 */
38 public class PreferencesUtil {
39 private static final Logger logger = Logger
40 .getLogger(PreferencesUtil.class);
41
42 private static List<Feature> preferredFeatureList;
43 private static List<Rank> preferredRankList;
44
45 private static SourceViewerDecorationSupport decorationSupport;
46
47 public static void setNomenclaturalCode(String preferredCode) {
48 getPrefStore().setValue(Resources.CODE_PREFERENCE, preferredCode);
49 }
50
51 public static void setDefaultNomenclaturalCode() {
52 setNomenclaturalCode(Resources.DEFAULT_CODE_PREFERENCE);
53 }
54
55 public static String getPreferredNomenclaturalCodeAsString() {
56 String nameCodePreference = getNameCodePreference();
57
58 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
59 return "International Code of Botanical Nomenclature (ICBN)";
60 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
61 return "International Code of Zoological Nomenclature (ICZN)";
62 }
63 return null;
64 }
65
66 /**
67 * @return
68 */
69 public static NomenclaturalCode getPreferredNomenclaturalCode() {
70
71 String nameCodePreference = getNameCodePreference();
72
73 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
74 return NomenclaturalCode.ICBN;
75 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
76 return NomenclaturalCode.ICZN;
77 }
78 return null;
79 }
80
81 private static String getNameCodePreference() {
82 try{
83 return getPrefStore().getString(Resources.CODE_PREFERENCE);
84 }catch(NullPointerException e){
85 logger.warn("PreferenceStore was not available. That is OK if you are running a unit test. Setting ICBN as default code.");
86 return Resources.CODE_PREFERENCE_ICBN;
87 }
88 }
89
90 /**
91 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
92 * to have shown in preferences.
93 * </p>
94 * <p>
95 * <code>Feature</code>s are shown unless otherwise specified.
96 * </p>
97 * @return
98 */
99 public static List<Feature> getPreferredFeatures() {
100
101 // Initialize preferredFeatureSet as necessary
102 if (preferredFeatureList == null) {
103
104 setPreferredFeatures(new ArrayList<Feature>());
105
106 for (Feature feature : CdmStore.getFeatures()) {
107
108 // If the feature is set to show, add it to preferredFeatureSet
109 if (PreferencesUtil.getFeaturePreference(feature)) {
110 addPreferredFeature(feature);
111 }
112 }
113
114 Collections.sort(preferredFeatureList, new Comparator<Feature>(){
115
116 public int compare(Feature feature1, Feature feature2) {
117 String feature1Label = feature1.getLabel(CdmStore.getDefaultLanguage());
118 String feature2Label = feature2.getLabel(CdmStore.getDefaultLanguage());
119 return feature1Label.compareToIgnoreCase(feature2Label);
120 }
121
122 });
123 }
124
125 return preferredFeatureList;
126 }
127
128 /**
129 * True if <code>Feature</code> is set to "show" in preferences.
130 *
131 * @param feature
132 * @return
133 */
134 public static boolean getFeaturePreference(Feature feature) {
135
136 String preferenceKey = getPreferenceKey(feature);
137
138 // If feature does not yet have a pref, set it to true
139 if (!PreferencesUtil.getPrefStore().contains(preferenceKey)) {
140 PreferencesUtil.getPrefStore().setDefault(preferenceKey, true);
141 }
142
143 return PreferencesUtil.getPrefStore().getBoolean(preferenceKey);
144 }
145
146 /**
147 * Set the show state of a <code>Feature</code> in the
148 * <code>PreferenceStore</code>.
149 * <p>
150 * Also sets <code>preferredFeatureSet</code> to null to force it be
151 * re-populated the next time {@link getPreferredFeatures} is called.
152 *
153 * @param feature
154 * @param show
155 */
156 public static void setFeaturePreference(Feature feature, boolean show) {
157 setPreferredFeatures(null);
158 PreferencesUtil.getPrefStore().setValue(PreferencesUtil.getPreferenceKey(feature), show);
159 }
160
161 public static IPreferenceStore getPrefStore() {
162 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
163 }
164
165 /**
166 * Construct a unique key using <code>Feature</code>'s <code>Uuid</code>
167 *
168 * @param feature
169 * @return
170 */
171 private static String getPreferenceKey(Feature feature) {
172 return Resources.FEATURE_PREFERENCE
173 . concat(".")
174 . concat(feature.getUuid().toString());
175 }
176
177 /**
178 * Returns a <code>Set</code> of the <code>Rank</code>s that the user has chosen
179 * to have shown in preferences.
180 * </p>
181 * <p>
182 * <code>Rank</code>s are shown unless otherwise specified.
183 * </p>
184 * @return
185 */
186 public static List<Rank> getPreferredRanks() {
187
188 // Initialize preferredRankSet as necessary
189 if (preferredRankList == null) {
190
191 setPreferredRanks(new ArrayList<Rank>());
192
193 SortedSet<Rank> ranks = CdmStore.getRanks();
194 if (ranks != null) {
195
196 for (Rank rank : CdmStore.getRanks()) {
197
198 // If the feature is set to show, add it to preferredFeatureSet
199 if (PreferencesUtil.getRankPreference(rank)) {
200 addPreferredRank(rank);
201 }
202 }
203 } // else { TODO: error message
204
205 Collections.sort(preferredRankList, new Comparator<Rank>(){
206
207 public int compare(Rank rank1, Rank rank2) {
208 return rank1.isHigher(rank2) ? 1 : 0;
209 }
210 });
211
212 }
213
214 return preferredRankList;
215 }
216
217 /**
218 * True if <code>Rank</code> is set to "show" in preferences.
219 *
220 * @param rank
221 * @return
222 */
223 public static boolean getRankPreference(Rank rank) {
224
225 String preferenceKey = PreferencesUtil.getPreferenceKey(rank);
226
227 // If rank does not yet have a pref, set it to true
228 if (!getPrefStore().contains(preferenceKey)) {
229 getPrefStore().setDefault(preferenceKey, true);
230 }
231
232 return getPrefStore().getBoolean(preferenceKey);
233 }
234
235 /**
236 * Set the show state of a <code>Rank</code> in the
237 * <code>PreferenceStore</code>.
238 * <p>
239 * Also sets <code>preferredRankSet</code> to null to force it be
240 * re-populated the next time {@link getPreferredRanks} is called.
241 *
242 * @param rank
243 * @param show
244 */
245 public static void setRankPreference(Rank rank, boolean show) {
246 setPreferredRanks(null);
247 getPrefStore().setValue(PreferencesUtil.getPreferenceKey(rank), show);
248
249 }
250
251 /**
252 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
253 *
254 * @param feature
255 * @return
256 */
257 private static String getPreferenceKey(Rank rank) {
258 return Resources.RANK_PREFERENCE
259 . concat(".")
260 . concat(rank.getUuid().toString());
261 }
262
263 /**
264 * Returns a new name object that conforms to the nomenclatural
265 * code specified in user preferences.
266 *
267 * @return
268 */
269 public static NonViralName<?> getInstanceOfPreferredNameClass() {
270 NomenclaturalCode code = getPreferredNomenclaturalCode();
271
272 // Check whether name code preference needs to be initialized
273 if (code == null) {
274 setDefaultNomenclaturalCode();
275 }
276
277 if (code.equals(NomenclaturalCode.ICBN)) {
278 return BotanicalName.NewInstance(null);
279 } else if (code.equals(NomenclaturalCode.ICZN)) {
280 return ZoologicalName.NewInstance(null);
281 }
282 return NonViralName.NewInstance(null);
283 }
284
285 /**
286 * Appends the code (i.e. ICBN or ICZN) specified in the user preferences
287 * to a string. The result is separated by a period: "<code>key.ICBN</code>".
288 *
289 * @param key
290 * @return
291 */
292 public static String concatCodeMessageSuffix(String key) {
293 String code = getNameCodePreference();
294 return key + "." + code;
295 }
296
297
298 public static void setPreferredFeatures(List<Feature> preferredFeatureList) {
299 PreferencesUtil.preferredFeatureList = preferredFeatureList;
300 }
301
302
303 public static void setPreferredRanks(List<Rank> preferredRankList) {
304 PreferencesUtil.preferredRankList = preferredRankList;
305 }
306
307 public static void addPreferredRank(Rank rank) {
308 preferredRankList.add(rank);
309 }
310
311 public static void addPreferredFeature(Feature feature) {
312 preferredFeatureList.add(feature);
313 }
314
315 public static SourceViewerDecorationSupport getDecorationSupport() {
316 if (decorationSupport == null) {
317 decorationSupport = new SourceViewerDecorationSupport(null, null, null, EditorsUI.getSharedTextColors());
318 }
319 return decorationSupport;
320 }
321 }