分类: Java
2009-05-29 21:28:49
------------------------------------------------
本文系本站原创,欢迎转载!
private IWorkbenchWindow busyOpenWorkbenchWindow (final String perspID,
final IAdaptable input) throws WorkbenchException {
// Create a workbench window (becomes active window)
final WorkbenchWindow newWindowArray[] = new WorkbenchWindow[1];
StartupThreading.runWithWorkbenchExceptions(new StartupRunnable() {
public void runWithException() {
newWindowArray[0] = newWorkbenchWindow();
}
});
final WorkbenchWindow newWindow = newWindowArray[0];
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() {
newWindow.create(); // 这个创建shell即顶级窗口
}
});
windowManager.add(newWindow);
StartupThreading.runWithWorkbenchExceptions(new StartupRunnable() {
public void runWithException() {
newWindow.open();
}
});
return newWindow;
}
上一节提到上面函数busyOpenWorkbenchWindow 中三个很重要的操作
1.new Workbenchwindow()
2.newwindow.create
3.newwindow.open()
下面就来一一解开。
1.Workbenchwindow的构造函数完成了workbenchwindow的初始化,像workbenchwindowconfigure,workbenchadvisor
super(null);
this.number = number;
// Make sure there is a workbench. This call will throw
// an exception if workbench not created yet.
Final Iworkbench workbench = PlatformUI.getWorkbench();
IserviceLocatorCreator slc = (IserviceLocatorCreator) workbench
.getService(IserviceLocatorCreator.class);
this.serviceLocator = (ServiceLocator) slc
.createServiceLocator(workbench, null, new Idisposable(){
public void dispose() {
final Shell shell = getShell(); if (shell != null && !shell.isDisposed()) {
close();
}
}
});
initializeDefaultServices();
// Add contribution managers that are exposed to other plugins.
addMenuBar();//创建menubar
addCoolBar(SWT.NONE); // style is unused
addStatusLine();
// register with the tracker
getExtensionTracker()
.registerHandler(
actionSetHandler,
ExtensionTracker
.createExtensionPointFilter(getActionSetExtensionPoint()));
fireWindowOpening();//getWindowAdvisor().preWindowOpen()创建windowadvisor,
// set the shell style
setShellStyle(getWindowConfigurer().getShellStyle());
// Fill the action bars
fillActionBars(FILL_ALL_ACTION_BARS);
}
public void fillActionBars(int flags) {
Workbench workbench = getWorkbenchImpl();
workbench.largeUpdateStart();
try {
getActionBarAdvisor().fillActionBars(flags);
//创建ActionBarAdvisor,然后调用fillacitonbars给
Menumangor,coolmangor,stateline赋值,从而达到不同菜单,工具栏显示
// 3.3 start
}
}
private/* private - DO NOT CHANGE */
ActionBarAdvisor getActionBarAdvisor() {
if (actionBarAdvisor == null) {
actionBarAdvisor = getWindowAdvisor().createActionBarAdvisor(
getWindowConfigurer().getActionBarConfigurer());
Assert.isNotNull(actionBarAdvisor);
}
return actionBarAdvisor;
}
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
这样就创建了一个actionbaradvisor
public void fillActionBars(int flags) {
if ((flags & FILL_PROXY) == 0) {
makeActions(actionBarConfigurer.getWindowConfigurer().getWindow());
}
if ((flags & FILL_MENU_BAR) != 0) {
fillMenuBar(actionBarConfigurer.getMenuManager());
}
if ((flags & FILL_COOL_BAR) != 0) {
fillCoolBar(actionBarConfigurer.getCoolBarManager());
}
if ((flags & FILL_STATUS_LINE) != 0) {
fillStatusLine(actionBarConfigurer.getStatusLineManager());
}
}
Fillactionbars就是调用actionbaradvisor的初始化函数初始化menumanager,coolmanager等。
Workbenchwindow的构造函数完成一切工作。
2.create函数,上面其实还有一个没有提到,就是shell的创建,这个其实是在Workbenchwindow.create创建的,public void create() {
shell = createShell();
contents = createContents(shell);
//initialize the bounds of the shell to that appropriate for the
// contents
initializeBounds();
}
protected final Shell createShell() {
Shell newParent = getParentShell();
if(newParent != null && newParent.isDisposed()){
parentShell = new SameShellProvider(null);
newParent = getParentShell();//Find a better parent
}
//Create the shell
Shell newShell = new Shell(newParent, getShellStyle());
return newShell;
}
protected Control createContents(Composite parent) {
// by default, just create a composite
return new Composite(parent, SWT.NONE);
}
可以看出workbenchwindow的contents是一个composite变量,且以shell为父窗口,这样整个窗口结构建立起来了,然后其他的想menu,coolbar,perspective都是content的内容。
3.然后讲下workbenchwindow.Open函数,他就是执行一些布局,并显示,相当于openwindow。
public int open() {
if (getPages().length == 0) {
showEmptyWindowContents();
}
fireWindowCreated();
getWindowAdvisor().openIntro();
int result = super.open();
//这个会调用shell的open这样就打开窗体。
getShell().layout();
}
到此,窗口机制建立了,窗体显示了。
当然注意。Workbench,workbecnhwindow都有相对应的configure类,他们都是一对一的。
Workbench只有一个,且它的实例句柄是静态的,
private static Workbench instance;
这样通过静态函数PlatformUI.getWorkbench就可以获取
PlatformUI.getWorkbench();
{
if (Workbench.getInstance() == null) {
// app forgot to call createAndRunWorkbench beforehand
throw new IllegalStateException(WorkbenchMessages.PlatformUI_NoWorkbench);
}
return Workbench.getInstance();
}
public static final Workbench getInstance() {
return instance;
}
如果在workbench里,用这个
private Workbench getWorkbenchImpl() {
return Workbench.getInstance();
}
附rcp各种概念解释:
Workbench,这个可以等价于rcp应用程序,是唯一的,主要管理workbenchwindow。
Workbenchwindow是指一个应用窗体。
Perspective主要用来布局,可以有很多个。
Workbenchpage是指一个具体的perspective的布局,一般是一个数组,保存各种,
所以一般用getactivepage来获取活动page也就是活动perpectives