开发者

Render view based on another view in Eclipse plugin

开发者 https://www.devze.com 2023-03-08 04:12 出处:网络
I am developing an Eclipse plug-in that has currently 2 views. In my first view I have a list of connections displayed in a TableViewer (name and connection status).In my second view I want to load th

I am developing an Eclipse plug-in that has currently 2 views. In my first view I have a list of connections displayed in a TableViewer (name and connection status).In my second view I want to load the tables in a database (the connection). This loading will be done by clicking a menu item on a connection ("view details"). These tables will be displayed in a TreeViewer because they can also have children. I have tried to do it this way:

My View class:

public class DBTreeView extends ViewPart {

private TreeViewer treeViewer;
private Connection root = null;

    public DBTreeView() {
        Activator.getDefault().setDbTreeView(this);
    }

    public void createPartControl(Composite parent) {
        treeViewer = new TreeViewer(parent);
        treeViewer.setContentProvider(new DBTreeContentProvider());
        treeViewer.setLabelProvider(new DBTreeLabelProvider());
    }

    public void setInput(Connection conn){
        root = conn;
        treeViewer.setInput(root);
        treeViewer.refresh();
    }
}

I made a setInput method that is called from the action registered with the menu item in the connections view with the currently selected connection as argument:

MViewContentsAction class:

public void run(){
    selectedConnection =  Activator.getDefault().getConnectionsView().getSelectedConnection();  
    Activator.getDefault().getDbTreeView().setInput(selectedConnection);    
}

In my ContentProvider class:

public Object[] getChildren(Object arg0) {
    if (arg0 instanceof Connection){
        return ((Connection) arg0).getTables().toArray();
    }
    return EMPTY_ARRAY;
}

where EMPTY_ARRAY is an...empty array

The problem I'm facing is that when in debug mode, this piece of code is not executed somehow:

开发者_运维百科
Activator.getDefault().getDbTreeView().setInput(selectedConnection);

And also nothing happens in the tree view when clicking the menu item. Any ideas? Thank you


Huh. Ok, what you're doing here is.. not really the right way. What you should be doing is registering your TableViewer as a selection provider.

getSite().setSelectionProvider(tableViewer);

Then, define a selection listener and add it to the view with the tree viewer like this:

ISelectionListener listener = new ISelectionListener() {
     public void selectionChanged(IWorkbenchPart part, ISelection sel) {
        if (!(sel instanceof IStructuredSelection))
           return;
        IStructuredSelection ss = (IStructuredSelection) sel;
        // rest of your code dealing with checking whether selection is what is
        //expected and if it is, setting it as an input to             
        //your tree viewer                     
       }
    };

   public void createPartControl(Composite parent) {
     getSite().getPage().addSelectionListener(listener);
  }

Now your tree viewer's input will be changed according to what is selected in the table viewer (btw, don't forget to call treeviewer.refresh() after you set new input).

See an example here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号