【问题标题】:What is the proper way to add custom Controls to the Eclipse toolbar (using WorkbenchWindowControlContribution)?将自定义控件添加到 Eclipse 工具栏(使用 WorkbenchWindowControlContribution)的正确方法是什么?
【发布时间】:2017-02-10 14:56:22
【问题描述】:

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

虽然可以在plugin.xml 中添加新命令(和相应的按钮),但我还需要一个文本框和一个标签,这需要在plugin.xml 中添加一个Control,并在扩展 org.eclipse.ui.menus.WorkbenchWindowControlContribution 的 Java。实际上,这归结为在子类中覆盖 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;
     }
    

    但是,标签位置不正确:

  • 使用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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我的建议是首先创建一个独立的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 );
      

      然后您可以尝试将控件贡献给工作台。扩展名应按照本文所述指定:Contributed control to the status bar not visible

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

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

      【讨论】:

        【解决方案3】:

        您需要创建一个 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-18
          • 1970-01-01
          • 2014-02-17
          • 2019-03-22
          相关资源
          最近更新 更多