Debian Codegraph @

doc/org.eclipse.emf.cheatsheets/src/org/eclipse/emf/cheatsheets/actions/NewJavaProjectAction.java

raw
1 /**
2 * Copyright (c) 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.cheatsheets.actions;
12
13 import org.eclipse.core.resources.IFolder;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IProjectDescription;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jdt.core.IClasspathEntry;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.JavaCore;
22
23 import org.eclipse.emf.cheatsheets.CheatSheetsPlugin;
24 import org.eclipse.emf.common.util.BasicMonitor;
25
26
27 /**
28 * <p>Action that creates a new Java project in workspace.</p>
29 * @since 2.2.0
30 */
31 public class NewJavaProjectAction extends NewProjectAction
32 {
33 protected static final String SOURCE_FOLDER = "src";
34 protected static final String OUTPUT_FOLDER = "bin";
35
36 /**
37 * Create a new Java project
38 * @param projectName Name of the project
39 * @param monitor Monitoring the action
40 * @return Java project
41 */
42 @Override
43 protected IProject createProject(String projectName, IProgressMonitor monitor) throws CoreException
44 {
45 monitor.beginTask(CheatSheetsPlugin.INSTANCE.getString("_UI_CreateJavaProject_message", new String []{ projectName }), 5);
46 IProject project = super.createProject(projectName, BasicMonitor.subProgress(monitor, 1));
47 if (project != null)
48 {
49 IProjectDescription description = project.getDescription();
50 if (!description.hasNature(JavaCore.NATURE_ID))
51 {
52 IJavaProject javaProject = JavaCore.create(project);
53 if (javaProject != null)
54 {
55 String[] natures = description.getNatureIds();
56 String[] javaNatures = new String [natures.length + 1];
57 System.arraycopy(natures, 0, javaNatures, 0, natures.length);
58 javaNatures[natures.length] = JavaCore.NATURE_ID;
59 description.setNatureIds(javaNatures);
60 project.setDescription(description, BasicMonitor.subProgress(monitor, 1));
61
62 IFolder sourceFolder = project.getFolder(SOURCE_FOLDER);
63 if (!sourceFolder.exists())
64 {
65 sourceFolder.create(true, true, BasicMonitor.subProgress(monitor, 1));
66 }
67
68 javaProject.setOutputLocation(project.getFolder(OUTPUT_FOLDER).getFullPath(), BasicMonitor.subProgress(monitor, 1));
69 IClasspathEntry[] entries = new IClasspathEntry []{
70 JavaCore.newSourceEntry(sourceFolder.getFullPath()),
71 JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")) };
72 javaProject.setRawClasspath(entries, BasicMonitor.subProgress(monitor, 1));
73 }
74 }
75 }
76 monitor.done();
77 return project;
78 }
79 }