2012-04-17 10 views
1

Eclipseプラグインがありますトグルブレッドクラムビューのようにテキストエディタ内にツールバーが必要です。私がこれを行えるようにするEclipseの汎用ユーティリティクラスはありますか?Eclipseテキストエディタ内でツールバーを作成

@Override 
protected ISourceViewer createSourceViewer(Composite parent, 
              IVerticalRuler ruler, 
              int styles) 
{ 
    composite = new Composite(parent, SWT.NONE); 
    GridLayout gridLayout = new GridLayout(1, true); 
    gridLayout.numColumns = 1; 
    gridLayout.marginHeight = 0; 
    gridLayout.marginWidth = 0; 
    composite.setLayout(gridLayout); 

    ToolBar toolBar = new ToolBar(composite, SWT.FLAT); 
    GridData gridData = new GridData(GridData.FILL, SWT.TOP, true, false); 
    toolBar.setLayoutData(gridData); 
    toolBarManager = new ToolBarManager(toolBar); 

    return super.createSourceViewer(composite, ruler, styles); 
} 

答えて

1

あなたはorg.eclipse.ui.editors.text.TextEditorクラスに基づいて、テキストエディタを持っていると仮定すると、あなたはAbstractDecoratedTextEditor.createSourceViewer(Composite parent, ...)をオーバーライドする必要があります。基本的には

  • GridLayout(1, false)parentに新しいトップCompositeを作成します。 (これは、parent引数のCompositeFillLayoutがあるため必要です)。
  • ToolBarManagerを作成し、 'mng.createControl(top)'をGridData(FILL, TOP, true, false)で作成します。
  • 新規作成Compositeの上位にGridData(FILL, FILL, true, true)の子を作成します。
  • super.createSourceViewer(child, ...)を呼び出します。
+0

私はplsはそれを修正する上記のコードでこれをしようとしていた、ワットは、このコードで間違って起こっています。 – RTA

+0

スーパーコールに渡す2番目の「コンポジット」(上記3を参照)を作成する必要があります。 –

+0

上記のコードで次の行を追加しましたが、今開いていたエディタと重なっています。Composite child = new Composite(parent、SWT.NONE);child.setLayoutData(new GridData(GridData.FILL、GridData.FILL、true、true));戻り値super.createSourceViewer(子、定規、スタイル); – RTA

1

トニーの答えは良いですが、時には
super.createSourceViewer(composite, ruler, styles);
は親のレイアウトを変更し、実際のエディタ領域には、RTAはトニーの答えにコメントと同じように行方不明になります。
私はRTAとまったく同じことをしたいときにこの問題に遭遇しました。
はここに私のソリューションです:

@Override 
protected ISourceViewer createSourceViewer(Composite parent, 
     IVerticalRuler ruler, int styles) { 
    changeParentLayout(parent); 
    Label label = createPathLabel(parent); 
    ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles); 
    updateSourceViewerLayout(parent, label); 
    return viewer; 
} 

//change the parent layout to grid layout, 
//so that the source file area can be shown 
protected void changeParentLayout(Composite parent) { 
    parent.setLayout(new GridLayout(1, false)); 
} 

//i need a label here, ToolBar will be the same 
protected Label createPathLabel(Composite parent) { 
    Label lblNewLabel = new Label(parent, SWT.NONE); 
    lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false, 1, 1)); 
    lblNewLabel.setText(getFilePath()); 
    return lblNewLabel; 
} 

//after adding the label i need and call super.createSourceViewer() 
//now all widgets are ready, we need to change the editor area's layout data to grid data 
//here if you only have two widgets: label and area, you can directly choose the edit area widget. i used a loop to find all sub widgets 
protected void updateSourceViewerLayout(Composite parent, Label label) { 
    Control[] children = parent.getChildren(); 
    if (children.length < 2) return; 
    for (Control child : children) { 
     if (child != label) { 
      child.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); 
     } 
    } 
} 

private String getFilePath() { 
    //get the path I want 
    return ""; 
} 
関連する問題