diff options
| author | Paul Webster | 2013-04-22 15:12:22 +0000 |
|---|---|---|
| committer | Paul Webster | 2013-04-22 15:39:50 +0000 |
| commit | b976b22fca1d2c3b279d920137e2fcd1beccc927 (patch) | |
| tree | 1b6f2083475323911a5986882a7479e751fe6966 | |
| parent | 80a33bbc7214e4aa595ff509f067ea681e63c4af (diff) | |
| download | eclipse.platform.ui-b976b22fca1d2c3b279d920137e2fcd1beccc927.tar.gz eclipse.platform.ui-b976b22fca1d2c3b279d920137e2fcd1beccc927.tar.xz eclipse.platform.ui-b976b22fca1d2c3b279d920137e2fcd1beccc927.zip | |
Bug 394336 - File->Close menu item is disabled even when current editor
is activated
Fix is to make sure our handler setEnabled(*) is being called and the
handler enabled change firing is propagated. Then remove calls that
generate stack overflows.
Change-Id: Ifbd0e94e37d43b76b1dcbfbcbacb28c262c3ec11
3 files changed, 81 insertions, 8 deletions
diff --git a/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/HandlerServiceHandler.java b/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/HandlerServiceHandler.java index 78ba3d363ec..47b58181e8a 100644 --- a/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/HandlerServiceHandler.java +++ b/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/HandlerServiceHandler.java @@ -20,6 +20,7 @@ import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.e4.core.commands.ExpressionContext; import org.eclipse.e4.core.commands.internal.HandlerServiceImpl.ExecutionContexts; import org.eclipse.e4.core.contexts.ContextInjectionFactory; +import org.eclipse.e4.core.contexts.EclipseContextFactory; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; @@ -36,6 +37,26 @@ public class HandlerServiceHandler extends AbstractHandler { this.commandId = commandId; } + @Override + public boolean isEnabled() { + ExecutionContexts contexts = HandlerServiceImpl.peek(); + // setEnabled(contexts); + IEclipseContext executionContext = getExecutionContext(contexts); + if (executionContext == null) { + return super.isEnabled(); + } + Object handler = HandlerServiceImpl.lookUpHandler(executionContext, commandId); + if (handler == null) { + setBaseEnabled(false); + return super.isEnabled(); + } + IEclipseContext staticContext = getStaticContext(contexts); + Boolean result = (Boolean) ContextInjectionFactory.invoke(handler, CanExecute.class, + executionContext, staticContext, Boolean.TRUE); + setBaseEnabled(result.booleanValue()); + return super.isEnabled(); + } + /* * (non-Javadoc) * @@ -43,22 +64,24 @@ public class HandlerServiceHandler extends AbstractHandler { */ @Override public void setEnabled(Object evaluationContext) { + boolean createContext = false; IEclipseContext executionContext = getExecutionContext(evaluationContext); if (executionContext == null) { return; } - IEclipseContext staticContext = getStaticContext(evaluationContext); Object handler = HandlerServiceImpl.lookUpHandler(executionContext, commandId); if (handler == null) { - setBaseEnabled(false); return; } - try { - Boolean result = ((Boolean) ContextInjectionFactory.invoke(handler, CanExecute.class, - executionContext, staticContext, Boolean.TRUE)); - setBaseEnabled(result.booleanValue()); - } catch (Exception e) { - e.printStackTrace(); + IEclipseContext staticContext = getStaticContext(evaluationContext); + if (staticContext == null) { + staticContext = EclipseContextFactory.create(); + createContext = true; + } + ContextInjectionFactory.invoke(handler, SetEnabled.class, executionContext, staticContext, + Boolean.TRUE); + if (createContext) { + staticContext.dispose(); } } @@ -114,6 +137,8 @@ public class HandlerServiceHandler extends AbstractHandler { ExecutionContexts contexts = HandlerServiceImpl.peek(); if (contexts != null) { Object handler = HandlerServiceImpl.lookUpHandler(contexts.context, commandId); + // TODO used to check if E4HandlerProxy.getHandler() was an IHandler + // then IHandler.isHandled(); return handler != null; } diff --git a/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/SetEnabled.java b/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/SetEnabled.java new file mode 100644 index 00000000000..e634b2efb2d --- /dev/null +++ b/bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/SetEnabled.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + ******************************************************************************/ + +package org.eclipse.e4.core.commands.internal; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Use this annotation to tag a method to call the handler setEnabled method. + * <p> + * This annotation must not be applied to more than one method per class. If several class methods + * are tagged with this annotation, only one of them will be called. + * </p> + * + * @since 1.3 + * @noreference + */ +@Documented +@Target({ ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface SetEnabled { + // intentionally left empty +} diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/handlers/E4HandlerProxy.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/handlers/E4HandlerProxy.java index 73743eea942..c1e856a5237 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/handlers/E4HandlerProxy.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/handlers/E4HandlerProxy.java @@ -26,6 +26,7 @@ import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.e4.core.commands.ExpressionContext; import org.eclipse.e4.core.commands.internal.HandlerServiceHandler; import org.eclipse.e4.core.commands.internal.HandlerServiceImpl; +import org.eclipse.e4.core.commands.internal.SetEnabled; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; @@ -114,4 +115,16 @@ public class E4HandlerProxy implements IHandlerListener, IElementUpdater { ((IElementUpdater) handler).updateElement(element, parameters); } } + + @SetEnabled + void setEnabled(@Optional IEvaluationContext evalContext) { + if (evalContext == null) { + IEclipseContext appContext = ((Workbench) PlatformUI.getWorkbench()).getApplication() + .getContext(); + evalContext = new ExpressionContext(appContext); + } + if (handler instanceof IHandler2) { + ((IHandler2) handler).setEnabled(evalContext); + } + } } |
