KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > eclipse > actions > AntRebuildAction


1 package org.enhydra.kelp.eclipse.actions;
2
3
4 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
24  * Insert the type's description here.
25  * @see IWorkbenchWindowActionDelegate
26  */

27 public class AntRebuildAction implements IWorkbenchWindowActionDelegate {
28
29     ExternalTool antRebuildTool = null;
30     IProject project = null;
31     IWorkbenchWindow window = null;
32
33     /**
34      * The constructor.
35      */

36     public AntRebuildAction() {
37     }
38
39     /**
40      * Insert the method's description here.
41      * @see IWorkbenchWindowActionDelegate#run
42      */

43     public void run(IAction action) {
44         String JavaDoc prjName = null;
45         String JavaDoc 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 JavaDoc 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 //---------------------Dacha---------------------------------
74

75             antRebuildTool = new ExternalTool();
76             String JavaDoc 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 JavaDoc[0]); //default: do not include in any rebuild
82
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 JavaDoc, InterruptedException JavaDoc {
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 JavaDoc e) {
101                 e.printStackTrace();
102             }
103 // try {
104
// context.run(null, window.getShell());
105
// } catch (Exception e) {
106
// e.printStackTrace();
107
// }
108

109 // try {
110
//
111
// // Refresh project, so the generated files could be visible
112
// project.refreshLocal(IResource.DEPTH_INFINITE, null);
113
// } catch (CoreException e) {
114
// System.err.println(prjName);
115
// }
116
}else
117             System.err.println("Fail to start Ant Rebuild, no project is selected!");
118         
119     }
120
121     /**
122      * Insert the method's description here.
123      * @see IWorkbenchWindowActionDelegate#selectionChanged
124      */

125     public void selectionChanged(IAction action, ISelection selection) {
126     }
127
128     /**
129      * Insert the method's description here.
130      * @see IWorkbenchWindowActionDelegate#dispose
131      */

132     public void dispose() {
133     }
134
135     /**
136      * Insert the method's description here.
137      * @see IWorkbenchWindowActionDelegate#init
138      */

139     public void init(IWorkbenchWindow window) {
140     }
141 }
142
Popular Tags