tags:

views:

27

answers:

1

Hi, sorry for posting the same problem again, but the previous one is not in good format. I dont know why.

I am creating a RCP application. I need to open multiple instances of the same view but with different data. I did it by setting secondary id for different instances of the same view. Specifically, my problem is as follows: Please take a look,

i have a graph view called "Views.GraphView". I opened different instances of it from a command called "openGraphView" to show different graphs. the command is as follows:

page.showView("Views.GraphView", Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);

Now, I have a command called "TreeLayout" on this "Views.GraphView" toolbar, which suppose to change the layout of the graph and it will operate on each instace of the view. But for this, i think, i need to identify which instance of the view is active. the "TreeLayout" command looks something like this:

IViewPart findView = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). findView( "Views.GraphView"); //i think in the findView i need to give the id of the view [but how can i put the secondary id!!]

GraphView view = (GraphView) findView; view.changeLayout(); //i wrote this method in the graph view to change the layout

//i just tried to print the secondary id, but it did not print anyting System.out.println("From treelayout command:- " + view.getViewSite().getSecondaryId());

So how can i identify whcih instance of the view is currently active and to operate on it? Please, provide some solution.

Thanks,

-Syeed

A: 

You can use IWorkBenchPage.findViewReference(String viewId, String viewId) , if it returns null, the view with viewId and viewId is not present in the current perspective.

If you have a ViewReference you can use ViewReference.getView(boolean restore) to get the view

so in your handler you get something like:

final IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(
                    event).getActivePage();
final int instanceNum = 8;//the number of instances that are created
for (int index = 0; index < instanceNum; index++) {
    final IViewReference viewReference = page.findViewReference(
                        "Views.GraphView", Integer.toString(index));
    if (viewReference != null) {
        final IViewPart view = viewReference.getView(true);
        if (view instanceof GraphView) {
            final GraphView graphView = (GraphView) view;
            graphView.changeLayout();
        }
    }
}
davyM
Hi,Many thanks for your reply. I tried your code, but unfortunately it did not work.If i open 2 or more instances of the graph view, the command works for the last instance only and not for the previous ones. Can you provide some more information.thanks,Syeed
syeed
My first idea is that page.findViewReference returns only the viewreference of the active viewpart. Can you please debug to confirm this?
davyM
Hi, I debug the code. page.findViewReference returns the reference to all the view instances that are currently opened (no matter whcih one is currently selected). But the reference works only for the last instance of the view (in my case the changeLayout() worked for the last instance). Also, if i close the last instance of the view, still it did not work for the previous view instances.Please provide some solution.-Syeed
syeed

related questions