Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 157052 Details for
Bug 300275
Java Class Constructor class
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Version 01 - 2 ideas: constructor from scratch or wrapper for NewTypeWizardPage
JavaClassConstructorVer01.txt (text/plain), 17.89 KB, created by
Miles Billsman
on 2010-01-24 02:30:27 EST
(
hide
)
Description:
Version 01 - 2 ideas: constructor from scratch or wrapper for NewTypeWizardPage
Filename:
MIME Type:
Creator:
Miles Billsman
Created:
2010-01-24 02:30:27 EST
Size:
17.89 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.ide4edu.javalite.constructors >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.ide4edu/javalite/plugins/org.eclipse.ide4edu.javalite.constructors/META-INF/MANIFEST.MF,v >retrieving revision 1.2 >diff -u -r1.2 MANIFEST.MF >--- META-INF/MANIFEST.MF 21 Jan 2010 15:19:27 -0000 1.2 >+++ META-INF/MANIFEST.MF 24 Jan 2010 07:11:51 -0000 >@@ -8,6 +8,8 @@ > Require-Bundle: org.eclipse.core.resources;bundle-version="[3.5.0,4.0.0)", > org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)", > org.eclipse.jdt.core;bundle-version="[3.5.0,4.0.0)", >- org.eclipse.jdt.launching;bundle-version="[3.5.0,4.0.0)" >+ org.eclipse.jdt.launching;bundle-version="[3.5.0,4.0.0)", >+ org.eclipse.jdt.ui;bundle-version="3.5.1", >+ org.eclipse.jface;bundle-version="3.5.1" > Bundle-ActivationPolicy: lazy > Bundle-Activator: org.eclipse.ide4edu.javalite.constructors.Activator >Index: src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.ide4edu/javalite/plugins/org.eclipse.ide4edu.javalite.constructors/src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor.java,v >retrieving revision 1.1 >diff -u -r1.1 NewJavaClassConstructor.java >--- src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor.java 21 Jan 2010 03:14:23 -0000 1.1 >+++ src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor.java 24 Jan 2010 07:11:51 -0000 >@@ -1,5 +1,187 @@ > package org.eclipse.ide4edu.javalite.constructors; > >+import org.eclipse.core.resources.IFile; >+import org.eclipse.core.resources.IProject; >+import org.eclipse.core.resources.IResource; >+import org.eclipse.core.resources.IWorkspace; >+import org.eclipse.core.resources.IWorkspaceRoot; >+import org.eclipse.core.resources.ResourcesPlugin; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IProgressMonitor; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.NullProgressMonitor; >+import org.eclipse.core.runtime.Status; >+import org.eclipse.jdt.core.ICompilationUnit; >+import org.eclipse.jdt.core.IJavaProject; >+import org.eclipse.jdt.core.IPackageFragment; >+import org.eclipse.jdt.core.IPackageFragmentRoot; >+import org.eclipse.jdt.core.JavaCore; >+ >+/** >+ * Version 1 >+ * Java Class constructor that codes everything up from scratch. >+ */ > public class NewJavaClassConstructor { >+ >+ private String className = "MyClass"; >+ private String packageName = IPackageFragment.DEFAULT_PACKAGE_NAME; >+ private String projectName = "My Project"; >+ >+ /** >+ * Specifies whether this class should be created in the (default package) >+ * i.e at the root level of the project? >+ * >+ * When beginner programmers start out, all of their classes will be created >+ * at the root level of the project. When they move on to being intermediate >+ * programmers, they will be able to specify the package the class should >+ * be created in. >+ */ >+ private Boolean shouldUseDefaultPackage = true; >+ >+ /** >+ * Specifies whether to create a new default project if the workspace is >+ * empty. Otherwise we will prompt for project creation. >+ */ >+ private Boolean shouldCreateProjectIfNecessary = true; >+ >+ public void setProjectName(String projectName) { >+ this.projectName = projectName.trim(); >+ } >+ >+ public void setPackageName(String packageName) { >+ this.packageName = packageName.trim(); >+ } >+ >+ public void setClassName(String className) { >+ this.className = className.trim(); >+ } >+ >+ public void setUseDefaultPackage(Boolean shouldUseDefaultPackage) { >+ this.shouldUseDefaultPackage = shouldUseDefaultPackage; >+ } >+ >+ public void setCreateProjectIfNecessary(Boolean shouldCreateProjectIfNecessary) { >+ this.shouldCreateProjectIfNecessary = shouldCreateProjectIfNecessary; >+ } >+ >+ /** >+ * This method returns the status of the receiver in the form of >+ * an {@link IStatus} instance. The answer will reflect any >+ * potential error state in the receiver, or {@link Status#OK_STATUS} >+ * if there is no error. >+ * >+ * @return Instance of {@link IStatus} >+ */ >+ public IStatus getStatus() { >+ IStatus status = getWorkspace().validateName(className, IResource.FILE); >+ if (!status.isOK()) return status; >+ >+ IProject project= getWorkspaceRoot().getProject(projectName); >+ if (!shouldCreateProjectIfNecessary && !project.exists()) { >+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, >+ "There is no project by the name " + projectName + >+ " to create a new class in."); >+ } >+ >+ // TODO Add package checking. >+ >+ // TODO This should be made more robust so that class names in different locations are allowed. >+ // Check that a class of this name doesn't already exist. >+ // TODO Check whether it should be "classname.java" or just "classname" >+ IFile file = project.getFile(className); >+ if (file.exists()){ >+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, >+ "A class with this name already exists."); >+ } >+ >+ return Status.OK_STATUS; >+ } >+ >+ /** >+ * @param monitor >+ * @return The newly created class or null if there were problems creating >+ * the class. >+ * @throws CoreException >+ */ >+ public ICompilationUnit construct(IProgressMonitor monitor) throws CoreException { >+ if (!getStatus().isOK()) return null; >+ >+ ICompilationUnit javaClass = null; >+ >+ IJavaProject javaProject = getJavaProject(monitor); >+ >+ // TODO create package if necessary, probably need to use getPackage(path) >+ // TODO if we leave this search in, refactor to a private helper method >+ IPackageFragment javaPackage = null; >+ IPackageFragment[] packages = javaProject.getPackageFragments(); >+// javaProject.getPackageFragmentRoot(resource); >+ >+ // Look for the package the new class file will be created within. >+ for (IPackageFragment mypackage : packages) { >+ // Package fragments include all packages in the classpath >+ // We will only look at the package from the source folder >+ // K_BINARY would include also included JARS, e.g. rt.jar >+ if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE && >+ mypackage.getElementName() == packageName) { >+ javaPackage = mypackage; >+ System.out.println("Package: '" + mypackage.getElementName() + "'"); >+ break; >+ } >+ } >+ >+ String javaClassFilename = className + ".java"; >+ >+ // TODO fix this when we get a package automatically created so we know we have a package handle >+ if (!(javaPackage == null)){ >+ // 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 >+ javaClass = javaPackage.createCompilationUnit( >+ javaClassFilename, >+ "public class " + className + " { }", >+ true, >+ monitor); >+ } >+ >+ return javaClass; >+ } >+ >+ >+ private IJavaProject getJavaProject(IProgressMonitor monitor) >+ throws CoreException { >+ >+ // 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? >+ IProject project = getWorkspaceRoot().getProject(projectName); >+ IJavaProject javaProject; >+ >+ if (!project.exists()){ >+ // Create a new project if there is no project by the given name >+ if (shouldCreateProjectIfNecessary){ >+ NewJavaProjectConstructor constructor = new NewJavaProjectConstructor(); >+ constructor.setProjectName(projectName); >+ javaProject = constructor.construct(new NullProgressMonitor()); >+ } else { >+ // TODO deal with the project not existing and shouldCreateProject being false >+ javaProject = null; >+ } >+ } >+ // Or open the project if we found it and it's not open. >+ else { >+ // TODO figure out if we need this opening business >+ if (!project.isOpen()){ >+ project.open(monitor); >+ } >+ javaProject = JavaCore.create(project); >+ } >+ return javaProject; >+ } >+ >+ >+ >+ private IWorkspaceRoot getWorkspaceRoot() { >+ return getWorkspace().getRoot(); >+ } >+ >+ private IWorkspace getWorkspace() { >+ return ResourcesPlugin.getWorkspace(); >+ } > > } >Index: src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor2.java >=================================================================== >RCS file: src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor2.java >diff -N src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor2.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor2.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,200 @@ >+package org.eclipse.ide4edu.javalite.constructors; >+ >+import org.eclipse.core.resources.IFile; >+import org.eclipse.core.resources.IProject; >+import org.eclipse.core.resources.IResource; >+import org.eclipse.core.resources.IWorkspace; >+import org.eclipse.core.resources.IWorkspaceRoot; >+import org.eclipse.core.resources.ResourcesPlugin; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IProgressMonitor; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.NullProgressMonitor; >+import org.eclipse.core.runtime.Status; >+import org.eclipse.jdt.core.ICompilationUnit; >+import org.eclipse.jdt.core.IJavaProject; >+import org.eclipse.jdt.core.IPackageFragment; >+import org.eclipse.jdt.core.IPackageFragmentRoot; >+import org.eclipse.jdt.core.JavaCore; >+import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; >+import org.eclipse.swt.widgets.Composite; >+ >+/** >+ * Version 2 >+ * Java Class constructor that basically wraps the NewTypeWizardPage in the >+ * hopes that this will make initial templating, extending superclasses and >+ * implementing interfaces easier to implement. >+ */ >+public class NewJavaClassConstructor2 { >+ >+ private NewTypeWizardPageWrapper wrapper = new NewTypeWizardPageWrapper(); >+ >+ private String packageName = IPackageFragment.DEFAULT_PACKAGE_NAME; >+ private String projectName = "My Project"; >+ >+ /** >+ * Specifies whether this class should be created in the (default package) >+ * i.e at the root level of the project? >+ * >+ * When beginner programmers start out, all of their classes will be created >+ * at the root level of the project. When they move on to being intermediate >+ * programmers, they will be able to specify the package the class should >+ * be created in. >+ */ >+ private Boolean shouldUseDefaultPackage = true; >+ >+ /** >+ * Specifies whether to create a new default project if the workspace is >+ * empty. Otherwise we will prompt for project creation. >+ */ >+ private Boolean shouldCreateProjectIfNecessary = true; >+ >+ >+ >+ public NewJavaClassConstructor2(){ >+ setClassName("MyClass"); >+ } >+ >+ >+ public void setProjectName(String projectName) { >+ this.projectName = projectName.trim(); >+ } >+ >+ public void setPackageName(String packageName) { >+ this.packageName = packageName.trim(); >+ } >+ >+ public void setClassName(String className) { >+ wrapper.setTypeName(className.trim(), true); >+ } >+ >+ public String getClassName() { >+ return wrapper.getTypeName(); >+ } >+ >+ // TODO Eventually add support for Superclasses and Interfaces >+ >+ public void setUseDefaultPackage(Boolean shouldUseDefaultPackage) { >+ this.shouldUseDefaultPackage = shouldUseDefaultPackage; >+ } >+ >+ public void setCreateProjectIfNecessary(Boolean shouldCreateProjectIfNecessary) { >+ this.shouldCreateProjectIfNecessary = shouldCreateProjectIfNecessary; >+ } >+ >+ >+ /** >+ * This method returns the status of the receiver in the form of >+ * an {@link IStatus} instance. The answer will reflect any >+ * potential error state in the receiver, or {@link Status#OK_STATUS} >+ * if there is no error. >+ * >+ * @return Instance of {@link IStatus} >+ */ >+ public IStatus getStatus() { >+ IStatus status = getWorkspace().validateName(getClassName(), IResource.FILE); >+ if (!status.isOK()) return status; >+ >+ IProject project= getWorkspaceRoot().getProject(projectName); >+ if (!shouldCreateProjectIfNecessary && !project.exists()) { >+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, >+ "There is no project by the name " + projectName + >+ " to create a new class in."); >+ } >+ >+ return Status.OK_STATUS; >+ } >+ >+ /** >+ * @param monitor >+ * @return The newly created class file or null if there were problems >+ * creating the class. >+ * @throws CoreException >+ * @throws InterruptedException >+ */ >+ public ICompilationUnit construct(IProgressMonitor monitor) throws CoreException, InterruptedException { >+ if (!getStatus().isOK()) return null; >+ >+ ICompilationUnit javaClass = null; >+ >+ IJavaProject javaProject = getJavaProject(monitor); >+ >+ // TODO create package if it doesn't exist >+ // TODO then call wrapper.setPackageFragment() >+ >+ // Create new class. >+ wrapper.createType(monitor); >+ >+ return javaClass; >+ >+ } >+ >+ >+ private IJavaProject getJavaProject(IProgressMonitor monitor) >+ throws CoreException { >+ >+ IProject project = getWorkspaceRoot().getProject(projectName); >+ IJavaProject javaProject; >+ >+ if (!project.exists()) { >+ // Create a new project if there is no project by the given name >+ if (shouldCreateProjectIfNecessary) { >+ NewJavaProjectConstructor constructor = new NewJavaProjectConstructor(); >+ constructor.setProjectName(projectName); >+ javaProject = constructor.construct(new NullProgressMonitor()); >+ } else { >+ // TODO deal with the project not existing and shouldCreateProject being false >+ javaProject = null; >+ } >+ } >+ // Or open the project if we found it and it's not open. >+ else { >+ javaProject = JavaCore.create(project); >+ } >+ return javaProject; >+ } >+ >+ private IWorkspaceRoot getWorkspaceRoot() { >+ return getWorkspace().getRoot(); >+ } >+ >+ private IWorkspace getWorkspace() { >+ return ResourcesPlugin.getWorkspace(); >+ } >+ >+ >+ >+ >+ >+ >+ >+ >+ >+ /** >+ * Wrapper for NewTypeWizardPage that allows us to use all of the class >+ * creation code already written. >+ */ >+ private class NewTypeWizardPageWrapper extends NewTypeWizardPage { >+ >+ public NewTypeWizardPageWrapper(){ >+ this(true, ""); >+ } >+ >+ public NewTypeWizardPageWrapper(boolean isClass, String pageName) { >+ super(isClass, pageName); >+ } >+ >+ public NewTypeWizardPageWrapper(int typeKind, String pageName) { >+ super(typeKind, pageName); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) >+ */ >+ public void createControl(Composite parent) { >+ >+ } >+ >+ } >+ >+} >#P org.eclipse.ide4edu.javalite.constructors.tests >Index: src/org/eclipse/ide4edu/javalite/constructors/tests/NewJavaClassConstructorTests.java >=================================================================== >RCS file: src/org/eclipse/ide4edu/javalite/constructors/tests/NewJavaClassConstructorTests.java >diff -N src/org/eclipse/ide4edu/javalite/constructors/tests/NewJavaClassConstructorTests.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ide4edu/javalite/constructors/tests/NewJavaClassConstructorTests.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,61 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 The Eclipse Foundation and others >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Eclipse Foundation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.ide4edu.javalite.constructors.tests; >+ >+import static org.junit.Assert.*; >+ >+import org.eclipse.core.resources.IFile; >+import org.eclipse.core.resources.ResourcesPlugin; >+import org.eclipse.core.runtime.NullProgressMonitor; >+import org.eclipse.ide4edu.javalite.constructors.NewJavaClassConstructor; >+import org.eclipse.ide4edu.javalite.constructors.NewJavaClassConstructor2; >+import org.eclipse.ide4edu.javalite.constructors.NewJavaProjectConstructor; >+import org.junit.Test; >+ >+public class NewJavaClassConstructorTests { >+ >+ @Test >+ public void testClassCreation() throws Exception { >+ NewJavaProjectConstructor projectConstructor = new NewJavaProjectConstructor(); >+ projectConstructor.setProjectName("AppleProject"); >+ projectConstructor.construct(new NullProgressMonitor()); >+ >+ NewJavaClassConstructor classConstructor = new NewJavaClassConstructor(); >+ classConstructor.setClassName("AppleClass"); >+ classConstructor.setProjectName("AppleProject"); >+ classConstructor.construct(new NullProgressMonitor()); >+ >+ IFile javaClassFile = ResourcesPlugin.getWorkspace( >+ ).getRoot().getProject("AppleProject").getFile("AppleClass.java"); >+ assertTrue(javaClassFile.exists()); >+ // TODO add test for contents of file, are they "public class AppleClass { }"? >+ >+ } >+ >+ @Test >+ public void testClassCreation3() throws Exception { >+ NewJavaProjectConstructor projectConstructor = new NewJavaProjectConstructor(); >+ projectConstructor.setProjectName("AppleProject"); >+ projectConstructor.construct(new NullProgressMonitor()); >+ >+ NewJavaClassConstructor2 classConstructor = new NewJavaClassConstructor2(); >+ classConstructor.setClassName("AppleClass"); >+ classConstructor.setProjectName("AppleProject"); >+ classConstructor.construct(new NullProgressMonitor()); >+ >+ IFile javaClassFile = ResourcesPlugin.getWorkspace( >+ ).getRoot().getProject("AppleProject").getFile("AppleClass.java"); >+ assertTrue(javaClassFile.exists()); >+ // TODO add test for contents of file, are they "public class AppleClass { }"? >+ >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
wayne.beaton
:
iplog+
Actions:
View
|
Diff
Attachments on
bug 300275
: 157052 |
157675