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
.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
= new TaxonServiceConfiguratorImpl();
280 configurator
.setDoTaxa(true);
281 configurator
.setDoSynonyms(true);
282 configurator
.setDoNamesWithoutTaxa(true);
283 configurator
.setDoTaxaByCommonNames(true);
285 configurator
.setTaxonPropertyPath(Arrays
.asList("$", "titleCache",
286 "name", "name.$", "relationsFromThisTaxon.$"));
288 configurator
.setSynonymPropertyPath(Arrays
.asList("$", "titleCache",
289 "name", "name.$", "synonymRelations.relatedTo.*"));
292 // match mode is a simple like, actually all other match modes are kind
295 .setMatchMode(eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
.LIKE
);
296 // we set page number and size here as this should always be unlimited
297 configurator
.setPageNumber(0);
298 // TODO currently limit results to 10000
299 configurator
.setPageSize(10000);
305 * Store search preferences
307 * @param configurator
309 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
312 public static void setSearchConfigurator(
313 ITaxonServiceConfigurator configurator
) {
314 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA
,
315 configurator
.isDoTaxa());
316 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
317 configurator
.isDoSynonyms());
318 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES
,
319 configurator
.isDoNamesWithoutTaxa());
320 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
,
321 configurator
.isDoTaxaByCommonNames());
326 * firePreferencesChanged
330 * a {@link java.lang.Class} object.
332 public static void firePreferencesChanged(Class clazz
) {
333 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE
,
338 * Set default values for preferences
340 public static void setDefaults() {
341 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA
, true);
342 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
,
344 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT
,
345 "http://edit.br.fgov.be/edit_wp5/v1/areas.php");
346 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP
, true);
347 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT
,
348 "http://www.biodiversitylibrary.org/openurl");
349 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH
, "1000");
350 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT
, "1000");
355 * checkNomenclaturalCode
358 public static void checkNomenclaturalCode() {
359 // First time Editor is opened, no nomenclatural code has been set
360 if (PreferencesUtil
.getPreferredNomenclaturalCode() == null) {
362 StoreUtil
.info("No nomencatural code set.");
364 Shell shell
= StoreUtil
.getShell();
366 // Query user re: preferred nom. code
367 Dialog dialog
= new InitNomenclaturalCodePrefDialog(shell
);
370 // Short message confirming user's choice
371 NomenclaturalCode code
= PreferencesUtil
372 .getPreferredNomenclaturalCode();
376 "Nomenclatural code set",
377 "The following has been set as your preferred nomenclatural code:\n\n\t"
378 + NomenclaturalCodeHelper
379 .getDescription(code
)
380 + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
386 * getMapServiceAccessPoint
389 * @return a {@link java.lang.String} object.
391 public static String
getMapServiceAccessPoint() {
392 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT
);
397 * shouldConnectAtStartUp
402 public static boolean shouldConnectAtStartUp() {
403 return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP
);
408 * getDefaultFeatureTreeForTextualDescription
411 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
413 public static FeatureTree
getDefaultFeatureTreeForTextualDescription() {
414 String uuidString
= getPreferenceStore().getString(
415 FEATURE_TREE_DEFAULT_TEXT
);
416 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
417 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
422 * getDefaultFeatureTreeForStructuredDescription
425 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
427 public static FeatureTree
getDefaultFeatureTreeForStructuredDescription() {
428 String uuidString
= getPreferenceStore().getString(
429 FEATURE_TREE_DEFAULT_STRUCTURE
);
430 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(
431 IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
436 * setSortRanksHierarchichally
442 public static void setSortRanksHierarchichally(boolean selection
) {
443 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY
, selection
);
448 * getSortRanksHierarchichally
453 public static boolean getSortRanksHierarchichally() {
454 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY
);
457 public static boolean isMultilanguageTextEditingCapability() {
458 return getPreferenceStore().getBoolean(
459 MULTILANGUAGE_TEXT_EDITING_CAPABILITY
);
462 public static Language
getGlobalLanguage() {
463 String languageUuidString
= getPreferenceStore().getString(
464 GLOBAL_LANGUAGE_UUID
);
466 if (CdmUtils
.isEmpty(languageUuidString
)) {
467 return Language
.DEFAULT();
470 UUID languageUuid
= UUID
.fromString(languageUuidString
);
471 return (Language
) CdmStore
.getService(ITermService
.class).load(
475 public static void setGlobalLanguage(Language language
) {
476 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID
,
477 language
.getUuid().toString());
478 CdmStore
.setDefaultLanguage(language
);
484 public static Map
<MarkerType
, Boolean
> getEditMarkerTypePreferences() {
485 List
<MarkerType
> markerTypes
= CdmStore
.getTermManager()
486 .getPreferredTerms(MarkerType
.class);
488 Map
<MarkerType
, Boolean
> result
= new HashMap
<MarkerType
, Boolean
>();
490 for (MarkerType markerType
: markerTypes
) {
491 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
492 Boolean value
= getPreferenceStore().getBoolean(name
);
494 result
.put(markerType
, value
);
501 * @param markerTypeEditingMap
503 public static void setEditMarkerTypePreferences(
504 Map
<MarkerType
, Boolean
> markerTypeEditingMap
) {
505 for (MarkerType markerType
: markerTypeEditingMap
.keySet()) {
506 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
507 getPreferenceStore().setValue(name
,
508 markerTypeEditingMap
.get(markerType
));
513 private static String
getMarkerTypeEditingPreferenceKey(
514 MarkerType markerType
) {
515 markerType
= (MarkerType
) HibernateProxyHelper
.deproxy(markerType
);
516 return markerType
.getClass().getName() + EDIT_MARKER_TYPE_PREFIX
;
521 * setEditMarkerTypePreference
525 * a {@link org.eclipse.ui.IEditorInput} object.
527 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
531 public static void setEditMarkerTypePreference(MarkerType markerType
,
533 getPreferenceStore().setValue(
534 getMarkerTypeEditingPreferenceKey(markerType
), edit
);
540 public static DerivedUnitFacadeConfigurator
getDerivedUnitConfigurator() {
541 DerivedUnitFacadeConfigurator configurator
= DerivedUnitFacadeConfigurator
543 configurator
.setMoveDerivedUnitMediaToGallery(true);
544 configurator
.setMoveFieldObjectMediaToGallery(true);