0

2つの列を持つTreeテーブルでビューを作成するEclipseプラグインを作成しようとしています。現在、eclipseが提供するサンプルビューテンプレートを使用していますが、表示する列ごとに異なるデータを指定する方法がわかりません。コードは次のとおりです。

public class Variables extends ViewPart {

    /**
     * The ID of the view as specified by the extension.
     */
    public static final String ID = "com.amazon.funce.views.Variables";

    private TreeViewer viewer;
    private DrillDownAdapter drillDownAdapter;
    private Action action1;
    private Action action2;
    private Action doubleClickAction;

    /*
     * The content provider class is responsible for
     * providing objects to the view. It can wrap
     * existing objects in adapters or simply return
     * objects as-is. These objects may be sensitive
     * to the current input of the view, or ignore
     * it and always show the same content 
     * (like Task List, for example).
     */

    class TreeObject implements IAdaptable {
        private String name;
        private TreeParent parent;

        public TreeObject(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setParent(TreeParent parent) {
            this.parent = parent;
        }
        public TreeParent getParent() {
            return parent;
        }
        public String toString() {
            return getName();
        }
        public Object getAdapter(Class key) {
            return null;
        }
    }

    class TreeParent extends TreeObject {
        private ArrayList children;
        public TreeParent(String name) {
            super(name);
            children = new ArrayList();
        }
        public void addChild(TreeObject child) {
            children.add(child);
            child.setParent(this);
        }
        public void removeChild(TreeObject child) {
            children.remove(child);
            child.setParent(null);
        }
        public TreeObject [] getChildren() {
            return (TreeObject [])children.toArray(new TreeObject[children.size()]);
        }
        public boolean hasChildren() {
            return children.size()>0;
        }
    }

    class ViewContentProvider implements IStructuredContentProvider, 
                                           ITreeContentProvider {
        private TreeParent invisibleRoot;

        public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        }
        public void dispose() {
        }
        public Object[] getElements(Object parent) {
            if (parent.equals(getViewSite())) {
                if (invisibleRoot==null) initialize();
                return getChildren(invisibleRoot);
            }
            return getChildren(parent);
        }
        public Object getParent(Object child) {
            if (child instanceof TreeObject) {
                return ((TreeObject)child).getParent();
            }
            return null;
        }
        public Object [] getChildren(Object parent) {
            if (parent instanceof TreeParent) {
                return ((TreeParent)parent).getChildren();
            }
            return new Object[0];
        }
        public boolean hasChildren(Object parent) {
            if (parent instanceof TreeParent)
                return ((TreeParent)parent).hasChildren();
            return false;
        }
/*
 * We will set up a dummy model to initialize tree heararchy.
 * In a real code, you will connect to a real model and
 * expose its hierarchy.
 */
        private void initialize() {
            TreeObject to1 = new TreeObject("Leaf 1");
            TreeObject to2 = new TreeObject("Leaf 2");
            TreeObject to3 = new TreeObject("Leaf 3");
            TreeParent p1 = new TreeParent("Parent 1");
            p1.addChild(to1);
            p1.addChild(to2);
            p1.addChild(to3);

            TreeObject to4 = new TreeObject("Leaf 4");
            TreeParent p2 = new TreeParent("Parent 2");
            p2.addChild(to4);

            TreeParent root = new TreeParent("Root");
            root.addChild(p1);
            root.addChild(p2);

            invisibleRoot = new TreeParent("");
            invisibleRoot.addChild(root);
        }
    }
    class ViewLabelProvider extends LabelProvider {

        public String getText(Object obj) {
            return obj.toString();
        }
        public Image getImage(Object obj) {
            String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
            if (obj instanceof TreeParent)
               imageKey = ISharedImages.IMG_OBJ_FOLDER;
            return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
        }
    }
    class NameSorter extends ViewerSorter {
    }

    /**
     * The constructor.
     */
    public Variables() {
    }

    /**
     * This is a callback that will allow us
     * to create the viewer and initialize it.
     */
    public void createPartControl(Composite parent) {
        viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

        //new
        Tree tree = viewer.getTree();
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);

        TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
        column1.setText("Name");
        column1.setWidth(400);
        TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
        column2.setText("Value");
        column2.setWidth(200);

        //end new
        drillDownAdapter = new DrillDownAdapter(viewer);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        viewer.setSorter(new NameSorter());
        viewer.setInput(getViewSite());

        // Create the help context id for the viewer's control
        PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "com.Amazon.FuncE.viewer");
    }

現在、私が持っているのは2つの列ですが、どちらも同じことを示しています。どの種類のデータが1つの列または他の列に入るのかをどのように指定できますか?

ありがとう

4

1 に答える 1

1

問題はラベルプロバイダーにあります。異なる列に異なるテキストを提供する機能を提供しないインターフェースを実装しています。代わりにITableLabelProviderを実装する必要があります。

于 2012-06-21T19:13:55.827 に答える