在上一篇中,我们已经建立了一个向导想生成项目,这一篇中将介绍怎样在Eclipse透视图中建立一个项目及所需的东西。
首先,我们在向导点击Finish完后,然后生成一个项目,如图:

这里就不多说了,贴出代码出来:

NewCodeGenWizard
1
/** *//**
2
* @see org.eclipse.jface.wizard.Wizard#performFinish()
3
*/
4
public boolean performFinish()
{
5
createJavaProject();
6
if (_newProject == null)
{
7
return false;
8
} else
{
9
updatePerspective();
10
selectAndReveal(_newProject);
11
return true;
12
}
13
}
14
15
/** *//**
16
* 创建一个Java项目
17
*/
18
public void createJavaProject()
{
19
_newProject = createNewProject();
20
IJavaProject ijavaproject = JavaCore.create(_newProject);
21
try
{
22
createOutputLocation(ijavaproject);
23
addJavaBuilder(ijavaproject);
24
setClasspath(ijavaproject);
25
_newProject.build(IncrementalProjectBuilder.FULL_BUILD, null);
26
} catch (JavaModelException e)
{
27
e.printStackTrace();
28
} catch (CoreException e)
{
29
e.printStackTrace();
30
}
31
}
32
33
/** *//**
34
* 创建一个新项目
35
*
36
* @return
37
*/
38
private IProject createNewProject()
{
39
if (_newProject != null)
40
return _newProject;
41
42
// 得到一个项目的句柄
43
final IProject newProjectHandle = _mainPage.getProjectHandle();
44
45
// 得到一个项目的描述
46
IPath newPath = null;
47
if (!_mainPage.useDefaults())
48
newPath = _mainPage.getLocationPath();
49
50
IWorkspace workspace = ResourcesPlugin.getWorkspace();
51
final IProjectDescription description = workspace
52
.newProjectDescription(newProjectHandle.getName());
53
description.setLocation(newPath);
54
55
// 增加Java Nature,否则在ijavaproject.setOutputLocation这句就会出错
56
addJavaNature(description);
57
58
// 创建一个项目的操作
59
WorkspaceModifyOperation op = new WorkspaceModifyOperation()
{
60
protected void execute(IProgressMonitor monitor)
61
throws CoreException
{
62
createProject(description, newProjectHandle, monitor);
63
}
64
};
65
66
// 运行一个新的项目操作,即会执行op中的execute方法
67
try
{
68
getContainer().run(true, true, op);
69
} catch (InterruptedException e)
{
70
return null;
71
} catch (InvocationTargetException e)
{
72
return null;
73
}
74
75
_newProject = newProjectHandle;
76
77
return _newProject;
78
}
79
80
/** *//**
81
* 增加Java的Nature
82
*
83
* @param iprojectdescription
84
*/
85
private void addJavaNature(IProjectDescription iprojectdescription)
{
86
ArrayList arraylist = new ArrayList();
87
arraylist.addAll(Arrays.asList(iprojectdescription.getNatureIds()));
88
arraylist.add("org.eclipse.jdt.core.javanature");
89
iprojectdescription.setNatureIds((String[]) arraylist
90
.toArray(new String[arraylist.size()]));
91
}
92
93
/** *//**
94
* 创建一个新项目
95
*
96
* @param description
97
* @param projectHandle
98
* @param monitor
99
* @throws CoreException
100
* @throws OperationCanceledException
101
*/
102
private void createProject(IProjectDescription description,
103
IProject projectHandle, IProgressMonitor monitor)
104
throws CoreException, OperationCanceledException
{
105
try
{
106
monitor.beginTask("", 2000);
107
108
projectHandle.create(description, new SubProgressMonitor(monitor,
109
1000));
110
111
if (monitor.isCanceled())
112
throw new OperationCanceledException();
113
114
projectHandle.open(IResource.BACKGROUND_REFRESH,
115
new SubProgressMonitor(monitor, 1000));
116
117
} finally
{
118
monitor.done();
119
}
120
}
121
122
/** *//**
123
* 创建输出文件
124
*
125
* @param ijavaproject
126
* @throws JavaModelException
127
* @throws CoreException
128
*/
129
private void createOutputLocation(IJavaProject ijavaproject)
130
throws JavaModelException, CoreException
{
131
IFolder ifolder = ijavaproject.getProject().getFolder("bin");
132
createFolder(ifolder);
133
IPath ipath = ifolder.getFullPath();
134
ijavaproject.setOutputLocation(ipath, null);
135
}
136
137
/** *//**
138
* 增加Java生成器
139
*
140
* @param ijavaproject
141
* @throws CoreException
142
*/
143
private void addJavaBuilder(IJavaProject ijavaproject) throws CoreException
{
144
IProjectDescription iprojectdescription = ijavaproject.getProject()
145
.getDescription();
146
ICommand icommand = iprojectdescription.newCommand();
147
icommand.setBuilderName("org.eclipse.jdt.core.javabuilder");
148
iprojectdescription.setBuildSpec(new ICommand[]
{ icommand });
149
iprojectdescription
150
.setNatureIds(new String[]
{ "org.eclipse.jdt.core.javanature" });
151
ijavaproject.getProject().setDescription(iprojectdescription, null);
152
}
153
154
/** *//**
155
* 设置Classpath
156
*
157
* @param ijavaproject
158
* @throws JavaModelException
159
* @throws CoreException
160
*/
161
private void setClasspath(IJavaProject ijavaproject)
162
throws JavaModelException, CoreException
{
163
ijavaproject.setRawClasspath(new IClasspathEntry[0], null);
164
addSourceFolders(ijavaproject);
165
addJRELibraries(ijavaproject);
166
addCodeGenLibraries(ijavaproject);
167
}
168
169
/** *//**
170
* 增加JRE库
171
*
172
* @param ijavaproject
173
* @throws JavaModelException
174
*/
175
private void addJRELibraries(IJavaProject ijavaproject)
176
throws JavaModelException
{
177
ArrayList arraylist = new ArrayList();
178
arraylist.addAll(Arrays.asList(ijavaproject.getRawClasspath()));
179
arraylist.addAll(Arrays.asList(PreferenceConstants
180
.getDefaultJRELibrary()));
181
ijavaproject.setRawClasspath((IClasspathEntry[]) arraylist
182
.toArray(new IClasspathEntry[arraylist.size()]), null);
183
}
184
185
/** *//**
186
* 增加代码生成中所需要的Jar库
187
*
188
* @param ijavaproject
189
* @throws JavaModelException
190
*/
191
private void addCodeGenLibraries(IJavaProject ijavaproject)
192
throws JavaModelException
{
193
// TODO:添加代码生成项目中所需要的Jar包
194
}
195
196
/** *//**
197
* 增加多个源文件目录
198
*
199
* @param ijavaproject
200
* @throws JavaModelException
201
* @throws CoreException
202
*/
203
private void addSourceFolders(IJavaProject ijavaproject)
204
throws JavaModelException, CoreException
{
205
ArrayList arraylist = new ArrayList();
206
arraylist.addAll(Arrays.asList(ijavaproject.getRawClasspath()));
207
addSourceFolder(ijavaproject, arraylist, "src/java");
208
addSourceFolder(ijavaproject, arraylist, "src/resource");
209
ijavaproject.setRawClasspath((IClasspathEntry[]) arraylist
210
.toArray(new IClasspathEntry[arraylist.size()]), null);
211
}
212
213
/** *//**
214
* 增加源文件目录
215
*
216
* @param ijavaproject
217
* @param arraylist
218
* @param s
219
* @throws CoreException
220
*/
221
private void addSourceFolder(IJavaProject ijavaproject,
222
ArrayList arraylist, String s) throws CoreException
{
223
IFolder ifolder = ijavaproject.getProject().getFolder(s);
224
createFolder(ifolder);
225
IPackageFragmentRoot ipackagefragmentroot = ijavaproject
226
.getPackageFragmentRoot(ifolder);
227
arraylist.add(JavaCore.newSourceEntry(ipackagefragmentroot.getPath()));
228
}
229
230
/** *//**
231
* 创建一个文件夹
232
*
233
* @param ifolder
234
* @throws CoreException
235
*/
236
private void createFolder(IFolder ifolder) throws CoreException
{
237
IContainer icontainer = ifolder.getParent();
238
if (icontainer != null && !icontainer.exists()
239
&& (icontainer instanceof IFolder))
240
createFolder((IFolder) icontainer);
241
ifolder.create(true, true, null);
242
}
243
244
/** *//**
245
* @see org.eclipse.jface.wizard.Wizard#addPages()
246
*/
247
public void addPages()
{
248
super.addPages();
249
_mainPage = new WizardNewProjectCreationPage("海迈平台-代码生成");
250
_mainPage.setTitle("创建一个代码生成项目框架");
251
_mainPage.setDescription("创建一个代码生成项目框架");
252
addPage(_mainPage);
253
}
254
255
/** *//**
256
* 更新Eclipse的透视图
257
*/
258
protected void updatePerspective()
{
259
try
{
260
IWorkbenchPage iworkbenchpage = PlatformUI.getWorkbench()
261
.getActiveWorkbenchWindow().getActivePage();
262
if (iworkbenchpage.findView("org.eclipse.ui.views.PropertySheet") == null)
263
iworkbenchpage.showView("org.eclipse.ui.views.PropertySheet");
264
} catch (PartInitException partinitexception)
{
265
partinitexception.printStackTrace();
266
}
267
}
268
269
/** *//**
270
* @param iresource
271
*/
272
protected void selectAndReveal(IResource iresource)
{
273
selectAndReveal(iresource, _workbench.getActiveWorkbenchWindow());
274
}
275
276
/** *//**
277
* 选择该项目并显示出来
278
*
279
* @param iresource
280
* @param iworkbenchwindow
281
*/
282
private void selectAndReveal(IResource iresource,
283
IWorkbenchWindow iworkbenchwindow)
{
284
BasicNewResourceWizard.selectAndReveal(iresource, iworkbenchwindow);
285
}
在创建新项目的时候,要给项目描述类增加一个Java Nature,具体这个Java Nature是什么意思我不太懂
。Eclipse里面很多都采用Adopter模式,所以在WorkspaceModifyOperation中的execute方法是创建项目的核心。其他可以参考代码。相信不是很难理解。
友情小贴士:
1.在点击“完成”按钮后,调用PerformFinish方法。
2.创建一个项目的时候,可以根据page来得到Project句柄,再根据ResourcesPlugin.getWorkspace();来得到工作区,由于每个工作区都含有多个不同插件的描述,可以从工作区创建一个项目描述:workspace .newProjectDescription(newProjectHandle.getName());并设置相应的路径(可以从Page中得到),并为项目描述增加一些必要的信息,如JavaNature。真正创建项目: projectHandle.create(description, new SubProgressMonitor(monitor,1000), projectHandle.open(IResource.BACKGROUND_REFRESH,new SubProgressMonitor(monitor, 1000));
3.为项目设置Java编译器,
ICommand icommand = iprojectdescription.newCommand();
icommand.setBuilderName("org.eclipse.jdt.core.javabuilder");
iprojectdescription.setBuildSpec(new ICommand[] { icommand });
4.为项目设置源路径
ArrayList arraylist = new ArrayList();
arraylist.addAll(Arrays.asList(ijavaproject.getRawClasspath()));
addSourceFolder(ijavaproject, arraylist, "src/java");
addSourceFolder(ijavaproject, arraylist, "src/resource");
ijavaproject.setRawClasspath((IClasspathEntry[]) arraylist.toArray(new IClasspathEntry[arraylist.size()]), null);
5.更新透视图
1
IWorkbenchPage iworkbenchpage = PlatformUI.getWorkbench()
2
.getActiveWorkbenchWindow().getActivePage();
3
if (iworkbenchpage.findView("org.eclipse.ui.views.PropertySheet") == null)
4
iworkbenchpage.showView("org.eclipse.ui.views.PropertySheet");
6.显示项目出来
BasicNewResourceWizard.selectAndReveal(iresource, iworkbenchwindow);