java - 为 SWT 树和表编写通用代码

标签 java swt

SWT 控件类 TreeTable 有几个具有相同签名且工作方式相同的方法。

例如:

  • getItems
  • getSelection

但是这些方法是直接在TreeTable 上定义的,没有声明这些方法的公共(public)父类(super class)或接口(interface)。因此,很难编写同时适用于 TreeTable 的代码。

是否有解决方案可以为这两个类编写通用代码?

最佳答案

一个可能的解决方案是创建一个具有通用方法的类,子类包装一个Tree 或一个Table,并将这些方法委托(delegate)给它们包装的控件。

使用此类类的通用代码可能如下所示:

CollectionControl c = CollectionControl.create(treeOrTable);
int nrItems = c.getSelectionCount();

以下是此类的示例:

/**
 * Wrapps a {@link Tree} or {@link Table} to make it possible to work with them
 * in a generic way.
 * 
 * This class could be an interface in Java 8, which allows static methods on interfaces.
 */
public abstract class CollectionControl {
    public abstract CollectionItem[] getItems();
    public abstract CollectionItem[] getSelection();
    public abstract int getSelectionCount();
    public abstract Control getControl();
    public abstract int getColumnCount();

    interface CollectionItem {
        String getText(int columnIx);
    }

    /**
     * @param control Either a {@link Tree} or {@link Table}..
     * @return A collection which wraps the argument an delegate method calls to it.
     * @throws IllegalArgumentException if the argument is not a Tree or a Table.
     */
    public static CollectionControl create(Control control) {
        if (control instanceof Tree) {
            return new TreeControl((Tree) control);
        } else if (control instanceof Table) {
            return new TableControl((Table) control);
        }

        throw new IllegalArgumentException();
    }


    private static class TreeControl extends CollectionControl {
        private Tree tree;

        public TreeControl(Tree tree) {
            this.tree = tree;
        }

        @Override
        public CollectionItem[] getSelection() {
            CollectionItem[] items = new CollectionItem[tree.getSelectionCount()];
            int ix = 0;
            for (TreeItem item : tree.getSelection()) {
                items[ix++] = new TreeCollectionItem(item);
            }
            return items;
        }

        @Override
        public int getSelectionCount() {
            return tree.getSelectionCount();
        }

        @Override
        public Tree getControl() {
            return tree;
        }

        @Override
        public CollectionItem[] getItems() {
            CollectionItem[] items = new CollectionItem[tree.getItemCount()];
            int ix = 0;
            for (TreeItem item : tree.getItems()) {
                items[ix++] = new TreeCollectionItem(item);
            }
            return items;
        }

        @Override
        public int getColumnCount() {
            return tree.getColumnCount();
        }

        private static class TreeCollectionItem implements CollectionItem {
            private TreeItem item;
            public TreeCollectionItem(TreeItem item) {
                this.item = item;
            }
            @Override
            public String getText(int columnIx) {
                return item.getText(columnIx);
            }
        }
    }

    private static class TableControl extends CollectionControl {
        private Table table;

        public TableControl(Table table) {
            this.table = table;
        }

        @Override
        public CollectionItem[] getSelection() {
            CollectionItem[] items = new CollectionItem[table.getSelectionCount()];
            int ix = 0;
            for (TableItem item : table.getSelection()) {
                items[ix++] = new TableCollectionItem(item);
            }
            return items;
        }

        @Override
        public int getSelectionCount() {
            return table.getSelectionCount();
        }

        @Override
        public Table getControl() {
            return table;
        }

        @Override
        public CollectionItem[] getItems() {
            CollectionItem[] items = new CollectionItem[table.getItemCount()];
            int ix = 0;
            for (TableItem item : table.getItems()) {
                items[ix++] = new TableCollectionItem(item);
            }
            return items;
        }

        @Override
        public int getColumnCount() {
            return table.getColumnCount();
        }

        private static class TableCollectionItem implements CollectionItem {
            private TableItem item;
            public TableCollectionItem(TableItem item) {
                this.item = item;
            }
            @Override
            public String getText(int columnIx) {
                return item.getText(columnIx);
            }
        }
    }
}

关于java - 为 SWT 树和表编写通用代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175554/

相关文章:

java - 无法解析配置 ':compileClasspath' 的所有文件

java - 使用 SWT 重命名 TreeViewer 节点

java - SWT:输入验证:防止输入下标和上标数字

java - 我在哪里可以下载 swt 的 javadoc(仅限)

java - 使用多个 CCombobox 的良好 Java SWT 示例

java - Android-Listview setOnItemClickListener 错误

java - Web 应用程序中的 Spring 应用程序上下文刷新

java - 如何使用匿名内部类?

Java - 在线程中的 while 循环后无法访问 StringBuilder

swt - 如何动态隐藏或显示 SWT 密码