0

I am creating an 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 instance 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 which instance of the view is currently active and to operate on it?

Lii
  • 11,553
  • 8
  • 64
  • 88
syeed
  • 1
  • 1
  • 1
  • This is a duplicate of http://stackoverflow.com/questions/3201341/how-can-i-identify-whcih-instance-of-the-view-is-currently-active-in-rcp – Andy Thomas Jul 08 '10 at 22:24
  • How do you know any of the views of this type will be active when the action occurs? Does the application have no other types of views or editors? – Andy Thomas Jul 08 '10 at 22:24

4 Answers4

1

using the below code you should be able to get the current active view.

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart()
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Sharif
  • 1,488
  • 17
  • 21
1

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();
        }
    }
}
Davy Meers
  • 1,758
  • 14
  • 8
  • 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 Jul 08 '10 at 08:46
  • My first idea is that page.findViewReference returns only the viewreference of the active viewpart. Can you please debug to confirm this? – Davy Meers Jul 08 '10 at 09:11
  • 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 Jul 08 '10 at 10:12
1

The view.getViewSite().getSecondaryId() method is the good one to identify a secondary view. This method only returns the Null string for the "primary" view: the one opened when user click Window -> Show View - Your View.

I don't understand why your view toolbar button has to operate on all the view instances. TO my eyes, you should have one button in each view instance toolbar operating only in its own view. If you really need to operate from one button to ALL the views I think you will have to keep the open views references yourself, because I think the workbench doesn't provide a findViews method returning a view array.

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
0

Active view and Editor name display on MessageDialogbox

public class Active_Workbench extends AbstractHandler{

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        // A part service tracks the creation and activation of parts within a workbench page.
        IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();

        MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
                event).getShell(), "Current Workbench Window", service.getActivePart().getTitle().toString());

        return null;
    }
}

I hope this answer is useful.

Chetan Bhagat
  • 610
  • 6
  • 21