package testbuild.actions; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IncrementalProjectBuilder; 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.core.runtime.SubProgressMonitor; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.console.IConsoleConstants; public class BuildJob extends WorkspaceJob { private IProject m_project = null; public BuildJob(IProject project, String name) { super(name); m_project = project; } synchronized private void build(IProgressMonitor monitor) throws CoreException { try { String name = "Building " + m_project.getName(); monitor.beginTask(name, IProgressMonitor.UNKNOWN); m_project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new SubProgressMonitor(monitor, 1)); } finally { monitor.done(); } } private void showConsole() { Display display = Display.getDefault(); if (display != null) { display.asyncExec(new Runnable() { public void run() { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window != null) { IWorkbenchPage page = window.getActivePage(); if (page != null) { try { page.showView(IConsoleConstants.ID_CONSOLE_VIEW); } catch (PartInitException e) { e.printStackTrace(); } } } } }); } } @Override public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { if (m_project != null) { showConsole(); build(monitor); } return Status.OK_STATUS; } }