Skip to content

Commit

Permalink
Use equinox preferences APIs in UserStringMappings eclipse-platform#497
Browse files Browse the repository at this point in the history
Replacing deprecated Preferences API usage with modern Equinox
Preferences API.

Partially fixes
eclipse-platform#497
  • Loading branch information
zelenyhleb committed Jul 14, 2023
1 parent 972581d commit abf098b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -73,7 +76,11 @@ public UserExtensionMappings(String key) {
protected Map<String, Integer> loadMappingsFromPreferences() {
final Map<String, Integer> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Integer> 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<String, Integer> 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<String, Integer> map= referenceMap();
final Map<String, Integer> 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);
Expand All @@ -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<String, Integer> loadMappingsFromPreferences() {
final Map<String, Integer> result= new HashMap<>();
final Map<String, Integer> 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;
}
}

0 comments on commit abf098b

Please sign in to comment.