這是些用來留作筆記的代碼,只適合有緣者。
功能包括:
創建新的java工程
設置默認的JRE庫
創建源文件夾
創建輸出文件夾
創建包
創建Java類
代碼未經任何重構和整理,只適合有緣者,
嚴禁亂噴!!
1 package javame.model.action;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.eclipse.core.resources.ICommand;
8 import org.eclipse.core.resources.IFolder;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.resources.IProjectDescription;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.IWorkspace;
13 import org.eclipse.core.resources.IWorkspaceRoot;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jdt.core.IClasspathEntry;
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.ui.PreferenceConstants;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.text.edits.TextEdit;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
33
34 public class JavaModelAction implements IWorkbenchWindowActionDelegate {
35 private IWorkbenchWindow window;
36
37 @Override
38 public void dispose() {
39 // TODO Auto-generated method stub
40
41 }
42
43 @Override
44 public void init(IWorkbenchWindow window) {
45 // TODO Auto-generated method stub
46 this.window = window;
47 }
48
49 @Override
50 public void run(IAction action) {
51 // 獲取工作區
52 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
53
54 // ///////////////////////////////////創建新項目///////////////////////////
55 final IProject project = root.getProject("xyz");
56 // 為項目指定存放路徑,默認放在當前工作區
57 IPath projectPath = new Path("g:/myplugIn");
58
59 // 設置工程的位置
60 IWorkspace workspace = root.getWorkspace();
61 final IProjectDescription description = workspace
62 .newProjectDescription(project.getName());
63 description.setLocation(projectPath);
64
65 // 設置工程標記,即為java工程
66 String[] javaNature = description.getNatureIds();
67 String[] newJavaNature = new String[javaNature.length + 1];
68 System.arraycopy(javaNature, 0, newJavaNature, 0, javaNature.length);
69 newJavaNature[javaNature.length] = "org.eclipse.jdt.core.javanature"; // 這個標記證明本工程是Java工程
70 description.setNatureIds(newJavaNature);
71
72 // /////////////////////////////
73 try {
74 NullProgressMonitor monitor = new NullProgressMonitor();
75 project.create(description, monitor);
76 project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(
77 monitor, 1000));
78 } catch (CoreException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
82
83 // 下面代碼只在新建向導的情況下才可用
84 /*
85 * //創建新項目,WorkspaceModifyOperation位于org.eclipse.ui.ide中
86 * WorkspaceModifyOperation workspaceModifyOperation = new
87 * WorkspaceModifyOperation() {
88 *
89 * @Override protected void execute(IProgressMonitor monitor) throws
90 * CoreException, InvocationTargetException, InterruptedException { try
91 * { monitor.beginTask("", 2000); project.create(description, monitor);
92 *
93 * if(monitor.isCanceled()){ throw new OperationCanceledException(); }
94 *
95 * project.open(IResource.BACKGROUND_REFRESH, new
96 * SubProgressMonitor(monitor, 1000)); } catch (Exception e) {
97 * e.printStackTrace(); } finally{ monitor.done(); } } };
98 * //接下來調用IWizard的getContainer().run()方法.
99 */
100
101 // 轉化成java工程
102 IJavaProject javaProject = JavaCore.create(project);
103 // //////////////////////////////////添加JRE庫////////////////////////////
104 try {
105 // 獲取默認的JRE庫
106 IClasspathEntry[] jreLibrary = PreferenceConstants
107 .getDefaultJRELibrary();
108 // 獲取原來的build path
109 IClasspathEntry[] oldClasspathEntries = javaProject
110 .getRawClasspath();
111 List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
112 list.addAll(Arrays.asList(jreLibrary));
113 list.addAll(Arrays.asList(oldClasspathEntries));
114
115 javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list
116 .size()]), null);
117 } catch (JavaModelException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 }
121
122 // //////////////////////////////////創建輸出路徑/////////////////////////////
123 IFolder binFolder = javaProject.getProject().getFolder("bin");
124 try {
125 binFolder.create(true, true, null);
126 javaProject.setOutputLocation(binFolder.getFullPath(), null);
127 } catch (CoreException e) {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
131
132 // /////////////////////////設置Java生成器///////////////////////
133 try {
134 IProjectDescription description2 = javaProject.getProject()
135 .getDescription();
136 ICommand command = description2.newCommand();
137 command.setBuilderName("org.eclipse.jdt.core.javabuilder");
138 description2.setBuildSpec(new ICommand[] { command });
139 description2
140 .setNatureIds(new String[] { "org.eclipse.jdt.core.javanature" });
141 javaProject.getProject().setDescription(description2, null);
142 } catch (CoreException e1) {
143 // TODO Auto-generated catch block
144 e1.printStackTrace();
145 }
146
147 // /////////////////////////////創建源代碼文件夾//////////////////////////
148 // ///////////源文件夾和文件夾相似,只是使用PackageFragmentRoot進行了封裝////////
149 IFolder srcFolder = javaProject.getProject().getFolder("src");
150 try {
151 srcFolder.create(true, true, null);
152 // this.createFolder(srcFolder);
153 // 創建SourceLibrary
154 IClasspathEntry srcClasspathEntry = JavaCore
155 .newSourceEntry(srcFolder.getFullPath());
156
157 // 得到舊的build path
158 IClasspathEntry[] oldClasspathEntries = javaProject
159 .readRawClasspath();
160
161 // 添加新的
162 List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
163 list.addAll(Arrays.asList(oldClasspathEntries));
164 list.add(srcClasspathEntry);
165
166 // 原來存在一個與工程名相同的源文件夾,必須先刪除
167 IClasspathEntry temp = JavaCore.newSourceEntry(new Path("/xyz"));
168 if (list.contains(temp)) {
169 list.remove(temp);
170 }
171
172 System.out.println(list.size());
173
174 javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list
175 .size()]), null);
176 } catch (CoreException e) {
177 // TODO Auto-generated catch block
178 e.printStackTrace();
179 }
180
181 // ///////////////////////////////創建包//////////////////////////
182 // IPackageFragmentRoot packageFragmentRoot =
183 // javaProject.getPackageFragmentRoot(javaProject.getResource());
184
185 try {
186 // 先找指定的源文件夾所在的IPackageFragmentRoot
187 IPackageFragmentRoot packageFragmentRoot = javaProject
188 .findPackageFragmentRoot(new Path("/xyz/src"));
189 // 根據IPackageFragmentRoot創建IPackageFragment,IPackageFragment就是包了
190 IPackageFragment packageFragment = packageFragmentRoot
191 .createPackageFragment("com.aptech.plugin", true, null);
192 } catch (JavaModelException e) {
193 // TODO Auto-generated catch block
194 e.printStackTrace();
195 }
196
197 // //////////////////////////////////創建Java文件////////////////////////
198 try {
199 String javaCode = "package com.aptech.plugin;public class HelloWorld{public static void main(String[] args){System.out.println(\"中華人民共和國\");}}";
200 IPackageFragmentRoot packageFragmentRoot = javaProject
201 .findPackageFragmentRoot(new Path("/xyz/src"));
202 IPackageFragment packageFragment = packageFragmentRoot
203 .getPackageFragment("com.aptech.plugin");
204 packageFragment.createCompilationUnit("HelloWorld.java", javaCode,
205 true, new NullProgressMonitor());
206
207 } catch (JavaModelException e) {
208 // TODO Auto-generated catch block
209 e.printStackTrace();
210 } catch (Exception e) {
211 // TODO Auto-generated catch block
212 e.printStackTrace();
213 }
214
215 // ///////////////////////////////格式化java代碼//////////////////
216 }
217
218 @Override
219 public void selectionChanged(IAction action, ISelection selection) {
220 // TODO Auto-generated method stub
221 }
222
223 }
224