实现多路复用组件
为了在多路复用器中使用,站点接口需要向
org.eclipse.ui.part.SiteMultiplexerInitializer
作用域注册一个备用缺省实现。由于这会扩展站点作用域,所以在多路复用器中使用时,更明确的多路复用器版本将优先于缺省版本。
多路复用组件几乎总是使用以下模式:
- 实现 INestedComponent 生命周期接口:
- 在其构造函数中获取 IMultiplexer 并使用它来访问其接口的共享版本;
- 存储部件的状态;
- 侦听子代中的状态更改并适当地更新存储的状态;
- 每次激活组件时,它应该将存储的状态转发至多路复用器中的共享接口;
- 当组件活动时,它应该将状态更改直接转发至共享接口。
以下示例演示工作台使用的代码如何多路复用具有 ChildSelectionHandler 的 ISelectionHandler 接口。
/**
* Multiplexed version of the ISelectionHandler interface
*
* @since 3.1
*/
public class ChildSelectionHandler implements ISelectionHandler, INestedComponent {
private ISelectionHandler parent;
private ISelection selection;
private boolean isActive = false;
private IMultiplexer multiplexer;
public ChildSelectionHandler(IMultiplexer mplex) throws DependencyException {
this.multiplexer = mplex;
// Get access to the shared ISelectionHandler being multiplexed (we should
// only modify it when we're the active child)
this.parent = (ISelectionHandler)
mplex.getSharedComponents().getComponent(ISelectionHandler.class);
// Set the initial state (the part's initial selection will be null
// until it explicitly sets it).
}
public IMultiplexer getMultiplexer() {
// Return the multiplexer we were created with
return multiplexer;
}
public void activate() {
// Forward our stored selection to the shared interface
parent.setSelection(selection);
isActive = true;
}
public void deactivate() {
isActive = false;
}
public void setSelection(ISelection newSelection) {
// Remember the child's new selection
selection = newSelection;
if (isActive) {
// If we're active, forward the selection directly to the
// shared interface
parent.setSelection(newSelection);
}
}
}
以下是 ChildSelectionHandler 的相关联的扩展点标记。
<extension point="org.eclipse.core.component.types">
<component
implementation="org.eclipse.ui.internal.part.services.ChildSelectionHandler"
interface="org.eclipse.ui.part.services.ISelectionHandler"
singleton="false"
initializer="org.eclipse.ui.part.SiteMultiplexerInitializer"/>
</extension>