1 package org.enhydra.kelp.eclipse.actions; 2 3 4 import java.lang.reflect.InvocationTargetException ; 5 6 import org.eclipse.core.resources.IProject; 7 import org.eclipse.core.resources.IResource; 8 import org.eclipse.core.runtime.IProgressMonitor; 9 import org.eclipse.jdt.core.IJavaElement; 10 import org.eclipse.jface.action.IAction; 11 import org.eclipse.jface.operation.IRunnableWithProgress; 12 import org.eclipse.jface.viewers.ISelection; 13 import org.eclipse.jface.viewers.IStructuredSelection; 14 import org.eclipse.ui.IWorkbenchPage; 15 import org.eclipse.ui.IWorkbenchWindow; 16 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 17 import org.eclipse.ui.PlatformUI; 18 import org.eclipse.ui.externaltools.internal.core.DefaultRunnerContext; 19 import org.eclipse.ui.externaltools.internal.core.ExternalTool; 20 import org.enhydra.kelp.eclipse.KelpPlugin; 21 import org.enhydra.tool.ToolBoxInfo; 22 23 27 public class AntRebuildAction implements IWorkbenchWindowActionDelegate { 28 29 ExternalTool antRebuildTool = null; 30 IProject project = null; 31 IWorkbenchWindow window = null; 32 33 36 public AntRebuildAction() { 37 } 38 39 43 public void run(IAction action) { 44 String prjName = null; 45 String prjPath = null; 46 project = null; 47 48 IWorkbenchPage page = KelpPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage(); 49 if (page != null) { 50 ISelection selection = page.getSelection(); 51 if (selection instanceof IStructuredSelection) { 52 IStructuredSelection ss = (IStructuredSelection)selection; 53 if (!ss.isEmpty()) { 54 Object obj = ss.getFirstElement(); 55 if (obj instanceof IJavaElement) { 56 IResource res =((IJavaElement)obj).getResource(); 57 if (res == null) 58 project = ((IJavaElement)obj).getJavaProject().getProject(); 59 else 60 project = res.getProject(); 61 62 } 63 if (obj instanceof IResource) { 64 project = ((IResource)obj).getProject(); 65 } 66 } 67 } 68 } 69 if (project != null) { 70 prjName = project.getName(); 71 prjPath = project.getLocation().toString(); 72 73 75 antRebuildTool = new ExternalTool(); 76 String location = ToolBoxInfo.getEnhydraRoot() + "/bin/ant"; 77 if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { 78 location += ".bat"; 79 } 80 antRebuildTool.setBlock(true); 81 antRebuildTool.setBuildTypes(new String [0]); antRebuildTool.setLocation(location); 83 antRebuildTool.setArguments("rebuild"); 84 antRebuildTool.setName("Ant Rebuild"); 85 antRebuildTool.setRefreshScope("${project}"); 86 antRebuildTool.setShowLog(true); 87 antRebuildTool.setType(ExternalTool.TOOL_TYPE_PROGRAM); 88 antRebuildTool.setWorkingDirectory("${workspace_loc:/"+prjName+"}"); 89 90 91 window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 92 IRunnableWithProgress runnable = new IRunnableWithProgress() { 93 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 94 DefaultRunnerContext context = new DefaultRunnerContext(antRebuildTool, project, window.getWorkbench().getWorkingSetManager()); 95 context.run(monitor, window.getShell()); 96 }; 97 }; 98 try { 99 window.run(false, false, runnable); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 109 }else 117 System.err.println("Fail to start Ant Rebuild, no project is selected!"); 118 119 } 120 121 125 public void selectionChanged(IAction action, ISelection selection) { 126 } 127 128 132 public void dispose() { 133 } 134 135 139 public void init(IWorkbenchWindow window) { 140 } 141 } 142 | Popular Tags |