|
| 1 | +package de.vogella.jdt.projectcreation.handlers; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.eclipse.core.commands.AbstractHandler; |
| 7 | +import org.eclipse.core.commands.ExecutionEvent; |
| 8 | +import org.eclipse.core.commands.ExecutionException; |
| 9 | +import org.eclipse.core.resources.IFolder; |
| 10 | +import org.eclipse.core.resources.IProject; |
| 11 | +import org.eclipse.core.resources.IProjectDescription; |
| 12 | +import org.eclipse.core.resources.IResource; |
| 13 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 14 | +import org.eclipse.core.runtime.CoreException; |
| 15 | +import org.eclipse.core.runtime.IPath; |
| 16 | +import org.eclipse.core.runtime.Path; |
| 17 | +import org.eclipse.jdt.core.IClasspathEntry; |
| 18 | +import org.eclipse.jdt.core.IJavaProject; |
| 19 | +import org.eclipse.jdt.core.IPackageFragment; |
| 20 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 21 | +import org.eclipse.jdt.core.JavaCore; |
| 22 | +import org.eclipse.jdt.core.JavaModelException; |
| 23 | +import org.eclipse.jdt.launching.IVMInstall; |
| 24 | +import org.eclipse.jdt.launching.JavaRuntime; |
| 25 | +import org.eclipse.jdt.launching.LibraryLocation; |
| 26 | +import org.eclipse.jdt.ui.PreferenceConstants; |
| 27 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 28 | +import org.eclipse.swt.widgets.Shell; |
| 29 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 30 | + |
| 31 | +/** |
| 32 | + * Our sample handler extends AbstractHandler, an IHandler base class. |
| 33 | + * |
| 34 | + * @see org.eclipse.core.commands.IHandler |
| 35 | + * @see org.eclipse.core.commands.AbstractHandler |
| 36 | + */ |
| 37 | +public class CreateProjectHandler extends AbstractHandler { |
| 38 | + |
| 39 | + private static final String SOURCE_FOLDER = "src"; |
| 40 | + |
| 41 | + /** |
| 42 | + * the command has been executed, so extract extract the needed information |
| 43 | + * from the application context. |
| 44 | + */ |
| 45 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 46 | + Shell shell = HandlerUtil.getActiveShell(event); |
| 47 | + |
| 48 | + try { |
| 49 | + IProject project = ResourcesPlugin.getWorkspace().getRoot() |
| 50 | + .getProject("aaaa.example"); |
| 51 | + if (project.exists()) { |
| 52 | + MessageDialog.openInformation(shell, "Error", |
| 53 | + "Project exists already"); |
| 54 | + } |
| 55 | + project.create(null); |
| 56 | + project.open(null); |
| 57 | + |
| 58 | + IProjectDescription description = project.getDescription(); |
| 59 | + description.setNatureIds(new String[] { JavaCore.NATURE_ID }); |
| 60 | + project.setDescription(description, null); |
| 61 | + |
| 62 | + IJavaProject javaProject = JavaCore.create(project); |
| 63 | + |
| 64 | + IFolder binFolder = project.getFolder("bin"); |
| 65 | + binFolder.create(false, true, null); |
| 66 | + javaProject.setOutputLocation(binFolder.getFullPath(), null); |
| 67 | + |
| 68 | + List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(); |
| 69 | + IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall(); |
| 70 | + LibraryLocation[] locations = JavaRuntime |
| 71 | + .getLibraryLocations(vmInstall); |
| 72 | + for (LibraryLocation element : locations) { |
| 73 | + entries.add(JavaCore.newLibraryEntry( |
| 74 | + element.getSystemLibraryPath(), null, null)); |
| 75 | + } |
| 76 | + |
| 77 | + IFolder sourceFolder = project.getFolder(SOURCE_FOLDER); |
| 78 | + sourceFolder.create(false, true, null); |
| 79 | + |
| 80 | + IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder); |
| 81 | + IClasspathEntry[] oldEntries = javaProject.getRawClasspath(); |
| 82 | + IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1]; |
| 83 | + System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length); |
| 84 | + newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath()); |
| 85 | + javaProject.setRawClasspath(newEntries, null); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + createPackage(project); |
| 90 | + |
| 91 | + try { |
| 92 | + project.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 93 | + } catch (CoreException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + } catch (CoreException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + private void createPackage(IProject project) { |
| 103 | + IJavaProject javaProject = JavaCore.create(project); |
| 104 | + IFolder folder = project.getFolder(SOURCE_FOLDER); |
| 105 | + // folder.create(true, true, null); |
| 106 | + IPackageFragmentRoot srcFolder = javaProject |
| 107 | + .getPackageFragmentRoot(folder); |
| 108 | + try { |
| 109 | + srcFolder.createPackageFragment( |
| 110 | + project.getName(), true, null); |
| 111 | + } catch (JavaModelException e) { |
| 112 | + e.printStackTrace(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments