01: package org.drools.eclipse.action;
02:
03: import org.drools.eclipse.DroolsEclipsePlugin;
04: import org.drools.eclipse.builder.DroolsBuilder;
05: import org.drools.eclipse.wizard.project.NewDroolsProjectWizard;
06: import org.eclipse.core.resources.ICommand;
07: import org.eclipse.core.resources.IProject;
08: import org.eclipse.core.resources.IProjectDescription;
09: import org.eclipse.core.runtime.CoreException;
10: import org.eclipse.core.runtime.IProgressMonitor;
11: import org.eclipse.jdt.core.IClasspathEntry;
12: import org.eclipse.jdt.core.IJavaProject;
13: import org.eclipse.jdt.core.JavaCore;
14: import org.eclipse.jdt.core.JavaModelException;
15: import org.eclipse.jface.action.IAction;
16: import org.eclipse.jface.viewers.ISelection;
17: import org.eclipse.jface.viewers.IStructuredSelection;
18: import org.eclipse.ui.IObjectActionDelegate;
19: import org.eclipse.ui.IWorkbenchPart;
20:
21: public class ConvertToDroolsProjectAction implements
22: IObjectActionDelegate {
23:
24: private IJavaProject project;
25:
26: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
27: }
28:
29: public void run(IAction action) {
30: if (project != null && project.exists()) {
31: try {
32: addDroolsBuilder(project, null);
33: addDroolsLibraries(project, null);
34: } catch (Throwable t) {
35: DroolsEclipsePlugin.log(t);
36: }
37: }
38:
39: }
40:
41: public void selectionChanged(IAction action, ISelection selection) {
42: if (selection instanceof IStructuredSelection) {
43: IStructuredSelection structured = (IStructuredSelection) selection;
44: if (structured.size() == 1) {
45: Object element = structured.getFirstElement();
46: if (element instanceof IJavaProject) {
47: project = (IJavaProject) element;
48: } else if (element instanceof IProject) {
49: IJavaProject javaProject = JavaCore
50: .create((IProject) element);
51: if (javaProject != null && javaProject.exists()) {
52: project = javaProject;
53: }
54: }
55: }
56: }
57: }
58:
59: public static void addDroolsBuilder(IJavaProject project,
60: IProgressMonitor monitor) throws CoreException {
61: IProjectDescription description = project.getProject()
62: .getDescription();
63: // check whether Drools builder is already part of the project
64: ICommand[] commands = description.getBuildSpec();
65: for (int i = 0; i < commands.length; i++) {
66: if (DroolsBuilder.BUILDER_ID.equals(commands[i]
67: .getBuilderName())) {
68: return;
69: }
70: }
71: // add Drools builder
72: ICommand[] newCommands = new ICommand[commands.length + 1];
73: System.arraycopy(commands, 0, newCommands, 0, commands.length);
74:
75: ICommand droolsCommand = description.newCommand();
76: droolsCommand.setBuilderName(DroolsBuilder.BUILDER_ID);
77: newCommands[commands.length] = droolsCommand;
78:
79: description.setBuildSpec(newCommands);
80: project.getProject().setDescription(description, monitor);
81: }
82:
83: public static void addDroolsLibraries(IJavaProject project,
84: IProgressMonitor monitor) throws JavaModelException {
85: IClasspathEntry[] classpathEntries = project.getRawClasspath();
86: for (int i = 0; i < classpathEntries.length; i++) {
87: if (NewDroolsProjectWizard.DROOLS_CLASSPATH_CONTAINER_PATH
88: .equals(classpathEntries[i].getPath().toString())) {
89: return;
90: }
91: }
92: NewDroolsProjectWizard.addDroolsLibraries(project, null);
93: }
94:
95: }
|