Refactored menu preference selection
[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.List;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.ui.editors.text.EditorsUI;
18 import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
19
20 import eu.etaxonomy.cdm.model.description.Feature;
21 import eu.etaxonomy.cdm.model.name.BotanicalName;
22 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
23 import eu.etaxonomy.cdm.model.name.NonViralName;
24 import eu.etaxonomy.cdm.model.name.Rank;
25 import eu.etaxonomy.cdm.model.name.ZoologicalName;
26 import eu.etaxonomy.taxeditor.model.Resources;
27 import eu.etaxonomy.taxeditor.store.CdmStore;
28 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
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 private static final String FEATURE_PREFERENCE = "FEATURE_PREFERENCE";
41
42
43 private static SourceViewerDecorationSupport decorationSupport;
44
45 /**
46 *
47 * @return
48 */
49 public static IPreferenceStore getPrefStore() {
50 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
51 }
52
53 /**
54 *
55 * @param preferredCode
56 */
57 public static void setNomenclaturalCode(String preferredCode) {
58 getPrefStore().setValue(Resources.CODE_PREFERENCE, preferredCode);
59 }
60
61 /**
62 *
63 */
64 public static void setDefaultNomenclaturalCode() {
65 setNomenclaturalCode(Resources.DEFAULT_CODE_PREFERENCE);
66 }
67
68 /**
69 *
70 * @return
71 */
72 public static String getPreferredNomenclaturalCodeAsString() {
73 String nameCodePreference = getNameCodePreference();
74
75 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
76 return "International Code of Botanical Nomenclature (ICBN)";
77 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
78 return "International Code of Zoological Nomenclature (ICZN)";
79 }
80 return null;
81 }
82
83 /**
84 * @return
85 */
86 public static NomenclaturalCode getPreferredNomenclaturalCode() {
87
88 String nameCodePreference = getNameCodePreference();
89
90 if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICBN)) {
91 return NomenclaturalCode.ICBN;
92 } else if (nameCodePreference.equals(Resources.CODE_PREFERENCE_ICZN)) {
93 return NomenclaturalCode.ICZN;
94 }
95 return null;
96 }
97
98 /**
99 *
100 * @return
101 */
102 private static String getNameCodePreference() {
103 try{
104 return getPrefStore().getString(Resources.CODE_PREFERENCE);
105 }catch(NullPointerException e){
106 logger.warn("PreferenceStore was not available. That is OK if you are running a unit test. Setting ICBN as default code.");
107 return Resources.CODE_PREFERENCE_ICBN;
108 }
109 }
110
111 /**
112 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
113 * to have shown in preferences.
114 * </p>
115 * <p>
116 * <code>Feature</code>s are shown unless otherwise specified.
117 * </p>
118 * @return
119 */
120 public static List<Feature> getPreferredFeatures() {
121
122 ArrayList<Feature> preferedFeatures = new ArrayList<Feature>();
123
124 for (Feature feature : CdmStore.getFeatures()) {
125 if(getPrefStore().getBoolean(getPreferenceKey(feature))){
126 preferedFeatures.add(feature);
127 }
128 }
129
130 return preferedFeatures;
131 }
132
133 /**
134 *
135 * @param preferredFeatureList
136 */
137 public static void setPreferredFeatures(List<Feature> preferredFeatureList) {
138
139 for (Feature feature : CdmStore.getFeatures()) {
140 getPrefStore().setValue(getPreferenceKey(feature), preferredFeatureList.contains(feature));
141 }
142 }
143
144 /**
145 * Construct a unique key using <code>Feature</code>'s <code>Uuid</code>
146 *
147 * @param feature
148 * @return
149 */
150 private static String getPreferenceKey(Feature feature) {
151 return FEATURE_PREFERENCE
152 . concat(".")
153 . concat(feature.getUuid().toString());
154 }
155
156 /**
157 * Returns a <code>Set</code> of the <code>Rank</code>s that the user has chosen
158 * to have shown in preferences.
159 * </p>
160 * <p>
161 * <code>Rank</code>s are shown unless otherwise specified.
162 * </p>
163 * @return
164 */
165 public static List<Rank> getPreferredRanks() {
166
167 ArrayList<Rank> preferedRanks = new ArrayList<Rank>();
168
169 for (Rank rank : CdmStore.getRanks()) {
170 if(getPrefStore().getBoolean(getPreferenceKey(rank))){
171 preferedRanks.add(rank);
172 }
173 }
174
175 return preferedRanks;
176 }
177
178 /**
179 *
180 * @param preferedRankList
181 */
182 public static void setPreferredRanks(List<Rank> preferedRankList) {
183 for (Rank rank : CdmStore.getRanks()) {
184 getPrefStore().setValue(getPreferenceKey(rank), preferedRankList.contains(rank));
185 }
186 }
187
188
189
190 /**
191 * Construct a unique key using <code>Rank</code>'s <code>Uuid</code>
192 *
193 * @param feature
194 * @return
195 */
196 private static String getPreferenceKey(Rank rank) {
197 return Resources.RANK_PREFERENCE
198 . concat(".")
199 . concat(rank.getUuid().toString());
200 }
201
202 /**
203 * Returns a new name object that conforms to the nomenclatural
204 * code specified in user preferences.
205 *
206 * @return
207 */
208 public static NonViralName<?> getInstanceOfPreferredNameClass() {
209 NomenclaturalCode code = getPreferredNomenclaturalCode();
210
211 // Check whether name code preference needs to be initialized
212 if (code == null) {
213 setDefaultNomenclaturalCode();
214 }
215
216 if (code.equals(NomenclaturalCode.ICBN)) {
217 return BotanicalName.NewInstance(null);
218 } else if (code.equals(NomenclaturalCode.ICZN)) {
219 return ZoologicalName.NewInstance(null);
220 }
221 return NonViralName.NewInstance(null);
222 }
223
224 /**
225 * Appends the code (i.e. ICBN or ICZN) specified in the user preferences
226 * to a string. The result is separated by a period: "<code>key.ICBN</code>".
227 *
228 * @param key
229 * @return
230 */
231 public static String concatCodeMessageSuffix(String key) {
232 String code = getNameCodePreference();
233 return key + "." + code;
234 }
235
236 /**
237 *
238 * @return
239 */
240 public static SourceViewerDecorationSupport getDecorationSupport() {
241 if (decorationSupport == null) {
242 decorationSupport = new SourceViewerDecorationSupport(null, null, null, EditorsUI.getSharedTextColors());
243 }
244 return decorationSupport;
245 }
246 }