2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.preference
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
22 import org
.eclipse
.ui
.PlatformUI
;
24 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeConfigurator
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.config
.FindTaxaAndNamesConfiguratorImpl
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.IFindTaxaAndNamesConfigurator
;
29 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
30 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
31 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.IDefinedTerm
;
33 import eu
.etaxonomy
.cdm
.model
.common
.ISimpleTerm
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
35 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
36 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
37 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
38 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
39 import eu
.etaxonomy
.cdm
.strategy
.match
.DefaultMatchStrategy
;
40 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
41 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
42 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchMode
;
43 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
44 import eu
.etaxonomy
.taxeditor
.model
.NomenclaturalCodeHelper
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
47 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
48 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.DefaultLanguageDialog
;
52 * PreferencesUtil class.
60 public class PreferencesUtil
implements IPreferenceKeys
{
65 public static final String PREFERRED_TERMS_CHANGE
= "preferred_terms";
72 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
74 public static IPreferenceStore
getPreferenceStore() {
75 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
80 * setPreferredNomenclaturalCode
83 * @param preferredCode
84 * a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
87 public static void setPreferredNomenclaturalCode(
88 NomenclaturalCode preferredCode
) {
89 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY
,
90 getPreferenceKey(preferredCode
));
95 * getPreferredNomenclaturalCode
98 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
100 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
102 for (NomenclaturalCode code
: NomenclaturalCodeHelper
.getAllCodes()) {
103 String preferredCode
= getPreferenceStore().getString(
104 PREFERRED_NOMENCLATURAL_CODE_KEY
);
105 if (getPreferenceKey(code
).equals(preferredCode
)) {
112 public static String
getPreferredDefaultLangugae(){
113 String preferredLanguage
= getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR
);
114 if(StringUtils
.isNotEmpty(preferredLanguage
) && StringUtils
.isNotBlank(preferredLanguage
)){
115 return preferredLanguage
;
121 * Get the match strategy for the given class that was stored in preferences
122 * or the default strategy if it was not stored in preferences
125 * a {@link java.lang.Class} object.
126 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
128 public static IMatchStrategy
getMatchStrategy(Class clazz
) {
129 String className
= clazz
.getName();
130 if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX
+ className
)) {
131 IMatchStrategy matchStrategy
= getDefaultMatchStrategy(clazz
);
133 for (String fieldName
: matchStrategy
.getMatchFieldPropertyNames()) {
134 String matchModeName
= getPreferenceStore().getString(
135 getMatchStrategyFieldName(className
, fieldName
));
136 MatchMode matchMode
= MatchMode
.valueOf(matchModeName
);
138 matchStrategy
.setMatchMode(fieldName
, matchMode
);
139 } catch (MatchException e
) {
140 MessagingUtils
.error(PreferencesUtil
.class, e
);
141 throw new RuntimeException(e
);
145 return matchStrategy
;
147 return getDefaultMatchStrategy(clazz
);
151 * Stores a matchStrategy into the preference store.
153 * @param matchStrategy
154 * a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
157 public static void setMatchStrategy(IMatchStrategy matchStrategy
) {
158 String className
= matchStrategy
.getMatchClass().getName();
159 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX
+ className
, true);
161 Set
<String
> matchFields
= matchStrategy
.getMatchFieldPropertyNames();
163 for (String fieldName
: matchFields
) {
164 getPreferenceStore().setValue(
165 getMatchStrategyFieldName(className
, fieldName
),
166 matchStrategy
.getMatchMode(fieldName
).name());
171 * Helper method to create the preference property for a match field.
177 private static String
getMatchStrategyFieldName(String className
,
179 return MATCH_STRATEGY_PREFIX
+ className
+ "." + fieldName
;
183 * Returns the default match strategy for a given class.
186 * a {@link java.lang.Class} object.
187 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
189 public static IMatchStrategy
getDefaultMatchStrategy(Class clazz
) {
190 return DefaultMatchStrategy
.NewInstance(clazz
);
195 * getDateFormatPattern
198 * @return a {@link java.lang.String} object.
200 public static String
getDateFormatPattern() {
201 // TODO make this configurable in properties
202 String pattern
= "Y-M-d H:m";
208 * addTermToPreferredTerms
216 public static <T
extends TermBase
> void addTermToPreferredTerms(T term
) {
218 // VocabularyEnum vocabulary =
219 // VocabularyEnum.getVocabularyEnum(term.getClass());
221 // getPreferenceStore().setValue(getPreferenceKey(term),
222 // VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
224 // firePreferencesChanged(term.getClass());
228 * Construct a unique key using the CdmBase object's uuid
233 private static String
getPreferenceKey(ICdmBase cdmBase
) {
234 cdmBase
= (ICdmBase
) HibernateProxyHelper
.deproxy(cdmBase
);
236 String key
= cdmBase
.getClass().getName().concat(".")
237 .concat(cdmBase
.getUuid().toString());
238 if (key
.contains("javassist")) {
239 MessagingUtils
.info("proxy");
245 * Construct a unique key using the CdmBase object's uuid
250 public static String
getPreferenceKey(ISimpleTerm simpleTerm
) {
251 simpleTerm
= (ISimpleTerm
) HibernateProxyHelper
.deproxy(simpleTerm
);
252 String key
= simpleTerm
.getClass().getName().concat(".")
253 .concat(simpleTerm
.getUuid().toString());
254 if (key
.contains("javassist")) {
255 MessagingUtils
.warn(PreferencesUtil
.class,
256 "Trying to persist a preference based on a proxy class.");
264 * Construct a unique key using the CdmBase object's uuid
269 public static String
getPreferenceKey(IDefinedTerm definedTerm
) {
270 definedTerm
= (IDefinedTerm
) HibernateProxyHelper
.deproxy(definedTerm
);
271 String key
= definedTerm
.getClass().getName().concat(".")
272 .concat(definedTerm
.getUuid().toString());
273 if (key
.contains("javassist")) {
274 MessagingUtils
.warn(PreferencesUtil
.class,
275 "Trying to persist a preference based on a proxy class.");
281 * Retrieves search preferences from the preference store
283 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
285 public static IFindTaxaAndNamesConfigurator
getSearchConfigurator() {
286 IFindTaxaAndNamesConfigurator configurator
= initializeSearchConfigurator();
288 configurator
.setDoTaxa(getPreferenceStore().getBoolean(
289 TAXON_SERVICE_CONFIGURATOR_TAXA
));
290 configurator
.setDoSynonyms(getPreferenceStore().getBoolean(
291 TAXON_SERVICE_CONFIGURATOR_SYNONYMS
));
292 configurator
.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
293 TAXON_SERVICE_CONFIGURATOR_NAMES
));
294 configurator
.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
295 TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
));
301 * create new preferences, setting all search options to true
304 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
307 public static IFindTaxaAndNamesConfigurator
initializeSearchConfigurator() {
308 IFindTaxaAndNamesConfigurator configurator
= new FindTaxaAndNamesConfiguratorImpl();
310 configurator
.setDoTaxa(true);
311 configurator
.setDoSynonyms(true);
312 configurator
.setDoNamesWithoutTaxa(true);
313 configurator
.setDoTaxaByCommonNames(true);
315 configurator
.setTaxonPropertyPath(Arrays
.asList("$", "titleCache",
316 "name", "name.$", "relationsFromThisTaxon.$"));
318 configurator
.setSynonymPropertyPath(Arrays
.asList("$", "titleCache",
319 "name", "name.$", "synonymRelations.relatedTo.*"));
322 // match mode is a simple like, actually all other match modes are kind
325 .setMatchMode(eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
.ANYWHERE
);
326 // we set page number and size here as this should always be unlimited
327 configurator
.setPageNumber(0);
328 // TODO currently limit results to 10000
329 configurator
.setPageSize(10000);
335 * Store search preferences
337 * @param configurator
339 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
342 public static void setSearchConfigurator(
343 IFindTaxaAndNamesConfigurator configurator
) {
344 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA
,
345 configurator
.isDoTaxa());
346 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
347 configurator
.isDoSynonyms());
348 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES
,
349 configurator
.isDoNamesWithoutTaxa());
350 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
,
351 configurator
.isDoTaxaByCommonNames());
356 * firePreferencesChanged
360 * a {@link java.lang.Class} object.
362 public static void firePreferencesChanged(Class clazz
) {
363 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE
,
368 * Set default values for preferences
370 public static void setDefaults() {
371 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA
, true);
372 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
374 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT
,
375 "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
376 //FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
377 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP
, false);
378 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT
,
379 "http://www.biodiversitylibrary.org/openurl");
380 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH
, "1000");
381 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT
, "1000");
386 * checkNomenclaturalCode
389 public static void checkNomenclaturalCode() {
390 // First time Editor is opened, no nomenclatural code has been set
393 if (PreferencesUtil
.getPreferredNomenclaturalCode() == null) {
394 PreferencesUtil
.setPreferredNomenclaturalCode(NomenclaturalCode
.ICNAFP
);
397 StoreUtil.info("No nomencatural code set.");
399 Shell shell = StoreUtil.getShell();
401 Query user re: preferred nom. code
402 Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
405 // Short message confirming user's choice
406 NomenclaturalCode code = PreferencesUtil
407 .getPreferredNomenclaturalCode();
411 "Nomenclatural code set",
412 "The following has been set as your preferred nomenclatural code:\n\n\t"
413 + NomenclaturalCodeHelper
414 .getDescription(code)
415 + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
419 public static void checkDefaultLanguage(){
420 if(PreferencesUtil
.getPreferredDefaultLangugae() == null){
421 Shell shell
= StoreUtil
.getShell();
422 int open
= new DefaultLanguageDialog(shell
).open();
423 if(open
== 0){//FIXME:window performed ok. Find variable for it
424 PlatformUI
.getWorkbench().restart();
427 //TODO:In case of a reinstall, the config.ini will be overwritten
428 // here you create config.ini with the stored key from preferences
434 * getMapServiceAccessPoint
437 * @return a {@link java.lang.String} object.
439 public static String
getMapServiceAccessPoint() {
440 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT
);
445 * shouldConnectAtStartUp
450 public static boolean shouldConnectAtStartUp() {
451 //FIXME : force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
452 //return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
458 * getDefaultFeatureTreeForTextualDescription
461 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
463 public static FeatureTree
getDefaultFeatureTreeForTextualDescription() {
464 String uuidString
= getPreferenceStore().getString(
465 FEATURE_TREE_DEFAULT_TEXT
);
466 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
467 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
472 * getDefaultFeatureTreeForStructuredDescription
475 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
477 public static FeatureTree
getDefaultFeatureTreeForStructuredDescription() {
478 String uuidString
= getPreferenceStore().getString(
479 FEATURE_TREE_DEFAULT_STRUCTURE
);
480 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
481 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
486 * setSortRanksHierarchichally
492 public static void setSortRanksHierarchichally(boolean selection
) {
493 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY
, selection
);
498 * getSortRanksHierarchichally
503 public static boolean getSortRanksHierarchichally() {
504 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY
);
507 public static boolean isMultilanguageTextEditingCapability() {
508 return getPreferenceStore().getBoolean(
509 MULTILANGUAGE_TEXT_EDITING_CAPABILITY
);
512 public static Language
getGlobalLanguage() {
513 String languageUuidString
= getPreferenceStore().getString(
514 GLOBAL_LANGUAGE_UUID
);
516 if (CdmUtils
.isBlank(languageUuidString
)) {
517 return Language
.getDefaultLanguage();
520 UUID languageUuid
= UUID
.fromString(languageUuidString
);
521 return (Language
) CdmStore
.getService(ITermService
.class).load(
525 public static void setGlobalLanguage(Language language
) {
526 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID
,
527 language
.getUuid().toString());
528 CdmStore
.setDefaultLanguage(language
);
534 public static Map
<MarkerType
, Boolean
> getEditMarkerTypePreferences() {
535 List
<MarkerType
> markerTypes
= CdmStore
.getTermManager()
536 .getPreferredTerms(MarkerType
.class);
538 Map
<MarkerType
, Boolean
> result
= new HashMap
<MarkerType
, Boolean
>();
540 for (MarkerType markerType
: markerTypes
) {
541 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
542 Boolean value
= getPreferenceStore().getBoolean(name
);
544 result
.put(markerType
, value
);
551 * @param markerTypeEditingMap
553 public static void setEditMarkerTypePreferences(
554 Map
<MarkerType
, Boolean
> markerTypeEditingMap
) {
555 for (MarkerType markerType
: markerTypeEditingMap
.keySet()) {
556 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
557 getPreferenceStore().setValue(name
,
558 markerTypeEditingMap
.get(markerType
));
563 private static String
getMarkerTypeEditingPreferenceKey(
564 MarkerType markerType
) {
565 markerType
= (MarkerType
) HibernateProxyHelper
.deproxy(markerType
);
566 return markerType
.getClass().getName() + EDIT_MARKER_TYPE_PREFIX
;
571 * setEditMarkerTypePreference
575 * a {@link org.eclipse.ui.IEditorInput} object.
577 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
581 public static void setEditMarkerTypePreference(MarkerType markerType
,
583 getPreferenceStore().setValue(
584 getMarkerTypeEditingPreferenceKey(markerType
), edit
);
590 public static DerivedUnitFacadeConfigurator
getDerivedUnitConfigurator() {
591 DerivedUnitFacadeConfigurator configurator
= DerivedUnitFacadeConfigurator
593 configurator
.setMoveDerivedUnitMediaToGallery(true);
594 configurator
.setMoveFieldObjectMediaToGallery(true);