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
;
48 * <p>PreferencesUtil class.</p>
55 public class PreferencesUtil
implements IPreferenceKeys
{
60 public static final String PREFERRED_TERMS_CHANGE
= "preferred_terms";
65 * <p>getPreferenceStore</p>
67 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
69 public static IPreferenceStore
getPreferenceStore() {
70 return TaxeditorStorePlugin
.getDefault().getPreferenceStore();
74 * <p>setPreferredNomenclaturalCode</p>
76 * @param preferredCode a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
78 public static void setPreferredNomenclaturalCode(NomenclaturalCode preferredCode
) {
79 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY
, getPreferenceKey(preferredCode
));
83 * <p>getPreferredNomenclaturalCode</p>
85 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
87 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
89 for (NomenclaturalCode code
: NomenclaturalCodeHelper
.getAllCodes()) {
90 String preferredCode
= getPreferenceStore().getString(PREFERRED_NOMENCLATURAL_CODE_KEY
);
91 if (getPreferenceKey(code
).equals(preferredCode
)) {
99 * Get the match strategy for the given class that was stored in preferences
100 * or the default strategy if it was not stored in preferences
102 * @param clazz a {@link java.lang.Class} object.
103 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
105 public static IMatchStrategy
getMatchStrategy(Class clazz
){
106 String className
= clazz
.getName();
107 if(getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX
+ className
)){
108 IMatchStrategy matchStrategy
= getDefaultMatchStrategy(clazz
);
110 for(String fieldName
: matchStrategy
.getMatchFieldPropertyNames()){
111 String matchModeName
= getPreferenceStore().getString(getMatchStrategyFieldName(className
, fieldName
));
112 MatchMode matchMode
= MatchMode
.valueOf(matchModeName
);
114 matchStrategy
.setMatchMode(fieldName
, matchMode
);
115 } catch (MatchException e
) {
116 StoreUtil
.error(PreferencesUtil
.class, e
);
117 throw new RuntimeException(e
);
121 return matchStrategy
;
123 return getDefaultMatchStrategy(clazz
);
127 * Stores a matchStrategy into the preference store.
129 * @param matchStrategy a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
131 public static void setMatchStrategy(IMatchStrategy matchStrategy
){
132 String className
= matchStrategy
.getMatchClass().getName();
133 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX
+ className
, true);
135 Set
<String
> matchFields
= matchStrategy
.getMatchFieldPropertyNames();
137 for(String fieldName
: matchFields
){
138 getPreferenceStore().setValue(getMatchStrategyFieldName(className
, fieldName
),
139 matchStrategy
.getMatchMode(fieldName
).name());
144 * Helper method to create the preference property for a match field.
150 private static String
getMatchStrategyFieldName(String className
, String fieldName
){
151 return MATCH_STRATEGY_PREFIX
+ className
+ "." + fieldName
;
155 * Returns the default match strategy for a given class.
157 * @param clazz a {@link java.lang.Class} object.
158 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
160 public static IMatchStrategy
getDefaultMatchStrategy(Class clazz
){
161 return DefaultMatchStrategy
.NewInstance(clazz
);
166 * <p>getDateFormatPattern</p>
168 * @return a {@link java.lang.String} object.
170 public static String
getDateFormatPattern(){
171 // TODO make this configurable in properties
172 String pattern
= "Y-M-d H:m";
177 * <p>addTermToPreferredTerms</p>
179 * @param term a T object.
180 * @param <T> a T object.
182 public static <T
extends TermBase
> void addTermToPreferredTerms(T term
){
184 // VocabularyEnum vocabulary = VocabularyEnum.getVocabularyEnum(term.getClass());
186 // getPreferenceStore().setValue(getPreferenceKey(term), VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
188 // firePreferencesChanged(term.getClass());
192 * Construct a unique key using the CdmBase object's uuid
197 private static String
getPreferenceKey(ICdmBase cdmBase
) {
198 cdmBase
= (ICdmBase
) HibernateProxyHelper
.deproxy(cdmBase
);
200 String key
= cdmBase
.getClass().getName()
202 . concat(cdmBase
.getUuid().toString());
203 if (key
.contains("javassist")) {
204 StoreUtil
.info("proxy");
210 * Construct a unique key using the CdmBase object's uuid
215 private static String
getPreferenceKey(IDefinedTerm definedTerm
) {
216 definedTerm
= (IDefinedTerm
) HibernateProxyHelper
.deproxy(definedTerm
);
217 String key
= definedTerm
.getClass().getName()
219 . concat(definedTerm
.getUuid().toString());
220 if (key
.contains("javassist")) {
221 StoreUtil
.warn(PreferencesUtil
.class, "Trying to persist a preference based on a proxy class.");
227 * Retrieves search preferences from the preference store
229 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
231 public static ITaxonServiceConfigurator
getSearchConfigurator() {
232 ITaxonServiceConfigurator configurator
= initializeSearchConfigurator();
234 configurator
.setDoTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_TAXA
));
235 configurator
.setDoSynonyms(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
));
236 configurator
.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_NAMES
));
237 configurator
.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
));
243 * create new preferences, setting all search options to true
245 * @return a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
247 public static ITaxonServiceConfigurator
initializeSearchConfigurator(){
248 ITaxonServiceConfigurator configurator
= TaxonServiceConfiguratorImpl
.NewInstance();
250 configurator
.setDoTaxa(true);
251 configurator
.setDoSynonyms(true);
252 configurator
.setDoNamesWithoutTaxa(true);
253 configurator
.setDoTaxaByCommonNames(true);
255 configurator
.setTaxonPropertyPath(Arrays
.asList("$",
256 "titleCache", "name", "name.$", "relationsFromThisTaxon.$"));
258 configurator
.setSynonymPropertyPath(Arrays
.asList("$",
259 "titleCache", "name", "name.$", "synonymRelations.relatedTo.*"));
262 // match mode is a simple like, actually all other match modes are kind of bogus
263 configurator
.setMatchMode(eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
.LIKE
);
264 // we set page number and size here as this should always be unlimited
265 configurator
.setPageNumber(0);
266 // TODO currently limit results to 10000
267 configurator
.setPageSize(10000);
273 * Store search preferences
275 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
277 public static void setSearchConfigurator(ITaxonServiceConfigurator configurator
){
278 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA
, configurator
.isDoTaxa());
279 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
, configurator
.isDoSynonyms());
280 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES
, configurator
.isDoNamesWithoutTaxa());
281 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
, configurator
.isDoTaxaByCommonNames());
285 * <p>firePreferencesChanged</p>
287 * @param clazz a {@link java.lang.Class} object.
289 public static void firePreferencesChanged(Class clazz
) {
290 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE
, null, clazz
);
294 * Set default values for preferences
296 public static void setDefaults() {
297 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA
, true);
298 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS
, true);
299 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT
, "http://edit.br.fgov.be/edit_wp5/v1/areas.php");
300 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP
, true);
304 * <p>checkNomenclaturalCode</p>
306 public static void checkNomenclaturalCode() {
307 // First time Editor is opened, no nomenclatural code has been set
308 if (PreferencesUtil
.getPreferredNomenclaturalCode() == null) {
310 StoreUtil
.info("No nomencatural code set.");
312 Shell shell
= StoreUtil
.getShell();
314 // Query user re: preferred nom. code
315 Dialog dialog
= new InitNomenclaturalCodePrefDialog(shell
);
318 // Short message confirming user's choice
319 NomenclaturalCode code
= PreferencesUtil
.getPreferredNomenclaturalCode();
320 MessageDialog
.openInformation(shell
, "Nomenclatural code set",
321 "The following has been set as your preferred nomenclatural code:\n\n\t" +
322 NomenclaturalCodeHelper
.getDescription(code
) + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
327 * <p>getMapServiceAccessPoint</p>
329 * @return a {@link java.lang.String} object.
331 public static String
getMapServiceAccessPoint() {
332 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT
);
336 * <p>shouldConnectAtStartUp</p>
340 public static boolean shouldConnectAtStartUp() {
341 return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP
);
345 * <p>getDefaultFeatureTreeForTextualDescription</p>
347 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
349 public static FeatureTree
getDefaultFeatureTreeForTextualDescription() {
350 String uuidString
= getPreferenceStore().getString(FEATURE_TREE_DEFAULT_TEXT
);
351 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
355 * <p>getDefaultFeatureTreeForStructuredDescription</p>
357 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
359 public static FeatureTree
getDefaultFeatureTreeForStructuredDescription() {
360 String uuidString
= getPreferenceStore().getString(FEATURE_TREE_DEFAULT_STRUCTURE
);
361 return CdmUtils
.isEmpty(uuidString
) ?
null : CdmStore
.getService(IFeatureTreeService
.class).load(UUID
.fromString(uuidString
));
365 * <p>setSortRanksHierarchichally</p>
367 * @param selection a boolean.
369 public static void setSortRanksHierarchichally(boolean selection
) {
370 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY
, selection
);
374 * <p>getSortRanksHierarchichally</p>
378 public static boolean getSortRanksHierarchichally(){
379 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY
);
382 public static boolean isMultilanguageTextEditingCapability() {
383 return getPreferenceStore().getBoolean(MULTILANGUAGE_TEXT_EDITING_CAPABILITY
);
386 public static Language
getGlobalLanguage(){
387 String languageUuidString
= getPreferenceStore().getString(GLOBAL_LANGUAGE_UUID
);
389 if(CdmUtils
.isEmpty(languageUuidString
)){
390 return Language
.DEFAULT();
393 UUID languageUuid
= UUID
.fromString(languageUuidString
);
394 return (Language
) CdmStore
.getService(ITermService
.class).load(languageUuid
);
397 public static void setGlobalLanguage(Language language
){
398 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID
, language
.getUuid().toString());
399 CdmStore
.setDefaultLanguage(language
);
405 public static Map
<MarkerType
, Boolean
> getEditMarkerTypePreferences() {
406 List
<MarkerType
> markerTypes
= CdmStore
.getTermManager().getPreferredMarkerTypes();
408 Map
<MarkerType
, Boolean
> result
= new HashMap
<MarkerType
, Boolean
>();
410 for(MarkerType markerType
: markerTypes
){
411 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
412 Boolean value
= getPreferenceStore().getBoolean(name
);
414 result
.put(markerType
, value
);
421 * @param markerTypeEditingMap
423 public static void setEditMarkerTypePreferences(
424 Map
<MarkerType
, Boolean
> markerTypeEditingMap
) {
425 for(MarkerType markerType
: markerTypeEditingMap
.keySet()){
426 String name
= getMarkerTypeEditingPreferenceKey(markerType
);
427 getPreferenceStore().setValue(name
, markerTypeEditingMap
.get(markerType
));
432 private static String
getMarkerTypeEditingPreferenceKey(MarkerType markerType
){
433 markerType
= (MarkerType
) HibernateProxyHelper
.deproxy(markerType
);
434 return markerType
.getClass().getName() + EDIT_MARKER_TYPE_PREFIX
;
438 * <p>setEditMarkerTypePreference</p>
440 * @param input a {@link org.eclipse.ui.IEditorInput} object.
441 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
442 * @param edit a boolean.
444 public static void setEditMarkerTypePreference(MarkerType markerType
, boolean edit
) {
445 getPreferenceStore().setValue(getMarkerTypeEditingPreferenceKey(markerType
), edit
);
451 public static DerivedUnitFacadeConfigurator
getDerivedUnitConfigurator() {
452 DerivedUnitFacadeConfigurator configurator
= DerivedUnitFacadeConfigurator
.NewInstance();
453 configurator
.setMoveDerivedUnitMediaToGallery(true);
454 configurator
.setMoveFieldObjectMediaToGallery(true);