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