关于Eclipse插件之IWorkbench IWorkbenchWindow IWorkbenchPage |WorkbenchPart......等的总结

时间:2024-10-22 19:33:20
1..IWorkbench:
workbench是eclipse用户界面中最底层的对象,它建立在Display之上,包含一个或多个IWorkbenchWindow,用于向终端用户呈现信息

当你的workbench插件被激活的时候,eclipse平台将为之创建一个实例,在平台的整个生命周期中,只允许出现该workbench的唯一实例

2..IWorkbenchWindow
上面提到IWorkbench利用其包含的IWorkbenchWindow向终端用户展现信息
"A workbench window is a top level window in a workbench. "
IWorkbenchWindow通常包含菜单栏,工具栏,状态栏和一个用来显示IWorkbenchPage的主要区域
每个IWorkbenchWindow可以包含一个或多个IWorkbenchPage,但是同一时刻只允许一个IWorkbenchPage被激活并向终端用户显示

3..IWorkbenchPage
IWorkbenchPage的功能就是组织一个或多个IEditorPart或IViewPart显示在一个IWorkbenchWindow中,最终呈现给终端用户
IWorkbenchPage包含一个或多个IEditorPart或IViewPart,这些IEditorPart和IViewPart完全被包含在这个IWorkbenchPage并且不能与其它的IWorkbenchPage共享
IWorkbenchPage上定义的Layout和ActionSet就是我们常说的透视图——perspective了
4..IWorkbenchPart
IWorkbenchPart就是上面提到的IEditorPart和IViewPart,它也只包含这两个子类型,看到这里大家应该明白,我们绝大部分的工作就是基于这个IWorkbenchPart
关于IEditorPart和IViewPart,相信大家应该已经十分了解了
IViewPart常常被用于导航信息的层次结构,例如我们的workspace,或者用来打开一个编辑器,或者用来显示这个编辑器的某些属性
IEditorPart多被用来编辑或浏览一个文档或一个输入的对象,而这个输入的对象必须是一个IEditorInput,IEditorPart遵循的就是打开-保存-关闭的生命周期模型
最后给出IWorkbenchPart的生命周期模型,希望对大家的开发能够有所帮助:
IWorkbenchPart拓展点被创建: 
实例化IWorkbenchPart 
创建一个IWorkbenchPartSite 
调用part.init(site) 
IWorkbenchPart成为可见: 
调用part.createControl(parent)绘制 IWorkbenchPart 
激活partOpened event
IWorkbenchPart被激活或者取得焦点: 
调用part.setFocus() 
激活partActivated event 
IWorkbenchPart被关闭: 
如需要保存,完成保存 
解除IWorkbenchPart的激活状态 
激活partClosed event 
从界面上移除IWorkbenchPart 
调用part.dispose()

关于IWorkbench 对象

public interface IWorkbench
extends IAdaptable, IServiceLocator
A workbench is the root object for the Eclipse Platform user interface.

A workbench has one or more main windows which present to the end user information based on some underlying model, typically on resources in an underlying workspace. A workbench usually starts with a single open window, and automatically closes when its last window closes.

Each workbench window has a collection of pages; the active page is the one that is being presented to the end user; at most one page is active in a window at a time.

Each workbench page has a collection of workbench parts, of which there are two kinds: views and editors. A page's parts are arranged (tiled or stacked) for presentation on the screen. The arrangement is not fixed; the user can arrange the parts as they see fit. A perspective is a template for a page, capturing a collection of parts and their arrangement.

The platform creates a workbench when the workbench plug-in is activated; since this happens at most once during the life of the running platform, there is only one workbench instance. Due to its singular nature, it is commonly referred to as the workbench.

The workbench supports a few services by default. If these services are used to allocate resources, it is important to remember to clean up those resources after you are done with them. Otherwise, the resources will exist until the workbench shuts down. The supported services are:

IBindingService
ICommandService
IContextService
IHandlerService
This interface is not intended to be implemented by clients.

See Also:
PlatformUI.getWorkbench()
Restriction:
This interface is not intended to be implemented by clients.

关于PlatformUI对象

public final class PlatformUI
extends Object
The central class for access to the Eclipse Platform User Interface. This class cannot be instantiated; all functionality is provided by static methods. Features provided:

creation of the workbench.
access to the workbench.
See Also:
IWorkbench

关于IEclipseContext对象

org.eclipse.e4.core.contexts
Interface IEclipseContext

public interface IEclipseContext
A context is used to isolate application code from its dependencies on an application framework or container. This helps avoid building in dependencies on a specific framework that inhibit reuse of the application code. Fundamentally a context supplies values (either data objects or services), and allows values to be set. Typically a client will be provided values from a context through injection, removing the need for clients to even depend on this interface.
While a context appears superficially to be a Map, it may in fact compute values for requested keys dynamically rather than simply retrieving a stored value.

Contexts may have a parent context, and may delegate lookup of a value to their parent. Whether a value is computed or stored in this context or a parent context is an implementation detail that clients need not be concerned with. The content of parent contexts cannot be modified by a child context.

Contexts may have child contexts. Children inherit context values from their parent as described earlier. At any time, one of the children may be considered the active child. The interpretation of what active means depends on the domain in which the context is used.

Like maps, values are stored in the context based on keys. Two types of keys can be used: strings and classes. When classes are used to access objects in the context, keys are calculated based on the class name, so the value stored for the class String can be retrieved using the key value of "java.lang.String".

Since:
1.3
Restriction:
This interface is not intended to be implemented by clients.
Restriction:
This interface is not intended to be extended by clients.

rcp(插件开发)如何打开编辑器、视图、透视图

如下:

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

1.打开编辑器
window.getActivePage().openEditor(input, Editor Id);

2.打开视图
window.getActivePage().showView(View Id);

3.打开透视图
PlatformUI.getWorkbench().showPerspective(Perspective Id,w.getActiveWorkbenchWindow());