TreeView 使用属性监听;保护以前TreeTable的功能

TreeView 使用属性监听感觉比Table复杂一些,写了一个仿照书上的例子:

 

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

import book.ch7.tree.UserStructure;

public class UsingTreeViewer {

    private static Shell sShell = null;
    private Tree tree = null;

    private void createSShell() {
        sShell = new Shell();
        sShell.setText("Shell");
        sShell.setSize(new Point(300, 200));
        sShell.setLayout(new FillLayout());
        tree = new Tree(sShell, SWT.NONE);
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);
        TreeColumn treeColumn = new TreeColumn(tree, SWT.NONE);
        treeColumn.setWidth(60);
        treeColumn.setText("ID");
        TreeColumn treeColumn1 = new TreeColumn(tree, SWT.NONE);
        treeColumn1.setWidth(60);
        treeColumn1.setText("Name");

        TreeViewer treeViewer = new TreeViewer(tree);
        treeViewer.setContentProvider(new MyUserTreeContentProvider());
        treeViewer.setLabelProvider(new MyTableLabelProvider());

        MyUser president = new MyUser("0", "President");
        MyUser manager1 = new MyUser("1", "Manager 1");
        MyUser manager2 = new MyUser("2", "Manager 2");
        president.addChildren(manager1);
        president.addChildren(manager2);

        MyUser student = new MyUser("2", "Manager 2");
        manager1.addChildren(student);
        manager1.addChildren(new MyUser("4", "student 2"));
        manager2.addChildren(new MyUser("5", "student 3"));
        manager2.addChildren(new MyUser("6", "student 4"));

        student.addChildren(new MyUser("6", "student 4"));

        TreeData data = new TreeData(president);
        treeViewer.setInput(data);

        data.add(president, new MyUser("3", "Manager 3"));
        data.add(student, new MyUser("3", "Manager 3"));

        data.remove(manager2);
        data.remove(manager1);
    }

    public static void main(String[] args) {
        Display display = Display.getDefault();
        new UsingTreeViewer().createSShell();

        sShell.open();
        sShell.layout();

        while (!sShell.isDisposed())
            if (!display.readAndDispatch())
                display.sleep();
    }

}

class MyTableLabelProvider implements ITableLabelProvider {
    public Image getColumnImage(Object element, int columnIndex) { return null; }
    public String getColumnText(Object element, int columnIndex) {
        if (element instanceof MyUser) {
            MyUser user = (MyUser) element;
            switch (columnIndex) {
                case 0:
                    return user.id;
                case 1:
                    return user.name;
            }
        }
        return null;
    }
    public void dispose() {}
    public boolean isLabelProperty(Object element, String property) { return false; }
    public void addListener(ILabelProviderListener listener) {}
    public void removeListener(ILabelProviderListener listener) {}
}

class MyUserTreeContentProvider implements ITreeContentProvider,
PropertyChangeListener {

    public Object[] getChildren(Object parentElement) {
        return ((MyUser) parentElement).children.toArray();
    }

    public Object getParent(Object element) {
        return ((MyUser) element).parent;
    }

    public boolean hasChildren(Object element) {
        Vector<MyUser> children = ((MyUser) element).children;
        return children.size() != 0;
    }

    public Object[] getElements(Object inputElement) {
        if (inputElement instanceof TreeData) {
            return ((TreeData) inputElement).getList().toArray();
        }
        return new Object[0];
    }

    private TreeViewer viewer;

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        this.viewer = (TreeViewer) viewer;
        if (oldInput != null)
            ((TreeData) oldInput).removePropertyChangeListener(this);
        if (newInput != null)
            ((TreeData) newInput).addPropertyChangeListener(this);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (UserStructure.ADD_USER.equals(evt.getPropertyName())) {
            Object[] values = (Object[])evt.getNewValue();
            viewer.add(values[0], values[1]);
        }
        if (UserStructure.REMOVE_USER.equals(evt.getPropertyName())) {
            viewer.remove(evt.getNewValue());
        }
    }

    public void dispose() {}
}

class TreeData {

    public static final String ADD_USER = "addUser";

    public static final String REMOVE_USER = "removeUser";

    List<MyUser> list = new ArrayList<MyUser>();

    public TreeData(MyUser president) {
        list.add(president);
        delegate = new PropertyChangeSupport(this);
    }

    public List<MyUser> getList() {
        return list;
    }

    public void add(MyUser parent, MyUser newUser) {
        if (parent != null && ! parent.children.contains(newUser)) {
            parent.addChildren(newUser);
            firePropertyChange(new PropertyChangeEvent(this, ADD_USER,
                    null, new Object[] {parent, newUser }));
        }
    }
    public void remove(MyUser theUser) {
        if (theUser != null
                && theUser.parent.children.remove(theUser)) {
            theUser.parent = null ;
            firePropertyChange(new PropertyChangeEvent(this, REMOVE_USER, null,
                    theUser));
        }
    }

    private PropertyChangeSupport delegate;

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        delegate.addPropertyChangeListener(listener);
    }

    public void firePropertyChange(PropertyChangeEvent evt) {
        delegate.firePropertyChange(evt);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        delegate.removePropertyChangeListener(listener);
    }
}

class MyUser {
    String id;
    String name;
    MyUser parent;
    Vector<MyUser> children = new Vector<MyUser>();

    public MyUser(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public void addChildren(MyUser child) {
        children.add(child);
        child.parent = this;
    }

}

 

你可能感兴趣的:(treeview)