Thursday, November 15, 2007

Creating customized toolbar in Eclipse view

By default, Eclipse view toolbar appears on the right side of the view as shown in the image below: This is fine when we the actions are not visually associated in any columns. However, one we need to associate a button or toolitem to a table column or a column header, we need to place the toolbar as closed as possible to the table column. Here is the trick: we need to design a layout, preferably a GridLayout, then place the toolbar on top of the tree view or table view.
    private Composite createCoolBar(Composite aParent) {
     // make the parent with grid layout
     GridLayout grid = new GridLayout(1,false);
     aParent.setLayout(grid);
     CoolBar coolBar = new CoolBar(aParent, SWT.FLAT);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
     coolBar.setLayoutData(data);
     // prepare the toolbar
     ToolBar toolbar = new ToolBar(coolBar, SWT.FLAT);
     
     // ------------- prepare the items
     // flatten
     ToolItem tiFlatten = new ToolItem(toolbar, SWT.PUSH);
     tiFlatten.setToolTipText("Flatten the node");
        ....
 return aParent; // no changes, reuse the parent
     }
    public void createPartControl(Composite aParent) {
     Composite parent = this.createCoolBar(aParent);
        treeViewer = new TreeViewer(parent, 
               SWT.SINGLE|SWT.FULL_SELECTION | SWT.BORDER);
        ....
        // tricky: needed to expand the tree
        GridData data = new GridData(GridData.FILL_BOTH);
        treeViewer.getTree().setLayoutData(data);

    }
 
The result is shown below:

0 comments: