动态刷新复合

时间:2012-07-07 04:16:24

标签: eclipse swt eclipse-rcp custom-component

我有一个专门的查看器旁边的树查看器。在树查看器中选择某些内容后,有关此对象的详细信息将显示在专用查看器中。 TreeViewer treeComposite controlMySpecializedViewer 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)简单得多。

1 个答案:

答案 0 :(得分:2)

是的,你不应该每次在树中选择更改时重新创建查看器,你应该只将树的选择作为输入发送给现有的查看器,此时它可以做任何你想做的事情。新的输入。您也永远不会在专业查看器的控件上设置布局数据,然后强制整个shell重新布局是浪费。