ref #5971: add an admin page to the prefs and add cdm pref nomenclatural code
[taxeditor.git] / eu.etaxonomy.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.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Set;
25 import java.util.StringTokenizer;
26 import java.util.UUID;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.PlatformUI;
36
37 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
38 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
39 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
40 import eu.etaxonomy.cdm.api.service.ITermService;
41 import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
42 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
43 import eu.etaxonomy.cdm.common.CdmUtils;
44 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
45 import eu.etaxonomy.cdm.model.common.ICdmBase;
46 import eu.etaxonomy.cdm.model.common.IDefinedTerm;
47 import eu.etaxonomy.cdm.model.common.ISimpleTerm;
48 import eu.etaxonomy.cdm.model.common.Language;
49 import eu.etaxonomy.cdm.model.common.MarkerType;
50 import eu.etaxonomy.cdm.model.common.TermBase;
51 import eu.etaxonomy.cdm.model.description.FeatureTree;
52 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
53 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
54 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
55 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
56 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
57 import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
58 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
59 import eu.etaxonomy.cdm.strategy.match.MatchException;
60 import eu.etaxonomy.cdm.strategy.match.MatchMode;
61 import eu.etaxonomy.taxeditor.model.AbstractUtility;
62 import eu.etaxonomy.taxeditor.model.MessagingUtils;
63 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
64 import eu.etaxonomy.taxeditor.store.CdmStore;
65 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
66 import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
67
68 /**
69 * <p>
70 * PreferencesUtil class.
71 * </p>
72 *
73 * @author p.ciardelli
74 * @author n.hoffmann
75 * @created 05.12.2008
76 * @version 1.0
77 */
78 public class PreferencesUtil implements IPreferenceKeys {
79
80 /**
81 *
82 */
83 public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
84
85 public static final String P2_REPOSITORIES_DELIM = ",";
86 public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
87
88
89
90 /**
91 * <p>
92 * getPreferenceStore
93 * </p>
94 *
95 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
96 */
97 public static IPreferenceStore getPreferenceStore() {
98 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
99 }
100
101 /**
102 * <p>
103 * setPreferredNomenclaturalCode
104 * </p>
105 *
106 * @param preferredCode
107 * a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
108 * object.
109 */
110 public static NomenclaturalCode setPreferredNomenclaturalCode(
111 NomenclaturalCode preferredCode, boolean local) {
112 if (local){
113 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
114 getPreferenceKey(preferredCode));
115 }
116 else{
117 ICdmApplicationConfiguration controller;
118 controller = CdmStore.getCurrentApplicationConfiguration();
119 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
120 CdmPreference preference = null;
121 if (controller == null){
122 return null;
123 }
124 if (preferredCode == null){
125 preference = controller.getPreferenceService().find(key);
126 if (preference == null){
127 return null;
128 } else{
129 int index = StringUtils.lastIndexOf(preference.getValue(), ".");
130 UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
131 preferredCode = NomenclaturalCode.getByUuid(uuid);
132 return preferredCode;
133 }
134 } else{
135 preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferredCode.getKey());
136 controller.getPreferenceService().set(preference);
137
138 }
139 }
140 return preferredCode;
141
142
143
144 }
145
146 public static NomenclaturalCode getPreferredNomenclaturalCode(){
147 if (getPreferenceStore().getBoolean(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
148 return getPreferredNomenclaturalCode(true);
149 } else{
150 return getPreferredNomenclaturalCode(false);
151 }
152 }
153
154 /**
155 * <p>
156 * getPreferredNomenclaturalCode
157 * </p>
158 *
159 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
160 */
161 public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
162 ICdmApplicationConfiguration controller;
163 CdmPreference pref = null;
164 if (!useLocal){
165 try{
166 controller = CdmStore.getCurrentApplicationConfiguration();
167 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
168 pref = controller.getPreferenceService().find(key);
169 }catch(Exception e){
170 e.printStackTrace();
171 }
172 }
173
174 String preferredCode;
175 if(pref == null){
176 preferredCode = getPreferenceStore().getString(
177 PREFERRED_NOMENCLATURAL_CODE_KEY);
178 }else{
179 preferredCode = pref.getValue();
180 }
181
182 for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
183 if (getPreferenceKey(code).equals(preferredCode)) {
184 return code;
185 }
186 }
187 return null;
188 }
189
190 public static String getPreferredDefaultLangugae(){
191 String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
192 if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
193 return preferredLanguage;
194 }
195 return null;
196 }
197
198 /**
199 * Get the match strategy for the given class that was stored in preferences
200 * or the default strategy if it was not stored in preferences
201 *
202 * @param clazz
203 * a {@link java.lang.Class} object.
204 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
205 */
206 public static IMatchStrategy getMatchStrategy(Class clazz) {
207 String className = clazz.getName();
208 if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
209 IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
210
211 for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
212 String matchModeName = getPreferenceStore().getString(
213 getMatchStrategyFieldName(className, fieldName));
214 MatchMode matchMode = MatchMode.valueOf(matchModeName);
215 try {
216 matchStrategy.setMatchMode(fieldName, matchMode);
217 } catch (MatchException e) {
218 MessagingUtils.error(PreferencesUtil.class, e);
219 throw new RuntimeException(e);
220 }
221 }
222
223 return matchStrategy;
224 }
225 return getDefaultMatchStrategy(clazz);
226 }
227
228 /**
229 * Stores a matchStrategy into the preference store.
230 *
231 * @param matchStrategy
232 * a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
233 * object.
234 */
235 public static void setMatchStrategy(IMatchStrategy matchStrategy) {
236 String className = matchStrategy.getMatchClass().getName();
237 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
238
239 Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
240
241 for (String fieldName : matchFields) {
242 getPreferenceStore().setValue(
243 getMatchStrategyFieldName(className, fieldName),
244 matchStrategy.getMatchMode(fieldName).name());
245 }
246 }
247
248 /**
249 * Helper method to create the preference property for a match field.
250 *
251 * @param className
252 * @param fieldName
253 * @return
254 */
255 private static String getMatchStrategyFieldName(String className,
256 String fieldName) {
257 return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
258 }
259
260 /**
261 * Returns the default match strategy for a given class.
262 *
263 * @param clazz
264 * a {@link java.lang.Class} object.
265 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
266 */
267 public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
268 return DefaultMatchStrategy.NewInstance(clazz);
269 }
270
271 /**
272 * <p>
273 * getDateFormatPattern
274 * </p>
275 *
276 * @return a {@link java.lang.String} object.
277 */
278 public static String getDateFormatPattern() {
279 // TODO make this configurable in properties
280 String pattern = "Y-M-d H:m";
281 return pattern;
282 }
283
284 /**
285 * <p>
286 * addTermToPreferredTerms
287 * </p>
288 *
289 * @param term
290 * a T object.
291 * @param <T>
292 * a T object.
293 */
294 public static <T extends TermBase> void addTermToPreferredTerms(T term) {
295
296 // VocabularyEnum vocabulary =
297 // VocabularyEnum.getVocabularyEnum(term.getClass());
298 //
299 // getPreferenceStore().setValue(getPreferenceKey(term),
300 // VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
301 //
302 // firePreferencesChanged(term.getClass());
303 }
304
305 /**
306 * Construct a unique key using the CdmBase object's uuid
307 *
308 * @param cdmBase
309 * @return
310 */
311 private static String getPreferenceKey(ICdmBase cdmBase) {
312 cdmBase = HibernateProxyHelper.deproxy(cdmBase);
313
314 String key = cdmBase.getClass().getName().concat(".")
315 .concat(cdmBase.getUuid().toString());
316 if (key.contains("javassist")) {
317 MessagingUtils.info("proxy");
318 }
319 return key;
320 }
321
322 /**
323 * Construct a unique key using the CdmBase object's uuid
324 *
325 * @param cdmBase
326 * @return
327 */
328 public static String getPreferenceKey(ISimpleTerm simpleTerm) {
329 simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
330 String key = simpleTerm.getClass().getName().concat(".")
331 .concat(simpleTerm.getUuid().toString());
332 if (key.contains("javassist")) {
333 MessagingUtils.warn(PreferencesUtil.class,
334 "Trying to persist a preference based on a proxy class.");
335 }
336 return key;
337 }
338
339
340
341 /**
342 * Construct a unique key using the CdmBase object's uuid
343 *
344 * @param cdmBase
345 * @return
346 */
347 public static String getPreferenceKey(IDefinedTerm definedTerm) {
348 definedTerm = HibernateProxyHelper.deproxy(definedTerm);
349 String key = definedTerm.getClass().getName().concat(".")
350 .concat(definedTerm.getUuid().toString());
351 if (key.contains("javassist")) {
352 MessagingUtils.warn(PreferencesUtil.class,
353 "Trying to persist a preference based on a proxy class.");
354 }
355 return key;
356 }
357
358 /**
359 * Retrieves search preferences from the preference store
360 *
361 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
362 */
363 public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
364 IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
365
366 configurator.setDoTaxa(getPreferenceStore().getBoolean(
367 TAXON_SERVICE_CONFIGURATOR_TAXA));
368 configurator.setDoSynonyms(getPreferenceStore().getBoolean(
369 TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
370 configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
371 TAXON_SERVICE_CONFIGURATOR_NAMES));
372 configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
373 TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
374 //configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
375
376 return configurator;
377 }
378
379 /**
380 * create new preferences, setting all search options to true
381 *
382 * @return a
383 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
384 * object.
385 */
386 public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
387 IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
388
389 configurator.setDoTaxa(true);
390 configurator.setDoSynonyms(true);
391 configurator.setDoNamesWithoutTaxa(true);
392 configurator.setDoTaxaByCommonNames(true);
393
394 configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
395 "name", "name.$", "relationsFromThisTaxon.$"));
396
397 configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
398 "name", "name.$", "synonymRelations.relatedTo.*"));
399
400 // DEFAULT VALUES
401 // match mode is a simple like, actually all other match modes are kind
402 // of bogus
403 configurator
404 .setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
405 // we set page number and size here as this should always be unlimited
406 configurator.setPageNumber(0);
407 // TODO currently limit results to 10000
408 configurator.setPageSize(10000);
409
410 return configurator;
411 }
412
413 /**
414 * Store search preferences
415 *
416 * @param configurator
417 * a
418 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
419 * object.
420 */
421 public static void setSearchConfigurator(
422 IFindTaxaAndNamesConfigurator configurator) {
423 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
424 configurator.isDoTaxa());
425 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
426 configurator.isDoSynonyms());
427 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
428 configurator.isDoNamesWithoutTaxa());
429 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
430 configurator.isDoTaxaByCommonNames());
431 }
432
433 /**
434 * <p>
435 * firePreferencesChanged
436 * </p>
437 *
438 * @param clazz
439 * a {@link java.lang.Class} object.
440 */
441 public static void firePreferencesChanged(Class clazz) {
442 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
443 null, clazz);
444 }
445
446 /**
447 * Set default values for preferences
448 */
449 public static void setDefaults() {
450 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
451 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
452 true);
453 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
454 "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
455 //FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
456 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
457 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
458 "http://www.biodiversitylibrary.org/openurl");
459 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
460 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
461 getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, false);
462 getPreferenceStore().setDefault(CHECKLIST_ID_IN_VOCABULARY, true);
463 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS, true);
464 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_AUTHORSHIP, true);
465 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_HYBRID, true);
466 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_LSID, true);
467 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAME_RELATIONSHIP, true);
468 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NAMECACHE, true);
469 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_CODE, true);
470 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_REFERENCE, true);
471 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_NOMENCLATURAL_STATUS, true);
472 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_PROTOLOGUE, true);
473 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_RANK, true);
474 getPreferenceStore().setDefault(SHOW_NAME_DETAILS_SECTION_TYPE_DESIGNATION, true);
475 }
476
477 /**
478 * <p>
479 * checkNomenclaturalCode
480 * </p>
481 */
482 public static void checkNomenclaturalCode() {
483 // First time Editor is opened, no nomenclatural code has been set
484
485
486 if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
487 PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP, true);
488 /*
489
490 StoreUtil.info("No nomencatural code set.");
491
492 Shell shell = StoreUtil.getShell();
493
494 Query user re: preferred nom. code
495 Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
496 dialog.open();
497
498 // Short message confirming user's choice
499 NomenclaturalCode code = PreferencesUtil
500 .getPreferredNomenclaturalCode();
501 MessageDialog
502 .openInformation(
503 shell,
504 "Nomenclatural code set",
505 "The following has been set as your preferred nomenclatural code:\n\n\t"
506 + NomenclaturalCodeHelper
507 .getDescription(code)
508 + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
509 }
510 }
511
512 public static void checkDefaultLanguage(){
513 if(PreferencesUtil.getPreferredDefaultLangugae() == null){
514 Shell shell = AbstractUtility.getShell();
515 int open = new DefaultLanguageDialog(shell).open();
516 if(open == Window.OK){
517 PlatformUI.getWorkbench().restart();
518 }
519 }else{
520 //TODO:In case of a reinstall, the config.ini will be overwritten
521 // here you create config.ini with the stored key from preferences
522 }
523 }
524
525 /**
526 * <p>
527 * getMapServiceAccessPoint
528 * </p>
529 *
530 * @return a {@link java.lang.String} object.
531 */
532 public static String getMapServiceAccessPoint() {
533 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
534 }
535
536 /**
537 * <p>
538 * shouldConnectAtStartUp
539 * </p>
540 *
541 * @return a boolean.
542 */
543 public static boolean shouldConnectAtStartUp() {
544 //FIXME : force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
545 //return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
546 return false;
547 }
548
549 /**
550 * <p>
551 * getDefaultFeatureTreeForTextualDescription
552 * </p>
553 *
554 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
555 */
556 public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
557 String uuidString = getPreferenceStore().getString(
558 FEATURE_TREE_DEFAULT_TEXT);
559 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
560 IFeatureTreeService.class).load(UUID.fromString(uuidString));
561 }
562
563 /**
564 * <p>
565 * getDefaultFeatureTreeForStructuredDescription
566 * </p>
567 *
568 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
569 */
570 public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
571 String uuidString = getPreferenceStore().getString(
572 FEATURE_TREE_DEFAULT_STRUCTURE);
573 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
574 IFeatureTreeService.class).load(UUID.fromString(uuidString));
575 }
576
577 /**
578 * <p>
579 * setSortRanksHierarchichally
580 * </p>
581 *
582 * @param selection
583 * a boolean.
584 */
585 public static void setSortRanksHierarchichally(boolean selection) {
586 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
587 }
588
589 /**
590 * <p>
591 * getSortRanksHierarchichally
592 * </p>
593 *
594 * @return a boolean.
595 */
596 public static boolean getSortRanksHierarchichally() {
597 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
598 }
599
600 public static boolean isMultilanguageTextEditingCapability() {
601 return getPreferenceStore().getBoolean(
602 MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
603 }
604
605 public static Language getGlobalLanguage() {
606
607
608 String languageUuidString = getPreferenceStore().getString(
609 GLOBAL_LANGUAGE_UUID);
610
611 if(!CdmStore.isActive()) {
612 MessagingUtils.noDataSourceWarningDialog(languageUuidString);
613 return null;
614 }
615
616 if (CdmUtils.isBlank(languageUuidString)) {
617 return Language.getDefaultLanguage();
618 }
619
620 UUID languageUuid = UUID.fromString(languageUuidString);
621 return (Language) CdmStore.getService(ITermService.class).load(
622 languageUuid);
623 }
624
625 public static void setGlobalLanguage(Language language) {
626 if(language != null) {
627 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
628 CdmStore.setDefaultLanguage(language);
629 }
630
631 }
632
633 /**
634 * @return
635 */
636 public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
637 List<MarkerType> markerTypes = CdmStore.getTermManager()
638 .getPreferredTerms(MarkerType.class);
639
640 Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
641
642 for (MarkerType markerType : markerTypes) {
643 String name = getMarkerTypeEditingPreferenceKey(markerType);
644 Boolean value = getPreferenceStore().getBoolean(name);
645
646 result.put(markerType, value);
647 }
648
649 return result;
650 }
651
652 /**
653 * @param markerTypeEditingMap
654 */
655 public static void setEditMarkerTypePreferences(
656 Map<MarkerType, Boolean> markerTypeEditingMap) {
657 for (MarkerType markerType : markerTypeEditingMap.keySet()) {
658 String name = getMarkerTypeEditingPreferenceKey(markerType);
659 getPreferenceStore().setValue(name,
660 markerTypeEditingMap.get(markerType));
661 }
662
663 }
664
665 private static String getMarkerTypeEditingPreferenceKey(
666 MarkerType markerType) {
667 markerType = HibernateProxyHelper.deproxy(markerType);
668 return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
669 }
670
671 /**
672 * <p>
673 * setEditMarkerTypePreference
674 * </p>
675 *
676 * @param input
677 * a {@link org.eclipse.ui.IEditorInput} object.
678 * @param markerType
679 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
680 * @param edit
681 * a boolean.
682 */
683 public static void setEditMarkerTypePreference(MarkerType markerType,
684 boolean edit) {
685 getPreferenceStore().setValue(
686 getMarkerTypeEditingPreferenceKey(markerType), edit);
687 }
688
689 /**
690 * @return
691 */
692 public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
693 DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
694 .NewInstance();
695 configurator.setMoveDerivedUnitMediaToGallery(true);
696 configurator.setMoveFieldObjectMediaToGallery(true);
697 return configurator;
698 }
699
700 /**
701 * This method will write language properties to the config.ini located in the configuration folder
702 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
703 *
704 * @param setLanguage 0 is for german and 1 for english.
705 * @throws IOException
706 */
707 public void writePropertyToConfigFile(int setLanguage) throws IOException {
708 File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
709 //give warning to user if the directory has no write access
710 if(file == null){
711 throw new IOException();
712 }
713 Properties properties = load(file.getAbsolutePath()+"/config.ini");
714 switch(setLanguage){
715 case 0:
716 properties.setProperty("osgi.nl", "de");
717 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
718 break;
719 case 1:
720 properties.setProperty("osgi.nl", "en");
721 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
722 break;
723 default:
724 break;
725 }
726 save(file+"/config.ini", properties);
727 }
728
729 /**
730 * This method loads a property from a given file and returns it.
731 *
732 * @param filename
733 * @return
734 * @throws IOException
735 */
736 private Properties load(String filename) throws IOException {
737 FileInputStream in = new FileInputStream(filename);
738 Properties prop = new Properties();
739 prop.load(in);
740 in.close();
741 return prop;
742 }
743
744 /**
745 * This method saves a property to the specified file.
746 *
747 * @param filename
748 * @param properties
749 * @throws IOException
750 */
751 private void save(String filename, Properties properties) throws IOException{
752 FileOutputStream fos = new FileOutputStream(filename);
753 properties.store(fos, "");
754 fos.close();
755 }
756
757 /**
758 * Saves a list of P2 Metadata Repositories as string with specified delimiters
759 *
760 * @param p2Repos
761 */
762 public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
763 StringBuilder sb = new StringBuilder();
764 for(MetadataRepositoryElement p2Repo : p2Repos) {
765 sb.append(P2_REPOSITORIES_DELIM);
766 if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
767 sb.append("-");
768 } else {
769 sb.append(p2Repo.getName());
770 }
771 sb.append(P2_REPOSITORY_FIELDS_DELIM);
772 sb.append(p2Repo.getLocation().toString());
773 sb.append(P2_REPOSITORY_FIELDS_DELIM);
774 sb.append(String.valueOf(p2Repo.isEnabled()));
775 }
776 getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
777 }
778
779
780 /**
781 * Retrieves a list of previously saved P2 repositories
782 *
783 * @return
784 */
785 public static List<MetadataRepositoryElement> getP2Repositories() {
786 List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
787 String p2ReposPref = getPreferenceStore().getString(P2_REPOSITORY_LIST);
788 if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
789 StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
790
791 while(p2ReposPrefST.hasMoreTokens()) {
792 String p2RepoStr = p2ReposPrefST.nextToken();
793 StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
794 if(p2ReposStrST.countTokens()==3) {
795 String nickname = p2ReposStrST.nextToken();
796 URI uri = null;
797 try {
798 uri = new URI(p2ReposStrST.nextToken());
799 } catch (URISyntaxException e) {
800 continue;
801 }
802 boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
803 MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
804 mre.setNickname(nickname);
805 mre.setEnabled(enabled);
806 p2Repos.add(mre);
807 }
808 }
809 }
810
811 return p2Repos;
812 }
813
814 /**
815 * enables/disables nested composite. <br>
816 *
817 * @param ctrl - Composite to be en-/disabeld
818 * @param enabled - boolean
819 */
820 public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
821 if (ctrl instanceof Composite) {
822 Composite comp = (Composite) ctrl;
823 for (Control c : comp.getChildren()) {
824 recursiveSetEnabled(c, enabled);
825 }
826 } else {
827 ctrl.setEnabled(enabled);
828 }
829 }
830
831 /**
832 * <p>
833 * getSortRanksNaturally
834 * </p>
835 *
836 * @return a boolean.
837 */
838 public static boolean getSortNodesNaturally() {
839 return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
840 }
841
842 /**
843 * <p>
844 * setSortRanksNaturally
845 * </p>
846 *
847 * @param selection
848 * a boolean.
849 */
850 public static void setSortNodesNaturally(boolean selection) {
851 getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
852 }
853
854
855 /**
856 * <p>
857 * getSortRanksNaturally
858 * </p>
859 *
860 * @return a boolean.
861 */
862 public static boolean getSortNodesStrictlyAlphabetically() {
863 return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
864 }
865
866 /**
867 * <p>
868 * setSortRanksNaturally
869 * </p>
870 *
871 * @param selection
872 * a boolean.
873 */
874 public static void setSortNodesStrictlyAlphabetically(boolean selection) {
875 getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
876 }
877
878 /**
879 * <p>
880 * setStoreNavigatorState
881 * </p>
882 *
883 * @param selection
884 * a boolean.
885 */
886 public static boolean isStoreNavigatorState() {
887 return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
888
889 }
890
891 /**
892 * <p>
893 * setStoreNavigatorState
894 * </p>
895 *
896 * @param selection
897 * a boolean.
898 */
899 public static void setStoreNavigatorState(boolean selection) {
900 getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
901
902 }
903
904 /**
905 * @return
906 */
907 public static boolean isShowUpWidgetIsDisposedMessages() {
908 return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
909 }
910 public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
911 getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
912 }
913
914 /**
915 * @return
916 */
917 public static boolean isShowIdInVocabularyInChecklistEditor() {
918 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
919 }
920 public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
921 getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
922 }
923
924 /**
925 * @return
926 */
927 public static boolean isShowRankInChecklistEditor() {
928 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
929 }
930 public static void setShowRankInChecklistEditor(boolean selection) {
931 getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
932 }
933
934 }