KickJava   Java API By Example, From Geeks To Geeks.

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


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.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.handlers.HandlerUtil;
26
27 /**
28  * Closes all active editors
29  * <p>
30  * Replacement for CloseAllAction
31  * </p>
32  *
33  * @since 3.3
34  *
35  */

36 public class CloseAllHandler extends AbstractEvaluationHandler {
37     private Expression enabledWhen;
38
39     public CloseAllHandler() {
40         registerEnablement();
41     }
42
43     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
44         IWorkbenchWindow window = HandlerUtil
45                 .getActiveWorkbenchWindowChecked(event);
46         IWorkbenchPage page = window.getActivePage();
47         if (page != null) {
48             page.closeAllEditors(true);
49         }
50
51         return null;
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
58      */

59     protected Expression getEnabledWhenExpression() {
60         if (enabledWhen == null) {
61             enabledWhen = new Expression() {
62                 public EvaluationResult evaluate(IEvaluationContext context)
63                         throws CoreException {
64                     IEditorPart part = InternalHandlerUtil
65                             .getActiveEditor(context);
66                     if (part != null) {
67                         return EvaluationResult.TRUE;
68
69                     }
70                     return EvaluationResult.FALSE;
71                 }
72
73                 /*
74                  * (non-Javadoc)
75                  *
76                  * @see org.eclipse.core.expressions.Expression#collectExpressionInfo(org.eclipse.core.expressions.ExpressionInfo)
77                  */

78                 public void collectExpressionInfo(ExpressionInfo info) {
79                     info.addVariableNameAccess(ISources.ACTIVE_EDITOR_NAME);
80                 }
81             };
82         }
83         return enabledWhen;
84     }
85 }
86
Popular Tags