简体   繁体   中英

How to redirect key presses to another shell in an Eclipse-based app?

I'm working on a way to maximise an EditorPart in my Eclipse-based RCP app to be absolutely full-screen, no trim, no menu, and so on. Actually, it's a GEF Editor. It's a bit of a hack, but it kind of works:

GraphicalViewer viewer = (GraphicalViewer)getWorkbenchPart().getAdapter(GraphicalViewer.class);
        Control control = viewer.getControl();
        Composite oldParent = control.getParent();

        Shell shell = new Shell(SWT.APPLICATION_MODAL);
        shell.setFullScreen(true);
        shell.setMaximized(true);

        // Some code here to listen to Esc key to dispose Shell and return...

        shell.setLayout(new FillLayout());
        control.setParent(shell);
        shell.open();

Basically it sets the parent of the GraphicalViewer control to the newly created, maximised Shell. Pressing escape will return the control to it's original parent (code not shown for brevity).

The only thing that doesn't work is receiving global key presses ( Del , Ctrl + Z , Ctrl + A ) the ones that are declared for the Workbench and forwarded to the EditorPart. Is there any way I can hook into these or redirect them from the EditorPart to forward them on to the GraphicalViewer?

Thanks in advance

The short answer is you can't do that. Once you re-parent that composite out of the workbench window, it's totally busted. The system won't correctly deal with the part, as events (like activation, focus, setBounds(*)) aren't being processed.

The only supported way would be to open a new Workbench window with a perspective that only contained the editor area, and extending your org.eclipse.ui.application.WorkbenchWindowAdvisor.createWindowContents(Shell) method for that window+perspective combination to hide all of the trim you don't want, using org.eclipse.ui.application.IWorkbenchWindowConfigurer .

ex, in MyWorkbenchWindowAdvisor:

public void createWindowContents(Shell shell) {
    if (isCreatingSpecialPerspective()) {
        final IWorkbenchWindowConfigurer config = getWindowConfigurer();
        config.setShowCoolBar(false);
        config.setShowFastViewBars(false);
        config.setShowMenuBar(false);
        config.setShowPerspectiveBar(false);
        config.setShowProgressIndicator(false);
        config.setShowStatusLine(false);
    }
    super.createWindowContents(shell);
}

Also check out http://code.google.com/p/eclipse-fullscreen/ which has EPLed code in it concerned with running an RCP program with fullscreen support.

My suggestion is to try to approach this problem from another angle. Instead of listening for keystrokes in your code and acting upon them, define your own key binding scheme and then create commands and make them use this scheme. This would mean that you no longer have to listen for the key strokes in your code, but instead do whatever needs to be done through commands executed by these key strokes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM