cf3a9d8a3665f2f1bf18810cc0a0c1d8ca346f3d
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
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.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Set;
25 import java.util.StringTokenizer;
26 import java.util.UUID;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.PlatformUI;
36
37 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
38 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
39 import eu.etaxonomy.cdm.api.service.ITermService;
40 import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
41 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
42 import eu.etaxonomy.cdm.common.CdmUtils;
43 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
44 import eu.etaxonomy.cdm.model.common.ICdmBase;
45 import eu.etaxonomy.cdm.model.common.IDefinedTerm;
46 import eu.etaxonomy.cdm.model.common.ISimpleTerm;
47 import eu.etaxonomy.cdm.model.common.Language;
48 import eu.etaxonomy.cdm.model.common.MarkerType;
49 import eu.etaxonomy.cdm.model.common.TermBase;
50 import eu.etaxonomy.cdm.model.description.FeatureTree;
51 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
52 import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
53 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
54 import eu.etaxonomy.cdm.strategy.match.MatchException;
55 import eu.etaxonomy.cdm.strategy.match.MatchMode;
56 import eu.etaxonomy.taxeditor.model.AbstractUtility;
57 import eu.etaxonomy.taxeditor.model.MessagingUtils;
58 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
59 import eu.etaxonomy.taxeditor.store.CdmStore;
60 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
61 import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
62
63 /**
64 * <p>
65 * PreferencesUtil class.
66 * </p>
67 *
68 * @author p.ciardelli
69 * @author n.hoffmann
70 * @created 05.12.2008
71 * @version 1.0
72 */
73 public class PreferencesUtil implements IPreferenceKeys {
74
75 /**
76 *
77 */
78 public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
79
80 public static final String P2_REPOSITORIES_DELIM = ",";
81 public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
82
83
84
85 /**
86 * <p>
87 * getPreferenceStore
88 * </p>
89 *
90 * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
91 */
92 public static IPreferenceStore getPreferenceStore() {
93 return TaxeditorStorePlugin.getDefault().getPreferenceStore();
94 }
95
96 /**
97 * <p>
98 * setPreferredNomenclaturalCode
99 * </p>
100 *
101 * @param preferredCode
102 * a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
103 * object.
104 */
105 public static void setPreferredNomenclaturalCode(
106 NomenclaturalCode preferredCode) {
107 getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
108 getPreferenceKey(preferredCode));
109 }
110
111 /**
112 * <p>
113 * getPreferredNomenclaturalCode
114 * </p>
115 *
116 * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
117 */
118 public static NomenclaturalCode getPreferredNomenclaturalCode() {
119
120 for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
121 String preferredCode = getPreferenceStore().getString(
122 PREFERRED_NOMENCLATURAL_CODE_KEY);
123 if (getPreferenceKey(code).equals(preferredCode)) {
124 return code;
125 }
126 }
127 return null;
128 }
129
130 public static String getPreferredDefaultLangugae(){
131 String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
132 if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
133 return preferredLanguage;
134 }
135 return null;
136 }
137
138 /**
139 * Get the match strategy for the given class that was stored in preferences
140 * or the default strategy if it was not stored in preferences
141 *
142 * @param clazz
143 * a {@link java.lang.Class} object.
144 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
145 */
146 public static IMatchStrategy getMatchStrategy(Class clazz) {
147 String className = clazz.getName();
148 if (getPreferenceStore().getBoolean(MATCH_STRATEGY_PREFIX + className)) {
149 IMatchStrategy matchStrategy = getDefaultMatchStrategy(clazz);
150
151 for (String fieldName : matchStrategy.getMatchFieldPropertyNames()) {
152 String matchModeName = getPreferenceStore().getString(
153 getMatchStrategyFieldName(className, fieldName));
154 MatchMode matchMode = MatchMode.valueOf(matchModeName);
155 try {
156 matchStrategy.setMatchMode(fieldName, matchMode);
157 } catch (MatchException e) {
158 MessagingUtils.error(PreferencesUtil.class, e);
159 throw new RuntimeException(e);
160 }
161 }
162
163 return matchStrategy;
164 }
165 return getDefaultMatchStrategy(clazz);
166 }
167
168 /**
169 * Stores a matchStrategy into the preference store.
170 *
171 * @param matchStrategy
172 * a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
173 * object.
174 */
175 public static void setMatchStrategy(IMatchStrategy matchStrategy) {
176 String className = matchStrategy.getMatchClass().getName();
177 getPreferenceStore().setValue(MATCH_STRATEGY_PREFIX + className, true);
178
179 Set<String> matchFields = matchStrategy.getMatchFieldPropertyNames();
180
181 for (String fieldName : matchFields) {
182 getPreferenceStore().setValue(
183 getMatchStrategyFieldName(className, fieldName),
184 matchStrategy.getMatchMode(fieldName).name());
185 }
186 }
187
188 /**
189 * Helper method to create the preference property for a match field.
190 *
191 * @param className
192 * @param fieldName
193 * @return
194 */
195 private static String getMatchStrategyFieldName(String className,
196 String fieldName) {
197 return MATCH_STRATEGY_PREFIX + className + "." + fieldName;
198 }
199
200 /**
201 * Returns the default match strategy for a given class.
202 *
203 * @param clazz
204 * a {@link java.lang.Class} object.
205 * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
206 */
207 public static IMatchStrategy getDefaultMatchStrategy(Class clazz) {
208 return DefaultMatchStrategy.NewInstance(clazz);
209 }
210
211 /**
212 * <p>
213 * getDateFormatPattern
214 * </p>
215 *
216 * @return a {@link java.lang.String} object.
217 */
218 public static String getDateFormatPattern() {
219 // TODO make this configurable in properties
220 String pattern = "Y-M-d H:m";
221 return pattern;
222 }
223
224 /**
225 * <p>
226 * addTermToPreferredTerms
227 * </p>
228 *
229 * @param term
230 * a T object.
231 * @param <T>
232 * a T object.
233 */
234 public static <T extends TermBase> void addTermToPreferredTerms(T term) {
235
236 // VocabularyEnum vocabulary =
237 // VocabularyEnum.getVocabularyEnum(term.getClass());
238 //
239 // getPreferenceStore().setValue(getPreferenceKey(term),
240 // VocabularyStore.getTermVocabulary(vocabulary).getTerms().contains(term));
241 //
242 // firePreferencesChanged(term.getClass());
243 }
244
245 /**
246 * Construct a unique key using the CdmBase object's uuid
247 *
248 * @param cdmBase
249 * @return
250 */
251 private static String getPreferenceKey(ICdmBase cdmBase) {
252 cdmBase = HibernateProxyHelper.deproxy(cdmBase);
253
254 String key = cdmBase.getClass().getName().concat(".")
255 .concat(cdmBase.getUuid().toString());
256 if (key.contains("javassist")) {
257 MessagingUtils.info("proxy");
258 }
259 return key;
260 }
261
262 /**
263 * Construct a unique key using the CdmBase object's uuid
264 *
265 * @param cdmBase
266 * @return
267 */
268 public static String getPreferenceKey(ISimpleTerm simpleTerm) {
269 simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
270 String key = simpleTerm.getClass().getName().concat(".")
271 .concat(simpleTerm.getUuid().toString());
272 if (key.contains("javassist")) {
273 MessagingUtils.warn(PreferencesUtil.class,
274 "Trying to persist a preference based on a proxy class.");
275 }
276 return key;
277 }
278
279
280
281 /**
282 * Construct a unique key using the CdmBase object's uuid
283 *
284 * @param cdmBase
285 * @return
286 */
287 public static String getPreferenceKey(IDefinedTerm definedTerm) {
288 definedTerm = HibernateProxyHelper.deproxy(definedTerm);
289 String key = definedTerm.getClass().getName().concat(".")
290 .concat(definedTerm.getUuid().toString());
291 if (key.contains("javassist")) {
292 MessagingUtils.warn(PreferencesUtil.class,
293 "Trying to persist a preference based on a proxy class.");
294 }
295 return key;
296 }
297
298 /**
299 * Retrieves search preferences from the preference store
300 *
301 * @return an {@link ITaxonServiceConfigurator} to pass to search methods
302 */
303 public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
304 IFindTaxaAndNamesConfigurator configurator = initializeSearchConfigurator();
305
306 configurator.setDoTaxa(getPreferenceStore().getBoolean(
307 TAXON_SERVICE_CONFIGURATOR_TAXA));
308 configurator.setDoSynonyms(getPreferenceStore().getBoolean(
309 TAXON_SERVICE_CONFIGURATOR_SYNONYMS));
310 configurator.setDoNamesWithoutTaxa(getPreferenceStore().getBoolean(
311 TAXON_SERVICE_CONFIGURATOR_NAMES));
312 configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
313 TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
314 //configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
315
316 return configurator;
317 }
318
319 /**
320 * create new preferences, setting all search options to true
321 *
322 * @return a
323 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
324 * object.
325 */
326 public static IFindTaxaAndNamesConfigurator initializeSearchConfigurator() {
327 IFindTaxaAndNamesConfigurator configurator = new FindTaxaAndNamesConfiguratorImpl();
328
329 configurator.setDoTaxa(true);
330 configurator.setDoSynonyms(true);
331 configurator.setDoNamesWithoutTaxa(true);
332 configurator.setDoTaxaByCommonNames(true);
333
334 configurator.setTaxonPropertyPath(Arrays.asList("$", "titleCache",
335 "name", "name.$", "relationsFromThisTaxon.$"));
336
337 configurator.setSynonymPropertyPath(Arrays.asList("$", "titleCache",
338 "name", "name.$", "synonymRelations.relatedTo.*"));
339
340 // DEFAULT VALUES
341 // match mode is a simple like, actually all other match modes are kind
342 // of bogus
343 configurator
344 .setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
345 // we set page number and size here as this should always be unlimited
346 configurator.setPageNumber(0);
347 // TODO currently limit results to 10000
348 configurator.setPageSize(10000);
349
350 return configurator;
351 }
352
353 /**
354 * Store search preferences
355 *
356 * @param configurator
357 * a
358 * {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
359 * object.
360 */
361 public static void setSearchConfigurator(
362 IFindTaxaAndNamesConfigurator configurator) {
363 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_TAXA,
364 configurator.isDoTaxa());
365 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
366 configurator.isDoSynonyms());
367 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_NAMES,
368 configurator.isDoNamesWithoutTaxa());
369 getPreferenceStore().setValue(TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES,
370 configurator.isDoTaxaByCommonNames());
371 }
372
373 /**
374 * <p>
375 * firePreferencesChanged
376 * </p>
377 *
378 * @param clazz
379 * a {@link java.lang.Class} object.
380 */
381 public static void firePreferencesChanged(Class clazz) {
382 getPreferenceStore().firePropertyChangeEvent(PREFERRED_TERMS_CHANGE,
383 null, clazz);
384 }
385
386 /**
387 * Set default values for preferences
388 */
389 public static void setDefaults() {
390 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_TAXA, true);
391 getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
392 true);
393 getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
394 "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
395 //FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
396 getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
397 getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
398 "http://www.biodiversitylibrary.org/openurl");
399 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
400 getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
401 getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, false);
402 getPreferenceStore().setDefault(CHECKLIST_ID_IN_VOCABULARY, true);
403 }
404
405 /**
406 * <p>
407 * checkNomenclaturalCode
408 * </p>
409 */
410 public static void checkNomenclaturalCode() {
411 // First time Editor is opened, no nomenclatural code has been set
412
413
414 if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
415 PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
416 /*
417
418 StoreUtil.info("No nomencatural code set.");
419
420 Shell shell = StoreUtil.getShell();
421
422 Query user re: preferred nom. code
423 Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
424 dialog.open();
425
426 // Short message confirming user's choice
427 NomenclaturalCode code = PreferencesUtil
428 .getPreferredNomenclaturalCode();
429 MessageDialog
430 .openInformation(
431 shell,
432 "Nomenclatural code set",
433 "The following has been set as your preferred nomenclatural code:\n\n\t"
434 + NomenclaturalCodeHelper
435 .getDescription(code)
436 + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
437 }
438 }
439
440 public static void checkDefaultLanguage(){
441 if(PreferencesUtil.getPreferredDefaultLangugae() == null){
442 Shell shell = AbstractUtility.getShell();
443 int open = new DefaultLanguageDialog(shell).open();
444 if(open == Window.OK){
445 PlatformUI.getWorkbench().restart();
446 }
447 }else{
448 //TODO:In case of a reinstall, the config.ini will be overwritten
449 // here you create config.ini with the stored key from preferences
450 }
451 }
452
453 /**
454 * <p>
455 * getMapServiceAccessPoint
456 * </p>
457 *
458 * @return a {@link java.lang.String} object.
459 */
460 public static String getMapServiceAccessPoint() {
461 return getPreferenceStore().getString(EDIT_MAP_SERVICE_ACCES_POINT);
462 }
463
464 /**
465 * <p>
466 * shouldConnectAtStartUp
467 * </p>
468 *
469 * @return a boolean.
470 */
471 public static boolean shouldConnectAtStartUp() {
472 //FIXME : force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
473 //return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
474 return false;
475 }
476
477 /**
478 * <p>
479 * getDefaultFeatureTreeForTextualDescription
480 * </p>
481 *
482 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
483 */
484 public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
485 String uuidString = getPreferenceStore().getString(
486 FEATURE_TREE_DEFAULT_TEXT);
487 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
488 IFeatureTreeService.class).load(UUID.fromString(uuidString));
489 }
490
491 /**
492 * <p>
493 * getDefaultFeatureTreeForStructuredDescription
494 * </p>
495 *
496 * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
497 */
498 public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
499 String uuidString = getPreferenceStore().getString(
500 FEATURE_TREE_DEFAULT_STRUCTURE);
501 return CdmUtils.isEmpty(uuidString) ? null : CdmStore.getService(
502 IFeatureTreeService.class).load(UUID.fromString(uuidString));
503 }
504
505 /**
506 * <p>
507 * setSortRanksHierarchichally
508 * </p>
509 *
510 * @param selection
511 * a boolean.
512 */
513 public static void setSortRanksHierarchichally(boolean selection) {
514 getPreferenceStore().setValue(SORT_RANKS_HIERARCHICHALLY, selection);
515 }
516
517 /**
518 * <p>
519 * getSortRanksHierarchichally
520 * </p>
521 *
522 * @return a boolean.
523 */
524 public static boolean getSortRanksHierarchichally() {
525 return getPreferenceStore().getBoolean(SORT_RANKS_HIERARCHICHALLY);
526 }
527
528 public static boolean isMultilanguageTextEditingCapability() {
529 return getPreferenceStore().getBoolean(
530 MULTILANGUAGE_TEXT_EDITING_CAPABILITY);
531 }
532
533 public static Language getGlobalLanguage() {
534
535
536 String languageUuidString = getPreferenceStore().getString(
537 GLOBAL_LANGUAGE_UUID);
538
539 if(!CdmStore.isActive()) {
540 MessagingUtils.noDataSourceWarningDialog(languageUuidString);
541 return null;
542 }
543
544 if (CdmUtils.isBlank(languageUuidString)) {
545 return Language.getDefaultLanguage();
546 }
547
548 UUID languageUuid = UUID.fromString(languageUuidString);
549 return (Language) CdmStore.getService(ITermService.class).load(
550 languageUuid);
551 }
552
553 public static void setGlobalLanguage(Language language) {
554 if(language != null) {
555 getPreferenceStore().setValue(GLOBAL_LANGUAGE_UUID,language.getUuid().toString());
556 CdmStore.setDefaultLanguage(language);
557 }
558
559 }
560
561 /**
562 * @return
563 */
564 public static Map<MarkerType, Boolean> getEditMarkerTypePreferences() {
565 List<MarkerType> markerTypes = CdmStore.getTermManager()
566 .getPreferredTerms(MarkerType.class);
567
568 Map<MarkerType, Boolean> result = new HashMap<MarkerType, Boolean>();
569
570 for (MarkerType markerType : markerTypes) {
571 String name = getMarkerTypeEditingPreferenceKey(markerType);
572 Boolean value = getPreferenceStore().getBoolean(name);
573
574 result.put(markerType, value);
575 }
576
577 return result;
578 }
579
580 /**
581 * @param markerTypeEditingMap
582 */
583 public static void setEditMarkerTypePreferences(
584 Map<MarkerType, Boolean> markerTypeEditingMap) {
585 for (MarkerType markerType : markerTypeEditingMap.keySet()) {
586 String name = getMarkerTypeEditingPreferenceKey(markerType);
587 getPreferenceStore().setValue(name,
588 markerTypeEditingMap.get(markerType));
589 }
590
591 }
592
593 private static String getMarkerTypeEditingPreferenceKey(
594 MarkerType markerType) {
595 markerType = HibernateProxyHelper.deproxy(markerType);
596 return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
597 }
598
599 /**
600 * <p>
601 * setEditMarkerTypePreference
602 * </p>
603 *
604 * @param input
605 * a {@link org.eclipse.ui.IEditorInput} object.
606 * @param markerType
607 * a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
608 * @param edit
609 * a boolean.
610 */
611 public static void setEditMarkerTypePreference(MarkerType markerType,
612 boolean edit) {
613 getPreferenceStore().setValue(
614 getMarkerTypeEditingPreferenceKey(markerType), edit);
615 }
616
617 /**
618 * @return
619 */
620 public static DerivedUnitFacadeConfigurator getDerivedUnitConfigurator() {
621 DerivedUnitFacadeConfigurator configurator = DerivedUnitFacadeConfigurator
622 .NewInstance();
623 configurator.setMoveDerivedUnitMediaToGallery(true);
624 configurator.setMoveFieldObjectMediaToGallery(true);
625 return configurator;
626 }
627
628 /**
629 * This method will write language properties to the config.ini located in the configuration folder
630 * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
631 *
632 * @param setLanguage 0 is for german and 1 for english.
633 * @throws IOException
634 */
635 public void writePropertyToConfigFile(int setLanguage) throws IOException {
636 File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
637 //give warning to user if the directory has no write access
638 if(file == null){
639 throw new IOException();
640 }
641 Properties properties = load(file.getAbsolutePath()+"/config.ini");
642 switch(setLanguage){
643 case 0:
644 properties.setProperty("osgi.nl", "de");
645 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
646 break;
647 case 1:
648 properties.setProperty("osgi.nl", "en");
649 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
650 break;
651 default:
652 break;
653 }
654 save(file+"/config.ini", properties);
655 }
656
657 /**
658 * This method loads a property from a given file and returns it.
659 *
660 * @param filename
661 * @return
662 * @throws IOException
663 */
664 private Properties load(String filename) throws IOException {
665 FileInputStream in = new FileInputStream(filename);
666 Properties prop = new Properties();
667 prop.load(in);
668 in.close();
669 return prop;
670 }
671
672 /**
673 * This method saves a property to the specified file.
674 *
675 * @param filename
676 * @param properties
677 * @throws IOException
678 */
679 private void save(String filename, Properties properties) throws IOException{
680 FileOutputStream fos = new FileOutputStream(filename);
681 properties.store(fos, "");
682 fos.close();
683 }
684
685 /**
686 * Saves a list of P2 Metadata Repositories as string with specified delimiters
687 *
688 * @param p2Repos
689 */
690 public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
691 StringBuilder sb = new StringBuilder();
692 for(MetadataRepositoryElement p2Repo : p2Repos) {
693 sb.append(P2_REPOSITORIES_DELIM);
694 if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
695 sb.append("-");
696 } else {
697 sb.append(p2Repo.getName());
698 }
699 sb.append(P2_REPOSITORY_FIELDS_DELIM);
700 sb.append(p2Repo.getLocation().toString());
701 sb.append(P2_REPOSITORY_FIELDS_DELIM);
702 sb.append(String.valueOf(p2Repo.isEnabled()));
703 }
704 getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
705 }
706
707
708 /**
709 * Retrieves a list of previously saved P2 repositories
710 *
711 * @return
712 */
713 public static List<MetadataRepositoryElement> getP2Repositories() {
714 List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
715 String p2ReposPref = getPreferenceStore().getString(P2_REPOSITORY_LIST);
716 if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
717 StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
718
719 while(p2ReposPrefST.hasMoreTokens()) {
720 String p2RepoStr = p2ReposPrefST.nextToken();
721 StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
722 if(p2ReposStrST.countTokens()==3) {
723 String nickname = p2ReposStrST.nextToken();
724 URI uri = null;
725 try {
726 uri = new URI(p2ReposStrST.nextToken());
727 } catch (URISyntaxException e) {
728 continue;
729 }
730 boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
731 MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
732 mre.setNickname(nickname);
733 mre.setEnabled(enabled);
734 p2Repos.add(mre);
735 }
736 }
737 }
738
739 return p2Repos;
740 }
741
742 /**
743 * enables/disables nested composite. <br>
744 *
745 * @param ctrl - Composite to be en-/disabeld
746 * @param enabled - boolean
747 */
748 public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
749 if (ctrl instanceof Composite) {
750 Composite comp = (Composite) ctrl;
751 for (Control c : comp.getChildren()) {
752 recursiveSetEnabled(c, enabled);
753 }
754 } else {
755 ctrl.setEnabled(enabled);
756 }
757 }
758
759 /**
760 * <p>
761 * getSortRanksNaturally
762 * </p>
763 *
764 * @return a boolean.
765 */
766 public static boolean getSortNodesNaturally() {
767 return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
768 }
769
770 /**
771 * <p>
772 * setSortRanksNaturally
773 * </p>
774 *
775 * @param selection
776 * a boolean.
777 */
778 public static void setSortNodesNaturally(boolean selection) {
779 getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
780 }
781
782
783 /**
784 * <p>
785 * getSortRanksNaturally
786 * </p>
787 *
788 * @return a boolean.
789 */
790 public static boolean getSortNodesStrictlyAlphabetically() {
791 return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
792 }
793
794 /**
795 * <p>
796 * setSortRanksNaturally
797 * </p>
798 *
799 * @param selection
800 * a boolean.
801 */
802 public static void setSortNodesStrictlyAlphabetically(boolean selection) {
803 getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
804 }
805
806 /**
807 * <p>
808 * setStoreNavigatorState
809 * </p>
810 *
811 * @param selection
812 * a boolean.
813 */
814 public static boolean isStoreNavigatorState() {
815 return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
816
817 }
818
819 /**
820 * <p>
821 * setStoreNavigatorState
822 * </p>
823 *
824 * @param selection
825 * a boolean.
826 */
827 public static void setStoreNavigatorState(boolean selection) {
828 getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
829
830 }
831
832 /**
833 * @return
834 */
835 public static boolean isShowUpWidgetIsDisposedMessages() {
836 return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
837 }
838 public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
839 getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
840 }
841
842 /**
843 * @return
844 */
845 public static boolean isShowIdInVocabularyInChecklistEditor() {
846 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
847 }
848 public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
849 getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
850 }
851
852 /**
853 * @return
854 */
855 public static boolean isShowRankInChecklistEditor() {
856 return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
857 }
858 public static void setShowRankInChecklistEditor(boolean selection) {
859 getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
860 }
861
862 }