【发布时间】:2017-02-10 14:56:22
【问题描述】:
我正在开发一组 Eclipse 插件,其中一个负责将工具栏添加到 Eclipse 工作区。
虽然可以在plugin.xml 中添加新命令(和相应的按钮),但我还需要一个文本框和一个标签,这需要在plugin.xml 中添加一个Control,并在扩展 org.eclipse.ui.menus.WorkbenchWindowControlContribution 的 Java。实际上,这归结为在子类中覆盖 createControl(Composite parent)。
这部分我很清楚。问题是我不确定应该返回什么类型的 Control 对象。
我尝试了以下方法:
-
创建一个
ToolBarManager,给它添加一个SWTLabel和一个STWText(都包裹在单独的ControlContribution对象中),并返回ToolBarManager.createControl(parent)获得的工具栏:@Override protected Control createControl(Composite parent) { ToolBarManager manager = new ToolBarManager(SWT.FLAT | SWT.HORIZONTAL); LabelContributionItem labelItem = new LabelContributionItem("myLabelId"); manager.add(labelItem); TextContributionItem textItem = new TextContributionItem("myTextId"); manager.add(textItem); ToolBar toolbar = manager.createControl(parent); return toolbar; }但是,标签位置不正确:
-
使用
GridLayout作为控件返回(代码改编自this answer):@Override protected Control createControl(Composite parent) { Composite composite = new Composite(parent, SWT.SINGLE); GridLayout compositeLayout = new GridLayout(2, false); compositeLayout.marginTop = -1; compositeLayout.marginBottom = 0; compositeLayout.marginLeft = 5; compositeLayout.marginWidth = 0; composite.setLayout(compositeLayout); Label myLabel = new Label(composite, SWT.BORDER | SWT.SINGLE); myLabel.setText("myLabel"); Text myText = new Text(composite, SWT.BORDER | SWT.SINGLE); myText.setText("myText"); return composite; }结果是一个大小和对齐不正确的文本框,加上标签周围的边框(添加了最右边的文本框以进行比较):
我还尝试了其他一些组合和布局,但无法使其正常工作。此外,我想在文本框中添加一个ControlDecoration,如下所示:
要使 ControlDecoration 的鼠标悬停文本正常工作,文本框左侧需要留有空白 (source):
使用 ControlDecoration 的客户端通常应确保为装饰保留足够的边距空间
事实证明,添加这个空格也很麻烦,除了使用 GridLayout 的 marginLeft 参数时(但 GridLayout 给出了上述对齐问题)。
【问题讨论】:
标签: java eclipse-plugin swt jface