Steph
Steph

Reputation: 27

How can I associate custom widgets with TreeItems in SWT?

I have created a custom widget--an SWT Group that consists of several buttons, labels, images and perhaps several other widgets I might need to add in the future. I would like to make this custom widget a tree item, so that I can get the indentation and the expand/collapse functionality of a tree.

Here is an image that shows what I am trying to achieve:

enter image description here

I created this example with the Google Web Toolkit and I'd like to implement it with SWT.

The SWT TreeItem has methods to set the text and the image, but I could not find a way to make the tree item be a custom widget. If it is not possible to associate custom widgets with SWT tree items, suggestions about other ways to organize custom widgets in a tree with indentation and expand/collapse functionality would be very helpful too. Thanks!

Upvotes: 1

Views: 963

Answers (2)

Stefan
Stefan

Reputation: 12260

It seems to be possible to use an event SWT.PaintItem to add custom drawing:

https://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm

Source: Embed custom widget in SWT Tree or Table

1   Display display = new Display();
2   Shell shell = new Shell(display);
3   shell.setBounds(10, 10, 350, 200);
4   Image xImage = new Image (display, 16, 16);
5   GC gc = new GC(xImage);
6   gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
7   gc.drawLine(1, 1, 14, 14);
8   gc.drawLine(1, 14, 14, 1);
9   gc.drawOval(2, 2, 11, 11);
10  gc.dispose();
11  final int IMAGE_MARGIN = 2;
12  final Tree tree = new Tree(shell, SWT.CHECK);
13  tree.setBounds(10, 10, 300, 150);
14  TreeItem item = new TreeItem(tree, SWT.NONE);
15  item.setText("root item");
16  for (int i = 0; i < 4; i++) {
17     TreeItem newItem = new TreeItem(item, SWT.NONE);
18     newItem.setText("descendent " + i);
19     if (i % 2 == 0) newItem.setData(xImage);
20     item.setExpanded(true);
21     item = newItem;
22  }
23  tree.addListener(SWT.MeasureItem, new Listener() {
24     public void handleEvent(Event event) {
25        TreeItem item = (TreeItem)event.item;
26        Image trailingImage = (Image)item.getData();
27        if (trailingImage != null) {
28           event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
29        }
30     }
31  });
32  tree.addListener(SWT.PaintItem, new Listener() {
33     public void handleEvent(Event event) {
34        TreeItem item = (TreeItem)event.item;
35        Image trailingImage = (Image)item.getData();
36        if (trailingImage != null) {
37           int x = event.x + event.width + IMAGE_MARGIN;
38           int itemHeight = tree.getItemHeight();
39           int imageHeight = trailingImage.getBounds().height;
40           int y = event.y + (itemHeight - imageHeight) / 2;
41           event.gc.drawImage(trailingImage, x, y);
42        }
43     }
44  });
45  shell.open();
46  while (!shell.isDisposed()) {
47     if (!display.readAndDispatch()) display.sleep();
48  }
49  xImage.dispose();
50  display.dispose();

Upvotes: 0

Steph
Steph

Reputation: 27

The PGroup widget from the Eclipse Nebula project does the job. It lets you enclose SWT Composites and supports collapsing and expanding. Indentation can be achieved by using layouts (e.g., GridLayout) for the contents of the PGroup and layout data (e.g., GridData) to specify the indentation of a component inside the PGroup.

The ExpandBar (which is a standard SWT widget) also seems like a possible solution, but I have not experimented with it.

Upvotes: 1

Related Questions