blob: aeb0c6fe8caafa767da917a424f638881b04a335 [file] [log] [blame]
package org.eclipse.ui.externaltools.internal.ui;
/**********************************************************************
Copyright (c) 2002 IBM Corp. and others.
All rights reserved.   This program and the accompanying materials
are made available under the terms of the Common Public License v0.5
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v05.html
 
Contributors:
**********************************************************************/
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import org.apache.tools.ant.BuildListener;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.ui.externaltools.internal.core.DefaultRunnerContext;
import org.eclipse.ui.externaltools.internal.core.ExternalTool;
import org.eclipse.ui.externaltools.internal.core.ExternalToolsPlugin;
import org.eclipse.ui.externaltools.internal.core.ToolMessages;
import org.eclipse.ui.externaltools.internal.core.ToolUtil;
/**
* This action will display the external tool configuration dialog.
* In addition, as a tool bar item, it's drop down list will include
* tools to run directly.
*/
public class ExternalToolsAction extends ActionDelegate implements IWorkbenchWindowPulldownDelegate2, IMenuCreator {
private IWorkbenchWindow window;
private IAction realAction;
/**
* Creates the external tool configure action
*/
public ExternalToolsAction() {
super();
}
/* (non-Javadoc)
* Method declared on IActionDelegate.
*/
public void run(IAction action) {
if (action.isEnabled())
showConfigurationDialog();
}
/* (non-Javadoc)
* Method declared on IActionDelegate.
*/
public void selectionChanged(IAction action, ISelection selection) {
if (realAction == null) {
realAction = action;
realAction.setMenuCreator(this);
}
}
/* (non-Javadoc)
* Method declared on IWorkbenchWindowPulldownDelegate.
*/
public Menu getMenu(Control parent) {
Menu menu= new Menu(parent);
return createMenu(menu, false);
}
/* (non-Javadoc)
* Method declared on IWorkbenchWindowPulldownDelegate2.
*/
public Menu getMenu(Menu parent) {
Menu menu= new Menu(parent);
return createMenu(menu, true);
}
/* (non-Javadoc)
* Method declared on IWorkbenchWindowActionDelegate.
*/
public void dispose() {
}
/* (non-Javadoc)
* Method declared on IWorkbenchWindowActionDelegate.
*/
public void init(IWorkbenchWindow window) {
this.window = window;
}
/**
* Creates the menu for the action
*/
private Menu createMenu(Menu menu, final boolean wantFastAccess) {
// Add listener to repopulate the menu each time
// it is shown because of dynamic history list
menu.addMenuListener(new MenuAdapter() {
public void menuShown(MenuEvent e) {
Menu m = (Menu)e.widget;
MenuItem[] items = m.getItems();
for (int i=0; i < items.length; i++)
items[i].dispose();
populateMenu(m, wantFastAccess);
}
});
return menu;
}
/**
* Populates the menu with its items
*/
private void populateMenu(Menu menu, boolean wantFastAccess) {
// Add a menu item for each tool in the history
ArrayList tools = ExternalToolsPlugin.getDefault().getRegistry().getExternalTools();
if (tools.size() > 0) {
for (int i = 0; i < tools.size(); i++) {
ExternalTool tool = (ExternalTool)tools.get(i);
StringBuffer label = new StringBuffer();
if (i < 9 && wantFastAccess) {
//add the numerical accelerator
label.append('&');
label.append(i+1);
label.append(' ');
}
label.append(tool.getName());
MenuItem item = new MenuItem(menu, SWT.NONE);
item.setText(label.toString());
item.setData(tool);
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
runTool((ExternalTool)e.widget.getData());
}
});
}
// Add a separator
new MenuItem(menu, SWT.SEPARATOR);
}
// Add a menu to edit the configurations
MenuItem item = new MenuItem(menu, SWT.NONE);
item.setText(ToolMessages.getString("ExternalToolsAction.configure")); //$NON-NLS-1$
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
showConfigurationDialog();
}
});
}
/**
* Runs the specified tool
*/
private void runTool(final ExternalTool tool) {
if (tool == null)
return;
ToolUtil.saveDirtyEditors(window);
if (tool.getShowLog()) {
ToolUtil.showLogConsole(window);
ToolUtil.clearLogDocument();
}
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
DefaultRunnerContext context = new DefaultRunnerContext(tool, null, window.getWorkbench().getWorkingSetManager());
context.run(monitor, window.getShell());
} catch (BuildCanceledException e) {
throw new InterruptedException();
} catch (Exception e) {
throw new InvocationTargetException(e, e.getMessage());
}
};
};
try {
new ProgressMonitorDialog(window.getShell()).run(true, true, runnable);
} catch (InterruptedException e) {
return;
} catch (InvocationTargetException e) {
IStatus status = new Status(IStatus.ERROR, ExternalToolsPlugin.PLUGIN_ID, 0, ToolMessages.getString("ExternalToolsAction.internalError"), e); //$NON-NLS-1$;
ErrorDialog.openError(
window.getShell(),
ToolMessages.getString("ExternalToolsAction.runErrorTitle"), //$NON-NLS-1$;
ToolMessages.getString("ExternalToolsAction.runAntProblem"), //$NON-NLS-1$;
status);
return;
}
}
/**
* Shows the tool configuration dialog
*/
private void showConfigurationDialog() {
ConfigurationDialog dialog;
dialog = new ConfigurationDialog(window.getShell());
dialog.open();
}
}