1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-11 18:25:40 +02:00

LaunchBar - junits - more tweaks and refactoring

Change-Id: Ia786fd63c07b3240c1535036b443675dd4931eca
Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
Reviewed-on: https://git.eclipse.org/r/30732
Tested-by: Hudson CI
This commit is contained in:
Alena Laskavaia 2014-07-28 15:55:37 -04:00 committed by Elena Laskavaia
parent 05795e449e
commit 86864c02fe
9 changed files with 648 additions and 206 deletions

View file

@ -8,6 +8,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.junit;bundle-version="4.11.0", org.junit;bundle-version="4.11.0",
org.mockito, org.mockito,
org.eclipse.cdt.launchbar.core;bundle-version="1.0.0", org.eclipse.cdt.launchbar.core;bundle-version="1.0.0",
org.eclipse.debug.core org.eclipse.debug.core,
org.eclipse.cdt.debug.core,
org.eclipse.core.filesystem
Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy Bundle-ActivationPolicy: lazy

View file

@ -14,6 +14,8 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.launchbar.core.internal; package org.eclipse.cdt.launchbar.core.internal;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -22,6 +24,7 @@ import java.util.HashSet;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.launchbar.core.AbstractLaunchTarget; import org.eclipse.cdt.launchbar.core.AbstractLaunchTarget;
import org.eclipse.cdt.launchbar.core.AbstractLaunchTargetType; import org.eclipse.cdt.launchbar.core.AbstractLaunchTargetType;
import org.eclipse.cdt.launchbar.core.ConfigBasedLaunchConfigurationProvider; import org.eclipse.cdt.launchbar.core.ConfigBasedLaunchConfigurationProvider;
@ -33,10 +36,17 @@ import org.eclipse.cdt.launchbar.core.ILaunchDescriptor;
import org.eclipse.cdt.launchbar.core.ILaunchDescriptorType; import org.eclipse.cdt.launchbar.core.ILaunchDescriptorType;
import org.eclipse.cdt.launchbar.core.ILaunchTarget; import org.eclipse.cdt.launchbar.core.ILaunchTarget;
import org.eclipse.cdt.launchbar.core.ILaunchTargetType; import org.eclipse.cdt.launchbar.core.ILaunchTargetType;
import org.eclipse.cdt.launchbar.core.ProjectBasedLaunchConfigurationProvider;
import org.eclipse.cdt.launchbar.core.ProjectBasedLaunchDescriptorType;
import org.eclipse.core.internal.preferences.EclipsePreferences; import org.eclipse.core.internal.preferences.EclipsePreferences;
import org.eclipse.core.internal.resources.Project;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationType;
@ -52,11 +62,11 @@ public class LaunchBarManagerTest extends TestCase {
private LaunchBarManager manager; private LaunchBarManager manager;
private ILaunchConfigurationProvider provider; private ILaunchConfigurationProvider provider;
private ILaunchDescriptor desc; private ILaunchDescriptor desc;
private ILaunchDescriptorType descType; ILaunchDescriptorType descType;
private ILaunchConfigurationType lctype; private ILaunchConfigurationType lctype;
private ILaunchConfiguration lc; private ILaunchConfiguration lc;
class TargetType extends AbstractLaunchTargetType { public class TargetType extends AbstractLaunchTargetType {
private String id; private String id;
ArrayList<ILaunchTarget> targets = new ArrayList<>(); ArrayList<ILaunchTarget> targets = new ArrayList<>();
@ -90,6 +100,8 @@ public class LaunchBarManagerTest extends TestCase {
} }
private ILaunchTarget mytarget = new LaunchTarget("target_1", targetType); private ILaunchTarget mytarget = new LaunchTarget("target_1", targetType);
private ILaunchManager lman; private ILaunchManager lman;
private IProject aaa;
private ArrayList<ILaunchMode> globalmodes = new ArrayList<>();
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@ -97,6 +109,8 @@ public class LaunchBarManagerTest extends TestCase {
doReturn(new IExtension[] {}).when(point).getExtensions(); doReturn(new IExtension[] {}).when(point).getExtensions();
lman = mock(ILaunchManager.class); lman = mock(ILaunchManager.class);
doReturn(new ILaunchConfiguration[] {}).when(lman).getLaunchConfigurations(); doReturn(new ILaunchConfiguration[] {}).when(lman).getLaunchConfigurations();
doReturn(globalmodes.toArray(new ILaunchMode[globalmodes.size()])).when(lman).getLaunchModes();
final IEclipsePreferences store = new EclipsePreferences(); final IEclipsePreferences store = new EclipsePreferences();
manager = new LaunchBarManager() { manager = new LaunchBarManager() {
@Override @Override
@ -119,7 +133,7 @@ public class LaunchBarManagerTest extends TestCase {
lctype = mockLCType("lctype1"); lctype = mockLCType("lctype1");
lc = mockLC("bla", lctype); lc = mockLC("bla", lctype);
// other init // other init
provider = spy(new ConfigBasedLaunchConfigurationProvider(lctype.getIdentifier())); provider = new ConfigBasedLaunchConfigurationProvider(lctype.getIdentifier());
descType = new ConfigBasedLaunchDescriptorType("desctype1", lctype.getIdentifier()); descType = new ConfigBasedLaunchDescriptorType("desctype1", lctype.getIdentifier());
desc = new ConfigBasedLaunchDescriptor(descType, lc); desc = new ConfigBasedLaunchDescriptor(descType, lc);
} }
@ -145,7 +159,7 @@ public class LaunchBarManagerTest extends TestCase {
return lctype; return lctype;
} }
protected ILaunchMode[] mockLaunchMode(ILaunchConfigurationType type, String... modes) { protected ILaunchMode[] mockLaunchModes(ILaunchConfigurationType type, String... modes) {
ILaunchMode res[] = new ILaunchMode[modes.length]; ILaunchMode res[] = new ILaunchMode[modes.length];
for (int i = 0; i < modes.length; i++) { for (int i = 0; i < modes.length; i++) {
String modeId = modes[i]; String modeId = modes[i];
@ -154,8 +168,10 @@ public class LaunchBarManagerTest extends TestCase {
res[i] = mode; res[i] = mode;
doReturn(modeId).when(mode).getIdentifier(); doReturn(modeId).when(mode).getIdentifier();
doReturn(mode).when(lman).getLaunchMode(modeId); doReturn(mode).when(lman).getLaunchMode(modeId);
globalmodes.add(mode);
} }
doReturn(new HashSet<>(Arrays.asList(modes))).when(type).getSupportedModes(); doReturn(new HashSet<>(Arrays.asList(modes))).when(type).getSupportedModes();
doReturn(globalmodes.toArray(new ILaunchMode[globalmodes.size()])).when(lman).getLaunchModes();
return res; return res;
} }
@ -172,6 +188,25 @@ public class LaunchBarManagerTest extends TestCase {
manager.addConfigProvider(descType.getId(), targetType.getId(), false, provider); manager.addConfigProvider(descType.getId(), targetType.getId(), false, provider);
} }
@Test
public void testAddConfigProviderBad() throws CoreException {
provider = spy(provider);
doThrow(new NullPointerException()).when(provider).init(manager);
manager.addTargetType(targetType);
manager.addDescriptorType(descType, 5);
manager.addConfigProvider(descType.getId(), targetType.getId(), false, provider);
verify(provider).init(manager);
}
@Test
public void testAddDescriptorTypeBad() throws CoreException {
descType = spy(descType);
doThrow(new NullPointerException()).when(descType).init(manager);
manager.addDescriptorType(descType, 5);
verify(descType).init(manager);
}
@Test @Test
public void testAddConfigProviderNoTarget() { public void testAddConfigProviderNoTarget() {
try { try {
@ -211,12 +246,7 @@ public class LaunchBarManagerTest extends TestCase {
public void testAddConfigProviderTwo2() throws CoreException { public void testAddConfigProviderTwo2() throws CoreException {
manager.addTargetType(targetType); manager.addTargetType(targetType);
manager.addDescriptorType(descType, 5); manager.addDescriptorType(descType, 5);
ILaunchConfigurationProvider provider2 = new ConfigBasedLaunchConfigurationProvider("type2") { ILaunchConfigurationProvider provider2 = new ConfigBasedLaunchConfigurationProvider("type2");
@Override
public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor descriptor) throws CoreException {
return lctype;
}
};
manager.addConfigProvider(descType.getId(), targetType.getId(), true, provider2); manager.addConfigProvider(descType.getId(), targetType.getId(), true, provider2);
TargetType targetType2 = new TargetType("t2"); TargetType targetType2 = new TargetType("t2");
manager.addTargetType(targetType2); manager.addTargetType(targetType2);
@ -224,18 +254,29 @@ public class LaunchBarManagerTest extends TestCase {
ILaunchConfigurationType lctype2 = mockLCType("lctypeid2"); ILaunchConfigurationType lctype2 = mockLCType("lctypeid2");
ILaunchConfiguration lc2 = mockLC("bla2", lctype2); ILaunchConfiguration lc2 = mockLC("bla2", lctype2);
ConfigBasedLaunchDescriptor desc2 = new ConfigBasedLaunchDescriptor(descType, lc2); ConfigBasedLaunchDescriptor desc2 = new ConfigBasedLaunchDescriptor(descType, lc2);
assertEquals(lctype, manager.getLaunchConfigurationType(desc2, null)); assertEquals(lctype2, manager.getLaunchConfigurationType(desc2, null));
} }
@Test @Test
public void testGetLaunchTargets() throws CoreException { public void testGetLaunchTargets() throws CoreException {
basicSetup(); basicSetup();
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(desc); manager.setActiveLaunchDescriptor(desc);
ILaunchTarget[] launchTargets = manager.getLaunchTargets(); ILaunchTarget[] launchTargets = manager.getLaunchTargets();
assertEquals(1, launchTargets.length); assertEquals(1, launchTargets.length);
assertEquals(mytarget, launchTargets[0]); assertEquals(mytarget, launchTargets[0]);
} }
@Test
public void testGetLaunchTargetsNoProvider() throws CoreException {
manager.addTargetType(targetType);
targetType.targets.add(mytarget);
manager.addDescriptorType(descType, 5);
manager.launchObjectAdded(lc);
ILaunchTarget[] launchTargets = manager.getLaunchTargets();
assertEquals(0, launchTargets.length);
}
@Test @Test
public void testGetLaunchDescriptorsNull() { public void testGetLaunchDescriptorsNull() {
ILaunchDescriptor[] launchDescriptors = manager.getLaunchDescriptors(); ILaunchDescriptor[] launchDescriptors = manager.getLaunchDescriptors();
@ -274,7 +315,6 @@ public class LaunchBarManagerTest extends TestCase {
assertSame(res[0], launchDescriptors[0]); assertSame(res[0], launchDescriptors[0]);
} }
@Test @Test
public void testLaunchObjectAdded() throws CoreException { public void testLaunchObjectAdded() throws CoreException {
basicSetup(); basicSetup();
@ -294,6 +334,131 @@ public class LaunchBarManagerTest extends TestCase {
assertEquals(2, manager.getLaunchDescriptors().length); assertEquals(2, manager.getLaunchDescriptors().length);
} }
public IProject mockProject(String projectName) {
IProject project = mock(Project.class);
when(project.getAdapter(IResource.class)).thenReturn(project);
when(project.getProject()).thenReturn(project);
when(project.getName()).thenReturn(projectName);
IPath path = new Path(projectName);
when(project.getFullPath()).thenReturn(path);
when(project.getType()).thenReturn(IResource.PROJECT);
return project;
}
public ILaunchConfiguration mockLCProject(ILaunchConfiguration lc, String projectName) {
mockLCAttribute(lc, ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, projectName);
return lc;
}
public ILaunchConfiguration mockLCBinary(ILaunchConfiguration lc, String binname) {
mockLCAttribute(lc, ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, binname);
return lc;
}
public ILaunchConfiguration mockLCAttribute(ILaunchConfiguration lc, String attr, String value) {
try {
when(lc.getAttribute(eq(attr), anyString())).thenReturn(value);
} catch (CoreException e) {
throw new RuntimeException(e);
}
return lc;
}
protected String getProjectName(ILaunchConfiguration llc) {
try {
return llc.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
} catch (CoreException e) {
//
}
return "";
}
IProject getProjectByName(String p) {
if (p.equals("aaa"))
return aaa;
return mockProject(p);
}
/**
* This scenario when provider will change mapping element->descriptor, depending on other objects involved
*/
@Test
public void testLaunchObjectAddedRemapping() throws CoreException {
projectMappingSetup();
// user created a project
manager.launchObjectAdded(aaa);
assertEquals(1, manager.getLaunchDescriptors().length);
assertEquals(aaa.getName(), manager.getLaunchDescriptors()[0].getName());
// user clicked on descriptor geer to edit lc, new lc is created
manager.launchConfigurationAdded(lc);
assertEquals(1, manager.getLaunchDescriptors().length);
assertEquals(lc.getName(), manager.getLaunchDescriptors()[0].getName());
// user cloned lc and changed some settings
ILaunchConfiguration lc2 = mockLC("lc2", lctype);
mockLCProject(lc2, aaa.getName());
manager.launchConfigurationAdded(lc2);
assertEquals(2, manager.getLaunchDescriptors().length);
// user deleted lc
manager.launchConfigurationRemoved(lc2);
assertEquals(1, manager.getLaunchDescriptors().length);
// user deleted last lc, now we back to project default
manager.launchConfigurationRemoved(lc);
assertEquals(1, manager.getLaunchDescriptors().length);
}
protected void projectMappingSetup() {
descType = new ProjectBasedLaunchDescriptorType("desc2", lctype.getIdentifier()) {
protected IProject getProject(ILaunchConfiguration llc) {
return getProjectByName(getProjectName(llc));
}
@Override
protected boolean ownsProject(IProject element) {
return true;
}
};
provider = new ProjectBasedLaunchConfigurationProvider(lctype.getIdentifier()) {
protected IProject getProject(ILaunchConfiguration llc) {
return getProjectByName(getProjectName(llc));
}
};
desc = null;
basicSetup();
aaa = mockProject("aaa");
mockLCProject(lc, aaa.getName());
assertEquals(0, manager.getLaunchDescriptors().length);
}
public void testLaunchObjectAddedRemapping2() throws CoreException {
projectMappingSetup();
// test unmapping
manager.launchObjectAdded(aaa);
manager.launchObjectAdded(lc);
assertEquals(2, manager.getLaunchDescriptors().length);
manager.launchObjectAdded(aaa); // that would remove aaa mapping since lc is already there
assertEquals(1, manager.getLaunchDescriptors().length);
assertEquals(lc.getName(), manager.getLaunchDescriptors()[0].getName());
}
@Test
public void testLaunchObjectAddedBadDescriptor() throws CoreException {
descType = spy(descType);
basicSetup();
doThrow(new NullPointerException()).when(descType).ownsLaunchObject(any());
// check events
manager.launchObjectAdded(lc);
verify(descType).ownsLaunchObject(lc);
}
@Test
public void testLaunchObjectRemoveBadDescriptor() throws CoreException {
descType = spy(descType);
basicSetup();
doThrow(new NullPointerException()).when(descType).ownsLaunchObject(any());
// check events
manager.launchObjectRemoved(lc);
}
@Test @Test
public void testLaunchObjectRemoved() throws CoreException { public void testLaunchObjectRemoved() throws CoreException {
basicSetup(); basicSetup();
@ -305,14 +470,52 @@ public class LaunchBarManagerTest extends TestCase {
@Test @Test
public void testGetActiveLaunchDescriptor() throws CoreException { public void testGetActiveLaunchDescriptor() throws CoreException {
basicSetup();
Listener lis = mock(Listener.class); Listener lis = mock(Listener.class);
manager.addListener(lis); manager.addListener(lis);
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(desc); // manager.setActiveLaunchDescriptor(desc);
assertEquals(desc, manager.getActiveLaunchDescriptor()); assertEquals(desc, manager.getActiveLaunchDescriptor());
verify(lis).activeConfigurationDescriptorChanged(); verify(lis).activeConfigurationDescriptorChanged();
} }
@Test
public void testSetActiveLaunchDescriptorUnkn() throws CoreException {
basicSetup();
try {
manager.setActiveLaunchDescriptor(desc);
fail();
} catch (Exception e) {
// pass
}
}
@Test
public void testSetActiveLaunchDescriptorNullBad() throws CoreException {
basicSetup();
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(null);
assertEquals(desc, manager.getActiveLaunchDescriptor());
}
@Test
public void testSetActiveLaunchDescriptorLisBad() throws CoreException {
basicSetup();
Listener lis = mock(Listener.class);
manager.addListener(lis);
doThrow(new NullPointerException()).when(lis).activeConfigurationDescriptorChanged();
manager.launchConfigurationAdded(lc);
verify(lis).activeConfigurationDescriptorChanged();
}
@Test
public void testSetActiveLaunchDescriptorNull() throws CoreException {
basicSetup();
manager.launchObjectAdded(lc);
manager.launchObjectRemoved(lc);
manager.setActiveLaunchDescriptor(null);
}
@Test @Test
public void testGetLaunchModes() throws CoreException { public void testGetLaunchModes() throws CoreException {
ILaunchMode[] launchModes = manager.getLaunchModes(); ILaunchMode[] launchModes = manager.getLaunchModes();
@ -320,7 +523,20 @@ public class LaunchBarManagerTest extends TestCase {
} }
@Test @Test
public void testGetActiveLaunchMode() { public void testGetLaunchModesFew() throws CoreException {
basicSetup();
ILaunchConfigurationType lctype2 = mockLCType("lctype2");
mockLaunchModes(lctype2, "modex");
mockLaunchModes(lctype, "run", "debug", "foo");
manager.launchConfigurationAdded(lc);
ILaunchMode[] launchModes = manager.getLaunchModes();
assertEquals(3, launchModes.length);
}
@Test
public void testSetActiveLaunchMode() {
ILaunchMode mode = mock(ILaunchMode.class); ILaunchMode mode = mock(ILaunchMode.class);
doReturn("bla").when(mode).getIdentifier(); doReturn("bla").when(mode).getIdentifier();
doReturn("Bla").when(mode).getLabel(); doReturn("Bla").when(mode).getLabel();
@ -328,10 +544,55 @@ public class LaunchBarManagerTest extends TestCase {
assertEquals(mode, manager.getActiveLaunchMode()); assertEquals(mode, manager.getActiveLaunchMode());
} }
@Test
public void testSetActiveLaunchModeNull() {
manager.setActiveLaunchMode(null);
assertEquals(null, manager.getActiveLaunchMode());
}
@Test
public void testSetActiveLaunchModeUnsupported() {
basicSetup();
ILaunchConfigurationType lctype2 = mockLCType("lctype2");
ILaunchMode mode = mockLaunchModes(lctype2, "modex")[0];
mockLaunchModes(lctype, "run", "debug", "foo");
manager.launchConfigurationAdded(lc);
try {
manager.setActiveLaunchMode(mode);
fail();
} catch (Exception e) {
// works
assertNotEquals(mode, manager.getActiveLaunchMode());
}
}
@Test
public void testSetActiveLaunchModeLis() {
ILaunchMode mode = mock(ILaunchMode.class);
doReturn("bla").when(mode).getIdentifier();
Listener lis = mock(Listener.class);
manager.addListener(lis);
manager.setActiveLaunchMode(mode);
manager.setActiveLaunchMode(null);
verify(lis, times(2)).activeLaunchModeChanged();
}
@Test
public void testSetActiveLaunchModeLisBad() {
ILaunchMode mode = mock(ILaunchMode.class);
doReturn("bla").when(mode).getIdentifier();
Listener lis = mock(Listener.class);
manager.addListener(lis);
doThrow(new NullPointerException()).when(lis).activeLaunchModeChanged();
manager.setActiveLaunchMode(mode);
verify(lis).activeLaunchModeChanged();
}
@Test @Test
public void testGetActiveLaunchModeFromDesc() throws CoreException { public void testGetActiveLaunchModeFromDesc() throws CoreException {
basicSetup(); basicSetup();
mockLaunchMode(lctype, "run"); mockLaunchModes(lctype, "run");
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(desc); manager.setActiveLaunchDescriptor(desc);
ILaunchMode resmode = manager.getActiveLaunchMode(); ILaunchMode resmode = manager.getActiveLaunchMode();
assertNotNull(resmode); assertNotNull(resmode);
@ -341,7 +602,8 @@ public class LaunchBarManagerTest extends TestCase {
@Test @Test
public void testGetActiveLaunchModeFromDescDebug() throws CoreException { public void testGetActiveLaunchModeFromDescDebug() throws CoreException {
basicSetup(); basicSetup();
mockLaunchMode(lctype, "run", "debug"); mockLaunchModes(lctype, "run", "debug");
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(desc); manager.setActiveLaunchDescriptor(desc);
ILaunchMode resmode = manager.getActiveLaunchMode(); ILaunchMode resmode = manager.getActiveLaunchMode();
assertNotNull(resmode); assertNotNull(resmode);
@ -351,8 +613,9 @@ public class LaunchBarManagerTest extends TestCase {
@Test @Test
public void testGetActiveLaunchModeFromDescActive() throws CoreException { public void testGetActiveLaunchModeFromDescActive() throws CoreException {
basicSetup(); basicSetup();
mockLaunchMode(lctype, "run"); mockLaunchModes(lctype, "run");
ILaunchMode mode = mockLaunchMode(lctype, "foo")[0]; ILaunchMode mode = mockLaunchModes(lctype, "foo")[0];
manager.launchObjectAdded(lc);
manager.setActiveLaunchMode(mode); manager.setActiveLaunchMode(mode);
manager.setActiveLaunchDescriptor(desc); manager.setActiveLaunchDescriptor(desc);
ILaunchMode resmode = manager.getActiveLaunchMode(); ILaunchMode resmode = manager.getActiveLaunchMode();
@ -368,6 +631,16 @@ public class LaunchBarManagerTest extends TestCase {
assertEquals(mytarget, manager.getActiveLaunchTarget()); assertEquals(mytarget, manager.getActiveLaunchTarget());
} }
@Test
public void testAddTargetTypeBad() {
targetType = spy(targetType);
doThrow(new NullPointerException()).when(targetType).init(manager);
manager.addTargetType(targetType);
targetType.targets.add(mytarget);
assertEquals(mytarget, manager.getLaunchTarget(mytarget.getId()));
verify(targetType).init(manager);
}
@Test @Test
public void testGetLaunchTarget() { public void testGetLaunchTarget() {
manager.addTargetType(targetType); manager.addTargetType(targetType);
@ -397,9 +670,11 @@ public class LaunchBarManagerTest extends TestCase {
manager.addListener(lis); manager.addListener(lis);
basicSetup(); basicSetup();
// check events // check events
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(desc); manager.setActiveLaunchDescriptor(desc);
verify(lis).activeLaunchTargetChanged(); verify(lis).activeLaunchTargetChanged();
} }
@Test @Test
public void testAddListenerBad() throws CoreException { public void testAddListenerBad() throws CoreException {
Listener lis = mock(Listener.class); Listener lis = mock(Listener.class);
@ -407,6 +682,7 @@ public class LaunchBarManagerTest extends TestCase {
doThrow(new NullPointerException()).when(lis).activeLaunchTargetChanged(); doThrow(new NullPointerException()).when(lis).activeLaunchTargetChanged();
basicSetup(); basicSetup();
// check events // check events
manager.launchObjectAdded(lc);
manager.setActiveLaunchDescriptor(desc); manager.setActiveLaunchDescriptor(desc);
verify(lis).activeLaunchTargetChanged(); verify(lis).activeLaunchTargetChanged();
} }
@ -422,8 +698,9 @@ public class LaunchBarManagerTest extends TestCase {
@Test @Test
public void testLaunchConfigurationAdded() throws CoreException { public void testLaunchConfigurationAdded() throws CoreException {
provider = spy(provider);
basicSetup(); basicSetup();
ILaunchMode mode = mockLaunchMode(lctype, "foo")[0]; ILaunchMode mode = mockLaunchModes(lctype, "foo")[0];
manager.launchConfigurationAdded(lc); manager.launchConfigurationAdded(lc);
verify(provider).launchConfigurationAdded(lc); verify(provider).launchConfigurationAdded(lc);
ILaunchDescriptor[] launchDescriptors = manager.getLaunchDescriptors(); ILaunchDescriptor[] launchDescriptors = manager.getLaunchDescriptors();
@ -436,6 +713,7 @@ public class LaunchBarManagerTest extends TestCase {
} }
public void testLaunchConfigurationAddedDefault() throws CoreException { public void testLaunchConfigurationAddedDefault() throws CoreException {
provider = spy(provider);
basicSetup(); basicSetup();
// emulate default type (if running not from plugin) // emulate default type (if running not from plugin)
LocalTargetType localType = new LocalTargetType(); LocalTargetType localType = new LocalTargetType();
@ -458,6 +736,7 @@ public class LaunchBarManagerTest extends TestCase {
@Test @Test
public void testLaunchConfigurationRemoved() throws CoreException { public void testLaunchConfigurationRemoved() throws CoreException {
provider = spy(provider);
basicSetup(); basicSetup();
manager.launchConfigurationRemoved(lc); manager.launchConfigurationRemoved(lc);
verify(provider).launchConfigurationRemoved(lc); verify(provider).launchConfigurationRemoved(lc);

View file

@ -49,4 +49,9 @@ public abstract class AbstarctLaunchDescriptorType implements ILaunchDescriptorT
return false; return false;
return true; return true;
} }
@Override
public String toString() {
return getId();
}
} }

View file

@ -15,7 +15,7 @@ import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationType;
/** /**
* Abstract provider can work with any ITypeBaseLaunchDescriptor to provide launch configurations * Abstract provider can work with any ILaunchDescriptorConfigBased to provide launch configurations
*/ */
public abstract class AbstractLaunchConfigurationProvider implements ILaunchConfigurationProvider { public abstract class AbstractLaunchConfigurationProvider implements ILaunchConfigurationProvider {
protected ILaunchBarManager manager; protected ILaunchBarManager manager;

View file

@ -12,6 +12,12 @@ package org.eclipse.cdt.launchbar.core;
import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfiguration;
/**
* Interface for descriptors which are based on launch configurations
*/
public interface ILaunchDescriptorConfigBased extends ILaunchDescriptor { public interface ILaunchDescriptorConfigBased extends ILaunchDescriptor {
/**
* Return launch configuration on which it is based
*/
public ILaunchConfiguration getConfig(); public ILaunchConfiguration getConfig();
} }

View file

@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2014 QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit [http://licensing.qnx.com] or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at [http://licensing.qnx.com/license-guide/]
* for other information.
*******************************************************************************/
package org.eclipse.cdt.launchbar.core;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
public abstract class ProjectBasedLaunchConfigurationProvider extends ConfigBasedLaunchConfigurationProvider {
public ProjectBasedLaunchConfigurationProvider(String launchConfigurationTypeId) {
super(launchConfigurationTypeId);
}
@Override
public boolean launchConfigurationAdded(ILaunchConfiguration configuration) throws CoreException {
boolean res = super.launchConfigurationAdded(configuration);
IProject project = getProject(configuration);
getManager().launchObjectRemoved(project);
return res;
}
@Override
public boolean launchConfigurationRemoved(ILaunchConfiguration configuration) throws CoreException {
boolean res = super.launchConfigurationRemoved(configuration);
IProject project = getProject(configuration);
getManager().launchObjectAdded(project);
return res;
}
protected abstract IProject getProject(ILaunchConfiguration llc);
}

View file

@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2014 QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit [http://licensing.qnx.com] or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at [http://licensing.qnx.com/license-guide/]
* for other information.
*******************************************************************************/
package org.eclipse.cdt.launchbar.core;
import org.eclipse.core.resources.IProject;
import org.eclipse.debug.core.ILaunchConfiguration;
public class ProjectBasedLaunchDescriptor extends ConfigBasedLaunchDescriptor {
private IProject project;
public ProjectBasedLaunchDescriptor(ILaunchDescriptorType type, IProject p, ILaunchConfiguration lc) {
super(type, lc);
this.project = p;
}
public String getName() {
if (getConfig() == null)
return project.getName();
else
return getConfig().getName();
}
public IProject getProject() {
return project;
}
}

View file

@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2014 QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit [http://licensing.qnx.com] or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at [http://licensing.qnx.com/license-guide/]
* for other information.
*******************************************************************************/
package org.eclipse.cdt.launchbar.core;
import org.eclipse.cdt.launchbar.core.internal.Activator;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
public abstract class ProjectBasedLaunchDescriptorType extends ConfigBasedLaunchDescriptorType {
public ProjectBasedLaunchDescriptorType(String descTypeId, String launchConfigTypeId) {
super(descTypeId, launchConfigTypeId);
}
@Override
public boolean ownsLaunchObject(Object element) {
if (super.ownsLaunchObject(element))
return true;
if (element instanceof IProject && ownsProject((IProject) element))
return true;
return false;
}
protected abstract boolean ownsProject(IProject element);
@Override
public ILaunchDescriptor getDescriptor(Object element) {
if (element instanceof ILaunchConfiguration) {
ILaunchConfiguration llc = (ILaunchConfiguration) element;
IProject project = getProject(llc);
return new ProjectBasedLaunchDescriptor(this, project, llc);
} else if (element instanceof IProject) {
try {
ILaunchDescriptor[] lds = getManager().getLaunchDescriptors();
for (int i = 0; i < lds.length; i++) {
ILaunchDescriptor ld = lds[i];
if (ld instanceof ProjectBasedLaunchDescriptor
&& element.equals(((ProjectBasedLaunchDescriptor) ld).getProject())) {
return null; // somebody else has it
}
}
} catch (CoreException e) {
Activator.log(e);
}
return new ProjectBasedLaunchDescriptor(this, (IProject) element, null);
}
return null;
}
protected abstract IProject getProject(ILaunchConfiguration llc);
}

View file

@ -19,7 +19,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.eclipse.cdt.launchbar.core.ConfigBasedLaunchConfigurationProvider; import org.eclipse.cdt.launchbar.core.ConfigBasedLaunchConfigurationProvider;
import org.eclipse.cdt.launchbar.core.ConfigBasedLaunchDescriptorType; import org.eclipse.cdt.launchbar.core.ConfigBasedLaunchDescriptorType;
@ -49,30 +48,28 @@ import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences; import org.osgi.service.prefs.Preferences;
public class LaunchBarManager extends PlatformObject implements ILaunchBarManager, ILaunchConfigurationListener { public class LaunchBarManager extends PlatformObject implements ILaunchBarManager, ILaunchConfigurationListener {
private List<Listener> listeners = new LinkedList<>(); private List<Listener> listeners = new LinkedList<>();
private Map<String, ILaunchTargetType> targetTypes = new HashMap<>(); private Map<String, ILaunchTargetType> targetTypes = new HashMap<>();
private LinkedHashMap<ILaunchDescriptorType, Integer> descriptorTypes = new LinkedHashMap<>(); private LinkedHashMap<ILaunchDescriptorType, Integer> descriptorTypes = new LinkedHashMap<>();
private Map<String, ILaunchDescriptor> descriptors = new HashMap<>(); private final Map<String, ILaunchDescriptor> descriptors = new LinkedHashMap<>();
private List<ILaunchObjectProvider> objectProviders = new ArrayList<>(); private List<ILaunchObjectProvider> objectProviders = new ArrayList<>();
// Map descriptor type to target type to provider // Map descriptor type to target type to provider
private Map<String, Map<String, ILaunchConfigurationProvider>> configProviders = new HashMap<>(); private Map<String, Map<String, ILaunchConfigurationProvider>> configProviders = new HashMap<>();
// Map descriptor type to target type // Map descriptor type to target type
private Map<String, String> defaultTargetTypes = new HashMap<>(); private Map<String, String> defaultTargetTypes = new HashMap<>();
private Map<Object, ILaunchDescriptor> objectDescriptorMap = new HashMap<>(); private Map<Object, ILaunchDescriptor> objectDescriptorMap = new HashMap<>();
private ILaunchDescriptor activeLaunchDesc; private ILaunchDescriptor activeLaunchDesc;
private ILaunchMode activeLaunchMode; private ILaunchMode activeLaunchMode;
private ILaunchTarget activeLaunchTarget; private ILaunchTarget activeLaunchTarget;
private ILaunchDescriptor lastLaunchDesc;
private static final String PREF_ACTIVE_CONFIG_DESC = "activeConfigDesc"; private static final String PREF_ACTIVE_CONFIG_DESC = "activeConfigDesc";
private static final String PREF_ACTIVE_LAUNCH_MODE = "activeLaunchMode"; private static final String PREF_ACTIVE_LAUNCH_MODE = "activeLaunchMode";
private static final String PREF_ACTIVE_LAUNCH_TARGET = "activeLaunchTarget"; private static final String PREF_ACTIVE_LAUNCH_TARGET = "activeLaunchTarget";
public LaunchBarManager() throws CoreException { public LaunchBarManager() throws CoreException {
// Load up the active from the preferences before loading the descriptors
IEclipsePreferences store = getPreferenceStore();
String activeConfigDescId = store.get(PREF_ACTIVE_CONFIG_DESC, null);
IExtensionPoint point = getExtensionPoint(); IExtensionPoint point = getExtensionPoint();
IExtension[] extensions = point.getExtensions(); IExtension[] extensions = point.getExtensions();
// first pass - target, descriptors and object providers // first pass - target, descriptors and object providers
@ -85,7 +82,6 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
String priorityStr = element.getAttribute("priority"); String priorityStr = element.getAttribute("priority");
ILaunchDescriptorType type = (ILaunchDescriptorType) element.createExecutableExtension("class"); ILaunchDescriptorType type = (ILaunchDescriptorType) element.createExecutableExtension("class");
assert id.equals(type.getId()); assert id.equals(type.getId());
int priority = 1; int priority = 1;
if (priorityStr != null) { if (priorityStr != null) {
try { try {
@ -140,40 +136,21 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
// Load up the active from the preferences before loading the descriptors
IEclipsePreferences store = getPreferenceStore();
String activeConfigDescId = store.get(PREF_ACTIVE_CONFIG_DESC, null);
for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) {
descriptorType.init(this);
}
for (ILaunchTargetType targetType : targetTypes.values()) {
targetType.init(this);
}
for (ILaunchObjectProvider objectProvider : objectProviders) { for (ILaunchObjectProvider objectProvider : objectProviders) {
objectProvider.init(this); objectProvider.init(this);
} }
// Hook up the existing launch configurations and listen // Hook up the existing launch configurations and listen
ILaunchManager launchManager = getLaunchManager(); ILaunchManager launchManager = getLaunchManager();
for (ILaunchConfiguration configuration : launchManager.getLaunchConfigurations()) { for (ILaunchConfiguration configuration : launchManager.getLaunchConfigurations()) {
launchConfigurationAdded(configuration); launchConfigurationAdded(configuration);
} }
launchManager.addLaunchConfigurationListener(this); launchManager.addLaunchConfigurationListener(this);
// Now that all the descriptors are loaded, set the one // Now that all the descriptors are loaded, set the one
if (activeConfigDescId == null && !descriptors.isEmpty()) { ILaunchDescriptor configDesc = descriptors.get(activeConfigDescId);
activeConfigDescId = getId(descriptors.values().iterator().next()); if (configDesc == null) {
} configDesc = getLastUsedDescriptor();
if (activeConfigDescId != null) {
ILaunchDescriptor configDesc = descriptors.get(activeConfigDescId);
if (configDesc != null) {
setActiveLaunchDescriptor(configDesc);
}
} }
setActiveLaunchDescriptor(configDesc);
} }
protected static void sortMapByValue(LinkedHashMap<ILaunchDescriptorType, Integer> map) { protected static void sortMapByValue(LinkedHashMap<ILaunchDescriptorType, Integer> map) {
@ -193,24 +170,34 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
public void addDescriptorType(ILaunchDescriptorType type, int priority) { public void addDescriptorType(ILaunchDescriptorType type, int priority) {
descriptorTypes.put(type, priority); descriptorTypes.put(type, priority);
sortMapByValue(descriptorTypes); sortMapByValue(descriptorTypes);
} try {
type.init(this);
} catch (Exception e) {
Activator.log(e);
}
}
/** /**
* Programmatically add target type * Programmatically add target type
*/ */
public void addTargetType(ILaunchTargetType targetType) { public void addTargetType(ILaunchTargetType targetType) {
targetTypes.put(targetType.getId(), targetType); targetTypes.put(targetType.getId(), targetType);
try {
targetType.init(this);
} catch (Exception e) {
Activator.log(e);
}
} }
protected ILaunchManager getLaunchManager() { protected ILaunchManager getLaunchManager() {
return DebugPlugin.getDefault().getLaunchManager(); return DebugPlugin.getDefault().getLaunchManager();
} }
protected IExtensionPoint getExtensionPoint() { protected IExtensionPoint getExtensionPoint() {
return Platform.getExtensionRegistry().getExtensionPoint(Activator.PLUGIN_ID, "launchBarContributions"); return Platform.getExtensionRegistry().getExtensionPoint(Activator.PLUGIN_ID, "launchBarContributions");
} }
/** /**
* Programmatically add launch configuration provider * Programmatically add launch configuration provider
@ -232,7 +219,7 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
try { try {
configProvider.init(this); configProvider.init(this);
} catch (CoreException e) { } catch (Exception e) {
Activator.log(e); Activator.log(e);
} }
} }
@ -247,33 +234,31 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
@Override @Override
public ILaunchDescriptor launchObjectAdded(Object element) { public ILaunchDescriptor launchObjectAdded(Object element) {
ILaunchDescriptor desc = objectDescriptorMap.get(element); ILaunchDescriptor desc = null;
if (desc != null) // don't use objectDescriptorMap as cache - provider may decide to remap objects
return desc;
for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) { for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) {
try { try {
if (descriptorType.ownsLaunchObject(element)) { if (descriptorType.ownsLaunchObject(element)) {
desc = descriptorType.getDescriptor(element); desc = descriptorType.getDescriptor(element);
if (desc != null) { // own the object but do not create descriptor to ignore it ILaunchDescriptor old = objectDescriptorMap.get(element);
String id = getId(desc); if (old != null) { // old mapping is removed
ILaunchDescriptor old = descriptors.get(id); objectDescriptorMap.remove(element);
if (old != null && !desc.equals(old)) descriptors.remove(getId(old));
Activator.log(new IllegalStateException( }
"Id of descriptor must be unique within same type " if (desc != null) { // null if we own the object but do not create descriptor to ignore it
+ "(or descriptors with same id must be equal)")); descriptors.put(getId(desc), desc);
descriptors.put(id, desc);
objectDescriptorMap.put(element, desc); objectDescriptorMap.put(element, desc);
setActiveLaunchDescriptor(desc); break;
return desc;
} }
break; break;
} }
} catch (CoreException e) { } catch (Exception e) {
Activator.log(e.getStatus()); Activator.log(e);
} }
} }
return null; if (desc != null)
setActiveLaunchDescriptor(desc);
return desc;
} }
private String getId(ILaunchDescriptor desc) { private String getId(ILaunchDescriptor desc) {
@ -283,20 +268,26 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
@Override @Override
public void launchObjectRemoved(Object element) throws CoreException { public void launchObjectRemoved(Object element) {
ILaunchDescriptor desc = objectDescriptorMap.get(element); ILaunchDescriptor desc = objectDescriptorMap.get(element);
if (desc != null) { if (desc != null) {
descriptors.remove(getId(desc));
objectDescriptorMap.remove(element); objectDescriptorMap.remove(element);
descriptors.remove(getId(desc)); // can multiple elements maps to the same descriptor?
if (desc.equals(activeLaunchDesc)) { if (desc.equals(activeLaunchDesc)) {
// Roll back to the last one and make sure we don't come back // Roll back to the last one and make sure we don't come back
ILaunchDescriptor nextDesc = lastLaunchDesc; ILaunchDescriptor nextDesc = getLastUsedDescriptor();
activeLaunchDesc = null;
setActiveLaunchDescriptor(nextDesc); setActiveLaunchDescriptor(nextDesc);
} }
} }
} }
protected ILaunchDescriptor getLastUsedDescriptor() {
if (descriptors.size() == 0)
return null;
ILaunchDescriptor[] descs = descriptors.values().toArray(new ILaunchDescriptor[descriptors.size()]);
return descs[descs.length - 1];
}
@Override @Override
public ILaunchDescriptor[] getLaunchDescriptors() { public ILaunchDescriptor[] getLaunchDescriptors() {
ILaunchDescriptor[] descs = descriptors.values().toArray(new ILaunchDescriptor[descriptors.size()]); ILaunchDescriptor[] descs = descriptors.values().toArray(new ILaunchDescriptor[descriptors.size()]);
@ -315,62 +306,87 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
@Override @Override
public void setActiveLaunchDescriptor(ILaunchDescriptor configDesc) throws CoreException { public void setActiveLaunchDescriptor(ILaunchDescriptor configDesc) {
if (activeLaunchDesc == configDesc) if (activeLaunchDesc == configDesc) {
// Sync since targets could be changed since last time (and modes theoretically too)
syncActiveTarget();
syncActiveMode();
return; return;
lastLaunchDesc = activeLaunchDesc; }
if (configDesc != null && !descriptors.containsValue(configDesc))
throw new IllegalStateException("Active descriptor must be in the map of descriptors");
if (configDesc == null)
configDesc = getLastUsedDescriptor(); // do not set to null unless no descriptors
activeLaunchDesc = configDesc; activeLaunchDesc = configDesc;
IEclipsePreferences store = getPreferenceStore(); if (configDesc != null) { // keeps most used descriptor last
setPreference(store, PREF_ACTIVE_CONFIG_DESC, getId(activeLaunchDesc)); descriptors.remove(configDesc.getId());
descriptors.put(configDesc.getId(), configDesc);
}
// store in persistent storage
setPreference(getPreferenceStore(), PREF_ACTIVE_CONFIG_DESC, getId(activeLaunchDesc));
// Send notifications // Send notifications
for (Listener listener : listeners) { updateLaunchDescriptor(activeLaunchDesc);
try { // Set active target
listener.activeConfigurationDescriptorChanged(); syncActiveTarget();
} catch (Exception e) { // Set active mode
Activator.log(e); syncActiveMode();
} }
}
protected void syncActiveTarget() {
if (activeLaunchDesc == null) { if (activeLaunchDesc == null) {
setActiveLaunchMode(null);
setActiveLaunchTarget(null); setActiveLaunchTarget(null);
return; return;
} }
String activeTargetId = getPerDescriptorStore().get(PREF_ACTIVE_LAUNCH_TARGET, null);
// Set active target ILaunchTarget targets[] = new ILaunchTarget[] {
String activeTargetId = store.node(activeLaunchDesc.getId()).get(PREF_ACTIVE_LAUNCH_TARGET, null); activeLaunchTarget, // current active
String lastTargetId = store.get(PREF_ACTIVE_LAUNCH_TARGET, null); getLaunchTarget(activeTargetId), // last stored target from persistent storage
String targetIds[] = new String[] { activeTargetId, lastTargetId }; getDeafultLaunchTarget() // default target for this desc
};
ILaunchTarget target = null; ILaunchTarget target = null;
for (int i = 0; i < targetIds.length; i++) { for (int i = 0; i < targets.length; i++) {
String targetId = targetIds[i]; target = targets[i];
target = getLaunchTarget(targetId);
if (target != null && supportsTargetType(activeLaunchDesc, target.getType())) { if (target != null && supportsTargetType(activeLaunchDesc, target.getType())) {
break; break;
} }
} }
setActiveLaunchTarget(target); // if target is null this will pick default setActiveLaunchTarget(target);
}
// Set active mode protected void syncActiveMode() {
ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget); if (activeLaunchDesc == null) {
ILaunchMode foundMode = null; setActiveLaunchMode(null);
if (configType != null) { return;
String activeModeName = store.node(activeLaunchDesc.getId()).get(PREF_ACTIVE_LAUNCH_MODE, null); // last desc mode name }
String lastModeName = store.get(PREF_ACTIVE_LAUNCH_MODE, null); // last global mode name ILaunchMode foundMode = null;
Set<String> supportedModes = configType.getSupportedModes(); String storedModeId = getPerDescriptorStore().get(PREF_ACTIVE_LAUNCH_MODE, null); // last desc mode id
if (supportedModes.size() > 0) { // mna, what if no modes are supported? String lastActiveModeId = activeLaunchMode == null ? null : activeLaunchMode.getIdentifier();
ILaunchManager launchManager = getLaunchManager(); ILaunchMode[] supportedModes = getLaunchModes(); // this is based on active desc and target which are already set
String modeNames[] = new String[] { activeModeName, lastModeName, "debug", "run", supportedModes.iterator().next() }; if (supportedModes.length > 0) { // mna, what if no modes are supported?
for (int i = 0; i < modeNames.length && foundMode == null; i++) { String modeNames[] = new String[] {
String mode = modeNames[i]; storedModeId, lastActiveModeId,
if (mode != null && supportedModes.contains(mode)) { "debug", "run",
foundMode = launchManager.getLaunchMode(mode); supportedModes[0].getIdentifier()
} };
} for (int i = 0; i < modeNames.length && foundMode == null; i++) {
foundMode = getLaunchModeByName(modeNames[i], supportedModes);
} }
} }
setActiveLaunchMode(foundMode); setActiveLaunchMode(foundMode);
}
private ILaunchMode getLaunchModeByName(String mode, ILaunchMode[] supportedModes) {
if (mode == null)
return null;
for (int j = 0; j < supportedModes.length; j++) {
ILaunchMode lm = supportedModes[j];
if (lm.getIdentifier().equals(mode)) {
return lm;
}
}
return null;
} }
protected void setPreference(Preferences store, String prefId, String value) { protected void setPreference(Preferences store, String prefId, String value) {
@ -386,6 +402,12 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
} }
protected Preferences getPerDescriptorStore() {
if (activeLaunchDesc == null)
return getPreferenceStore();
return getPreferenceStore().node(activeLaunchDesc.getId());
}
protected IEclipsePreferences getPreferenceStore() { protected IEclipsePreferences getPreferenceStore() {
return InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID); return InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
} }
@ -393,16 +415,19 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
@Override @Override
public void updateLaunchDescriptor(ILaunchDescriptor configDesc) { public void updateLaunchDescriptor(ILaunchDescriptor configDesc) {
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.activeConfigurationDescriptorChanged(); try {
listener.activeConfigurationDescriptorChanged();
} catch (Exception e) {
Activator.log(e);
}
} }
} }
@Override @Override
public ILaunchMode[] getLaunchModes() throws CoreException { public ILaunchMode[] getLaunchModes() {
ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget); ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget);
if (configType == null) if (configType == null)
return new ILaunchMode[0]; return new ILaunchMode[0];
List<ILaunchMode> modeList = new ArrayList<>(); List<ILaunchMode> modeList = new ArrayList<>();
ILaunchMode[] modes = getLaunchManager().getLaunchModes(); ILaunchMode[] modes = getLaunchManager().getLaunchModes();
for (ILaunchMode mode : modes) { for (ILaunchMode mode : modes) {
@ -410,7 +435,6 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
modeList.add(mode); modeList.add(mode);
} }
} }
return modeList.toArray(new ILaunchMode[modeList.size()]); return modeList.toArray(new ILaunchMode[modeList.size()]);
} }
@ -423,17 +447,11 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
public void setActiveLaunchMode(ILaunchMode mode) { public void setActiveLaunchMode(ILaunchMode mode) {
if (activeLaunchMode == mode) if (activeLaunchMode == mode)
return; return;
try { ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget);
ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget); if (!(activeLaunchDesc == null || mode == null || (configType != null && configType.supportsMode(mode.getIdentifier()))))
if (!(activeLaunchDesc == null || mode == null || (configType != null && configType.supportsMode(mode.getIdentifier())))) throw new IllegalStateException("Mode is not supported by descriptor");
throw new IllegalStateException("Mode is not supported by descriptor");
} catch (CoreException e) {
Activator.log(e);
return;
}
// change mode // change mode
activeLaunchMode = mode; activeLaunchMode = mode;
// notify listeners // notify listeners
for (Listener listener : listeners) { for (Listener listener : listeners) {
try { try {
@ -442,12 +460,11 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
Activator.log(e); Activator.log(e);
} }
} }
// store mode
String modeId = mode == null ? null : mode.getIdentifier();
setPreference(getPreferenceStore(), PREF_ACTIVE_LAUNCH_MODE, modeId); // global store
if (activeLaunchDesc == null) if (activeLaunchDesc == null)
return; return;
setPreference(getPreferenceStore().node(activeLaunchDesc.getId()), PREF_ACTIVE_LAUNCH_MODE, modeId); // per desc store // store mode
String modeId = mode == null ? null : mode.getIdentifier();
setPreference(getPerDescriptorStore(), PREF_ACTIVE_LAUNCH_MODE, modeId); // per desc store
} }
@Override @Override
@ -458,7 +475,6 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
public ILaunchTarget[] getLaunchTargets(ILaunchDescriptor desc) { public ILaunchTarget[] getLaunchTargets(ILaunchDescriptor desc) {
if (desc == null) if (desc == null)
return new ILaunchTarget[0]; return new ILaunchTarget[0];
List<ILaunchTarget> targetList = new ArrayList<>(); List<ILaunchTarget> targetList = new ArrayList<>();
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(desc.getType().getId()); Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(desc.getType().getId());
if (targetMap != null) { if (targetMap != null) {
@ -483,12 +499,27 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
@Override @Override
public void setActiveLaunchTarget(ILaunchTarget target) { public void setActiveLaunchTarget(ILaunchTarget target) {
if (target == null) { if (target == null) {
// try and select another target // try and select another target XXX this should not be an API
target = getDeafultLaunchTarget(); target = getDeafultLaunchTarget();
} }
if (activeLaunchTarget == target) if (activeLaunchTarget == target)
return; return;
activeLaunchTarget = target; activeLaunchTarget = target;
updateLaunchTarget(activeLaunchTarget);
if (target == null) {
return; // no point storing null, if stored id is invalid it won't be used anyway
}
target.setActive();
if (activeLaunchDesc == null)
return;
// per desc store
if (supportsTargetType(activeLaunchDesc, target.getType()))
setPreference(getPerDescriptorStore(),
PREF_ACTIVE_LAUNCH_TARGET, target.getId());
}
@Override
public void updateLaunchTarget(ILaunchTarget target) {
for (Listener listener : listeners) { for (Listener listener : listeners) {
try { try {
listener.activeLaunchTargetChanged(); listener.activeLaunchTargetChanged();
@ -496,34 +527,16 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
Activator.log(e); Activator.log(e);
} }
} }
if (target == null) {
return; // no point storing null, if stored id is invalid it won't be used anyway
}
target.setActive();
// global store
setPreference(getPreferenceStore(), PREF_ACTIVE_LAUNCH_TARGET, target.getId());
if (activeLaunchDesc == null)
return;
// per desc store
if (supportsTargetType(activeLaunchDesc, target.getType()))
setPreference(getPreferenceStore().node(activeLaunchDesc.getName()),
PREF_ACTIVE_LAUNCH_TARGET, target.getId());
}
@Override
public void updateLaunchTarget(ILaunchTarget target) {
for (Listener listener : listeners)
listener.activeLaunchTargetChanged();
} }
protected ILaunchTarget getDeafultLaunchTarget() { protected ILaunchTarget getDeafultLaunchTarget() {
ILaunchTarget target = null; ILaunchTarget target = null;
ILaunchTarget[] targets = getLaunchTargets(); ILaunchTarget[] targets = getLaunchTargets();
if (targets.length > 0) { if (targets.length > 0) {
target = targets[0]; target = targets[0];
} }
return target; return target;
} }
@Override @Override
public ILaunchTarget getLaunchTarget(String targetId) { public ILaunchTarget getLaunchTarget(String targetId) {
@ -538,23 +551,27 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
} }
@Override @Override
public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException { public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor descriptor, ILaunchTarget target) {
if (descriptor == null) if (descriptor == null)
return null; return null;
try {
String descriptorTypeId = descriptor.getType().getId(); if (descriptor instanceof ILaunchDescriptorConfigBased) {
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorTypeId); // if descriptor is launch config based we don't need provider to determine the type
if (targetMap != null) { return ((ILaunchDescriptorConfigBased) descriptor).getConfig().getType();
String targetTypeId = target != null ? target.getType().getId() : defaultTargetTypes.get(descriptorTypeId); }
if (targetTypeId != null) { String descriptorTypeId = descriptor.getType().getId();
ILaunchConfigurationProvider configProvider = targetMap.get(targetTypeId); Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorTypeId);
if (configProvider != null) { if (targetMap != null) {
return configProvider.getLaunchConfigurationType(descriptor); String targetTypeId = target != null ? target.getType().getId() : defaultTargetTypes.get(descriptorTypeId);
if (targetTypeId != null) {
ILaunchConfigurationProvider configProvider = targetMap.get(targetTypeId);
if (configProvider != null) {
return configProvider.getLaunchConfigurationType(descriptor);
}
} }
} }
} } catch (Exception e) {
if (descriptor instanceof ILaunchDescriptorConfigBased) { Activator.log(e); // we calling provider code inside try block, better be safe
return ((ILaunchDescriptorConfigBased) descriptor).getConfig().getType();
} }
return null; return null;
} }
@ -562,25 +579,17 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
public boolean supportsTargetType(ILaunchDescriptor descriptor, ILaunchTargetType targetType) { public boolean supportsTargetType(ILaunchDescriptor descriptor, ILaunchTargetType targetType) {
if (descriptor == null || targetType == null) if (descriptor == null || targetType == null)
return false; return false;
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptor.getType().getId());
String descriptorTypeId = descriptor.getType().getId();
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorTypeId);
if (targetMap != null) { if (targetMap != null) {
String targetTypeId = targetType.getId(); return targetMap.get(targetType.getId()) != null;
ILaunchConfigurationProvider configProvider = targetMap.get(targetTypeId);
if (configProvider != null) {
return true;
}
} }
return false; return false;
} }
@Override @Override
public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException { public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException {
if (activeLaunchDesc == null || target == null) if (descriptor == null || target == null)
return null; return null;
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptor.getType().getId()); Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptor.getType().getId());
if (targetMap != null) { if (targetMap != null) {
ILaunchConfigurationProvider configProvider = targetMap.get(target.getType().getId()); ILaunchConfigurationProvider configProvider = targetMap.get(target.getType().getId());
@ -603,46 +612,43 @@ public class LaunchBarManager extends PlatformObject implements ILaunchBarManage
@Override @Override
public void launchConfigurationAdded(ILaunchConfiguration configuration) { public void launchConfigurationAdded(ILaunchConfiguration configuration) {
try { // TODO filter by launch configuration type to avoid loading plug-ins
// TODO filter by launch configuration type to avoid loading plug-ins for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) {
for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) { Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorType.getId());
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorType.getId()); for (ILaunchConfigurationProvider configProvider : targetMap.values()) {
for (ILaunchConfigurationProvider configProvider : targetMap.values()) { try {
if (configProvider.launchConfigurationAdded(configuration)) { if (configProvider.launchConfigurationAdded(configuration)) {
return; return;
} }
} catch (Exception e) {
Activator.log(e); // don't let one bad provider affect the rest
} }
} }
// No one claimed it, send it through the descriptorTypes
launchObjectAdded(configuration);
} catch (CoreException e) {
Activator.log(e.getStatus());
} }
// No one claimed it, send it through the descriptorTypes
launchObjectAdded(configuration);
} }
@Override @Override
public void launchConfigurationRemoved(ILaunchConfiguration configuration) { public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
try { // TODO filter by launch configuration type
// TODO filter by launch configuration type for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) {
for (ILaunchDescriptorType descriptorType : descriptorTypes.keySet()) { Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorType.getId());
Map<String, ILaunchConfigurationProvider> targetMap = configProviders.get(descriptorType.getId()); for (ILaunchConfigurationProvider configProvider : targetMap.values()) {
for (ILaunchConfigurationProvider configProvider : targetMap.values()) { try {
if (configProvider.launchConfigurationRemoved(configuration)) { if (configProvider.launchConfigurationRemoved(configuration)) {
return; return;
} }
} catch (Exception e) {
Activator.log(e); // don't let one bad provider affect the rest
} }
} }
launchObjectRemoved(configuration);
} catch (CoreException e) {
Activator.log(e.getStatus());
} }
launchObjectRemoved(configuration);
} }
@Override @Override
public void launchConfigurationChanged(ILaunchConfiguration configuration) { public void launchConfigurationChanged(ILaunchConfiguration configuration) {
// Nothing to do on changes // Nothing to do on changes
} }
} }