1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +02:00

LaunchBar - added interface to notify launch bar on object changes

* added launchObjectUpdated API
* added tests for it
* changed abstract provider to use it instead of hack that it used
before
* fixed login in launchObjectAdded to correctly update object map 

Change-Id: I697594c34097e5439ca1550d0d68758baf7208e2
Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
Reviewed-on: https://git.eclipse.org/r/30830
Tested-by: Hudson CI
This commit is contained in:
Alena Laskavaia 2014-07-31 16:57:12 -04:00 committed by Elena Laskavaia
parent 86a08c918e
commit 0b62fee46c
4 changed files with 64 additions and 28 deletions

View file

@ -334,6 +334,15 @@ public class LaunchBarManagerTest extends TestCase {
assertEquals(2, manager.getLaunchDescriptors().length);
}
@Test
public void testLaunchObjectChanged() throws CoreException {
basicSetup();
assertNull(manager.launchObjectChanged(lc));
manager.launchObjectAdded(lc);
assertEquals(1, manager.getLaunchDescriptors().length);
assertNotNull(manager.launchObjectChanged(lc));
}
public IProject mockProject(String projectName) {
IProject project = mock(Project.class);
when(project.getAdapter(IResource.class)).thenReturn(project);

View file

@ -50,6 +50,8 @@ public interface ILaunchBarManager extends IAdaptable {
void launchObjectRemoved(Object element) throws CoreException;
ILaunchDescriptor launchObjectChanged(Object element) throws CoreException;
interface Listener {
void activeConfigurationDescriptorChanged();

View file

@ -27,7 +27,7 @@ public abstract class ProjectBasedLaunchConfigurationProvider extends ConfigBase
public boolean launchConfigurationAdded(ILaunchConfiguration configuration) throws CoreException {
boolean res = super.launchConfigurationAdded(configuration);
IProject project = getProject(configuration);
getManager().launchObjectRemoved(project);
getManager().launchObjectChanged(project);
return res;
}
@ -35,7 +35,7 @@ public abstract class ProjectBasedLaunchConfigurationProvider extends ConfigBase
public boolean launchConfigurationRemoved(ILaunchConfiguration configuration) throws CoreException {
boolean res = super.launchConfigurationRemoved(configuration);
IProject project = getProject(configuration);
getManager().launchObjectAdded(project);
getManager().launchObjectChanged(project);
return res;
}

View file

@ -232,33 +232,54 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
return null;
}
@Override
public ILaunchDescriptor launchObjectAdded(Object element) {
protected ILaunchDescriptor remapLaunchObject(Object element) {
// remove old mapping. We have to do it anyway, no matter even nobody own it (and especially because of that)
ILaunchDescriptor old = objectDescriptorMap.get(element);
if (old != null) { // old mapping is removed
objectDescriptorMap.remove(element);
descriptors.remove(getId(old));
}
ILaunchDescriptor desc = null;
// don't use objectDescriptorMap as cache - provider may decide to remap objects
// re-do the mapping, change in object can change descriptor (for example project has new nature)
for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) {
try {
if (descriptorType.ownsLaunchObject(element)) {
desc = descriptorType.getDescriptor(element);
ILaunchDescriptor old = objectDescriptorMap.get(element);
if (old != null) { // old mapping is removed
objectDescriptorMap.remove(element);
descriptors.remove(getId(old));
}
if (desc != null) { // null if we own the object but do not create descriptor to ignore it
objectDescriptorMap.put(element, desc); // set it even if desc is null to keep object is map
if (desc != null) // null if we own the object but do not create descriptor
descriptors.put(getId(desc), desc);
objectDescriptorMap.put(element, desc);
break;
}
break;
}
} catch (Exception e) {
Activator.log(e);
}
}
if (desc != null)
if (old != null && old.equals(activeLaunchDesc) && !old.equals(desc)) {
// change of object caused changed of descriptor, which was active, reset since old is invalid now
// setting it to null, will actually rollback to last used one which is still valid
setActiveLaunchDescriptor(desc);
} else if (old == null && desc != null) {
// new object causes re-set of active descriptor too
setActiveLaunchDescriptor(desc);
}
return desc;
}
@Override
public ILaunchDescriptor launchObjectAdded(Object element) {
if (objectDescriptorMap.containsKey(element)) {
// it was added already, perform change
return launchObjectChanged(element);
}
return remapLaunchObject(element);
}
@Override
public ILaunchDescriptor launchObjectChanged(Object element) {
// only applied to object that were added via launchObjectAdded
if (!objectDescriptorMap.containsKey(element))
return null;
return remapLaunchObject(element);
}
private String getId(ILaunchDescriptor desc) {
@ -270,8 +291,8 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
@Override
public void launchObjectRemoved(Object element) {
ILaunchDescriptor desc = objectDescriptorMap.get(element);
objectDescriptorMap.remove(element);
if (desc != null) {
objectDescriptorMap.remove(element);
descriptors.remove(getId(desc)); // can multiple elements maps to the same descriptor?
if (desc.equals(activeLaunchDesc)) {
// Roll back to the last one and make sure we don't come back
@ -366,27 +387,32 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
ILaunchMode[] supportedModes = getLaunchModes(); // this is based on active desc and target which are already set
if (supportedModes.length > 0) { // mna, what if no modes are supported?
String modeNames[] = new String[] {
storedModeId, lastActiveModeId,
"debug", "run",
storedModeId,
lastActiveModeId,
"debug",
"run",
supportedModes[0].getIdentifier()
};
for (int i = 0; i < modeNames.length && foundMode == null; i++) {
foundMode = getLaunchModeByName(modeNames[i], supportedModes);
for (int i = 0; i < modeNames.length; i++) {
foundMode = getLaunchManager().getLaunchMode(modeNames[i]);
if (supportsMode(foundMode))
break;
}
}
setActiveLaunchMode(foundMode);
}
private ILaunchMode getLaunchModeByName(String mode, ILaunchMode[] supportedModes) {
public boolean supportsMode(ILaunchMode mode) {
// check that active descriptor supports the given mode
if (mode == null)
return null;
return false;
ILaunchMode[] supportedModes = getLaunchModes();
for (int j = 0; j < supportedModes.length; j++) {
ILaunchMode lm = supportedModes[j];
if (lm.getIdentifier().equals(mode)) {
return lm;
}
if (lm.equals(mode))
return true;
}
return null;
return false;
}
protected void setPreference(Preferences store, String prefId, String value) {
@ -447,8 +473,7 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
public void setActiveLaunchMode(ILaunchMode mode) {
if (activeLaunchMode == mode)
return;
ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget);
if (!(activeLaunchDesc == null || mode == null || (configType != null && configType.supportsMode(mode.getIdentifier()))))
if (activeLaunchDesc != null && mode != null && !supportsMode(mode))
throw new IllegalStateException("Mode is not supported by descriptor");
// change mode
activeLaunchMode = mode;