|
Lines 1-5
Link Here
|
| 1 |
package org.eclipse.ide4edu.javalite.constructors; |
1 |
package org.eclipse.ide4edu.javalite.constructors; |
| 2 |
|
2 |
|
|
|
3 |
import org.eclipse.core.resources.IFile; |
| 4 |
import org.eclipse.core.resources.IProject; |
| 5 |
import org.eclipse.core.resources.IResource; |
| 6 |
import org.eclipse.core.resources.IWorkspace; |
| 7 |
import org.eclipse.core.resources.IWorkspaceRoot; |
| 8 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 9 |
import org.eclipse.core.runtime.CoreException; |
| 10 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 11 |
import org.eclipse.core.runtime.IStatus; |
| 12 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 13 |
import org.eclipse.core.runtime.Status; |
| 14 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 15 |
import org.eclipse.jdt.core.IJavaProject; |
| 16 |
import org.eclipse.jdt.core.IPackageFragment; |
| 17 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 18 |
import org.eclipse.jdt.core.JavaCore; |
| 19 |
|
| 20 |
/** |
| 21 |
* Version 1 |
| 22 |
* Java Class constructor that codes everything up from scratch. |
| 23 |
*/ |
| 3 |
public class NewJavaClassConstructor { |
24 |
public class NewJavaClassConstructor { |
|
|
25 |
|
| 26 |
private String className = "MyClass"; |
| 27 |
private String packageName = IPackageFragment.DEFAULT_PACKAGE_NAME; |
| 28 |
private String projectName = "My Project"; |
| 29 |
|
| 30 |
/** |
| 31 |
* Specifies whether this class should be created in the (default package) |
| 32 |
* i.e at the root level of the project? |
| 33 |
* |
| 34 |
* When beginner programmers start out, all of their classes will be created |
| 35 |
* at the root level of the project. When they move on to being intermediate |
| 36 |
* programmers, they will be able to specify the package the class should |
| 37 |
* be created in. |
| 38 |
*/ |
| 39 |
private Boolean shouldUseDefaultPackage = true; |
| 40 |
|
| 41 |
/** |
| 42 |
* Specifies whether to create a new default project if the workspace is |
| 43 |
* empty. Otherwise we will prompt for project creation. |
| 44 |
*/ |
| 45 |
private Boolean shouldCreateProjectIfNecessary = true; |
| 46 |
|
| 47 |
public void setProjectName(String projectName) { |
| 48 |
this.projectName = projectName.trim(); |
| 49 |
} |
| 50 |
|
| 51 |
public void setPackageName(String packageName) { |
| 52 |
this.packageName = packageName.trim(); |
| 53 |
} |
| 54 |
|
| 55 |
public void setClassName(String className) { |
| 56 |
this.className = className.trim(); |
| 57 |
} |
| 58 |
|
| 59 |
public void setUseDefaultPackage(Boolean shouldUseDefaultPackage) { |
| 60 |
this.shouldUseDefaultPackage = shouldUseDefaultPackage; |
| 61 |
} |
| 62 |
|
| 63 |
public void setCreateProjectIfNecessary(Boolean shouldCreateProjectIfNecessary) { |
| 64 |
this.shouldCreateProjectIfNecessary = shouldCreateProjectIfNecessary; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* This method returns the status of the receiver in the form of |
| 69 |
* an {@link IStatus} instance. The answer will reflect any |
| 70 |
* potential error state in the receiver, or {@link Status#OK_STATUS} |
| 71 |
* if there is no error. |
| 72 |
* |
| 73 |
* @return Instance of {@link IStatus} |
| 74 |
*/ |
| 75 |
public IStatus getStatus() { |
| 76 |
IStatus status = getWorkspace().validateName(className, IResource.FILE); |
| 77 |
if (!status.isOK()) return status; |
| 78 |
|
| 79 |
IProject project= getWorkspaceRoot().getProject(projectName); |
| 80 |
if (!shouldCreateProjectIfNecessary && !project.exists()) { |
| 81 |
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, |
| 82 |
"There is no project by the name " + projectName + |
| 83 |
" to create a new class in."); |
| 84 |
} |
| 85 |
|
| 86 |
// TODO Add package checking. |
| 87 |
|
| 88 |
// TODO This should be made more robust so that class names in different locations are allowed. |
| 89 |
// Check that a class of this name doesn't already exist. |
| 90 |
// TODO Check whether it should be "classname.java" or just "classname" |
| 91 |
IFile file = project.getFile(className); |
| 92 |
if (file.exists()){ |
| 93 |
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, |
| 94 |
"A class with this name already exists."); |
| 95 |
} |
| 96 |
|
| 97 |
return Status.OK_STATUS; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param monitor |
| 102 |
* @return The newly created class or null if there were problems creating |
| 103 |
* the class. |
| 104 |
* @throws CoreException |
| 105 |
*/ |
| 106 |
public ICompilationUnit construct(IProgressMonitor monitor) throws CoreException { |
| 107 |
if (!getStatus().isOK()) return null; |
| 108 |
|
| 109 |
ICompilationUnit javaClass = null; |
| 110 |
|
| 111 |
IJavaProject javaProject = getJavaProject(monitor); |
| 112 |
|
| 113 |
// TODO create package if necessary, probably need to use getPackage(path) |
| 114 |
// TODO if we leave this search in, refactor to a private helper method |
| 115 |
IPackageFragment javaPackage = null; |
| 116 |
IPackageFragment[] packages = javaProject.getPackageFragments(); |
| 117 |
// javaProject.getPackageFragmentRoot(resource); |
| 118 |
|
| 119 |
// Look for the package the new class file will be created within. |
| 120 |
for (IPackageFragment mypackage : packages) { |
| 121 |
// Package fragments include all packages in the classpath |
| 122 |
// We will only look at the package from the source folder |
| 123 |
// K_BINARY would include also included JARS, e.g. rt.jar |
| 124 |
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE && |
| 125 |
mypackage.getElementName() == packageName) { |
| 126 |
javaPackage = mypackage; |
| 127 |
System.out.println("Package: '" + mypackage.getElementName() + "'"); |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
String javaClassFilename = className + ".java"; |
| 133 |
|
| 134 |
// TODO fix this when we get a package automatically created so we know we have a package handle |
| 135 |
if (!(javaPackage == null)){ |
| 136 |
// TODO make sure this class doesn't already exist? Or do we just deal with the exception thrown by createCompilationUnit if force parameter is false |
| 137 |
javaClass = javaPackage.createCompilationUnit( |
| 138 |
javaClassFilename, |
| 139 |
"public class " + className + " { }", |
| 140 |
true, |
| 141 |
monitor); |
| 142 |
} |
| 143 |
|
| 144 |
return javaClass; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
private IJavaProject getJavaProject(IProgressMonitor monitor) |
| 149 |
throws CoreException { |
| 150 |
|
| 151 |
// TODO Do we need to figure out how to get the project that the user right clicked on if the action is coming from the package explorer? |
| 152 |
IProject project = getWorkspaceRoot().getProject(projectName); |
| 153 |
IJavaProject javaProject; |
| 154 |
|
| 155 |
if (!project.exists()){ |
| 156 |
// Create a new project if there is no project by the given name |
| 157 |
if (shouldCreateProjectIfNecessary){ |
| 158 |
NewJavaProjectConstructor constructor = new NewJavaProjectConstructor(); |
| 159 |
constructor.setProjectName(projectName); |
| 160 |
javaProject = constructor.construct(new NullProgressMonitor()); |
| 161 |
} else { |
| 162 |
// TODO deal with the project not existing and shouldCreateProject being false |
| 163 |
javaProject = null; |
| 164 |
} |
| 165 |
} |
| 166 |
// Or open the project if we found it and it's not open. |
| 167 |
else { |
| 168 |
// TODO figure out if we need this opening business |
| 169 |
if (!project.isOpen()){ |
| 170 |
project.open(monitor); |
| 171 |
} |
| 172 |
javaProject = JavaCore.create(project); |
| 173 |
} |
| 174 |
return javaProject; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private IWorkspaceRoot getWorkspaceRoot() { |
| 180 |
return getWorkspace().getRoot(); |
| 181 |
} |
| 182 |
|
| 183 |
private IWorkspace getWorkspace() { |
| 184 |
return ResourcesPlugin.getWorkspace(); |
| 185 |
} |
| 4 |
|
186 |
|
| 5 |
} |
187 |
} |