01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal;
11:
12: import org.eclipse.core.commands.ExecutionEvent;
13: import org.eclipse.core.commands.ExecutionException;
14: import org.eclipse.core.expressions.EvaluationResult;
15: import org.eclipse.core.expressions.Expression;
16: import org.eclipse.core.expressions.ExpressionInfo;
17: import org.eclipse.core.expressions.IEvaluationContext;
18: import org.eclipse.core.runtime.CoreException;
19: import org.eclipse.ui.IEditorPart;
20: import org.eclipse.ui.ISources;
21: import org.eclipse.ui.IWorkbenchWindow;
22: import org.eclipse.ui.handlers.HandlerUtil;
23:
24: /**
25: * Closes the active editor.
26: * <p>
27: * Replacement for CloseEditorAction
28: * </p>
29: *
30: * @since 3.3
31: *
32: */
33: public class CloseEditorHandler extends AbstractEvaluationHandler {
34:
35: private Expression enabledWhen;
36:
37: public CloseEditorHandler() {
38: registerEnablement();
39: }
40:
41: public Object execute(ExecutionEvent event)
42: throws ExecutionException {
43: IWorkbenchWindow window = HandlerUtil
44: .getActiveWorkbenchWindowChecked(event);
45: IEditorPart part = HandlerUtil.getActiveEditorChecked(event);
46: window.getActivePage().closeEditor(part, true);
47:
48: return null;
49: }
50:
51: /*
52: * (non-Javadoc)
53: *
54: * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
55: */
56: protected Expression getEnabledWhenExpression() {
57: if (enabledWhen == null) {
58: enabledWhen = new Expression() {
59: public EvaluationResult evaluate(
60: 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: /*
72: * (non-Javadoc)
73: *
74: * @see org.eclipse.core.expressions.Expression#collectExpressionInfo(org.eclipse.core.expressions.ExpressionInfo)
75: */
76: public void collectExpressionInfo(ExpressionInfo info) {
77: info
78: .addVariableNameAccess(ISources.ACTIVE_EDITOR_NAME);
79: }
80: };
81: }
82: return enabledWhen;
83: }
84: }
|