2012-04-17 109 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, ...)。基本上

  • parentGridLayout(1, false)創建新的頂級Composite。 (這是需要的,因爲parent參數中的Composite具有FillLayout)。
  • 創建一個ToolBarManager並與GridData(FILL, TOP, true, false)做'mng.createControl(頂部)'。
  • GridData(FILL, FILL, true, true)創建新的子女Composite
  • 調用super.createSourceViewer(child, ...)
+0

我被上面的代碼嘗試此請更正,笏在此代碼會錯。 – RTA 2012-04-17 09:56:18

+0

您需要創建第二個'Composite' - 請參閱上面的第3項 - 您將其傳遞給超級呼叫。 – 2012-04-17 12:19:31

+0

我已經在上面的代碼中添加了以下代碼行,但現在它與要打開的編輯器重疊..複合child = new Composite(parent,SWT.NONE); (new GridData(GridData.FILL,GridData.FILL,true,true)); return super.createSourceViewer(child,ruler,styles); – RTA 2012-04-17 14:08:19

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 ""; 
} 
相關問題