Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 300275
Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +3 lines)
Lines 8-13 Link Here
8
Require-Bundle: org.eclipse.core.resources;bundle-version="[3.5.0,4.0.0)",
8
Require-Bundle: org.eclipse.core.resources;bundle-version="[3.5.0,4.0.0)",
9
 org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)",
9
 org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)",
10
 org.eclipse.jdt.core;bundle-version="[3.5.0,4.0.0)",
10
 org.eclipse.jdt.core;bundle-version="[3.5.0,4.0.0)",
11
 org.eclipse.jdt.launching;bundle-version="[3.5.0,4.0.0)"
11
 org.eclipse.jdt.launching;bundle-version="[3.5.0,4.0.0)",
12
 org.eclipse.jdt.ui;bundle-version="3.5.1",
13
 org.eclipse.jface;bundle-version="3.5.1"
12
Bundle-ActivationPolicy: lazy
14
Bundle-ActivationPolicy: lazy
13
Bundle-Activator: org.eclipse.ide4edu.javalite.constructors.Activator
15
Bundle-Activator: org.eclipse.ide4edu.javalite.constructors.Activator
(-)src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor.java (+182 lines)
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
}
(-)src/org/eclipse/ide4edu/javalite/constructors/NewJavaClassConstructor2.java (+200 lines)
Added Link Here
1
package org.eclipse.ide4edu.javalite.constructors;
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
import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
20
import org.eclipse.swt.widgets.Composite;
21
22
/**
23
 * Version 2
24
 * Java Class constructor that basically wraps the NewTypeWizardPage in the
25
 * hopes that this will make initial templating, extending superclasses and
26
 * implementing interfaces easier to implement.
27
 */
28
public class NewJavaClassConstructor2 {
29
	
30
	private NewTypeWizardPageWrapper wrapper = new NewTypeWizardPageWrapper();
31
	
32
	private String packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
33
	private String projectName = "My Project";
34
	
35
	/**
36
	 * Specifies whether this class should be created in the (default package) 
37
	 * i.e at the root level of the project? 
38
	 * 
39
	 * When beginner programmers start out, all of their classes will be created 
40
	 * at the root level of the project. When they move on to being intermediate 
41
	 * programmers, they will be able to specify the package the class should
42
	 * be created in.
43
	 */
44
	private Boolean shouldUseDefaultPackage = true;
45
	
46
	/**
47
	 * Specifies whether to create a new default project if the workspace is
48
	 * empty. Otherwise we will prompt for project creation.
49
	 */
50
	private Boolean shouldCreateProjectIfNecessary = true;
51
	
52
	
53
	
54
	public NewJavaClassConstructor2(){
55
		setClassName("MyClass");
56
	}
57
	
58
	
59
	public void setProjectName(String projectName) {
60
		this.projectName = projectName.trim();
61
	}
62
	
63
	public void setPackageName(String packageName) {
64
		this.packageName = packageName.trim();
65
	}
66
67
	public void setClassName(String className) {
68
		wrapper.setTypeName(className.trim(), true);
69
	}
70
	
71
	public String getClassName() {
72
		return wrapper.getTypeName();
73
	}
74
	
75
	// TODO Eventually add support for Superclasses and Interfaces 
76
77
	public void setUseDefaultPackage(Boolean shouldUseDefaultPackage) {
78
		this.shouldUseDefaultPackage = shouldUseDefaultPackage;
79
	}
80
81
	public void setCreateProjectIfNecessary(Boolean shouldCreateProjectIfNecessary) {
82
		this.shouldCreateProjectIfNecessary = shouldCreateProjectIfNecessary;
83
	}
84
	
85
	
86
	/**
87
	 * This method returns the status of the receiver in the form of
88
	 * an {@link IStatus} instance. The answer will reflect any
89
	 * potential error state in the receiver, or {@link Status#OK_STATUS}
90
	 * if there is no error.
91
	 * 
92
	 * @return Instance of {@link IStatus}
93
	 */
94
	public IStatus getStatus() {
95
		IStatus status = getWorkspace().validateName(getClassName(), IResource.FILE);
96
		if (!status.isOK()) return status;
97
		
98
		IProject project= getWorkspaceRoot().getProject(projectName);
99
		if (!shouldCreateProjectIfNecessary && !project.exists()) {
100
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, 
101
					"There is no project by the name " + projectName + 
102
					" to create a new class in.");
103
		}
104
		
105
		return Status.OK_STATUS;
106
	}
107
	
108
	/**
109
	 * @param monitor
110
	 * @return The newly created class file or null if there were problems 
111
	 * 		   creating the class.
112
	 * @throws CoreException
113
	 * @throws InterruptedException 
114
	 */
115
	public ICompilationUnit construct(IProgressMonitor monitor) throws CoreException, InterruptedException {
116
		if (!getStatus().isOK()) return null;
117
		
118
		ICompilationUnit javaClass = null;
119
		
120
		IJavaProject javaProject = getJavaProject(monitor);
121
		
122
		// TODO create package if it doesn't exist
123
		// TODO then call wrapper.setPackageFragment()
124
		
125
		// Create new class.
126
		wrapper.createType(monitor);
127
		
128
		return javaClass;
129
		
130
	}
131
	
132
	
133
	private IJavaProject getJavaProject(IProgressMonitor monitor)
134
			throws CoreException {
135
		
136
		IProject project = getWorkspaceRoot().getProject(projectName);
137
		IJavaProject javaProject;
138
139
		if (!project.exists()) {
140
			// Create a new project if there is no project by the given name
141
			if (shouldCreateProjectIfNecessary) {
142
				NewJavaProjectConstructor constructor = new NewJavaProjectConstructor();
143
				constructor.setProjectName(projectName);
144
				javaProject = constructor.construct(new NullProgressMonitor());
145
			} else {
146
				// TODO deal with the project not existing and shouldCreateProject being false
147
				javaProject = null;
148
			}
149
		}
150
		// Or open the project if we found it and it's not open.
151
		else {
152
			javaProject = JavaCore.create(project);
153
		}
154
		return javaProject;
155
	}
156
	
157
	private IWorkspaceRoot getWorkspaceRoot() {
158
		return getWorkspace().getRoot();
159
	}
160
161
	private IWorkspace getWorkspace() {
162
		return ResourcesPlugin.getWorkspace();
163
	}
164
	
165
	
166
	
167
	
168
	
169
	
170
	
171
	
172
	
173
	/**
174
	 * Wrapper for NewTypeWizardPage that allows us to use all of the class
175
	 * creation code already written.
176
	 */
177
	private class NewTypeWizardPageWrapper extends NewTypeWizardPage {
178
		
179
		public NewTypeWizardPageWrapper(){
180
			this(true, "");
181
		}
182
		
183
		public NewTypeWizardPageWrapper(boolean isClass, String pageName) {
184
			super(isClass, pageName);
185
		}
186
187
		public NewTypeWizardPageWrapper(int typeKind, String pageName) {
188
			super(typeKind, pageName);
189
		}
190
191
		/* (non-Javadoc)
192
		 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
193
		 */
194
		public void createControl(Composite parent) {
195
196
		}
197
198
	}
199
	
200
}
(-)src/org/eclipse/ide4edu/javalite/constructors/tests/NewJavaClassConstructorTests.java (+61 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 The Eclipse Foundation and others
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Eclipse Foundation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ide4edu.javalite.constructors.tests;
12
13
import static org.junit.Assert.*;
14
15
import org.eclipse.core.resources.IFile;
16
import org.eclipse.core.resources.ResourcesPlugin;
17
import org.eclipse.core.runtime.NullProgressMonitor;
18
import org.eclipse.ide4edu.javalite.constructors.NewJavaClassConstructor;
19
import org.eclipse.ide4edu.javalite.constructors.NewJavaClassConstructor2;
20
import org.eclipse.ide4edu.javalite.constructors.NewJavaProjectConstructor;
21
import org.junit.Test;
22
23
public class NewJavaClassConstructorTests {
24
25
	@Test
26
	public void testClassCreation() throws Exception {
27
		NewJavaProjectConstructor projectConstructor = new NewJavaProjectConstructor();
28
		projectConstructor.setProjectName("AppleProject");
29
		projectConstructor.construct(new NullProgressMonitor());
30
		
31
		NewJavaClassConstructor classConstructor = new NewJavaClassConstructor();
32
		classConstructor.setClassName("AppleClass");
33
		classConstructor.setProjectName("AppleProject");
34
		classConstructor.construct(new NullProgressMonitor());
35
		
36
		IFile javaClassFile = ResourcesPlugin.getWorkspace(
37
				).getRoot().getProject("AppleProject").getFile("AppleClass.java");
38
		assertTrue(javaClassFile.exists());
39
		// TODO add test for contents of file, are they "public class AppleClass { }"?
40
		
41
	}
42
	
43
	@Test
44
	public void testClassCreation3() throws Exception {
45
		NewJavaProjectConstructor projectConstructor = new NewJavaProjectConstructor();
46
		projectConstructor.setProjectName("AppleProject");
47
		projectConstructor.construct(new NullProgressMonitor());
48
		
49
		NewJavaClassConstructor2 classConstructor = new NewJavaClassConstructor2();
50
		classConstructor.setClassName("AppleClass");
51
		classConstructor.setProjectName("AppleProject");
52
		classConstructor.construct(new NullProgressMonitor());
53
		
54
		IFile javaClassFile = ResourcesPlugin.getWorkspace(
55
				).getRoot().getProject("AppleProject").getFile("AppleClass.java");
56
		assertTrue(javaClassFile.exists());
57
		// TODO add test for contents of file, are they "public class AppleClass { }"?
58
		
59
	}
60
	
61
}

Return to bug 300275