mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-12 11:35:21 +02:00
Add attributes to launch targets.
Follows the working copy pattern to ensure changes to the target are transactional. Change-Id: Iff4fda10bcebc80efa11bc15f75ad7df7d4f4bca
This commit is contained in:
parent
9a170471ad
commit
77fda006a5
7 changed files with 248 additions and 10 deletions
|
@ -9,15 +9,29 @@ package org.eclipse.launchbar.core.internal.target;
|
||||||
|
|
||||||
import org.eclipse.core.runtime.PlatformObject;
|
import org.eclipse.core.runtime.PlatformObject;
|
||||||
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTargetWorkingCopy;
|
||||||
|
import org.osgi.service.prefs.Preferences;
|
||||||
|
|
||||||
public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
||||||
|
|
||||||
private final String typeId;
|
private final String typeId;
|
||||||
private final String id;
|
private final String id;
|
||||||
|
final Preferences attributes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This should only be used to create the null target. There are no attributes supported on the
|
||||||
|
* null target.
|
||||||
|
*/
|
||||||
public LaunchTarget(String typeId, String id) {
|
public LaunchTarget(String typeId, String id) {
|
||||||
this.typeId = typeId;
|
this.typeId = typeId;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.attributes = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LaunchTarget(String typeId, String id, Preferences attributes) {
|
||||||
|
this.typeId = typeId;
|
||||||
|
this.id = id;
|
||||||
|
this.attributes = attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -30,6 +44,20 @@ public class LaunchTarget extends PlatformObject implements ILaunchTarget {
|
||||||
return typeId;
|
return typeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ILaunchTargetWorkingCopy getWorkingCopy() {
|
||||||
|
return new LaunchTargetWorkingCopy(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAttribute(String key, String defValue) {
|
||||||
|
if (attributes != null) {
|
||||||
|
return attributes.get(key, defValue);
|
||||||
|
} else {
|
||||||
|
return defValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
|
|
@ -66,6 +66,24 @@ public class LaunchTargetManager implements ILaunchTargetManager {
|
||||||
targets = new LinkedHashMap<>();
|
targets = new LinkedHashMap<>();
|
||||||
Preferences prefs = getTargetsPref();
|
Preferences prefs = getTargetsPref();
|
||||||
try {
|
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) {
|
||||||
|
String typeId = segments[0];
|
||||||
|
String name = segments[1];
|
||||||
|
|
||||||
|
Map<String, ILaunchTarget> type = targets.get(typeId);
|
||||||
|
if (type == null) {
|
||||||
|
type = new LinkedHashMap<>();
|
||||||
|
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()) {
|
for (String typeId : prefs.keys()) {
|
||||||
Map<String, ILaunchTarget> type = targets.get(typeId);
|
Map<String, ILaunchTarget> type = targets.get(typeId);
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
|
@ -74,9 +92,14 @@ public class LaunchTargetManager implements ILaunchTargetManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String name : prefs.get(typeId, "").split(DELIMETER1)) { //$NON-NLS-1$
|
for (String name : prefs.get(typeId, "").split(DELIMETER1)) { //$NON-NLS-1$
|
||||||
type.put(name, new LaunchTarget(typeId, name));
|
if (!type.containsKey(name)) {
|
||||||
|
type.put(name, new LaunchTarget(typeId, name, prefs.node(typeId + DELIMETER1 + name)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use children going forward
|
||||||
|
prefs.remove(typeId);
|
||||||
|
}
|
||||||
} catch (BackingStoreException e) {
|
} catch (BackingStoreException e) {
|
||||||
Activator.log(e);
|
Activator.log(e);
|
||||||
}
|
}
|
||||||
|
@ -166,11 +189,15 @@ public class LaunchTargetManager implements ILaunchTargetManager {
|
||||||
targets.put(typeId, type);
|
targets.put(typeId, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
ILaunchTarget target = new LaunchTarget(typeId, id);
|
Preferences prefs = getTargetsPref();
|
||||||
|
String childName = typeId + DELIMETER1 + id;
|
||||||
|
ILaunchTarget target = new LaunchTarget(typeId, id, prefs.node(childName));
|
||||||
type.put(id, target);
|
type.put(id, target);
|
||||||
|
try {
|
||||||
getTargetsPref().put(typeId,
|
prefs.flush();
|
||||||
type.values().stream().map(t -> t.getId()).collect(Collectors.joining(DELIMETER1)));
|
} catch (BackingStoreException e) {
|
||||||
|
Activator.log(e);
|
||||||
|
}
|
||||||
|
|
||||||
for (ILaunchTargetListener listener : listeners) {
|
for (ILaunchTargetListener listener : listeners) {
|
||||||
listener.launchTargetAdded(target);
|
listener.launchTargetAdded(target);
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2016 QNX Software Systems and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.launchbar.core.internal.target;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.eclipse.core.runtime.PlatformObject;
|
||||||
|
import org.eclipse.launchbar.core.internal.Activator;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTargetWorkingCopy;
|
||||||
|
import org.osgi.service.prefs.BackingStoreException;
|
||||||
|
|
||||||
|
public class LaunchTargetWorkingCopy extends PlatformObject implements ILaunchTargetWorkingCopy {
|
||||||
|
|
||||||
|
private final LaunchTarget original;
|
||||||
|
private final Map<String, String> changes = new HashMap<>();
|
||||||
|
|
||||||
|
public LaunchTargetWorkingCopy(LaunchTarget original) {
|
||||||
|
this.original = original;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ILaunchTarget getOriginal() {
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTypeId() {
|
||||||
|
return original.getTypeId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getId() {
|
||||||
|
return original.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ILaunchTargetWorkingCopy getWorkingCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAttribute(String key, String defValue) {
|
||||||
|
if (changes.containsKey(key)) {
|
||||||
|
return changes.get(key);
|
||||||
|
} else {
|
||||||
|
return original.getAttribute(key, defValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAttribute(String key, String value) {
|
||||||
|
changes.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ILaunchTarget save() {
|
||||||
|
try {
|
||||||
|
for (Map.Entry<String, String> entry : changes.entrySet()) {
|
||||||
|
original.attributes.put(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
original.attributes.flush();
|
||||||
|
} catch (BackingStoreException e) {
|
||||||
|
Activator.log(e);
|
||||||
|
}
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,10 +7,12 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.launchbar.core.internal.target;
|
package org.eclipse.launchbar.core.internal.target;
|
||||||
|
|
||||||
|
import org.eclipse.core.runtime.Platform;
|
||||||
import org.eclipse.launchbar.core.internal.Messages;
|
import org.eclipse.launchbar.core.internal.Messages;
|
||||||
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
||||||
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
|
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
|
||||||
import org.eclipse.launchbar.core.target.ILaunchTargetProvider;
|
import org.eclipse.launchbar.core.target.ILaunchTargetProvider;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTargetWorkingCopy;
|
||||||
import org.eclipse.launchbar.core.target.TargetStatus;
|
import org.eclipse.launchbar.core.target.TargetStatus;
|
||||||
|
|
||||||
public class LocalLaunchTargetProvider implements ILaunchTargetProvider {
|
public class LocalLaunchTargetProvider implements ILaunchTargetProvider {
|
||||||
|
@ -19,7 +21,11 @@ public class LocalLaunchTargetProvider implements ILaunchTargetProvider {
|
||||||
public void init(ILaunchTargetManager targetManager) {
|
public void init(ILaunchTargetManager targetManager) {
|
||||||
if (targetManager.getLaunchTarget(ILaunchTargetManager.localLaunchTargetTypeId,
|
if (targetManager.getLaunchTarget(ILaunchTargetManager.localLaunchTargetTypeId,
|
||||||
Messages.LocalTarget_name) == null) {
|
Messages.LocalTarget_name) == null) {
|
||||||
targetManager.addLaunchTarget(ILaunchTargetManager.localLaunchTargetTypeId, Messages.LocalTarget_name);
|
ILaunchTarget target = targetManager.addLaunchTarget(ILaunchTargetManager.localLaunchTargetTypeId, Messages.LocalTarget_name);
|
||||||
|
ILaunchTargetWorkingCopy wc = target.getWorkingCopy();
|
||||||
|
wc.setAttribute(ILaunchTarget.ATTR_OS, Platform.getOS());
|
||||||
|
wc.setAttribute(ILaunchTarget.ATTR_ARCH, Platform.getOSArch());
|
||||||
|
wc.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2015 QNX Software Systems and others.
|
* Copyright (c) 2015, 2016 QNX Software Systems and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -11,13 +11,21 @@ import org.eclipse.core.runtime.IAdaptable;
|
||||||
import org.eclipse.launchbar.core.internal.target.LaunchTarget;
|
import org.eclipse.launchbar.core.internal.target.LaunchTarget;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A launch target is a thing that a launch will run on. Launch targets are
|
* A launch target is a thing that a launch will run on. Launch targets are simple objects with the
|
||||||
* simple objects with the intention that the launch delegates and launches will
|
* intention that the launch delegates and launches will adapt this object to an object that will
|
||||||
* adapt this object to an object that will assist in performing the launch.
|
* assist in performing the launch.
|
||||||
*
|
*
|
||||||
* @noimplement not to be implemented by clients
|
* @noimplement not to be implemented by clients
|
||||||
*/
|
*/
|
||||||
public interface ILaunchTarget extends IAdaptable {
|
public interface ILaunchTarget extends IAdaptable {
|
||||||
|
|
||||||
|
// Standard attributes
|
||||||
|
public static final String ATTR_OS = "os"; //$NON-NLS-1$
|
||||||
|
public static final String ATTR_ARCH = "arch"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The null target, which is the default when no other target is available.
|
||||||
|
*/
|
||||||
public static final ILaunchTarget NULL_TARGET = new LaunchTarget("null", "---"); //$NON-NLS-1$ //$NON-NLS-2$
|
public static final ILaunchTarget NULL_TARGET = new LaunchTarget("null", "---"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,4 +53,23 @@ public interface ILaunchTarget extends IAdaptable {
|
||||||
*/
|
*/
|
||||||
String getTypeId();
|
String getTypeId();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a string attribute of this target
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* key
|
||||||
|
* @param defValue
|
||||||
|
* default value
|
||||||
|
* @return value of attribute
|
||||||
|
*/
|
||||||
|
String getAttribute(String key, String defValue);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a working copy of this launch target to allow setting of attributes. Note that when
|
||||||
|
* saving the attributes this target is updated. A new one is not created.
|
||||||
|
*
|
||||||
|
* @return launch target working copy
|
||||||
|
*/
|
||||||
|
ILaunchTargetWorkingCopy getWorkingCopy();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2016 QNX Software Systems and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.launchbar.core.target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Working copy to set attributes on a target and then save them.
|
||||||
|
*/
|
||||||
|
public interface ILaunchTargetWorkingCopy extends ILaunchTarget {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the original launch target.
|
||||||
|
*
|
||||||
|
* @return the original launch target
|
||||||
|
*/
|
||||||
|
ILaunchTarget getOriginal();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set an attribute.
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* key
|
||||||
|
* @param value
|
||||||
|
* value
|
||||||
|
*/
|
||||||
|
void setAttribute(String key, String value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save the changed attributes to the original working copy.
|
||||||
|
*
|
||||||
|
* @return original launch target
|
||||||
|
*/
|
||||||
|
ILaunchTarget save();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package org.eclipse.launchbar.core.tests;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
import org.eclipse.launchbar.core.internal.Activator;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTarget;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
|
||||||
|
import org.eclipse.launchbar.core.target.ILaunchTargetWorkingCopy;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
public class TargetAttributesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAttribute() {
|
||||||
|
ILaunchTargetManager manager = Activator.getLaunchTargetManager();
|
||||||
|
String targetType = "testType";
|
||||||
|
String targetId = "testTarget";
|
||||||
|
ILaunchTarget target = manager.getLaunchTarget(targetType, targetId);
|
||||||
|
if (target != null) {
|
||||||
|
manager.removeLaunchTarget(target);
|
||||||
|
}
|
||||||
|
target = manager.addLaunchTarget(targetType, targetId);
|
||||||
|
String attributeKey = "testKey";
|
||||||
|
String attributeValue = "testValue";
|
||||||
|
assertEquals(target.getAttribute(attributeKey, ""), "");
|
||||||
|
ILaunchTargetWorkingCopy wc = target.getWorkingCopy();
|
||||||
|
assertNotEquals(target, wc);
|
||||||
|
wc.setAttribute(attributeKey, attributeValue);
|
||||||
|
assertEquals(wc.getAttribute(attributeKey, ""), attributeValue);
|
||||||
|
ILaunchTarget savedTarget = wc.save();
|
||||||
|
assertEquals(target, savedTarget);
|
||||||
|
assertEquals(target.getAttribute(attributeKey, ""), attributeValue);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue