001: package org.drools.eclipse.wizard.project;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.lang.reflect.InvocationTargetException;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.List;
009:
010: import org.drools.eclipse.DroolsEclipsePlugin;
011: import org.drools.eclipse.builder.DroolsBuilder;
012: import org.drools.eclipse.util.DroolsClasspathContainer;
013: import org.eclipse.core.resources.ICommand;
014: import org.eclipse.core.resources.IContainer;
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IFolder;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IProjectDescription;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IResourceStatus;
021: import org.eclipse.core.resources.IWorkspace;
022: import org.eclipse.core.resources.IncrementalProjectBuilder;
023: import org.eclipse.core.resources.ResourcesPlugin;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IPath;
026: import org.eclipse.core.runtime.IProgressMonitor;
027: import org.eclipse.core.runtime.OperationCanceledException;
028: import org.eclipse.core.runtime.Path;
029: import org.eclipse.core.runtime.SubProgressMonitor;
030: import org.eclipse.jdt.core.IClasspathContainer;
031: import org.eclipse.jdt.core.IClasspathEntry;
032: import org.eclipse.jdt.core.IJavaProject;
033: import org.eclipse.jdt.core.IPackageFragment;
034: import org.eclipse.jdt.core.IPackageFragmentRoot;
035: import org.eclipse.jdt.core.JavaCore;
036: import org.eclipse.jdt.core.JavaModelException;
037: import org.eclipse.jdt.ui.PreferenceConstants;
038: import org.eclipse.jface.dialogs.ErrorDialog;
039: import org.eclipse.jface.dialogs.MessageDialog;
040: import org.eclipse.jface.resource.ImageDescriptor;
041: import org.eclipse.ui.actions.WorkspaceModifyOperation;
042: import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
043: import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
044:
045: /**
046: * A wizard to create a new Drools project.
047: *
048: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
049: */
050: public class NewDroolsProjectWizard extends BasicNewResourceWizard {
051:
052: public static final String DROOLS_CLASSPATH_CONTAINER_PATH = "DROOLS/Drools";
053:
054: private IProject newProject;
055: private WizardNewProjectCreationPage mainPage;
056: private NewDroolsProjectWizardPage extraPage;
057:
058: public void addPages() {
059: super .addPages();
060: mainPage = new WizardNewProjectCreationPage(
061: "basicNewProjectPage");
062: mainPage.setTitle("New Drools Project");
063: mainPage.setDescription("Create a new Drools Project");
064: this .addPage(mainPage);
065: extraPage = new NewDroolsProjectWizardPage();
066: addPage(extraPage);
067: setNeedsProgressMonitor(true);
068: }
069:
070: public boolean performFinish() {
071: createDroolsProject();
072: if (newProject == null) {
073: return false;
074: }
075: selectAndReveal(newProject);
076: return true;
077: }
078:
079: private void createDroolsProject() {
080: newProject = createNewProject();
081: WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
082: protected void execute(IProgressMonitor monitor)
083: throws CoreException {
084: try {
085: IJavaProject project = JavaCore.create(newProject);
086: createOutputLocation(project, monitor);
087: addJavaBuilder(project, monitor);
088: setClasspath(project, monitor);
089: createInitialContent(project, monitor);
090: newProject.build(
091: IncrementalProjectBuilder.FULL_BUILD,
092: monitor);
093: } catch (IOException _ex) {
094: ErrorDialog.openError(getShell(),
095: "Problem creating Drools project", null,
096: null);
097: }
098: }
099: };
100: try {
101: getContainer().run(true, true, op);
102: } catch (Throwable t) {
103: DroolsEclipsePlugin.log(t);
104: }
105: }
106:
107: private IProject createNewProject() {
108: if (newProject != null) {
109: return newProject;
110: }
111: final IProject newProjectHandle = mainPage.getProjectHandle();
112:
113: // get a project descriptor
114: IPath newPath = null;
115: if (!mainPage.useDefaults())
116: newPath = mainPage.getLocationPath();
117:
118: IWorkspace workspace = ResourcesPlugin.getWorkspace();
119: final IProjectDescription description = workspace
120: .newProjectDescription(newProjectHandle.getName());
121: description.setLocation(newPath);
122: addNatures(description);
123:
124: // create the new project operation
125: WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
126: protected void execute(IProgressMonitor monitor)
127: throws CoreException {
128: createProject(description, newProjectHandle, monitor);
129: }
130: };
131:
132: // run the new project creation operation
133: try {
134: getContainer().run(true, true, op);
135: } catch (InterruptedException e) {
136: return null;
137: } catch (InvocationTargetException e) {
138: Throwable t = e.getTargetException();
139: if (t instanceof CoreException) {
140: if (((CoreException) t).getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
141: MessageDialog.openError(getShell(),
142: "NewProject.errorMessage",
143: "NewProject.caseVariantExistsError"
144: + newProjectHandle.getName());
145: } else {
146: ErrorDialog.openError(getShell(),
147: "NewProject.errorMessage", null, // no special message
148: ((CoreException) t).getStatus());
149: }
150: } else {
151: DroolsEclipsePlugin.log(e);
152: }
153: return null;
154: }
155:
156: return newProjectHandle;
157: }
158:
159: private void addNatures(IProjectDescription projectDescription) {
160: List list = new ArrayList();
161: list.addAll(Arrays.asList(projectDescription.getNatureIds()));
162: list.add("org.eclipse.jdt.core.javanature");
163: projectDescription.setNatureIds((String[]) list
164: .toArray(new String[list.size()]));
165: }
166:
167: private void createProject(IProjectDescription description,
168: IProject projectHandle, IProgressMonitor monitor)
169: throws CoreException, OperationCanceledException {
170: try {
171: monitor.beginTask("", 2000);
172: projectHandle.create(description, new SubProgressMonitor(
173: monitor, 1000));
174: if (monitor.isCanceled()) {
175: throw new OperationCanceledException();
176: }
177: projectHandle.open(IResource.BACKGROUND_REFRESH,
178: new SubProgressMonitor(monitor, 1000));
179: } finally {
180: monitor.done();
181: }
182: }
183:
184: private void createOutputLocation(IJavaProject project,
185: IProgressMonitor monitor) throws JavaModelException,
186: CoreException {
187: IFolder folder = project.getProject().getFolder("bin");
188: createFolder(folder, monitor);
189: IPath path = folder.getFullPath();
190: project.setOutputLocation(path, null);
191: }
192:
193: private void addJavaBuilder(IJavaProject project,
194: IProgressMonitor monitor) throws CoreException {
195: IProjectDescription description = project.getProject()
196: .getDescription();
197: ICommand[] commands = description.getBuildSpec();
198: ICommand[] newCommands = new ICommand[commands.length + 2];
199: System.arraycopy(commands, 0, newCommands, 0, commands.length);
200:
201: ICommand javaCommand = description.newCommand();
202: javaCommand.setBuilderName("org.eclipse.jdt.core.javabuilder");
203: newCommands[commands.length] = javaCommand;
204:
205: ICommand droolsCommand = description.newCommand();
206: droolsCommand.setBuilderName(DroolsBuilder.BUILDER_ID);
207: newCommands[commands.length + 1] = droolsCommand;
208:
209: description.setBuildSpec(newCommands);
210: project.getProject().setDescription(description, monitor);
211: }
212:
213: private void setClasspath(IJavaProject project,
214: IProgressMonitor monitor) throws JavaModelException,
215: CoreException {
216: project.setRawClasspath(new IClasspathEntry[0], monitor);
217: addSourceFolders(project, monitor);
218: addJRELibraries(project, monitor);
219: addDroolsLibraries(project, monitor);
220: }
221:
222: private void addSourceFolders(IJavaProject project,
223: IProgressMonitor monitor) throws JavaModelException,
224: CoreException {
225: List list = new ArrayList();
226: list.addAll(Arrays.asList(project.getRawClasspath()));
227: addSourceFolder(project, list, "src/main/java", monitor);
228: addSourceFolder(project, list, "src/main/rules", monitor);
229: project.setRawClasspath((IClasspathEntry[]) list
230: .toArray(new IClasspathEntry[list.size()]), null);
231: }
232:
233: private void addJRELibraries(IJavaProject project,
234: IProgressMonitor monitor) throws JavaModelException {
235: List list = new ArrayList();
236: list.addAll(Arrays.asList(project.getRawClasspath()));
237: list.addAll(Arrays.asList(PreferenceConstants
238: .getDefaultJRELibrary()));
239: project.setRawClasspath((IClasspathEntry[]) list
240: .toArray(new IClasspathEntry[list.size()]), monitor);
241: }
242:
243: private static IPath getClassPathContainerPath() {
244: return new Path(DROOLS_CLASSPATH_CONTAINER_PATH);
245: }
246:
247: private static void createDroolsLibraryContainer(
248: IJavaProject project, IProgressMonitor monitor)
249: throws JavaModelException {
250: JavaCore
251: .setClasspathContainer(
252: getClassPathContainerPath(),
253: new IJavaProject[] { project },
254: new IClasspathContainer[] { new DroolsClasspathContainer(
255: project, getClassPathContainerPath()) },
256: monitor);
257: }
258:
259: public static void addDroolsLibraries(IJavaProject project,
260: IProgressMonitor monitor) throws JavaModelException {
261: createDroolsLibraryContainer(project, monitor);
262: List list = new ArrayList();
263: list.addAll(Arrays.asList(project.getRawClasspath()));
264: list.add(JavaCore
265: .newContainerEntry(getClassPathContainerPath()));
266: project.setRawClasspath((IClasspathEntry[]) list
267: .toArray(new IClasspathEntry[list.size()]), monitor);
268: }
269:
270: private void createInitialContent(IJavaProject project,
271: IProgressMonitor monitor) throws CoreException,
272: JavaModelException, IOException {
273: try {
274: if (extraPage.createJavaRuleFile()) {
275: createRuleSampleLauncher(project);
276: }
277: if (extraPage.createRuleFile()) {
278: createRule(project, monitor);
279: }
280: if (extraPage.createDecisionTableFile()) {
281: createDecisionTable(project, monitor);
282: }
283: if (extraPage.createJavaDecisionTableFile()) {
284: createDecisionTableSampleLauncher(project);
285: }
286: if (extraPage.createRuleFlowFile()) {
287: createRuleFlow(project, monitor);
288: }
289: if (extraPage.createJavaRuleFlowFile()) {
290: createRuleFlowSampleLauncher(project);
291: }
292: } catch (Throwable t) {
293: t.printStackTrace();
294: }
295: }
296:
297: /**
298: * Create the sample rule launcher file.
299: */
300: private void createRuleSampleLauncher(IJavaProject project)
301: throws JavaModelException, IOException {
302:
303: String s = "org/drools/eclipse/wizard/project/RuleLauncherSample.java.template";
304: IFolder folder = project.getProject()
305: .getFolder("src/main/java");
306: IPackageFragmentRoot packageFragmentRoot = project
307: .getPackageFragmentRoot(folder);
308: IPackageFragment packageFragment = packageFragmentRoot
309: .createPackageFragment("com.sample", true, null);
310: InputStream inputstream = getClass().getClassLoader()
311: .getResourceAsStream(s);
312: packageFragment.createCompilationUnit("DroolsTest.java",
313: new String(readStream(inputstream)), true, null);
314: }
315:
316: /**
317: * Create the sample decision table launcher file.
318: */
319: private void createDecisionTableSampleLauncher(IJavaProject project)
320: throws JavaModelException, IOException {
321:
322: String s = "org/drools/eclipse/wizard/project/DecisionTableLauncherSample.java.template";
323: IFolder folder = project.getProject()
324: .getFolder("src/main/java");
325: IPackageFragmentRoot packageFragmentRoot = project
326: .getPackageFragmentRoot(folder);
327: IPackageFragment packageFragment = packageFragmentRoot
328: .createPackageFragment("com.sample", true, null);
329: InputStream inputstream = getClass().getClassLoader()
330: .getResourceAsStream(s);
331: packageFragment.createCompilationUnit("DecisionTableTest.java",
332: new String(readStream(inputstream)), true, null);
333: }
334:
335: /**
336: * Create the sample rule file.
337: */
338: private void createRule(IJavaProject project,
339: IProgressMonitor monitor) throws CoreException {
340: String fileName = "org/drools/eclipse/wizard/project/Sample.drl.template";
341: IFolder folder = project.getProject().getFolder(
342: "src/main/rules");
343: IFile file = folder.getFile("Sample.drl");
344: InputStream inputstream = getClass().getClassLoader()
345: .getResourceAsStream(fileName);
346: if (!file.exists()) {
347: file.create(inputstream, true, monitor);
348: } else {
349: file.setContents(inputstream, true, false, monitor);
350: }
351: }
352:
353: /**
354: * Create the sample decision table file.
355: */
356: private void createDecisionTable(IJavaProject project,
357: IProgressMonitor monitor) throws CoreException {
358: String fileName = "org/drools/eclipse/wizard/project/Sample.xls.template";
359: IFolder folder = project.getProject().getFolder(
360: "src/main/rules");
361: IFile file = folder.getFile("Sample.xls");
362: InputStream inputstream = getClass().getClassLoader()
363: .getResourceAsStream(fileName);
364: if (!file.exists()) {
365: file.create(inputstream, true, monitor);
366: } else {
367: file.setContents(inputstream, true, false, monitor);
368: }
369: }
370:
371: /**
372: * Create the sample RuleFlow file.
373: */
374: private void createRuleFlow(IJavaProject project,
375: IProgressMonitor monitor) throws CoreException {
376: String fileName = "org/drools/eclipse/wizard/project/ruleflow.rf.template";
377: IFolder folder = project.getProject().getFolder(
378: "src/main/rules");
379: IFile file = folder.getFile("ruleflow.rf");
380: InputStream inputstream = getClass().getClassLoader()
381: .getResourceAsStream(fileName);
382: if (!file.exists()) {
383: file.create(inputstream, true, monitor);
384: } else {
385: file.setContents(inputstream, true, false, monitor);
386: }
387: fileName = "org/drools/eclipse/wizard/project/ruleflow.rfm.template";
388: folder = project.getProject().getFolder("src/main/rules");
389: file = folder.getFile("ruleflow.rfm");
390: inputstream = getClass().getClassLoader().getResourceAsStream(
391: fileName);
392: if (!file.exists()) {
393: file.create(inputstream, true, monitor);
394: } else {
395: file.setContents(inputstream, true, false, monitor);
396: }
397: fileName = "org/drools/eclipse/wizard/project/ruleflow.drl.template";
398: folder = project.getProject().getFolder("src/main/rules");
399: file = folder.getFile("ruleflow.drl");
400: inputstream = getClass().getClassLoader().getResourceAsStream(
401: fileName);
402: if (!file.exists()) {
403: file.create(inputstream, true, monitor);
404: } else {
405: file.setContents(inputstream, true, false, monitor);
406: }
407: }
408:
409: /**
410: * Create the sample RuleFlow launcher file.
411: */
412: private void createRuleFlowSampleLauncher(IJavaProject project)
413: throws JavaModelException, IOException {
414:
415: String s = "org/drools/eclipse/wizard/project/RuleFlowLauncherSample.java.template";
416: IFolder folder = project.getProject()
417: .getFolder("src/main/java");
418: IPackageFragmentRoot packageFragmentRoot = project
419: .getPackageFragmentRoot(folder);
420: IPackageFragment packageFragment = packageFragmentRoot
421: .createPackageFragment("com.sample", true, null);
422: InputStream inputstream = getClass().getClassLoader()
423: .getResourceAsStream(s);
424: packageFragment.createCompilationUnit("RuleFlowTest.java",
425: new String(readStream(inputstream)), true, null);
426: }
427:
428: protected void initializeDefaultPageImageDescriptor() {
429: ImageDescriptor desc = DroolsEclipsePlugin
430: .getImageDescriptor("icons/drools-large.PNG");
431: setDefaultPageImageDescriptor(desc);
432: }
433:
434: private byte[] readStream(InputStream inputstream)
435: throws IOException {
436: byte bytes[] = (byte[]) null;
437: int i = 0;
438: byte tempBytes[] = new byte[1024];
439: for (int j = inputstream.read(tempBytes); j != -1; j = inputstream
440: .read(tempBytes)) {
441: byte tempBytes2[] = new byte[i + j];
442: if (i > 0) {
443: System.arraycopy(bytes, 0, tempBytes2, 0, i);
444: }
445: System.arraycopy(tempBytes, 0, tempBytes2, i, j);
446: bytes = tempBytes2;
447: i += j;
448: }
449:
450: return bytes;
451: }
452:
453: private void addSourceFolder(IJavaProject project, List list,
454: String s, IProgressMonitor monitor) throws CoreException {
455: IFolder folder = project.getProject().getFolder(s);
456: createFolder(folder, monitor);
457: IPackageFragmentRoot ipackagefragmentroot = project
458: .getPackageFragmentRoot(folder);
459: list.add(JavaCore
460: .newSourceEntry(ipackagefragmentroot.getPath()));
461: }
462:
463: private void createFolder(IFolder folder, IProgressMonitor monitor)
464: throws CoreException {
465: IContainer container = folder.getParent();
466: if (container != null && !container.exists()
467: && (container instanceof IFolder))
468: createFolder((IFolder) container, monitor);
469: if (!folder.exists()) {
470: folder.create(true, true, monitor);
471: }
472: }
473: }
|