Project

General

Profile

Download (17.6 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.apache.commons.lang.StringUtils;
20
import org.eclipse.jface.preference.IPreferenceStore;
21
import org.eclipse.swt.widgets.Shell;
22
import org.eclipse.ui.PlatformUI;
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.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.MessagingUtils;
44
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.store.StoreUtil;
47
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
48
import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
49

    
50
/**
51
 * <p>
52
 * PreferencesUtil class.
53
 * </p>
54
 *
55
 * @author p.ciardelli
56
 * @author n.hoffmann
57
 * @created 05.12.2008
58
 * @version 1.0
59
 */
60
public class PreferencesUtil implements IPreferenceKeys {
61

    
62
	/**
63
	 *
64
	 */
65
	public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
66

    
67
	/**
68
	 * <p>
69
	 * getPreferenceStore
70
	 * </p>
71
	 *
72
	 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
73
	 */
74
	public static IPreferenceStore getPreferenceStore() {
75
		return TaxeditorStorePlugin.getDefault().getPreferenceStore();
76
	}
77

    
78
	/**
79
	 * <p>
80
	 * setPreferredNomenclaturalCode
81
	 * </p>
82
	 *
83
	 * @param preferredCode
84
	 *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
85
	 *            object.
86
	 */
87
	public static void setPreferredNomenclaturalCode(
88
			NomenclaturalCode preferredCode) {
89
		getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
90
				getPreferenceKey(preferredCode));
91
	}
92

    
93
	/**
94
	 * <p>
95
	 * getPreferredNomenclaturalCode
96
	 * </p>
97
	 *
98
	 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
99
	 */
100
	public static NomenclaturalCode getPreferredNomenclaturalCode() {
101

    
102
		for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
103
			String preferredCode = getPreferenceStore().getString(
104
					PREFERRED_NOMENCLATURAL_CODE_KEY);
105
			if (getPreferenceKey(code).equals(preferredCode)) {
106
				return code;
107
			}
108
		}
109
		return null;
110
	}
111

    
112
	public static String getPreferredDefaultLangugae(){
113
	    String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
114
	    if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
115
	        return preferredLanguage;
116
	    }
117
	    return null;
118
	}
119

    
120
	/**
121
	 * Get the match strategy for the given class that was stored in preferences
122
	 * or the default strategy if it was not stored in preferences
123
	 *
124
	 * @param clazz
125
	 *            a {@link java.lang.Class} object.
126
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
127
	 */
128
	public static IMatchStrategy getMatchStrategy(Class clazz) {
129
		String className = clazz.getName();
130
		if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
131
			IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
132

    
133
			for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
134
				String matchModeName = getPreferenceStore().getString(
135
						getMatchStrategyFieldName(className, fieldName));
136
				MatchMode matchMode = MatchMode.valueOf(matchModeName);
137
				try {
138
					matchStrategy.setMatchMode(fieldName, matchMode);
139
				} catch (MatchException e) {
140
					MessagingUtils.error(PreferencesUtil.class, e);
141
					throw new RuntimeException(e);
142
				}
143
			}
144

    
145
			return matchStrategy;
146
		}
147
		return getDefaultMatchStrategy(clazz);
148
	}
149

    
150
	/**
151
	 * Stores a matchStrategy into the preference store.
152
	 *
153
	 * @param matchStrategy
154
	 *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
155
	 *            object.
156
	 */
157
	public static void setMatchStrategy(IMatchStrategy matchStrategy) {
158
		String className = matchStrategy.getMatchClass().getName();
159
		getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
160

    
161
		Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
162

    
163
		for (String fieldName : matchFields) {
164
			getPreferenceStore().setValue(
165
					getMatchStrategyFieldName(className, fieldName),
166
					matchStrategy.getMatchMode(fieldName).name());
167
		}
168
	}
169

    
170
	/**
171
	 * Helper method to create the preference property for a match field.
172
	 *
173
	 * @param className
174
	 * @param fieldName
175
	 * @return
176
	 */
177
	private static String getMatchStrategyFieldName(String className,
178
			String fieldName) {
179
		return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
180
	}
181

    
182
	/**
183
	 * Returns the default match strategy for a given class.
184
	 *
185
	 * @param clazz
186
	 *            a {@link java.lang.Class} object.
187
	 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
188
	 */
189
	public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
190
		return DefaultMatchStrategy.NewInstance(clazz);
191
	}
192

    
193
	/**
194
	 * <p>
195
	 * getDateFormatPattern
196
	 * </p>
197
	 *
198
	 * @return a {@link java.lang.String} object.
199
	 */
200
	public static String getDateFormatPattern() {
201
		// TODO make this configurable in properties
202
		String pattern = "Y-M-d H:m";
203
		return pattern;
204
	}
205

    
206
	/**
207
	 * <p>
208
	 * addTermToPreferredTerms
209
	 * </p>
210
	 *
211
	 * @param term
212
	 *            a T object.
213
	 * @param <T>
214
	 *            a T object.
215
	 */
216
	public static <T extends TermBase> void addTermToPreferredTerms(T term) {
217

    
218
		// VocabularyEnum vocabulary =
219
		// VocabularyEnum.getVocabularyEnum(term.getClass());
220
		//
221
		// getPreferenceStore().setValue(getPreferenceKey(term),
222
		// VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
223
		//
224
		// firePreferencesChanged(term.getClass());
225
	}
226

    
227
	/**
228
	 * Construct a unique key using the CdmBase object's uuid
229
	 *
230
	 * @param cdmBase
231
	 * @return
232
	 */
233
	private static String getPreferenceKey(ICdmBase cdmBase) {
234
		cdmBase = (ICdmBase) HibernateProxyHelper.deproxy(cdmBase);
235

    
236
		String key = cdmBase.getClass().getName().concat(".")
237
				.concat(cdmBase.getUuid().toString());
238
		if (key.contains("javassist")) {
239
			MessagingUtils.info("proxy");
240
		}
241
		return key;
242
	}
243

    
244
	/**
245
	 * Construct a unique key using the CdmBase object's uuid
246
	 *
247
	 * @param cdmBase
248
	 * @return
249
	 */
250
	public static String getPreferenceKey(ISimpleTerm simpleTerm) {
251
		simpleTerm = (ISimpleTerm) HibernateProxyHelper.deproxy(simpleTerm);
252
		String key = simpleTerm.getClass().getName().concat(".")
253
				.concat(simpleTerm.getUuid().toString());
254
		if (key.contains("javassist")) {
255
			MessagingUtils.warn(PreferencesUtil.class,
256
					"Trying to persist a preference based on a proxy class.");
257
		}
258
		return key;
259
	}
260

    
261

    
262

    
263
	/**
264
	 * Construct a unique key using the CdmBase object's uuid
265
	 *
266
	 * @param cdmBase
267
	 * @return
268
	 */
269
	public static String getPreferenceKey(IDefinedTerm definedTerm) {
270
		definedTerm = (IDefinedTerm) HibernateProxyHelper.deproxy(definedTerm);
271
		String key = definedTerm.getClass().getName().concat(".")
272
				.concat(definedTerm.getUuid().toString());
273
		if (key.contains("javassist")) {
274
			MessagingUtils.warn(PreferencesUtil.class,
275
					"Trying to persist a preference based on a proxy class.");
276
		}
277
		return key;
278
	}
279

    
280
	/**
281
	 * Retrieves search preferences from the preference store
282
	 *
283
	 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
284
	 */
285
	public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
286
		IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
287

    
288
		configurator.setDoTaxa(getPreferenceStore().getBoolean(
289
				TAXON_SERVICE_CONFIGURATOR_TAXA));
290
		configurator.setDoSynonyms(getPreferenceStore().getBoolean(
291
				TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
292
		configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
293
				TAXON_SERVICE_CONFIGURATOR_NAMES));
294
		configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
295
				TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
296

    
297
		return configurator;
298
	}
299

    
300
	/**
301
	 * create new preferences, setting all search options to true
302
	 *
303
	 * @return a
304
	 *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
305
	 *         object.
306
	 */
307
	public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
308
		IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
309

    
310
		configurator.setDoTaxa(true);
311
		configurator.setDoSynonyms(true);
312
		configurator.setDoNamesWithoutTaxa(true);
313
		configurator.setDoTaxaByCommonNames(true);
314

    
315
		configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
316
				"name", "name.$", "relationsFromThisTaxon.$"));
317

    
318
		configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
319
				"name", "name.$", "synonymRelations.relatedTo.*"));
320

    
321
		// DEFAULT VALUES
322
		// match mode is a simple like, actually all other match modes are kind
323
		// of bogus
324
		configurator
325
				.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
326
		// we set page number and size here as this should always be unlimited
327
		configurator.setPageNumber(0);
328
		// TODO currently limit results to 10000
329
		configurator.setPageSize(10000);
330

    
331
		return configurator;
332
	}
333

    
334
	/**
335
	 * Store search preferences
336
	 *
337
	 * @param configurator
338
	 *            a
339
	 *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
340
	 *            object.
341
	 */
342
	public static void setSearchConfigurator(
343
			IFindTaxaAndNamesConfigurator configurator) {
344
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
345
				configurator.isDoTaxa());
346
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
347
				configurator.isDoSynonyms());
348
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
349
				configurator.isDoNamesWithoutTaxa());
350
		getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
351
				configurator.isDoTaxaByCommonNames());
352
	}
353

    
354
	/**
355
	 * <p>
356
	 * firePreferencesChanged
357
	 * </p>
358
	 *
359
	 * @param clazz
360
	 *            a {@link java.lang.Class} object.
361
	 */
362
	public static void firePreferencesChanged(Class clazz) {
363
		getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
364
				null, clazz);
365
	}
366

    
367
	/**
368
	 * Set default values for preferences
369
	 */
370
	public static void setDefaults() {
371
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
372
		getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
373
				true);
374
		getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
375
				"http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
376
		//FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
377
		getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
378
		getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
379
				"http://www.biodiversitylibrary.org/openurl");
380
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
381
		getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
382
	}
383

    
384
	/**
385
	 * <p>
386
	 * checkNomenclaturalCode
387
	 * </p>
388
	 */
389
	public static void checkNomenclaturalCode() {
390
		// First time Editor is opened, no nomenclatural code has been set
391

    
392

    
393
		if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
394
			PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
395
			/*
396

    
397
			StoreUtil.info("No nomencatural code set.");
398

    
399
			Shell shell = StoreUtil.getShell();
400

    
401
		 Query user re: preferred nom. code
402
			Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
403
			dialog.open();
404

    
405
			// Short message confirming user's choice
406
			NomenclaturalCode code = PreferencesUtil
407
					.getPreferredNomenclaturalCode();
408
			MessageDialog
409
					.openInformation(
410
							shell,
411
							"Nomenclatural code set",
412
							"The following has been set as your preferred nomenclatural code:\n\n\t"
413
									+ NomenclaturalCodeHelper
414
											.getDescription(code)
415
									+ "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
416
		}
417
	}
418

    
419
	public static void checkDefaultLanguage(){
420
	    if(PreferencesUtil.getPreferredDefaultLangugae() == null){
421
	       Shell shell = StoreUtil.getShell();
422
	       int open = new DefaultLanguageDialog(shell).open();
423
	       if(open == 0){//FIXME:window performed ok. Find variable for it
424
	           PlatformUI.getWorkbench().restart();
425
	       }
426
	    }else{
427
	        //TODO:In case of a reinstall, the config.ini will be overwritten
428
	        //     here you create config.ini with the stored key from preferences
429
	    }
430
	}
431

    
432
	/**
433
	 * <p>
434
	 * getMapServiceAccessPoint
435
	 * </p>
436
	 *
437
	 * @return a {@link java.lang.String} object.
438
	 */
439
	public static String getMapServiceAccessPoint() {
440
		return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
441
	}
442

    
443
	/**
444
	 * <p>
445
	 * shouldConnectAtStartUp
446
	 * </p>
447
	 *
448
	 * @return a boolean.
449
	 */
450
	public static boolean shouldConnectAtStartUp() {
451
		//FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
452
		//return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
453
		return false;
454
	}
455

    
456
	/**
457
	 * <p>
458
	 * getDefaultFeatureTreeForTextualDescription
459
	 * </p>
460
	 *
461
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
462
	 */
463
	public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
464
		String uuidString = getPreferenceStore().getString(
465
				FEATURE_TREE_DEFAULT_TEXT);
466
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
467
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
468
	}
469

    
470
	/**
471
	 * <p>
472
	 * getDefaultFeatureTreeForStructuredDescription
473
	 * </p>
474
	 *
475
	 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
476
	 */
477
	public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
478
		String uuidString = getPreferenceStore().getString(
479
				FEATURE_TREE_DEFAULT_STRUCTURE);
480
		return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
481
				IFeatureTreeService.class).load(UUID.fromString(uuidString));
482
	}
483

    
484
	/**
485
	 * <p>
486
	 * setSortRanksHierarchichally
487
	 * </p>
488
	 *
489
	 * @param selection
490
	 *            a boolean.
491
	 */
492
	public static void setSortRanksHierarchichally(boolean selection) {
493
		getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
494
	}
495

    
496
	/**
497
	 * <p>
498
	 * getSortRanksHierarchichally
499
	 * </p>
500
	 *
501
	 * @return a boolean.
502
	 */
503
	public static boolean getSortRanksHierarchichally() {
504
		return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
505
	}
506

    
507
	public static boolean isMultilanguageTextEditingCapability() {
508
		return getPreferenceStore().getBoolean(
509
				MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
510
	}
511

    
512
	public static Language getGlobalLanguage() {
513
		String languageUuidString = getPreferenceStore().getString(
514
				GLOBAL_LANGUAGE_UUID);
515

    
516
		if (CdmUtils.isBlank(languageUuidString)) {
517
			return Language.getDefaultLanguage();
518
		}
519

    
520
		UUID languageUuid = UUID.fromString(languageUuidString);
521
		return (Language) CdmStore.getService(ITermService.class).load(
522
				languageUuid);
523
	}
524

    
525
	public static void setGlobalLanguage(Language language) {
526
		getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,
527
				language.getUuid().toString());
528
		CdmStore.setDefaultLanguage(language);
529
	}
530

    
531
	/**
532
	 * @return
533
	 */
534
	public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
535
		List<MarkerType> markerTypes = CdmStore.getTermManager()
536
				.getPreferredTerms(MarkerType.class);
537

    
538
		Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
539

    
540
		for (MarkerType markerType : markerTypes) {
541
			String name = getMarkerTypeEditingPreferenceKey(markerType);
542
			Boolean value = getPreferenceStore().getBoolean(name);
543

    
544
			result.put(markerType, value);
545
		}
546

    
547
		return result;
548
	}
549

    
550
	/**
551
	 * @param markerTypeEditingMap
552
	 */
553
	public static void setEditMarkerTypePreferences(
554
			Map<MarkerType, Boolean> markerTypeEditingMap) {
555
		for (MarkerType markerType : markerTypeEditingMap.keySet()) {
556
			String name = getMarkerTypeEditingPreferenceKey(markerType);
557
			getPreferenceStore().setValue(name,
558
					markerTypeEditingMap.get(markerType));
559
		}
560

    
561
	}
562

    
563
	private static String getMarkerTypeEditingPreferenceKey(
564
			MarkerType markerType) {
565
		markerType = (MarkerType) HibernateProxyHelper.deproxy(markerType);
566
		return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
567
	}
568

    
569
	/**
570
	 * <p>
571
	 * setEditMarkerTypePreference
572
	 * </p>
573
	 *
574
	 * @param input
575
	 *            a {@link org.eclipse.ui.IEditorInput} object.
576
	 * @param markerType
577
	 *            a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
578
	 * @param edit
579
	 *            a boolean.
580
	 */
581
	public static void setEditMarkerTypePreference(MarkerType markerType,
582
			boolean edit) {
583
		getPreferenceStore().setValue(
584
				getMarkerTypeEditingPreferenceKey(markerType), edit);
585
	}
586

    
587
	/**
588
	 * @return
589
	 */
590
	public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
591
		DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
592
				.NewInstance();
593
		configurator.setMoveDerivedUnitMediaToGallery(true);
594
		configurator.setMoveFieldObjectMediaToGallery(true);
595
		return configurator;
596
	}
597

    
598
}
(14-14/20)