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
.eclipse
.jface
.dialogs
.Dialog
;
20 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
21 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
22 import org
.eclipse
.swt
.widgets
.Shell
;
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
.NomenclaturalCodeHelper
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
46 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
50 * PreferencesUtil class.
58 public class PreferencesUtil
implements IPreferenceKeys
{
63 public static final String PREFERRED_TERMS_CHANGE
= "preferred_terms";
70 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
72 public static IPreferenceStore
getPreferenceStore() {
73 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
78 * setPreferredNomenclaturalCode
81 * @param preferredCode
82 * a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
85 public static void setPreferredNomenclaturalCode(
86 NomenclaturalCode preferredCode
) {
87 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY
,
88 getPreferenceKey(preferredCode
));
93 * getPreferredNomenclaturalCode
96 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
98 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
100 for (NomenclaturalCode code
: NomenclaturalCodeHelper
.getAllCodes()) {
101 String preferredCode
= getPreferenceStore().getString(
102 PREFERRED_NOMENCLATURAL_CODE_KEY
);
103 if (getPreferenceKey(code
).equals(preferredCode
)) {
111 * Get the match strategy for the given class that was stored in preferences
112 * or the default strategy if it was not stored in preferences
115 * a {@link java.lang.Class} object.
116 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
118 public static IMatchStrategy
getMatchStrategy(Class clazz
) {
119 String className
= clazz
.getName();
120 if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX
+ className
)) {
121 IMatchStrategy matchStrategy
= getDefaultMatchStrategy(clazz
);
123 for (String fieldName
: matchStrategy
.getMatchFieldPropertyNames()) {
124 String matchModeName
= getPreferenceStore().getString(
125 getMatchStrategyFieldName(className
, fieldName
));
126 MatchMode matchMode
= MatchMode
.valueOf(matchModeName
);
128 matchStrategy
.setMatchMode(fieldName
, matchMode
);
129 } catch (MatchException e
) {
130 StoreUtil
.error(PreferencesUtil
.class, e
);
131 throw new RuntimeException(e
);
135 return matchStrategy
;
137 return getDefaultMatchStrategy(clazz
);
141 * Stores a matchStrategy into the preference store.
143 * @param matchStrategy
144 * a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
147 public static void setMatchStrategy(IMatchStrategy matchStrategy
) {
148 String className
= matchStrategy
.getMatchClass().getName();
149 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX
+ className
, true);
151 Set
<String
> matchFields
= matchStrategy
.getMatchFieldPropertyNames();
153 for (String fieldName
: matchFields
) {
154 getPreferenceStore().setValue(
155 getMatchStrategyFieldName(className
, fieldName
),
156 matchStrategy
.getMatchMode(fieldName
).name());
161 * Helper method to create the preference property for a match field.
167 private static String
getMatchStrategyFieldName(String className
,
169 return MATCH_STRATEGY_PREFIX
+ className
+ "." + fieldName
;
173 * Returns the default match strategy for a given class.
176 * a {@link java.lang.Class} object.
177 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
179 public static IMatchStrategy
getDefaultMatchStrategy(Class clazz
) {
180 return DefaultMatchStrategy
.NewInstance(clazz
);
185 * getDateFormatPattern
188 * @return a {@link java.lang.String} object.
190 public static String
getDateFormatPattern() {
191 // TODO make this configurable in properties
192 String pattern
= "Y-M-d H:m";
198 * addTermToPreferredTerms
206 public static <T
extends TermBase
> void addTermToPreferredTerms(T term
) {
208 // VocabularyEnum vocabulary =
209 // VocabularyEnum.getVocabularyEnum(term.getClass());
211 // getPreferenceStore().setValue(getPreferenceKey(term),
212 // VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
214 // firePreferencesChanged(term.getClass());
218 * Construct a unique key using the CdmBase object's uuid
223 private static String
getPreferenceKey(ICdmBase cdmBase
) {
224 cdmBase
= (ICdmBase
) HibernateProxyHelper
.deproxy(cdmBase
);
226 String key
= cdmBase
.getClass().getName().concat(".")
227 .concat(cdmBase
.getUuid().toString());
228 if (key
.contains("javassist")) {
229 StoreUtil
.info("proxy");
235 * Construct a unique key using the CdmBase object's uuid
240 public static String
getPreferenceKey(ISimpleTerm simpleTerm
) {
241 simpleTerm
= (ISimpleTerm
) HibernateProxyHelper
.deproxy(simpleTerm
);
242 String key
= simpleTerm
.getClass().getName().concat(".")
243 .concat(simpleTerm
.getUuid().toString());
244 if (key
.contains("javassist")) {
245 StoreUtil
.warn(PreferencesUtil
.class,
246 "Trying to persist a preference based on a proxy class.");
254 * Construct a unique key using the CdmBase object's uuid
259 public static String
getPreferenceKey(IDefinedTerm definedTerm
) {
260 definedTerm
= (IDefinedTerm
) HibernateProxyHelper
.deproxy(definedTerm
);
261 String key
= definedTerm
.getClass().getName().concat(".")
262 .concat(definedTerm
.getUuid().toString());
263 if (key
.contains("javassist")) {
264 StoreUtil
.warn(PreferencesUtil
.class,
265 "Trying to persist a preference based on a proxy class.");
271 * Retrieves search preferences from the preference store
273 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
275 public static IFindTaxaAndNamesConfigurator
getSearchConfigurator() {
276 IFindTaxaAndNamesConfigurator configurator
= initializeSearchConfigurator();
278 configurator
.setDoTaxa(getPreferenceStore().getBoolean(
279 TAXON_SERVICE_CONFIGURATOR_TAXA
));
280 configurator
.setDoSynonyms(getPreferenceStore().getBoolean(
281 TAXON_SERVICE_CONFIGURATOR_SYNONYMS
));
282 configurator
.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
283 TAXON_SERVICE_CONFIGURATOR_NAMES
));
284 configurator
.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
285 TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
));
291 * create new preferences, setting all search options to true
294 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
297 public static IFindTaxaAndNamesConfigurator
initializeSearchConfigurator() {
298 IFindTaxaAndNamesConfigurator configurator
= new FindTaxaAndNamesConfiguratorImpl();
300 configurator
.setDoTaxa(true);
301 configurator
.setDoSynonyms(true);
302 configurator
.setDoNamesWithoutTaxa(true);
303 configurator
.setDoTaxaByCommonNames(true);
305 configurator
.setTaxonPropertyPath(Arrays
.asList("$", "titleCache",
306 "name", "name.$", "relationsFromThisTaxon.$"));
308 configurator
.setSynonymPropertyPath(Arrays
.asList("$", "titleCache",
309 "name", "name.$", "synonymRelations.relatedTo.*"));
312 // match mode is a simple like, actually all other match modes are kind
315 .setMatchMode(eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
.ANYWHERE
);
316 // we set page number and size here as this should always be unlimited
317 configurator
.setPageNumber(0);
318 // TODO currently limit results to 10000
319 configurator
.setPageSize(10000);
325 * Store search preferences
327 * @param configurator
329 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
332 public static void setSearchConfigurator(
333 IFindTaxaAndNamesConfigurator configurator
) {
334 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA
,
335 configurator
.isDoTaxa());
336 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
337 configurator
.isDoSynonyms());
338 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES
,
339 configurator
.isDoNamesWithoutTaxa());
340 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
,
341 configurator
.isDoTaxaByCommonNames());
346 * firePreferencesChanged
350 * a {@link java.lang.Class} object.
352 public static void firePreferencesChanged(Class clazz
) {
353 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE
,
358 * Set default values for preferences
360 public static void setDefaults() {
361 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA
, true);
362 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
364 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT
,
365 "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
366 //FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
367 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP
, false);
368 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT
,
369 "http://www.biodiversitylibrary.org/openurl");
370 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH
, "1000");
371 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT
, "1000");
376 * checkNomenclaturalCode
379 public static void checkNomenclaturalCode() {
380 // First time Editor is opened, no nomenclatural code has been set
381 if (PreferencesUtil
.getPreferredNomenclaturalCode() == null) {
383 StoreUtil
.info("No nomencatural code set.");
385 Shell shell
= StoreUtil
.getShell();
387 // Query user re: preferred nom. code
388 Dialog dialog
= new InitNomenclaturalCodePrefDialog(shell
);
391 // Short message confirming user's choice
392 NomenclaturalCode code
= PreferencesUtil
393 .getPreferredNomenclaturalCode();
397 "Nomenclatural code set",
398 "The following has been set as your preferred nomenclatural code:\n\n\t"
399 + NomenclaturalCodeHelper
400 .getDescription(code
)
401 + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
407 * getMapServiceAccessPoint
410 * @return a {@link java.lang.String} object.
412 public static String
getMapServiceAccessPoint() {
413 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT
);
418 * shouldConnectAtStartUp
423 public static boolean shouldConnectAtStartUp() {
424 return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP
);
429 * getDefaultFeatureTreeForTextualDescription
432 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
434 public static FeatureTree
getDefaultFeatureTreeForTextualDescription() {
435 String uuidString
= getPreferenceStore().getString(
436 FEATURE_TREE_DEFAULT_TEXT
);
437 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
438 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
443 * getDefaultFeatureTreeForStructuredDescription
446 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
448 public static FeatureTree
getDefaultFeatureTreeForStructuredDescription() {
449 String uuidString
= getPreferenceStore().getString(
450 FEATURE_TREE_DEFAULT_STRUCTURE
);
451 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
452 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
457 * setSortRanksHierarchichally
463 public static void setSortRanksHierarchichally(boolean selection
) {
464 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY
, selection
);
469 * getSortRanksHierarchichally
474 public static boolean getSortRanksHierarchichally() {
475 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY
);
478 public static boolean isMultilanguageTextEditingCapability() {
479 return getPreferenceStore().getBoolean(
480 MULTILANGUAGE_TEXT_EDITING_CAPABILITY
);
483 public static Language
getGlobalLanguage() {
484 String languageUuidString
= getPreferenceStore().getString(
485 GLOBAL_LANGUAGE_UUID
);
487 if (CdmUtils
.isEmpty(languageUuidString
)) {
488 return Language
.DEFAULT();
491 UUID languageUuid
= UUID
.fromString(languageUuidString
);
492 return (Language
) CdmStore
.getService(ITermService
.class).load(
496 public static void setGlobalLanguage(Language language
) {
497 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID
,
498 language
.getUuid().toString());
499 CdmStore
.setDefaultLanguage(language
);
505 public static Map
<MarkerType
, Boolean
> getEditMarkerTypePreferences() {
506 List
<MarkerType
> markerTypes
= CdmStore
.getTermManager()
507 .getPreferredTerms(MarkerType
.class);
509 Map
<MarkerType
, Boolean
> result
= new HashMap
<MarkerType
, Boolean
>();
511 for (MarkerType markerType
: markerTypes
) {
512 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
513 Boolean value
= getPreferenceStore().getBoolean(name
);
515 result
.put(markerType
, value
);
522 * @param markerTypeEditingMap
524 public static void setEditMarkerTypePreferences(
525 Map
<MarkerType
, Boolean
> markerTypeEditingMap
) {
526 for (MarkerType markerType
: markerTypeEditingMap
.keySet()) {
527 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
528 getPreferenceStore().setValue(name
,
529 markerTypeEditingMap
.get(markerType
));
534 private static String
getMarkerTypeEditingPreferenceKey(
535 MarkerType markerType
) {
536 markerType
= (MarkerType
) HibernateProxyHelper
.deproxy(markerType
);
537 return markerType
.getClass().getName() + EDIT_MARKER_TYPE_PREFIX
;
542 * setEditMarkerTypePreference
546 * a {@link org.eclipse.ui.IEditorInput} object.
548 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
552 public static void setEditMarkerTypePreference(MarkerType markerType
,
554 getPreferenceStore().setValue(
555 getMarkerTypeEditingPreferenceKey(markerType
), edit
);
561 public static DerivedUnitFacadeConfigurator
getDerivedUnitConfigurator() {
562 DerivedUnitFacadeConfigurator configurator
= DerivedUnitFacadeConfigurator
564 configurator
.setMoveDerivedUnitMediaToGallery(true);
565 configurator
.setMoveFieldObjectMediaToGallery(true);