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