在我们使用Eclipse平台开发新的插件的时候,可能会用到perspectives,在plugin.xml文件中,其基本配置代码如下:
plugin.xml文件配置
1
<
extension
2 point ="org.eclipse.ui.perspectives" >
3 < perspective
4 class ="automatonplugin.AutomatonPerspectiveFactory"
5 icon ="icons/state.gif"
6 id ="AutomatonPlugin.automatonperspective"
7 name ="Automaton Perspective" >
8 </ perspective >
9 </ extension >
10 < extension
11 point ="org.eclipse.ui.perspectiveExtensions" >
12 < perspectiveExtension
13 targetID ="AutomatonPlugin.automatonperspective" >
14 < view
15 closeable ="false"
16 relative ="org.eclipse.ui.editorss"
17 ratio ="0.2"
18 relationship ="left"
19 id ="cn.tsinghua.mse.automatondesigner.ui.View_ToolBox" >
20 </ view >
21 </ perspectiveExtension >
22 </ extension >
2 point ="org.eclipse.ui.perspectives" >
3 < perspective
4 class ="automatonplugin.AutomatonPerspectiveFactory"
5 icon ="icons/state.gif"
6 id ="AutomatonPlugin.automatonperspective"
7 name ="Automaton Perspective" >
8 </ perspective >
9 </ extension >
10 < extension
11 point ="org.eclipse.ui.perspectiveExtensions" >
12 < perspectiveExtension
13 targetID ="AutomatonPlugin.automatonperspective" >
14 < view
15 closeable ="false"
16 relative ="org.eclipse.ui.editorss"
17 ratio ="0.2"
18 relationship ="left"
19 id ="cn.tsinghua.mse.automatondesigner.ui.View_ToolBox" >
20 </ view >
21 </ perspectiveExtension >
22 </ extension >
当然,与之对应要建立的是名为AutomatonPerspectiveFactory的类,你可以在它的public void createInitialLayout(IPageLayout layout) 函数中使用layout.addView()方法进行视图的添加,其基本代码示例如下:
1
String editorArea
=
layout.getEditorArea();
2 layout.setEditorAreaVisible( false );
3 layout.addView( " cn.tsinghua.mse.automatondesigner.ui.View_ToolBox " , IPageLayout.LEFT, 0.3f , editorArea);
2 layout.setEditorAreaVisible( false );
3 layout.addView( " cn.tsinghua.mse.automatondesigner.ui.View_ToolBox " , IPageLayout.LEFT, 0.3f , editorArea);
这个工作环境是左侧有一个工具箱,它是一个View,即cn.tsinghua.mse.automatondesigner.ui.View_ToolBox,当然,不在createInitialLayout函数中通过代码加载,而是用在xml文件中对其进行定义,XML文件为:
工具箱定义
1
<
extension
2 point ="org.eclipse.ui.views" >
3 < view
4 class ="cn.tsinghua.mse.automatondesigner.ui.View_ToolBox"
5 id ="cn.tsinghua.mse.automatondesigner.ui.View_ToolBox"
6 name ="工具箱"
7 allowMultiple ="false"
8 icon ="icons/ToolBox.ico"
9 >
10 </ view >
11 </ extension >
2 point ="org.eclipse.ui.views" >
3 < view
4 class ="cn.tsinghua.mse.automatondesigner.ui.View_ToolBox"
5 id ="cn.tsinghua.mse.automatondesigner.ui.View_ToolBox"
6 name ="工具箱"
7 allowMultiple ="false"
8 icon ="icons/ToolBox.ico"
9 >
10 </ view >
11 </ extension >
如果用户使用系统插件提供的向导新建一个文件时,我们可以使用Wizard来引导其创建对应的文件并设置一些列的属性,这部分主要有WizardPage和Wizard这两个类的子类进行管理,用户新建一个文件时,系统为其提供默认的perspective,可以使用如下代码进行转换:
设置默认perspective的代码
1
private
void
doFinish() {
2 IWorkbenchWindow window = PlatformUI.getWorkbench()
3 .getActiveWorkbenchWindow();
4 try {
5 Editor_Main main = (Editor_Main) IDE.openEditor(window
6 .getActivePage(), new AutomatonEditorInputer(page
7 .getAutomatonName()),
8 " cn.tsinghua.mse.automatondesigner.ui.Editor_Main " );
9 main.setMainWindow(window);
10 IPerspectiveDescriptor perspective = window.getWorkbench()
11 .getPerspectiveRegistry().findPerspectiveWithId(
12 " AutomatonPlugin.automatonperspective " );
13 window.getActivePage().setPerspective(perspective);
14 } catch (PartInitException e) {
15 e.printStackTrace();
16 }
17 }
2 IWorkbenchWindow window = PlatformUI.getWorkbench()
3 .getActiveWorkbenchWindow();
4 try {
5 Editor_Main main = (Editor_Main) IDE.openEditor(window
6 .getActivePage(), new AutomatonEditorInputer(page
7 .getAutomatonName()),
8 " cn.tsinghua.mse.automatondesigner.ui.Editor_Main " );
9 main.setMainWindow(window);
10 IPerspectiveDescriptor perspective = window.getWorkbench()
11 .getPerspectiveRegistry().findPerspectiveWithId(
12 " AutomatonPlugin.automatonperspective " );
13 window.getActivePage().setPerspective(perspective);
14 } catch (PartInitException e) {
15 e.printStackTrace();
16 }
17 }
核心部分是其中的10~13行。