2012-04-17 2 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, ...). В основном

  • Создать новую верхнюю Composite в parent с GridLayout(1, false). (Это необходимо, так как Composite в аргументе parent имеет значение FillLayout).
  • Создайте ToolBarManager и сделайте «mng.createControl (вверху)» с GridData(FILL, TOP, true, false).
  • Создайте нового ребенка Composite в верхней части с GridData(FILL, FILL, true, true).
  • Invoke super.createSourceViewer(child, ...).
+0

я пытался это, приведенными выше код пожалуйста исправить его, ваты будут неправильно в этом коде. – RTA

+0

Вам нужно создать второй «Композитный» - см. Пункт 3 выше, который вы передаете супервызову. –

+0

Я добавил следующую строку в код выше, но теперь она перекрывает редактор, который должен был открыть. Составной дочерний = новый Composite (parent, SWT.NONE); child.setLayoutData (новый GridData (GridData.FILL, GridData.FILL, true, true)); return 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 ""; 
} 
Смежные вопросы