我如何有条件地阻止用户在 eclipse E4 RCP 应用程序中导航到不同的 view/part?

How can i conditionally prevent a user from navigating to a different view/part in eclipse E4 RCP application?

我试图阻止用户在 eclipse E4 的视角下转到不同的 view/part application.When 我试图导航到相同的视角和视图,我正面临 Whosebug框架递归调用showPart方法异常

NavigationHelper.showPerspective(CommonConstants.PERSPECTIVE1, getEclipseContext());
NavigationHelper.showPart(CommonConstants.VIEW1, getEclipseContext());
NavigationHelper.showPart(CommonConstants.VIEW2, getEclipseContext());

我的showPart方法是这样的,

public static boolean showPart(String partId, IEclipseContext eclipseContext) {

        logger.debug("showPart::STARTED::" + partId);
        
        if (null == eclipseContext) {
            eclipseContext = getEclipseContext();
        }

        if (Model.getInstance().hasDataChanged()) {
            if (partId.equalsIgnoreCase(CommonConstants.VIEW1)
                    || partId.equalsIgnoreCase(CommonConstants.VIEW2)) {
                isNavigationSuccessful = true;
            } else {
                isNavigationSuccessful = false;
                Navigation.showWarning();
            }
        }

        if (isNavigationSuccessful) {                       
            findPartAndActivate(partId, eclipseContext, true);
        }
        logger.debug("isNavigationSuccessful = " + isNavigationSuccessful);
        logger.debug("showPart::END::" + partId);

        return isNavigationSuccessful;

    }

findPartAndActivate 看起来像这样

private static boolean findPartAndActivate(String partId, IEclipseContext eclipseContext, boolean giveFocus) {
        MTrimmedWindow applicationWindow = ((MTrimmedWindow) ((MApplication) eclipseContext.get(MApplication.class))
                .getChildren().get(0));
        IEclipseContext currentContext = applicationWindow.getContext();
        EPartService partService = currentContext.get(EPartService.class);
        EModelService modelService = currentContext.get(EModelService.class);
        MPart part = (MPart) modelService.find(partId, eclipseContext.get(MApplication.class).getChildren().get(0));        
        partService.activate(part, giveFocus);
        return true;
    }
    

一旦用户离开 part/View

,将调用 partDeactivated
 public void partDeactivated(@Active MPart part) {

            if (partInstance.getElementId() != part.getElementId()) {
                return;
            }                        
            
            if (transactionButton != null && !transactionButton.isDisposed() && transactionButton.isEnabled()
                    && isTransactionCompleted && NavigationHelper.getEditableViewInstance() != null && !partDeactivateFlag) {
                doTransaction();
                partDeactivateFlag = true;
            }

            if (Navigation.isPerspective()) {
                if (EModel.getInstance().hasDataChanged()/*&& !Model.getInstance().isSwitchFlag()*/) {
                    System.out.println("Changes");
                    //Model.getInstance().setSwitchFlag(true);
                    //partDeactivateFlag = true;
                    NavigationHelper.showPerspective(CommonConstants.PERSPECTIVE1, getEclipseContext());
                    NavigationHelper.showPart(CommonConstants.VIEW1, getEclipseContext());

                    NavigationHelper.showPart(CommonConstants.VIEW2, getEclipseContext());
                    
                    

                    
                }
            }
            viewDeactivated();  
        }        

    };

java.lang.WhosebugError : 空 org.eclipse.e4.ui.workbench.modeling.ElementMatcher.select(ElementMatcher.java:71) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:182) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:317) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:251) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElements(ModelServiceImpl.java:428) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElements(ModelServiceImpl.java:409) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElements(ModelServiceImpl.java:414) org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.find(ModelServiceImpl.java:448)

这是 Whosebug 跟踪..

我该如何解决..?

嗯,堆栈跟踪显示这是您的代码中的一个错误。您正在 运行ning NavigationHelper.showPart 部分停用侦听器中,但您的代码导致另一个部分停用事件再次调用停用侦听器,再次调用 showPart 等等。

您不能尝试在部分停用侦听器中直接显示不同的部分。

一种可能性是在 deactivate 事件完成后,在 运行 showPart 部分使用 Display.asyncExec deactivate 侦听器。