1:建立一個視圖類需要擴展org.eclipse.ui.part.ViewPart類
public class FirstView extends ViewPart {
}
2:在視圖中添加控件
主要在方法中createPartControl加入控件,(建議使用designer工具)
public class FirstView extends ViewPart {
private Text text;
public static final String ID = "rcpdemo.views.FirstView"; //$NON-NLS-1$
/**
* Create contents of the view part
* @param parent
*/
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
final Label label = new Label(container, SWT.NONE);
label.setAlignment(SWT.CENTER);
label.setText("姓名:");
label.setBounds(84, 65, 58, 24);
text = new Text(container, SWT.BORDER);
text.setBounds(144, 62, 80, 25);
//
createActions();
initializeToolBar();
initializeMenu();
}
3:配置plugin.xml文件
<extension
id="rcpdemo.views.FirstView"
name="first view"
point="org.eclipse.ui.views">
<view
class="rcpdemo.views.FirstView"
id="rcpdemo.views.FirstView"
name="New ViewPart"/>
</extension>
4:修改透視圖的代碼
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
//顯示視圖
String editorArea = layout.getEditorArea();
layout.addView("rcpdemo.views.FirstView", IPageLayout.RIGHT, 0.2f, editorArea);
}
}
在createInitialLayou中,我們可以通過以下幾個方法向透視圖中添加視圖、編輯器和菜單:
addView —— 添加視圖
addActionSet —— 添加菜單和工具欄
createFolder —— 創建一個IForderLayou,可以讓多個視圖重疊在同一個位置