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
.ITaxonServiceConfigurator
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.impl
.TaxonServiceConfiguratorImpl
;
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
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
35 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
36 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
37 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
38 import eu
.etaxonomy
.cdm
.strategy
.match
.DefaultMatchStrategy
;
39 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
40 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
41 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchMode
;
42 import eu
.etaxonomy
.taxeditor
.model
.NomenclaturalCodeHelper
;
43 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
44 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
45 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
49 * PreferencesUtil class.
57 public class PreferencesUtil
implements IPreferenceKeys
{
62 public static final String PREFERRED_TERMS_CHANGE
= "preferred_terms";
69 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
71 public static IPreferenceStore
getPreferenceStore() {
72 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
77 * setPreferredNomenclaturalCode
80 * @param preferredCode
81 * a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
84 public static void setPreferredNomenclaturalCode(
85 NomenclaturalCode preferredCode
) {
86 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY
,
87 getPreferenceKey(preferredCode
));
92 * getPreferredNomenclaturalCode
95 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
97 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
99 for (NomenclaturalCode code
: NomenclaturalCodeHelper
.getAllCodes()) {
100 String preferredCode
= getPreferenceStore().getString(
101 PREFERRED_NOMENCLATURAL_CODE_KEY
);
102 if (getPreferenceKey(code
).equals(preferredCode
)) {
110 * Get the match strategy for the given class that was stored in preferences
111 * or the default strategy if it was not stored in preferences
114 * a {@link java.lang.Class} object.
115 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
117 public static IMatchStrategy
getMatchStrategy(Class clazz
) {
118 String className
= clazz
.getName();
119 if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX
+ className
)) {
120 IMatchStrategy matchStrategy
= getDefaultMatchStrategy(clazz
);
122 for (String fieldName
: matchStrategy
.getMatchFieldPropertyNames()) {
123 String matchModeName
= getPreferenceStore().getString(
124 getMatchStrategyFieldName(className
, fieldName
));
125 MatchMode matchMode
= MatchMode
.valueOf(matchModeName
);
127 matchStrategy
.setMatchMode(fieldName
, matchMode
);
128 } catch (MatchException e
) {
129 StoreUtil
.error(PreferencesUtil
.class, e
);
130 throw new RuntimeException(e
);
134 return matchStrategy
;
136 return getDefaultMatchStrategy(clazz
);
140 * Stores a matchStrategy into the preference store.
142 * @param matchStrategy
143 * a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
146 public static void setMatchStrategy(IMatchStrategy matchStrategy
) {
147 String className
= matchStrategy
.getMatchClass().getName();
148 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX
+ className
, true);
150 Set
<String
> matchFields
= matchStrategy
.getMatchFieldPropertyNames();
152 for (String fieldName
: matchFields
) {
153 getPreferenceStore().setValue(
154 getMatchStrategyFieldName(className
, fieldName
),
155 matchStrategy
.getMatchMode(fieldName
).name());
160 * Helper method to create the preference property for a match field.
166 private static String
getMatchStrategyFieldName(String className
,
168 return MATCH_STRATEGY_PREFIX
+ className
+ "." + fieldName
;
172 * Returns the default match strategy for a given class.
175 * a {@link java.lang.Class} object.
176 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
178 public static IMatchStrategy
getDefaultMatchStrategy(Class clazz
) {
179 return DefaultMatchStrategy
.NewInstance(clazz
);
184 * getDateFormatPattern
187 * @return a {@link java.lang.String} object.
189 public static String
getDateFormatPattern() {
190 // TODO make this configurable in properties
191 String pattern
= "Y-M-d H:m";
197 * addTermToPreferredTerms
205 public static <T
extends TermBase
> void addTermToPreferredTerms(T term
) {
207 // VocabularyEnum vocabulary =
208 // VocabularyEnum.getVocabularyEnum(term.getClass());
210 // getPreferenceStore().setValue(getPreferenceKey(term),
211 // VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
213 // firePreferencesChanged(term.getClass());
217 * Construct a unique key using the CdmBase object's uuid
222 private static String
getPreferenceKey(ICdmBase cdmBase
) {
223 cdmBase
= (ICdmBase
) HibernateProxyHelper
.deproxy(cdmBase
);
225 String key
= cdmBase
.getClass().getName().concat(".")
226 .concat(cdmBase
.getUuid().toString());
227 if (key
.contains("javassist")) {
228 StoreUtil
.info("proxy");
234 * Construct a unique key using the CdmBase object's uuid
239 public static String
getPreferenceKey(IDefinedTerm definedTerm
) {
240 definedTerm
= (IDefinedTerm
) HibernateProxyHelper
.deproxy(definedTerm
);
241 String key
= definedTerm
.getClass().getName().concat(".")
242 .concat(definedTerm
.getUuid().toString());
243 if (key
.contains("javassist")) {
244 StoreUtil
.warn(PreferencesUtil
.class,
245 "Trying to persist a preference based on a proxy class.");
251 * Retrieves search preferences from the preference store
253 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
255 public static ITaxonServiceConfigurator
getSearchConfigurator() {
256 ITaxonServiceConfigurator configurator
= initializeSearchConfigurator();
258 configurator
.setDoTaxa(getPreferenceStore().getBoolean(
259 TAXON_SERVICE_CONFIGURATOR_TAXA
));
260 configurator
.setDoSynonyms(getPreferenceStore().getBoolean(
261 TAXON_SERVICE_CONFIGURATOR_SYNONYMS
));
262 configurator
.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
263 TAXON_SERVICE_CONFIGURATOR_NAMES
));
264 configurator
.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
265 TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
));
271 * create new preferences, setting all search options to true
274 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
277 public static ITaxonServiceConfigurator
initializeSearchConfigurator() {
278 ITaxonServiceConfigurator configurator
= TaxonServiceConfiguratorImpl
281 configurator
.setDoTaxa(true);
282 configurator
.setDoSynonyms(true);
283 configurator
.setDoNamesWithoutTaxa(true);
284 configurator
.setDoTaxaByCommonNames(true);
286 configurator
.setTaxonPropertyPath(Arrays
.asList("$", "titleCache",
287 "name", "name.$", "relationsFromThisTaxon.$"));
289 configurator
.setSynonymPropertyPath(Arrays
.asList("$", "titleCache",
290 "name", "name.$", "synonymRelations.relatedTo.*"));
293 // match mode is a simple like, actually all other match modes are kind
296 .setMatchMode(eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
.LIKE
);
297 // we set page number and size here as this should always be unlimited
298 configurator
.setPageNumber(0);
299 // TODO currently limit results to 10000
300 configurator
.setPageSize(10000);
306 * Store search preferences
308 * @param configurator
310 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
313 public static void setSearchConfigurator(
314 ITaxonServiceConfigurator configurator
) {
315 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA
,
316 configurator
.isDoTaxa());
317 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
318 configurator
.isDoSynonyms());
319 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES
,
320 configurator
.isDoNamesWithoutTaxa());
321 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
,
322 configurator
.isDoTaxaByCommonNames());
327 * firePreferencesChanged
331 * a {@link java.lang.Class} object.
333 public static void firePreferencesChanged(Class clazz
) {
334 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE
,
339 * Set default values for preferences
341 public static void setDefaults() {
342 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA
, true);
343 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
345 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT
,
346 "http://edit.br.fgov.be/edit_wp5/v1/areas.php");
347 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP
, true);
348 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT
,
349 "http://www.biodiversitylibrary.org/openurl");
350 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH
, "1000");
351 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT
, "1000");
356 * checkNomenclaturalCode
359 public static void checkNomenclaturalCode() {
360 // First time Editor is opened, no nomenclatural code has been set
361 if (PreferencesUtil
.getPreferredNomenclaturalCode() == null) {
363 StoreUtil
.info("No nomencatural code set.");
365 Shell shell
= StoreUtil
.getShell();
367 // Query user re: preferred nom. code
368 Dialog dialog
= new InitNomenclaturalCodePrefDialog(shell
);
371 // Short message confirming user's choice
372 NomenclaturalCode code
= PreferencesUtil
373 .getPreferredNomenclaturalCode();
377 "Nomenclatural code set",
378 "The following has been set as your preferred nomenclatural code:\n\n\t"
379 + NomenclaturalCodeHelper
380 .getDescription(code
)
381 + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
387 * getMapServiceAccessPoint
390 * @return a {@link java.lang.String} object.
392 public static String
getMapServiceAccessPoint() {
393 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT
);
398 * shouldConnectAtStartUp
403 public static boolean shouldConnectAtStartUp() {
404 return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP
);
409 * getDefaultFeatureTreeForTextualDescription
412 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
414 public static FeatureTree
getDefaultFeatureTreeForTextualDescription() {
415 String uuidString
= getPreferenceStore().getString(
416 FEATURE_TREE_DEFAULT_TEXT
);
417 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
418 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
423 * getDefaultFeatureTreeForStructuredDescription
426 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
428 public static FeatureTree
getDefaultFeatureTreeForStructuredDescription() {
429 String uuidString
= getPreferenceStore().getString(
430 FEATURE_TREE_DEFAULT_STRUCTURE
);
431 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
432 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
437 * setSortRanksHierarchichally
443 public static void setSortRanksHierarchichally(boolean selection
) {
444 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY
, selection
);
449 * getSortRanksHierarchichally
454 public static boolean getSortRanksHierarchichally() {
455 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY
);
458 public static boolean isMultilanguageTextEditingCapability() {
459 return getPreferenceStore().getBoolean(
460 MULTILANGUAGE_TEXT_EDITING_CAPABILITY
);
463 public static Language
getGlobalLanguage() {
464 String languageUuidString
= getPreferenceStore().getString(
465 GLOBAL_LANGUAGE_UUID
);
467 if (CdmUtils
.isEmpty(languageUuidString
)) {
468 return Language
.DEFAULT();
471 UUID languageUuid
= UUID
.fromString(languageUuidString
);
472 return (Language
) CdmStore
.getService(ITermService
.class).load(
476 public static void setGlobalLanguage(Language language
) {
477 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID
,
478 language
.getUuid().toString());
479 CdmStore
.setDefaultLanguage(language
);
485 public static Map
<MarkerType
, Boolean
> getEditMarkerTypePreferences() {
486 List
<MarkerType
> markerTypes
= CdmStore
.getTermManager()
487 .getPreferredMarkerTypes();
489 Map
<MarkerType
, Boolean
> result
= new HashMap
<MarkerType
, Boolean
>();
491 for (MarkerType markerType
: markerTypes
) {
492 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
493 Boolean value
= getPreferenceStore().getBoolean(name
);
495 result
.put(markerType
, value
);
502 * @param markerTypeEditingMap
504 public static void setEditMarkerTypePreferences(
505 Map
<MarkerType
, Boolean
> markerTypeEditingMap
) {
506 for (MarkerType markerType
: markerTypeEditingMap
.keySet()) {
507 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
508 getPreferenceStore().setValue(name
,
509 markerTypeEditingMap
.get(markerType
));
514 private static String
getMarkerTypeEditingPreferenceKey(
515 MarkerType markerType
) {
516 markerType
= (MarkerType
) HibernateProxyHelper
.deproxy(markerType
);
517 return markerType
.getClass().getName() + EDIT_MARKER_TYPE_PREFIX
;
522 * setEditMarkerTypePreference
526 * a {@link org.eclipse.ui.IEditorInput} object.
528 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
532 public static void setEditMarkerTypePreference(MarkerType markerType
,
534 getPreferenceStore().setValue(
535 getMarkerTypeEditingPreferenceKey(markerType
), edit
);
541 public static DerivedUnitFacadeConfigurator
getDerivedUnitConfigurator() {
542 DerivedUnitFacadeConfigurator configurator
= DerivedUnitFacadeConfigurator
544 configurator
.setMoveDerivedUnitMediaToGallery(true);
545 configurator
.setMoveFieldObjectMediaToGallery(true);