Project

General

Profile

Download (15.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
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.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.preference;
11

    
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.UUID;
18

    
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;
23

    
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;
46

    
47
/**
48
 * <p>PreferencesUtil class.</p>
49
 *
50
 * @author p.ciardelli
51
 * @author n.hoffmann
52
 * @created 05.12.2008
53
 * @version 1.0
54
 */
55
public class PreferencesUtil implements IPreferenceKeys{
56

    
57
	/**
58
	 * 
59
	 */
60
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
61
	
62

    
63
	
64
	/**
65
	 * <p>getPreferenceStore</p>
66
	 *
67
	 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
68
	 */
69
	public static IPreferenceStore getPreferenceStore() {
70
		return TaxeditorStorePlugin.getDefault().getPreferenceStore();
71
	}
72
		
73
	/**
74
	 * <p>setPreferredNomenclaturalCode</p>
75
	 *
76
	 * @param preferredCode a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
77
	 */
78
	public static void setPreferredNomenclaturalCode(NomenclaturalCode preferredCode) {
79
		getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY, getPreferenceKey(preferredCode));
80
	}
81
		
82
	/**
83
	 * <p>getPreferredNomenclaturalCode</p>
84
	 *
85
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
86
	 */
87
	public static NomenclaturalCode getPreferredNomenclaturalCode() {
88
	
89
		for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
90
			String preferredCode = getPreferenceStore().getString(PREFERRED_NOMENCLATURAL_CODE_KEY);
91
			if (getPreferenceKey(code).equals(preferredCode)) {
92
				return code;
93
			}
94
		}
95
		return null;
96
	}
97
	
98
	/**
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
101
	 *
102
	 * @param clazz a {@link java.lang.Class} object.
103
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
104
	 */
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);
109
			
110
			for(String fieldName : matchStrategy.getMatchFieldPropertyNames()){
111
				String matchModeName = getPreferenceStore().getString(getMatchStrategyFieldName(className, fieldName));
112
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
113
				try {
114
					matchStrategy.setMatchMode(fieldName, matchMode);
115
				} catch (MatchException e) {
116
					StoreUtil.error(PreferencesUtil.class, e);
117
					throw new RuntimeException(e);
118
				}
119
			}
120
			
121
			return matchStrategy;
122
		}
123
		return getDefaultMatchStrategy(clazz);
124
	}
125
	
126
	/**
127
	 * Stores a matchStrategy into the preference store.
128
	 *
129
	 * @param matchStrategy a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
130
	 */
131
	public static void setMatchStrategy(IMatchStrategy matchStrategy){
132
		String className = matchStrategy.getMatchClass().getName();
133
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
134
		
135
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
136
		
137
		for(String fieldName : matchFields){
138
			getPreferenceStore().setValue(getMatchStrategyFieldName(className, fieldName),
139
					matchStrategy.getMatchMode(fieldName).name());
140
		}
141
	}
142
	
143
	/**
144
	 * Helper method to create the preference property for a match field.
145
	 * 
146
	 * @param className
147
	 * @param fieldName
148
	 * @return
149
	 */
150
	private static String getMatchStrategyFieldName(String className, String fieldName){
151
		return MATCH_STRATEGY_PREFIX + className  + "." +  fieldName;
152
	}
153
	
154
	/**
155
	 * Returns the default match strategy for a given class.
156
	 *
157
	 * @param clazz a {@link java.lang.Class} object.
158
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
159
	 */
160
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz){
161
		return DefaultMatchStrategy.NewInstance(clazz);
162
	}
163
	
164
	
165
	/**
166
	 * <p>getDateFormatPattern</p>
167
	 *
168
	 * @return a {@link java.lang.String} object.
169
	 */
170
	public static String getDateFormatPattern(){
171
		// TODO make this configurable in properties
172
		String pattern = "Y-M-d H:m";
173
		return pattern;
174
	}
175
	
176
	/**
177
	 * <p>addTermToPreferredTerms</p>
178
	 *
179
	 * @param term a T object.
180
	 * @param <T> a T object.
181
	 */
182
	public static <T extends TermBase> void addTermToPreferredTerms(T term){
183

    
184
//		VocabularyEnum vocabulary = VocabularyEnum.getVocabularyEnum(term.getClass());
185
//		
186
//		getPreferenceStore().setValue(getPreferenceKey(term), VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
187
//		
188
//		firePreferencesChanged(term.getClass());
189
	}
190
	
191
	/**
192
	 * Construct a unique key using the CdmBase object's uuid
193
	 * 
194
	 * @param cdmBase
195
	 * @return
196
	 */
197
	private static String getPreferenceKey(ICdmBase cdmBase) {
198
		cdmBase = (ICdmBase) HibernateProxyHelper.deproxy(cdmBase);
199
		
200
		String key = cdmBase.getClass().getName()
201
				. concat(".")
202
				. concat(cdmBase.getUuid().toString());
203
		if (key.contains("javassist")) {
204
			StoreUtil.info("proxy");
205
		}
206
		return key;
207
	}
208

    
209
	/**
210
	 * Construct a unique key using the CdmBase object's uuid
211
	 * 
212
	 * @param cdmBase
213
	 * @return
214
	 */
215
	private static String getPreferenceKey(IDefinedTerm definedTerm) {
216
		definedTerm = (IDefinedTerm) HibernateProxyHelper.deproxy(definedTerm);
217
		String key = definedTerm.getClass().getName()
218
				. concat(".")
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.");
222
		}
223
		return key;
224
	}
225
	
226
	/**
227
	 * Retrieves search preferences from the preference store
228
	 *
229
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
230
	 */
231
	public static ITaxonServiceConfigurator getSearchConfigurator() {
232
		ITaxonServiceConfigurator configurator = initializeSearchConfigurator();
233
		
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));
238

    
239
		return configurator;
240
	}
241
	
242
	/**
243
	 * create new preferences, setting all search options to true
244
	 *
245
	 * @return a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
246
	 */
247
	public static ITaxonServiceConfigurator initializeSearchConfigurator(){
248
		ITaxonServiceConfigurator configurator = TaxonServiceConfiguratorImpl.NewInstance();
249
		
250
		configurator.setDoTaxa(true);
251
		configurator.setDoSynonyms(true);
252
		configurator.setDoNamesWithoutTaxa(true);
253
		configurator.setDoTaxaByCommonNames(true);
254
		
255
		configurator.setTaxonPropertyPath(Arrays.asList("$", 
256
				"titleCache", "name", "name.$", "relationsFromThisTaxon.$"));
257
		
258
		configurator.setSynonymPropertyPath(Arrays.asList("$", 
259
				"titleCache", "name", "name.$", "synonymRelations.relatedTo.*"));
260
		
261
		// DEFAULT VALUES		
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);
268
		
269
		return configurator;
270
	}
271
	
272
	/**
273
	 * Store search preferences
274
	 *
275
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
276
	 */
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());
282
	}
283

    
284
	/**
285
	 * <p>firePreferencesChanged</p>
286
	 *
287
	 * @param clazz a {@link java.lang.Class} object.
288
	 */
289
	public static void firePreferencesChanged(Class clazz) {
290
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE, null, clazz);
291
	}
292

    
293
	/**
294
	 * Set default values for preferences
295
	 */
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);	
301
		getPreferenceStore().setDefault(OPENURL_ACCESS_POINT, "http://www.biodiversitylibrary.org/openurl");
302
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
303
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
304
	}
305

    
306
	/**
307
	 * <p>checkNomenclaturalCode</p>
308
	 */
309
	public static void checkNomenclaturalCode() {	
310
		// First time Editor is opened, no nomenclatural code has been set
311
		if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
312

    
313
			StoreUtil.info("No nomencatural code set.");
314
			
315
			Shell shell = StoreUtil.getShell();
316
			
317
			// Query user re: preferred nom. code
318
			Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
319
			dialog.open();
320
			
321
			// Short message confirming user's choice
322
			NomenclaturalCode code = PreferencesUtil.getPreferredNomenclaturalCode();
323
			MessageDialog.openInformation(shell, "Nomenclatural code set", 
324
					"The following has been set as your preferred nomenclatural code:\n\n\t" + 
325
					NomenclaturalCodeHelper.getDescription(code) + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
326
		}
327
	}
328

    
329
	/**
330
	 * <p>getMapServiceAccessPoint</p>
331
	 *
332
	 * @return a {@link java.lang.String} object.
333
	 */
334
	public static String getMapServiceAccessPoint() {
335
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
336
	}
337

    
338
	/**
339
	 * <p>shouldConnectAtStartUp</p>
340
	 *
341
	 * @return a boolean.
342
	 */
343
	public static boolean shouldConnectAtStartUp() {
344
		return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
345
	}
346

    
347
	/**
348
	 * <p>getDefaultFeatureTreeForTextualDescription</p>
349
	 *
350
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
351
	 */
352
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
353
		String uuidString = getPreferenceStore().getString(FEATURE_TREE_DEFAULT_TEXT);
354
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(IFeatureTreeService.class).load(UUID.fromString(uuidString));
355
	}
356

    
357
	/**
358
	 * <p>getDefaultFeatureTreeForStructuredDescription</p>
359
	 *
360
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
361
	 */
362
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
363
		String uuidString = getPreferenceStore().getString(FEATURE_TREE_DEFAULT_STRUCTURE);
364
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(IFeatureTreeService.class).load(UUID.fromString(uuidString));
365
	}
366

    
367
	/**
368
	 * <p>setSortRanksHierarchichally</p>
369
	 *
370
	 * @param selection a boolean.
371
	 */
372
	public static void setSortRanksHierarchichally(boolean selection) {
373
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
374
	}	
375
	
376
	/**
377
	 * <p>getSortRanksHierarchichally</p>
378
	 *
379
	 * @return a boolean.
380
	 */
381
	public static boolean getSortRanksHierarchichally(){
382
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
383
	}
384

    
385
	public static boolean isMultilanguageTextEditingCapability() {
386
		return getPreferenceStore().getBoolean(MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
387
	}
388
	
389
	public static Language getGlobalLanguage(){
390
		String languageUuidString = getPreferenceStore().getString(GLOBAL_LANGUAGE_UUID);
391
				
392
		if(CdmUtils.isEmpty(languageUuidString)){
393
			return Language.DEFAULT();
394
		}
395
		
396
		UUID languageUuid = UUID.fromString(languageUuidString);
397
		return (Language) CdmStore.getService(ITermService.class).load(languageUuid);
398
	}
399
	
400
	public static void setGlobalLanguage(Language language){
401
		getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID, language.getUuid().toString());
402
		CdmStore.setDefaultLanguage(language);
403
	}
404

    
405
	/**
406
	 * @return
407
	 */
408
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
409
		List<MarkerType> markerTypes = CdmStore.getTermManager().getPreferredMarkerTypes();
410
		
411
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
412
		
413
		for(MarkerType markerType : markerTypes){
414
			String name = getMarkerTypeEditingPreferenceKey(markerType);
415
			Boolean value = getPreferenceStore().getBoolean(name);
416
			
417
			result.put(markerType, value);
418
		}
419
		
420
		return result;
421
	}
422

    
423
	/**
424
	 * @param markerTypeEditingMap
425
	 */
426
	public static void setEditMarkerTypePreferences(
427
			Map<MarkerType, Boolean> markerTypeEditingMap) {
428
		for(MarkerType markerType : markerTypeEditingMap.keySet()){
429
			String name = getMarkerTypeEditingPreferenceKey(markerType);
430
			getPreferenceStore().setValue(name, markerTypeEditingMap.get(markerType));
431
		}
432
		
433
	}
434
	
435
	private static String getMarkerTypeEditingPreferenceKey(MarkerType markerType){
436
		markerType = (MarkerType) HibernateProxyHelper.deproxy(markerType);
437
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
438
	}
439
	
440
	/**
441
	 * <p>setEditMarkerTypePreference</p>
442
	 *
443
	 * @param input a {@link org.eclipse.ui.IEditorInput} object.
444
	 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
445
	 * @param edit a boolean.
446
	 */
447
	public static void setEditMarkerTypePreference(MarkerType markerType, boolean edit) {
448
		getPreferenceStore().setValue(getMarkerTypeEditingPreferenceKey(markerType), edit); 
449
	}
450

    
451
	/**
452
	 * @return
453
	 */
454
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
455
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator.NewInstance();
456
		configurator.setMoveDerivedUnitMediaToGallery(true);
457
		configurator.setMoveFieldObjectMediaToGallery(true);
458
		return configurator;
459
	}
460

    
461
}
(12-12/15)