Eclipse Public License - v 1.0
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...
If you think the Android project m2e-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*******************************************************************************
* Copyright (c) 2014 Ricardo Gladwell/*www.java2s.com*/
* 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 me.gladwell.eclipse.m2e.android;
importstatic org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER;
importstatic org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationListener;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.project.IMavenProjectChangedListener;
import org.eclipse.m2e.core.project.MavenProjectChangedEvent;
import com.android.ide.eclipse.adt.AdtConstants;
publicclass AndroidMavenLaunchConfigurationListener implements ILaunchConfigurationListener,
IMavenProjectChangedListener {
privatestaticfinal String ANDROID_TEST_CLASSPATH_PROVIDER = "me.gladwell.m2e.android.classpathProvider";
publicvoid launchConfigurationAdded(ILaunchConfiguration configuration) {
updateLaunchConfiguration(configuration);
}
publicvoid launchConfigurationChanged(ILaunchConfiguration configuration) {
updateLaunchConfiguration(configuration);
}
publicvoid launchConfigurationRemoved(ILaunchConfiguration configuration) {
}
privatevoid updateLaunchConfiguration(ILaunchConfiguration configuration) {
try {
String projectName = configuration.getAttribute(ATTR_PROJECT_NAME, (String) null);
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (!configuration.getAttributes().containsValue(ANDROID_TEST_CLASSPATH_PROVIDER)
&& configuration.getType().getIdentifier().equals("org.eclipse.jdt.junit.launchconfig")
&& project.hasNature(AdtConstants.NATURE_DEFAULT) && project.hasNature(IMavenConstants.NATURE_ID)) {
final ILaunchConfigurationWorkingCopy workingCopy = configuration.getWorkingCopy();
workingCopy.setAttribute(ATTR_CLASSPATH_PROVIDER, ANDROID_TEST_CLASSPATH_PROVIDER);
new WorkspaceJob("Update launch configuration") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
workingCopy.doSave();
return Status.OK_STATUS;
}
}.schedule();
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoid mavenProjectChanged(MavenProjectChangedEvent[] events, IProgressMonitor monitor) {
for (MavenProjectChangedEvent event : events) {
try {
switch (event.getKind()) {
case MavenProjectChangedEvent.KIND_ADDED:
updateLaunchConfiguration(event.getMavenProject().getProject());
break;
default:
break;
}
} catch (Exception e) {
}
}
}
privatevoid updateLaunchConfiguration(IProject project) throws CoreException {
for (ILaunchConfiguration config : getLaunchConfiguration(project)) {
updateLaunchConfiguration(config);
}
}
private List<ILaunchConfiguration> getLaunchConfiguration(IProject project) throws CoreException {
List<ILaunchConfiguration> result = new ArrayList<ILaunchConfiguration>();
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfiguration[] configurations = launchManager.getLaunchConfigurations();
for (ILaunchConfiguration config : configurations) {
String projectName = config.getAttribute(ATTR_PROJECT_NAME, (String) null);
if (project.getName().equals(projectName)) {
result.add(config);
}
}
return result;
}
}