Reputation: 1299
I have an Eclipse plugin in which I need a toolbar within the text editor as like the toggle breadcrumb view. Is there any generic utility class in Eclipse that allows me to do this?
@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);
}
Upvotes: 1
Views: 1372
Reputation: 71
Tony's answer is good, but sometimes the
super.createSourceViewer(composite, ruler, styles);
will change the parent's layout, the real editor area will missing just as RTA commented in Tony's answer.
I met this problem when i want to do the exactly thing as RTA.
Here is my solution:
@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 "";
}
Upvotes: 2
Reputation: 12718
Assuming you have an text editor based on the org.eclipse.ui.editors.text.TextEditor
class, then you must override AbstractDecoratedTextEditor.createSourceViewer(Composite parent, ...)
. Basically
Composite
in parent
with a GridLayout(1, false)
. (This is needed as the Composite
in the parent
argument have a FillLayout
).ToolBarManager
and do 'mng.createControl(top)' with GridData(FILL, TOP, true, false)
.Composite
in top of with GridData(FILL, FILL, true, true)
.super.createSourceViewer(child, ...)
.Upvotes: 1