//
// This file is part of the Prose Development Tools for Eclipse package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is Prose Development Tools for Eclipse.
//
// The Initial Developer of the Original Code is Angela Nicoara. Portions
// created by Angela Nicoara are Copyright (C) 2006 Angela Nicoara.
// All Rights Reserved.
//
// Contributor(s):
// $Id: ProseApplicationLaunchShortcut.java,v 1.1 2008/11/18 12:18:41 anicoara Exp $
// ==============================================================================
//
package ch.ethz.prose.eclipse.internal.launcher;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.debug.ui.launcher.MainMethodSearchEngine;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.dialogs.SelectionDialog;
import ch.ethz.prose.eclipse.internal.core.ProsePlugin;
/**
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: ProseApplicationLaunchShortcut.java,v 1.1 2008/11/18 12:18:41 anicoara Exp $
*/
public class ProseApplicationLaunchShortcut implements ILaunchShortcut {
/* (non-Javadoc)
* @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, java.lang.String)
*/
public void launch(ISelection selection, String mode) {
if (selection instanceof IStructuredSelection) {
searchAndLaunch(((IStructuredSelection) selection).toArray(), mode, false);
} else {
MessageDialog.openError(getShell(), "Launch failed", "The selection does not contain a main type.");
}
}
/* (non-Javadoc)
* @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String)
*/
public void launch(IEditorPart editor, String mode) {
IEditorInput input = editor.getEditorInput();
IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class);
if (je != null) {
searchAndLaunch(new Object[] { je}, mode, true);
} else {
MessageDialog.openError(getShell(), "Launch failed", "The active editor does not contain a main type.");
}
}
/**
* @param search the java elements to search for a main type
* @param mode the mode to launch in
* @param editor activated on an editor (or from a selection in a viewer)
*/
public void searchAndLaunch(Object[] search, String mode, boolean editor) {
IType[] types = null;
ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
if (search != null) {
try {
IJavaElement[] elements = getJavaElements(search);
MainMethodSearchEngine engine = new MainMethodSearchEngine();
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements, false);
// TODO styling information was removed
// types = engine.searchMainMethods(dialog, scope, IJavaElementSearchConstants.CONSIDER_BINARIES
// | IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS, true);
types = engine.searchMainMethods(dialog, scope, true);
} catch (InterruptedException e) {
return;
} catch (InvocationTargetException e) {
MessageDialog.openError(getShell(), "Launch failed", e.getMessage());
return;
}
IType type = null;
if (types.length == 0) {
String message = null;
if (editor) {
message = "The active editor does not contain a main type.";
} else {
message = "The selection does not contain a main type.";
}
MessageDialog.openError(getShell(), "Launch failed", message);
} else if (types.length > 1) {
type = chooseType(types, mode, dialog);
} else {
type = types[0];
}
if (type != null) {
launch(type, mode);
}
}
}
/**
* Convenience method to get the window that owns this action's Shell.
*
* @return Plug-in shell
*/
protected Shell getShell() {
return ProsePlugin.getShell();
}
/**
* Prompts the user to select a type.
*
* @param types Selectable types
* @param mode Run or debug mode
* @return the selected type or <code>null</code> if none.
*/
protected IType chooseType(IType[] types, String mode, IRunnableContext context) {
try {
SelectionDialog dialog = JavaUI.createTypeDialog(
getShell(),
context,
SearchEngine.createJavaSearchScope(types),
IJavaElementSearchConstants.CONSIDER_CLASSES,
false,
"**");
//MainTypeSelectionDialog dialog = new MainTypeSelectionDialog(getShell(), types); TODO not longer supported
if (mode.equals(ILaunchManager.DEBUG_MODE)) {
dialog.setTitle("Debug Type");
} else {
dialog.setTitle("Run Type");
}
//dialog.setMultipleSelection(false); TODO now part of the dialog constructor
if (dialog.open() == Window.OK) { return (IType) dialog.getResult()[0]; } //TODO out of range exception possible?
return null;
} catch(Exception e) {
return null;
}
}
/**
* Returns the Java elements corresponding to the given objects.
*
* @param objects selected objects
* @return corresponding Java elements
*/
private IJavaElement[] getJavaElements(Object[] objects) {
List list = new ArrayList(objects.length);
for (int i = 0; i < objects.length; i++) {
Object object = objects[i];
if (object instanceof IAdaptable) {
IJavaElement element = (IJavaElement) ((IAdaptable) object).getAdapter(IJavaElement.class);
if (element != null) {
list.add(element);
}
}
}
return (IJavaElement[]) list.toArray(new IJavaElement[list.size()]);
}
/**
* Launches a configuration for the given type.
*
* @param type Type for which a configuration is launched
* @param mode Run/debug mode
*/
protected void launch(IType type, String mode) {
ILaunchConfiguration config = findLaunchConfiguration(type, mode);
if (config != null) {
DebugUITools.launch(config, mode);
}
}
/**
* Locate a configuration to relaunch for the given type. If one cannot be found, create one.
*
* @param type Type for which a launch configuration is returned
* @param mode Ru
* @return Re-useable config or <code>null</code> if none
*/
protected ILaunchConfiguration findLaunchConfiguration(IType type, String mode) {
ILaunchConfigurationType configType = getProseLaunchConfigType();
List candidateConfigs = Collections.EMPTY_LIST;
try {
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType);
candidateConfigs = new ArrayList(configs.length);
for (int i = 0; i < configs.length; i++) {
ILaunchConfiguration config = configs[i];
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "").equals(
type.getFullyQualifiedName())) { //$NON-NLS-1$
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "").equals(
type.getJavaProject().getElementName())) { //$NON-NLS-1$
candidateConfigs.add(config);
}
}
}
} catch (CoreException e) {
ProsePlugin.log(e);
}
// If there are no existing configs associated with the IType, create one.
// If there is exactly one config associated with the IType, return it.
// Otherwise, if there is more than one config associated with the IType, prompt the
// user to choose one.
int candidateCount = candidateConfigs.size();
if (candidateCount < 1) {
return createConfiguration(type);
} else if (candidateCount == 1) {
return (ILaunchConfiguration) candidateConfigs.get(0);
} else {
// Prompt the user to choose a config. A null result means the user
// cancelled the dialog, in which case this method returns null,
// since cancelling the dialog should also cancel launching anything.
ILaunchConfiguration config = chooseConfiguration(candidateConfigs, mode);
if (config != null) { return config; }
}
return null;
}
protected ILaunchConfigurationType getProseLaunchConfigType() {
return getLaunchManager().getLaunchConfigurationType(ProseLaunchConfiguration.ID_PROSE_APPLICATION);
}
protected ILaunchManager getLaunchManager() {
return DebugPlugin.getDefault().getLaunchManager();
}
/**
* Create & return a new configuration based on the specified <code>IType</code>.
*
* @param type Type for which a new configuration is created
* @return New configuration
*/
protected ILaunchConfiguration createConfiguration(IType type) {
ILaunchConfiguration config = null;
ILaunchConfigurationWorkingCopy wc = null;
try {
ILaunchConfigurationType configType = getProseLaunchConfigType();
wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(type.getElementName()));
} catch (CoreException exception) {
reportCreatingConfiguration(exception);
return null;
}
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, type.getFullyQualifiedName());
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, type.getJavaProject().getElementName());
wc.setAttribute(ProseLaunchConfiguration.ATTR_LAUNCH_PROSE_SERVER, true);
try {
config = wc.doSave();
} catch (CoreException exception) {
reportCreatingConfiguration(exception);
}
return config;
}
protected void reportCreatingConfiguration(final CoreException exception) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
ErrorDialog.openError(getShell(), "Error Launching",
"An exception occurred attempting to create launch configuration.", exception.getStatus());
}
});
}
/**
* Show a selection dialog that allows the user to choose one of the
* specified launch configurations. Return the chosen config, or
* <code>null</code> if the user cancelled the dialog.
*
* @param configList Selectable configurations
* @param mode Run/debug mode
* @return Launch configuration
*/
protected ILaunchConfiguration chooseConfiguration(List configList, String mode) {
IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), labelProvider);
dialog.setElements(configList.toArray());
dialog.setTitle("Launch Configuration Selection");
if (mode.equals(ILaunchManager.DEBUG_MODE)) {
dialog.setMessage("Choose a launch configuration to debug");
} else {
dialog.setMessage("Choose a launch configuration to run");
}
dialog.setMultipleSelection(false);
int result = dialog.open();
labelProvider.dispose();
if (result == Window.OK) { return (ILaunchConfiguration) dialog.getFirstResult(); }
return null;
}
}