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