1 11 12 package org.eclipse.ui.internal; 13 14 import org.eclipse.core.commands.ExecutionEvent; 15 import org.eclipse.core.commands.ExecutionException; 16 import org.eclipse.core.expressions.EvaluationResult; 17 import org.eclipse.core.expressions.Expression; 18 import org.eclipse.core.expressions.ExpressionInfo; 19 import org.eclipse.core.expressions.IEvaluationContext; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.ISources; 23 import org.eclipse.ui.IWorkbenchWindow; 24 import org.eclipse.ui.handlers.HandlerUtil; 25 26 35 public class CloseEditorHandler extends AbstractEvaluationHandler { 36 37 private Expression enabledWhen; 38 39 public CloseEditorHandler() { 40 registerEnablement(); 41 } 42 43 public Object execute(ExecutionEvent event) throws ExecutionException { 44 IWorkbenchWindow window = HandlerUtil 45 .getActiveWorkbenchWindowChecked(event); 46 IEditorPart part = HandlerUtil.getActiveEditorChecked(event); 47 window.getActivePage().closeEditor(part, true); 48 49 return null; 50 } 51 52 57 protected Expression getEnabledWhenExpression() { 58 if (enabledWhen == null) { 59 enabledWhen = new Expression() { 60 public EvaluationResult evaluate(IEvaluationContext context) 61 throws CoreException { 62 IEditorPart part = InternalHandlerUtil 63 .getActiveEditor(context); 64 if (part != null) { 65 return EvaluationResult.TRUE; 66 67 } 68 return EvaluationResult.FALSE; 69 } 70 71 76 public void collectExpressionInfo(ExpressionInfo info) { 77 info.addVariableNameAccess(ISources.ACTIVE_EDITOR_NAME); 78 } 79 }; 80 } 81 return enabledWhen; 82 } 83 } 84 | Popular Tags |