0

我目前正在学习 eclipse 插件是如何工作的。我想将表格放入视图中,并且我一直在弄乱示例视图插件一段时间,但我仍然无法让它工作。以下是示例视图插件的代码片段:

package com.example.helloworld2.views;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;

public class SampleView extends ViewPart {

    /**
     * The ID of the view as specified by the extension.
     */
    public static final String ID = "com.example.helloworld2.views.SampleView";

    private TableViewer viewer;
    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 ViewContentProvider implements IStructuredContentProvider {
        public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        }
        public void dispose() {
        }
        public Object[] getElements(Object parent) {
            return new String[] { "Onsdfsde", "Twosdfsd", "Threesdf" };
        }
    }
    class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        public String getColumnText(Object obj, int index) {
            return getText(obj);
        }
        public Image getColumnImage(Object obj, int index) {
            return getImage(obj);
        }
        public Image getImage(Object obj) {
            return PlatformUI.getWorkbench().
                    getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
        }
    }
    class NameSorter extends ViewerSorter {
    }

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

    /**
     * This is a callback that will allow us
     * to create the viewer and initialize it.
     */
    public void createPartControl(Composite parent) {
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        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.example.helloworld2.viewer");

    }

...
}

看起来视图内容提供者负责将内容放入视图中,但是我将如何插入表格呢?理想情况下,我最终想插入一个复杂的树表:

http://www.java2s.com/Tutorial/Java/0280__SWT/CreateaTreeTable.htm

但是,如果有人能用类似的东西甚至是我可以阅读的一些资源直接向我指出正确的方向,那就太好了。

谢谢

4

2 回答 2

1

作为“new TableViewer()”调用的一部分,您已经在 createPartControl() 中插入了一个表。要插入不同的小部件,您可以更改 createPartControl() 以执行不同的操作,例如改用 TreeTableViewer。

于 2012-06-21T00:44:12.530 回答
0

Table table = viewer.getTable();

从我从你的问题中推断出来,我认为上面的代码应该做你想做的事。

于 2012-07-18T10:11:04.503 回答