0

我在专门的查看器旁边有一个树查看器。在树查看器中选择某些内容时,有关此对象的详细信息将显示在专用查看器中。TreeViewer tree, Composite control, 和MySpecializedViewer viewer是实例变量。

public TheEverythingViewer(Composite parent) {
    control = new Composite(parent, SWT.NONE);
    control.setLayout(new GridLayout(2, false));

    tree = new TreeViewer(control);
    tree.setContentProvider(new MyContentProvider());
    tree.setLabelProvider(new MyLabelProvider());
    tree.setUseHashlookup(true);
    tree.getControl().setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, false, true, 1, 1));

    tree.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override public void selectionChanged(SelectionChangedEvent event) {
            try {
                IStructuredSelection sel = (IStructuredSelection) event.getSelection();
                MyClass myInput = (MyClass) sel.getFirstElement();

                if (viewer != null)
                    if (!viewer.getControl().isDisposed())
                        viewer.getControl().dispose();
                viewer = new MySpecializedViewer(control, table);

                control.getShell().layout();
            } catch (Exception e) {
                if (viewer != null)
                    if (!viewer.getControl().isDisposed())
                        viewer.getControl().dispose();
                viewer = null;
            }
        }
    });
}

难道我做错了什么?我只是想:

+--------------+--------------------------------------------+
| + Node       |                                            |
|   - Node     |                                            |
|   + Node     |                       My                   |
|     - Node   |                                            |
|   - Node     |      Specialized                           |
|              |                              Viewer        |
|              |                                            |
|              |                                            |
|              |                                            |
|              |                                            |
|              |                                            |
|              |                                            |
|              |                +--------+                  |
|              |                |        |                  |
|              |                |        |                  |
|              |                |        |                  |
|              |                +--------+                  |
|              |                                            |
|              |                                            |
|              |                                            |
|              |                                            |
+--------------+--------------------------------------------+

专用查看器具有需要根据所选节点消耗更多或更少空间的表。目前,创建专用查看器的新实例比更改其输入要简单得多(这在 ATM 上不起作用)。

4

1 回答 1

2

Yes, you shouldn't be recreating the viewer every time selection changes in your tree, you should just be sending the tree's selection to the existing viewer as its input, at which point it can do whatever you want it to do with the new input. You're also never setting the layout data on your specialized viewer's control, and then forcing the entire shell to re-layout is wasteful.

于 2012-07-07T06:21:24.300 回答