From c3f9546a473318e099f855d286ba8a12c27c6513 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Thu, 14 Apr 2016 10:26:14 -0400 Subject: [PATCH] Fix removing of targets. Make sure we clear their attributes. Change-Id: Ie2c6e4bd6168fae95db1680143d4c4cafc159369 --- .../internal/target/LaunchTargetManager.java | 50 +++++++++++-------- .../core/tests/TargetAttributesTest.java | 15 +++++- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTargetManager.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTargetManager.java index 6e313b74d1b..4345aa23f85 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTargetManager.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/target/LaunchTargetManager.java @@ -12,7 +12,6 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; @@ -66,7 +65,6 @@ public class LaunchTargetManager implements ILaunchTargetManager { targets = new LinkedHashMap<>(); Preferences prefs = getTargetsPref(); try { - // For backwards compat pre-attributes, load targets from type keys for (String childName : prefs.childrenNames()) { String[] segments = childName.split(DELIMETER1); if (segments.length == 2) { @@ -79,26 +77,30 @@ public class LaunchTargetManager implements ILaunchTargetManager { targets.put(typeId, type); } - // Creates the node. Will flush when attributes are added type.put(name, new LaunchTarget(typeId, name, prefs.node(childName))); } } - for (String typeId : prefs.keys()) { - Map type = targets.get(typeId); - if (type == null) { - type = new LinkedHashMap<>(); - targets.put(typeId, type); - } - - for (String name : prefs.get(typeId, "").split(DELIMETER1)) { //$NON-NLS-1$ - if (!type.containsKey(name)) { - type.put(name, new LaunchTarget(typeId, name, prefs.node(typeId + DELIMETER1 + name))); + // convert old type keys + if (prefs.keys().length > 0) { + for (String typeId : prefs.keys()) { + Map type = targets.get(typeId); + if (type == null) { + type = new LinkedHashMap<>(); + targets.put(typeId, type); } - } - // Use children going forward - prefs.remove(typeId); + for (String name : prefs.get(typeId, "").split(DELIMETER1)) { //$NON-NLS-1$ + if (!type.containsKey(name)) { + type.put(name, new LaunchTarget(typeId, name, prefs.node(typeId + DELIMETER1 + name))); + } + } + + // Use children going forward + prefs.remove(typeId); + } + + prefs.flush(); } } catch (BackingStoreException e) { Activator.log(e); @@ -209,17 +211,21 @@ public class LaunchTargetManager implements ILaunchTargetManager { @Override public void removeLaunchTarget(ILaunchTarget target) { initTargets(); - Map type = targets.get(target.getTypeId()); + String typeId = target.getTypeId(); + Map type = targets.get(typeId); if (type != null) { - type.remove(target.getId()); + type.remove(target); if (type.isEmpty()) { targets.remove(target.getTypeId()); - getTargetsPref().remove(target.getTypeId()); - } else { - getTargetsPref().put(target.getTypeId(), - type.values().stream().map(t -> t.getId()).collect(Collectors.joining(DELIMETER1))); } + // Remove the attribute node + try { + getTargetsPref().node(typeId + DELIMETER1 + target.getId()).removeNode(); + } catch (BackingStoreException e) { + Activator.log(e); + } + for (ILaunchTargetListener listener : listeners) { listener.launchTargetRemoved(target); } diff --git a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/tests/TargetAttributesTest.java b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/tests/TargetAttributesTest.java index 29eff1a2886..4a393b9ebc2 100644 --- a/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/tests/TargetAttributesTest.java +++ b/tests/org.eclipse.launchbar.core.tests/src/org/eclipse/launchbar/core/tests/TargetAttributesTest.java @@ -17,20 +17,31 @@ public class TargetAttributesTest { ILaunchTargetManager manager = Activator.getLaunchTargetManager(); String targetType = "testType"; String targetId = "testTarget"; + String attributeKey = "testKey"; + String attributeValue = "testValue"; + // Make sure the target doesn't exist ILaunchTarget target = manager.getLaunchTarget(targetType, targetId); if (target != null) { manager.removeLaunchTarget(target); } + // Add the target target = manager.addLaunchTarget(targetType, targetId); - String attributeKey = "testKey"; - String attributeValue = "testValue"; + // Attribute should be empty assertEquals(target.getAttribute(attributeKey, ""), ""); + // Set the attribute and make sure it's set ILaunchTargetWorkingCopy wc = target.getWorkingCopy(); assertNotEquals(target, wc); wc.setAttribute(attributeKey, attributeValue); assertEquals(wc.getAttribute(attributeKey, ""), attributeValue); ILaunchTarget savedTarget = wc.save(); + // Make sure we get our original back assertEquals(target, savedTarget); assertEquals(target.getAttribute(attributeKey, ""), attributeValue); + // Make sure remove removes the attribute + manager.removeLaunchTarget(target); + target = manager.addLaunchTarget(targetType, targetId); + assertEquals(target.getAttribute(attributeKey, ""), ""); + // Cleanup + manager.removeLaunchTarget(target); } }