繁体   English   中英

将自定义控件添加到Eclipse工具栏的正确方法是什么(使用WorkbenchWindowControlContribution)?

[英]What is the proper way to add custom Controls to the Eclipse toolbar (using WorkbenchWindowControlContribution)?

我正在开发一组Eclipse插件,其中一个负责将工具栏添加到Eclipse工作区。

虽然可以在plugin.xml添加新命令(和相应的按钮),但我还需要一个文本框和一个标签,这需要在plugin.xml添加一个Control ,以及一个扩展org.eclipse.ui.menus.WorkbenchWindowControlContribution Java实现org.eclipse.ui.menus.WorkbenchWindowControlContribution 实际上,这归结为在子类中重写createControl(Composite parent)

这部分对我来说很清楚。 问题是我不确定应该返回什么类型的Control对象。

我尝试过以下方法:

  • 创建一个ToolBarManager ,向其添加一个SWT Label和一个STW Text (两者都包含在单独的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; } 

    但是,标签位置不正确:

    使用ToolBarManager的结果

  • 使用GridLayout作为控件返回(代码改编GridLayout 答案 ):

     @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; } 

    结果是大小和对齐的文本框不正确,加上标签周围的边框(最右边的文本框用于比较):

    使用Composite与GridLayout的结果

我还尝试了一些其他组合和布局,但无法使其正常工作。 此外,我想在文本框中添加一个ControlDecoration ,如下所示:

控制装饰

要使ControlDecoration的鼠标悬停文本正常工作,文本框左侧需要有边距空间( ):

使用ControlDecoration的客户通常应确保为装饰保留足够的边距空间

添加此空间也证明很麻烦,除非使用GridLayout的marginLeft参数(但GridLayout给出了上述的对齐问题)。

我对标签也有同样的问题。 如果要将文本添加到工具栏,请使用CLabel

我的建议是首先创建一个独立的Composite ,它包含所需的控件和装饰,与工作台贡献无关。 在那里,您可以在简单的Shell测试布局,直到它看起来应该如何。

据我了解你的问题,这就是应该如何制定控制措施:

Composite composite = new Composite( shell, SWT.NONE );
composite.setLayout( new GridLayout( 2, false ) );
composite.setBackground( composite.getDisplay().getSystemColor( SWT.COLOR_GREEN ) );
Label label = new Label( composite, SWT.NONE );
label.setText( "MyLabel" );
Text text = new Text( composite, SWT.BORDER );
text.setText( "my text" );
ControlDecoration decoration = new ControlDecoration( text, SWT.TOP | SWT.LEFT );
FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
FieldDecoration fieldDecoration = registry.getFieldDecoration( DEC_CONTENT_PROPOSAL );
decoration.setImage( fieldDecoration.getImage() );
label.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
GridData gridData = new GridData( SWT.BEGINNING, SWT.CENTER, false, false );
gridData.horizontalIndent = 10;
text.setLayoutData( gridData );

然后,您可以尝试将控件提供给工作台。 应按照本文所述指定扩展名: 对状态栏的贡献控制不可见

createControl()应该返回上面提到的复合,看起来像这样

protected Control createControl( Composite parent ) {
  Composite composite = new Composite( shell, SWT.NONE );
  // ... create label, text, decoration as above
  return composite;
}

您需要创建SWT.SEPARATOR类型的工具项,然后将标签附加到该工具项。 以下代码演示了这一点:

public static ToolItem createToolBarLabel(ToolBar toolBar, String text, int width) {
    ToolItem labelItem = new ToolItem(toolBar, SWT.SEPARATOR);
    CLabel label = new CLabel(toolBar, SWT.NONE);
    label.setText(text);
    labelItem.setWidth(width);
    labelItem.setControl(label);

    return labelItem;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM