我正在开发一个Eclipse rcp应用程序,其中贡献了各种插件。每个插件都有其自己的处理器,该处理器以编程方式将其他元素插入可能存在的MToolBar(具有现有MToolBarElement)。插入元素的顺序取决于插件的初始化顺序,除了插件之间的依赖关系外,该顺序不确定。但是,在插件之间创建人为的依赖关系以强制插件的初始化顺序是不可替代的。
是否有一种开箱即用的方式来相对于彼此布置元素?例如,必须始终将元素X插入ID为“ foo.bar”的元素Y之后的工具栏中。 还是我必须自己管理将元素添加到工具栏的顺序?
每个插件都定义了自己的处理器
public class ApplicationModelProcessor {
@Execute
public void execute(
final MApplication application,
final ToolBarBuilder toolbarBuilder) {
// ToolBarBuilder is a class providing methods to configure toolbar
// elements and appending them to existing toolbars. So after the
// configuration of the builder "build" is called and toolbar elements
// are added to (existing) toolbars according to the configuration of
// the builder
toolbarBuilder.doSomeConfiguration();
toolbarBuilder.build();
}
在ToolBarBuilder类中,存在一个updateToolItem方法,该方法有效地将MToolBarElement添加到MToolBar
public class TooBarBuilder {
public MToolBarElement build() {
// The code happening here basically searches for existing MToolBar or
// creates a new MToolBar according to a given configuration of the
// builder
MToolBar toolbar = findOrCreateToolBar();
// and adds a new MToolBarElement to the given MToolBar
return updateToolItem(toolbar);
}
// This method is invoked at some point during the execution of the provided processor. The toolBar is looked up before and provided to the method-
private MToolBarElement updateToolItem(final MToolBar toolBar) {
final MToolBarElement result = createHandleToolItem();
// At this point I could manage the order by myself but is there a better way to do this?
toolBar.getChildren().add(result);
return result;
}
}
某些插件通过在“ org.eclipse.e4.workbench.model”扩展点注册的自己的ApplicationModelProcessor,向同一MToolBar提供MToolBarElement。
给予一个提供工具栏项目1和2的插件
AND 另一个提供工具栏项目3的插件
何时启动应用程序
THEN (工具栏项的顺序)必须恒定(项的绝对顺序并不重要,但顺序应始终保持不变)
当前有时是1,2,3或3,2,1
答案 0 :(得分:0)
最终的解决方案是将实现从直接添加MToolBarElements到MToolBar更改为添加MToolBarContributions,并让Eclipse小心地将MToolBarContripution推荐的MToolBarElements添加到MToolBar中的正确位置:
@Creatable
public class TooBarBuilder {
@Inject
private MApplication application;
private String positionInParent;
// The positionInParent is an expression that represents a position
// relative to another element in the MToolBar, for example:
// "after=another.toolbar.item.id"
public ToolBarBuilder positionInParent(final String positionInParent) {
this.positionInParent = positionInParent;
return this;
}
public MToolBarElement build() {
// The code happening here basically searches for existing MToolBar or
// creates a new MToolBar according to a given configuration of the
// builder
MToolBar toolbar = findOrCreateToolBar();
// and adds a new MToolBarElement to the given MToolBar
return updateToolItem(toolbar);
}
// This method is invoked at some point during the execution of the provided processor.
// The toolBar is looked up before and provided to the method.
private MToolBarElement updateToolItem(final MToolBar toolBar) {
final MToolBarElement result = createHandleToolItem();
MToolBarContribution contribution = MMenuFactory.INSTANCE.createToolBarContribution();
contribution.setElementId(getContributionId()); // any elementId is ok, details how the id is created are not of interest for the solution
contribution.setParentId(toolbar.getElementId());
contribution.setPositionInParent(positionInParent);
// after all model processors have been executed the MToolBarContribtions
// are evaluated. MToolBarElements in the created MToolBarContributions
// are added to the MToolBars referenced by the parentId. During this process
// the MToolBarElements are positioned relatively to each other according to
// "positionInParent"
application.getToolBarContributions().add(contribution);
}
}