4

I'm trying to replicate a UI similar to this:

http://librixxxi.blogspot.com/2011/06/punch-clock-021-and-clickable-edit.html

and I have been following the authors instructions (without success) on how to create buttons that are in each column of my table. The difference between my project as his is I am trying to use a Tree rather than a Table, and I am doing it in the context of an eclipse TreeViewer plugin. In theory it seems the implementation should be straightforward, but I can't seem to get it to work.

Here is my code, it is easily replicate-able as it is just the sample Java PDT sampleview with a treeviewer, plus a dozen or so extra lines in the createPartControl. Everything you don't see here is just the same as the sample:

 class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {

        public String getColumnText(Object obj, int i) {
            if(i == 0){
                return obj.toString();
            }
            return "";
        }
        public Image getColumnImage(Object obj, int i) {
            if(i == 0){
            String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
            if (obj instanceof TreeParent)
               imageKey = ISharedImages.IMG_OBJ_FOLDER;
            return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
            }
            return null;
        }
    }
    class NameSorter extends ViewerSorter {
    }

    /**
     * The constructor.
     */
    public ButtonView() {
    }

    /**
     * This is a callback that will allow us
     * to create the viewer and initialize it.
     */
    public void createPartControl(Composite parent) {
        viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

          Tree tree = viewer.getTree();
            tree.setLinesVisible(true);
            tree.setHeaderVisible(true);
            TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
            column1.setText("Name");
            column1.setWidth(400);
            TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
            column2.setText("Some info");
            column2.setWidth(300);


        // Button experimentation    
        TreeItem[] items = tree.getItems();
        for(int i = 0; i < items.length; i++){
            TreeEditor editor = new TreeEditor(tree);
            TreeItem item = items[i];

            Button button = new Button(tree, SWT.PUSH);

            button.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT));
            button.setSize(16,16);
            button.pack();

            editor.horizontalAlignment = SWT.RIGHT;
            editor.setEditor(button, item);
        }


        drillDownAdapter = new DrillDownAdapter(viewer);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        viewer.setSorter(new NameSorter());
        viewer.setInput(getViewSite());

        // Create the help context id for the viewer's control
        PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "ButtonTest.viewer");
        makeActions();
        hookContextMenu();
        hookDoubleClickAction();
        contributeToActionBars();
    }
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
KWJ2104
  • 1,959
  • 6
  • 38
  • 53

1 Answers1

1

When you say that you can't seem to get it to work, do you mean that you can't see the buttons in your Tree?

The javadoc of the SWT TreeEditor class gives an example of a tree editor stating that

"The editor must have the same size as the cell and must not be any smaller than 50 pixels."

The following lines assure that these conditions are met in the example:

editor.grabHorizontal = true;
editor.minimumWidth = 50;

So if you add these lines to your editor the buttons should be visible.

[Edit: What I did to reproduce the behaviour]

I used the standard RCP Mail Example project and added your "Button experimentation" code to it. Inside, I used simple text buttons.

public void createPartControl(Composite parent) {
  viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
  viewer.setContentProvider(new ViewContentProvider());
  viewer.setLabelProvider(new ViewLabelProvider());
  viewer.setInput(createDummyModel());

  experiment();
}

private void experiment() {

  // Button experimentation  
  Tree tree = viewer.getTree();
  TreeItem[] items = tree.getItems();

  for(int i = 0; i < items.length; i++){
    TreeEditor editor = new TreeEditor(tree);

    TreeItem item = items[i];

    Button button = new Button(tree, SWT.PUSH);

    button.setText("A");
    button.setSize(16,16);
    button.pack();

    editor.horizontalAlignment = SWT.RIGHT;
    editor.grabHorizontal = true;
    editor.minimumWidth = 50;
    editor.setEditor(button, item);
  }
}

When I execute the code like this, the buttons show up. When I comment out the lines setting the editor's grabHorizontal and minimumWidth values, the normal tree cell renderer is shown and the buttons are not visible.

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
  • Thanks for the response. I have tried that and it doesn't work. Yes, my problem is that I cannot see buttons. – KWJ2104 Aug 03 '12 at 23:44
  • Oh, I see. I tried to replicate the problem by adding a `TreeEditor` with buttons to a Tree in one of my own projects, and only got the buttons to be shown by adding above lines, so I hoped this could help you. – Modus Tollens Aug 03 '12 at 23:49
  • Np. Do you mind posting your snippet of the code? Maybe I can try to replicate your snippet to get something to appear. – KWJ2104 Aug 03 '12 at 23:52
  • Your code indeed does work. Thank you, I will be closely investigating this. – KWJ2104 Aug 04 '12 at 00:15