0

I am updating an existing working RCP 3 app from Kepler to Mars. It was written by another guy so having to learn a lot about RCP as I go.

What worked in Kepler was this:

public class ShowSelectViewDialogHandler extends DialogHandler {

/**
 * The name of the parameter providing the view identifier.
 */
private static final String VIEW_ID_COMMAND_PARAMETER_NAME = "org.eclipse.ui.views.showView.viewId"; //$NON-NLS-1$
private static final String MAKE_FAST_COMMAND_PARAMETER_NAME = "org.eclipse.ui.views.showView.makeFast"; //$NON-NLS-1$

private final IHandler handler;

/**
 * Creates a new ShowViewHandler that will open the view in its default location.
 */
public ShowSelectViewDialogHandler (final IHandler handler) {
    this.handler = handler;
}

@Override
public final Object execute(final ExecutionEvent event) throws ExecutionException {
    Object result = null;

    IWorkbenchWindow window = EDMUIApplication.instance().getWorkbenchAdvisor().getWorkbenchWindowAdvisor().getWindow();

    Map<String, String> parameters = event.getParameters();
    String viewId = parameters.get(ShowSelectViewDialogHandler.VIEW_ID_COMMAND_PARAMETER_NAME);
    String makeFast = parameters.get(ShowSelectViewDialogHandler.MAKE_FAST_COMMAND_PARAMETER_NAME);

    if (viewId == null) {
        ShowViewDialog dialog = new ShowViewDialog(window, new EDMUIViewRegistry(EDMUIConstants.CATEGORY_IDS));
        if (dialog.open() == Window.OK) {
            for (IViewDescriptor viewDescriptor : dialog.getSelection()) {
                result = this.openView(window, viewDescriptor.getId(), makeFast);
            }
        }
    } else {
        result = this.openView(window, viewId, makeFast);
    }

    return result;
}

/**
 * Opens the view with the given ID.
 * 
 * @param window - workbench window of the view.
 * @param viewId - id of the view to open.
 * @param makeFast - command parameter.
 * @return result of the handler execution.
 * @throws ExecutionException - if default handler execution fails.
 */
private Object openView(final IWorkbenchWindow window, final String viewId, final String makeFast) throws ExecutionException {
    Object result = null;
    try {
        Parameterization[] parameterization = this.createParameterization(viewId, makeFast, IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);
        result = this.executeDefaultHandler(this.handler, window, parameterization, IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);
    } catch (NotDefinedException ex) {
        throw new ExecutionException(ex.getMessage(), ex);
    }

    return result;
}

/**
 * Creates parameterization for the command.
 * 
 * @param viewId - view id parameter value.
 * @param makeFast - make fast parameter value.
 * @param commandId - id of the command.
 * @return created parameterization.
 * @throws NotDefinedException - if there is no such parameter.
 */
private Parameterization[] createParameterization(final String viewId, final String makeFast, final String commandId) throws NotDefinedException {
    ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
    Command command = commandService.getCommand(IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);

    IParameter viewIdParameter = command.getParameter(ShowSelectViewDialogHandler.VIEW_ID_COMMAND_PARAMETER_NAME);
    IParameter makeFastParameter = command.getParameter(ShowSelectViewDialogHandler.MAKE_FAST_COMMAND_PARAMETER_NAME);
    return new Parameterization[] { new Parameterization(viewIdParameter, viewId), new Parameterization(makeFastParameter, makeFast) };
}

But now ShowViewDialog signature has changed. Also the original author made the statement that his approach was based on ShowViewHandler and there must be a better more standard way of achieving the same affect, i.e. controlling the display of our reduced set of views.

Any ideas on how to achieve this? There might be a tutorial somewhere, I found the Vogella one, but it is fairly general.

Olddave
  • 397
  • 1
  • 2
  • 12
  • *How* did the signature change? – nitind Feb 19 '16 at 07:34
  • The signature changed from this: `ShowViewDialog(window, new EDMUIViewRegistry(EDMUIConstants.CATEGORY_IDS))` to `ShowViewDialog(Shell shell, MApplication application, MWindow window, EModelService modelService, IEclipseContext context)` – Olddave Feb 23 '16 at 00:15

1 Answers1

0

ShowViewDialog is an internal dialog so it should not have been used in the first place. As you have found internal dialogs can be changed without warning.

It looks like your code is using your own implementation of IViewRegistry. To stick to using only official APIs you would have to write your own version of the show view dialog. This is a fairly simple dialog using FilteredTree and the IViewRegistry getCategories and getViews methods.

There isn't a more standard way of doing this.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Looking at the ShowViewDialog as a basis for writing my own and it has a lot of internal dialog api imports. It also does not look simple at ~500 lines long. I suppose what I need to discover is the standard way of controlling which of your own views go where in the perspective. This seems to be the reason the previous developer over-rode ShowViewDialog. The internal ui imports are used in a number of the methods, e.g. getDialogBoundsSettings, configureShell and createFilteredTreeViewer. So do I need these methods? Or do I need to provide my own subclasses of the internal classes it uses? – Olddave Feb 23 '16 at 04:25
  • Most of those overrides are of methods in the JFace Dialog class that ShowViewDialog extends - they are not internal. Any new class would also be based on Dialog, you would have to write your own createFilteredTreeViewer method. – greg-449 Feb 23 '16 at 07:46