fixes #780
[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 for (Rank rank : CdmStore.getDefault().getRanksInternal()) {
194
195 // If the feature is set to show, add it to preferredFeatureSet
196 if (PreferencesUtil.getRankPreference(rank)) {
197 addPreferredRank(rank);
198 }
199 }
200
201 Collections.sort(preferredRankList, new Comparator<Rank>(){
202
203 public int compare(Rank rank1, Rank rank2) {
204 return rank1.isHigher(rank2) ? 1 : 0;
205 }
206 });
207
208 }
209
210 return preferredRankList;
211 }
212
213 /**
214 * True if <code>Rank</code> is set to "show" in preferences.
215 *
216 * @param rank
217 * @return
218 */
219 public static boolean getRankPreference(Rank rank) {
220
221 String preferenceKey = PreferencesUtil.getPreferenceKey(rank);
222
223 // If rank does not yet have a pref, set it to true
224 if (!getPrefStore().contains(preferenceKey)) {
225 getPrefStore().setDefault(preferenceKey, true);
226 }
227
228 return getPrefStore().getBoolean(preferenceKey);
229 }
230
231 /**
232 * Set the show state of a <code>Rank</code> in the
233 * <code>PreferenceStore</code>.
234 * <p>
235 * Also sets <code>preferredRankSet</code> to null to force it be
236 * re-populated the next time {@link getPreferredRanks} is called.
237 *
238 * @param rank
239 * @param show
240 */
241 public static void setRankPreference(Rank rank, boolean show) {
242 setPreferredRanks(null);
243 getPrefStore().setValue(PreferencesUtil.getPreferenceKey(rank), show);
244
245 }
246
247 /**
248 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
249 *
250 * @param feature
251 * @return
252 */
253 private static String getPreferenceKey(Rank rank) {
254 return Resources.RANK_PREFERENCE
255 . concat(".")
256 . concat(rank.getUuid().toString());
257 }
258
259 /**
260 * Returns a new name object that conforms to the nomenclatural
261 * code specified in user preferences.
262 *
263 * @return
264 */
265 public static NonViralName<?> getInstanceOfPreferredNameClass() {
266 NomenclaturalCode code = getPreferredNomenclaturalCode();
267
268 // Check whether name code preference needs to be initialized
269 if (code == null) {
270 setDefaultNomenclaturalCode();
271 }
272
273 if (code.equals(NomenclaturalCode.ICBN)) {
274 return BotanicalName.NewInstance(null);
275 } else if (code.equals(NomenclaturalCode.ICZN)) {
276 return ZoologicalName.NewInstance(null);
277 }
278 return NonViralName.NewInstance(null);
279 }
280
281 /**
282 * Appends the code (i.e. ICBN or ICZN) specified in the user preferences
283 * to a string. The result is separated by a period: "<code>key.ICBN</code>".
284 *
285 * @param key
286 * @return
287 */
288 public static String concatCodeMessageSuffix(String key) {
289 String code = getNameCodePreference();
290 return key + "." + code;
291 }
292
293
294 public static void setPreferredFeatures(List<Feature> preferredFeatureList) {
295 PreferencesUtil.preferredFeatureList = preferredFeatureList;
296 }
297
298
299 public static void setPreferredRanks(List<Rank> preferredRankList) {
300 PreferencesUtil.preferredRankList = preferredRankList;
301 }
302
303 public static void addPreferredRank(Rank rank) {
304 preferredRankList.add(rank);
305 }
306
307 public static void addPreferredFeature(Feature feature) {
308 preferredFeatureList.add(feature);
309 }
310
311 public static SourceViewerDecorationSupport getDecorationSupport() {
312 if (decorationSupport == null) {
313 decorationSupport = new SourceViewerDecorationSupport(null, null, null, EditorsUI.getSharedTextColors());
314 }
315 return decorationSupport;
316 }
317 }