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