2

I have a perspective with two views created via "extensions" . First view has a table and the second has a tree. I want to show only the first view when the application is open and when the user selects an item from the table, put this item name in the second view´s text field and hide the first view while the second view is open.I want also hide the second view and show the first view when the user push a button. Is that possible?.

I've managed to put item name in the second view but i´m not able to coordinate showing and hiding the views.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
EH_Aurrera
  • 33
  • 1
  • 4
  • Look at this question - [Programmatically showing a View from an Eclipse Plug-in](http://stackoverflow.com/q/171824/150166) - and this answer - [How to make view visible/invisible at runtime](http://stackoverflow.com/a/8295692/150166). – Martti Käärik Apr 17 '12 at 14:26

1 Answers1

2

Yes, it's possible to show and hide views.

1) Each view has to have a unique ID. This ID has to match the id in the view extension of the plugin.xml.

Here's one of my plugin.xml view extensions.

  <view
        class="gov.bop.rabid.ui.views.PrefetchView"
        icon="icons/folder_user.png"
        id="gov.bop.rabid.ui.views.PrefetchView"
        name="Prefetch"
        restorable="true">
  </view>

And here's the ID definition in the PrefetchView.

public static final String ID = "gov.bop.rabid.ui.views.PrefetchView";

Generally, I make the ID the same as the class name. It's less confusing for me.

2) In the Perspective class, the createInitialLayout method, you have to define an IFolderLayout with placeholders. Again, here's my code.

    IFolderLayout consoleFolder = layout.createFolder(CONSOLE_ID, 
            IPageLayout.BOTTOM, 0.75f, editorArea);
    consoleFolder.addPlaceholder(PrefetchedInmatesView.ID);
    consoleFolder.addPlaceholder(FoundInmatesView.ID);
    consoleFolder.addView(ProcessedInmatesView.ID);

    setClosable(layout, FoundInmatesView.ID, false);
    setClosable(layout, PrefetchedInmatesView.ID, false);
    setClosable(layout, ProcessedInmatesView.ID, false);

3) You need a static method that allows you to access any view from inside any other view. I put this static method in my Activator class, but you can put it anywhere you want.

public static IViewPart getView(IWorkbenchWindow window, String viewId) {
    IViewReference[] refs = window.getActivePage().getViewReferences();
    for (IViewReference viewReference : refs) {
        if (viewReference.getId().equals(viewId)) {
            return viewReference.getView(true);
        }
    }
    return null;
}

4) Finally, you show and hide views from your event code. Here's an example.

        final PhotoView view = (PhotoView) RabidPlugin.getView(window,
                PhotoView.ID);
        if (view == null)
            return;

                    *** Do stuff with the other view ***

        IWorkbenchPage page = window.getActivePage();
        page.hideView(page.findView(FoundInmatesView.ID));
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • thanks Gilbert for the quick response and your knowledge abusing you wont me doubts that arise: --I said that the views are created via "extensions" but really are via "" perspective extensions ", is it relevant?. point2-- the setCloseable method should I implement it? or being the creation of the view as I described I can select the option "closeable = true". – EH_Aurrera Apr 17 '12 at 15:57
  • Another issue that has escaped me: That is done so at the beginning only the first view appears while the second is hidden waiting a selection in the table to open (and close the first one)?.Thanks for Everything, is saving me a real obstruction – EH_Aurrera Apr 17 '12 at 16:12
  • @user1338650: I defined my views on the Extensions tab of the MANIFEST.MF. I showed you the resulting XML. The setClosable method defines whether the user can close the views or not. I assumed from what you described that you did not want the user to be able to click on the X and close the view. In the Application class, the start method, you display the first view. – Gilbert Le Blanc Apr 17 '12 at 17:53
  • Gilbert thanks once again and I have another question that I hope you don´t mind: Could you give an example as detailed as in the response to tell me the whole process (methods and classes which are implemented) for two imaginary views as viewA and viewB?Where viewA is the view presented initially and viewB is the hidden view which is opened when user selects an item in viewA. I mean from the point two of your response below.Really thanks, it´s very important. – EH_Aurrera Apr 17 '12 at 18:44