KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > CloseEditorHandler


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

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 /**
27  * Closes the active editor.
28  * <p>
29  * Replacement for CloseEditorAction
30  * </p>
31  *
32  * @since 3.3
33  *
34  */

35 public class CloseEditorHandler extends AbstractEvaluationHandler {
36
37     private Expression enabledWhen;
38
39     public CloseEditorHandler() {
40         registerEnablement();
41     }
42
43     public Object JavaDoc 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     /*
53      * (non-Javadoc)
54      *
55      * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
56      */

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                 /*
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.addVariableNameAccess(ISources.ACTIVE_EDITOR_NAME);
78                 }
79             };
80         }
81         return enabledWhen;
82     }
83 }
84
Popular Tags