From abf098b0b0ba64424aff3af022fcfd9329e51e83 Mon Sep 17 00:00:00 2001 From: Nikifor Fedorov Date: Sat, 1 Jul 2023 19:24:25 +0300 Subject: [PATCH] Use equinox preferences APIs in UserStringMappings #497 Replacing deprecated Preferences API usage with modern Equinox Preferences API. Partially fixes https://github.com/eclipse-platform/eclipse.platform/issues/497 --- .../internal/core/FileContentManager.java | 9 +- .../internal/core/UserStringMappings.java | 94 ++++++++++++------- 2 files changed, 69 insertions(+), 34 deletions(-) diff --git a/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/FileContentManager.java b/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/FileContentManager.java index 3faff38e0f4..8e14fac31f5 100644 --- a/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/FileContentManager.java +++ b/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/FileContentManager.java @@ -10,6 +10,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Nikifor Fedorov - Use equinox preferences APIs in UserStringMappings #497 *******************************************************************************/ package org.eclipse.team.internal.core; @@ -30,9 +31,11 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentTypeManager; +import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.team.core.IFileContentManager; import org.eclipse.team.core.IStringMapping; import org.eclipse.team.core.Team; +import org.osgi.service.prefs.BackingStoreException; /** * TODO: implement extension point @@ -73,7 +76,11 @@ public UserExtensionMappings(String key) { protected Map loadMappingsFromPreferences() { final Map result= super.loadMappingsFromPreferences(); if (loadMappingsFromOldWorkspace(result)) { - TeamPlugin.getPlugin().savePluginPreferences(); + try { + InstanceScope.INSTANCE.getNode(TeamPlugin.ID).flush(); + } catch (BackingStoreException e) { + TeamPlugin.log(IStatus.ERROR, e.getMessage(), e); + } } return result; } diff --git a/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/UserStringMappings.java b/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/UserStringMappings.java index cfe1bc0cd34..45d048fd36b 100644 --- a/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/UserStringMappings.java +++ b/team/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/UserStringMappings.java @@ -10,6 +10,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Nikifor Fedorov - Use equinox preferences APIs in UserStringMappings #497 *******************************************************************************/ package org.eclipse.team.internal.core; @@ -18,56 +19,64 @@ import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Optional; import java.util.StringTokenizer; +import java.util.stream.Stream; import org.eclipse.core.runtime.Assert; -import org.eclipse.core.runtime.Preferences; -import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; +import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.team.core.Team; +import org.osgi.service.prefs.BackingStoreException; +public class UserStringMappings { -public class UserStringMappings implements Preferences.IPropertyChangeListener { - - public static final Integer BINARY= Integer.valueOf(Team.BINARY); - public static final Integer TEXT= Integer.valueOf(Team.TEXT); - public static final Integer UNKNOWN= Integer.valueOf(Team.UNKNOWN); - + public static final Integer BINARY = Integer.valueOf(Team.BINARY); + public static final Integer TEXT = Integer.valueOf(Team.TEXT); + public static final Integer UNKNOWN = Integer.valueOf(Team.UNKNOWN); private static final String PREF_TEAM_SEPARATOR = "\n"; //$NON-NLS-1$ + private static final String EMPTY_PREF = ""; //$NON-NLS-1$ - private final Preferences fPreferences; private final String fKey; private Map fMap; public UserStringMappings(String key) { - fKey= key; - fPreferences= TeamPlugin.getPlugin().getPluginPreferences(); - fPreferences.addPropertyChangeListener(this); + fKey = key; + InstanceScope.INSTANCE.getNode(TeamPlugin.ID).addPreferenceChangeListener(this::preferenceChanged); } public Map referenceMap() { if (fMap == null) { - fMap= loadMappingsFromPreferences(); + fMap = loadMappingsFromPreferences(); } return fMap; } public void addStringMappings(String[] names, int[] types) { Assert.isTrue(names.length == types.length); - final Map map= referenceMap(); + final Map map = referenceMap(); for (int i = 0; i < names.length; i++) { switch (types[i]) { - case Team.BINARY: map.put(names[i], BINARY); break; - case Team.TEXT: map.put(names[i], TEXT); break; - case Team.UNKNOWN: map.put(names[i], UNKNOWN); break; + case Team.BINARY: + map.put(names[i], BINARY); + break; + case Team.TEXT: + map.put(names[i], TEXT); + break; + case Team.UNKNOWN: + map.put(names[i], UNKNOWN); + break; } } save(); } - public void setStringMappings(String [] names, int [] types) { + public void setStringMappings(String[] names, int[] types) { Assert.isTrue(names.length == types.length); referenceMap().clear(); addStringMappings(names, types); @@ -76,48 +85,67 @@ public void setStringMappings(String [] names, int [] types) { public int getType(String string) { if (string == null) return Team.UNKNOWN; - final Integer type= referenceMap().get(string); + final Integer type = referenceMap().get(string); return type != null ? type.intValue() : Team.UNKNOWN; } - @Override - public void propertyChange(PropertyChangeEvent event) { - if(event.getProperty().equals(fKey)) - fMap= null; - } - public void save() { // Now set into preferences final StringBuilder buffer = new StringBuilder(); final Iterator e = fMap.keySet().iterator(); while (e.hasNext()) { - final String filename = (String)e.next(); + final String filename = (String) e.next(); buffer.append(filename); buffer.append(PREF_TEAM_SEPARATOR); final Integer type = fMap.get(filename); buffer.append(type); buffer.append(PREF_TEAM_SEPARATOR); } - TeamPlugin.getPlugin().getPluginPreferences().setValue(fKey, buffer.toString()); + try { + IEclipsePreferences node = InstanceScope.INSTANCE.getNode(TeamPlugin.ID); + node.put(fKey, buffer.toString()); + node.flush(); + } catch (BackingStoreException ex) { + TeamPlugin.log(IStatus.ERROR, ex.getMessage(), ex); + } } protected Map loadMappingsFromPreferences() { - final Map result= new HashMap<>(); + final Map result = new HashMap<>(); - if (!fPreferences.contains(fKey)) + if (!nodeAccessibleAndExists(fKey)) return result; - - final String prefTypes = fPreferences.getString(fKey); - final StringTokenizer tok = new StringTokenizer(prefTypes, PREF_TEAM_SEPARATOR); + final StringTokenizer tok = new StringTokenizer(mappings(), PREF_TEAM_SEPARATOR); try { while (tok.hasMoreElements()) { final String name = tok.nextToken(); - final String mode= tok.nextToken(); + final String mode = tok.nextToken(); result.put(name, Integer.valueOf(mode)); } } catch (NoSuchElementException e) { } return result; } + + private String mappings() { + return Optional.ofNullable(InstanceScope.INSTANCE.getNode(TeamPlugin.ID)) // + .map(node -> node.get(fKey, null)) // + .orElse(EMPTY_PREF); + } + + private boolean nodeAccessibleAndExists(String key) { + try { + IEclipsePreferences node = InstanceScope.INSTANCE.getNode(TeamPlugin.ID); + return Stream.of(node.keys()).anyMatch(candidate -> key.equals(candidate)); + } catch (BackingStoreException e) { + TeamPlugin.log(IStatus.ERROR, e.getMessage(), e); + return false; + } + } + + private void preferenceChanged(PreferenceChangeEvent event) { + if (fKey.equals(event.getKey())) + fMap = null; + } }