Search code examples
eclipseeclipse-rcpe4

Eclipse 4 RCP - Application does not have an active window


I am trying to setup my view with EPartService, but still getting exception on that line with findPart call. Am i doing it right way?

Exception:

Caused by: java.lang.IllegalStateException: Application does not have an active window
    at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.getActiveWindowService(ApplicationPartServiceImpl.java:36)
    at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.findPart(ApplicationPartServiceImpl.java:87)

Code:

package cz.vutbr.fit.xhriba01.bc.handler;

import javax.inject.Inject;
import javax.inject.Named;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;

import cz.vutbr.fit.xhriba01.bc.ui.ExplorerView;
import cz.vutbr.fit.xhriba01.bc.ui.dialogs.NewFromDirectoryDialog;

public class NewFromDirectoryHandler {

    @Inject 
    private EPartService fPartService;

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {

        NewFromDirectoryDialog dialog = new NewFromDirectoryDialog(shell);
        dialog.create();
        if (dialog.open() == Window.OK) {
            String sourceDir = dialog.getSourceDir();
            String classDir = dialog.getClassDir();
            TreeViewer tv = ((ExplorerView)fPartService.findPart("bc.part.explorer").getObject()).getTreeViewer();
        }
    }

}

Solution

  • Try injecting the EPartService as an argument to the execute method:

    @Execute
    public void execute(EPartService fPartService, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell)
    

    It is best to avoid injecting fields in a handler as they will only be injected once when the handler is created. Things like EPartService change as the active part changes.