KickJava   Java API By Example, From Geeks To Geeks.

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


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.IEditorReference;
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 editors except the one that is active.
29  * <p>
30  * Replacement for CloseOthersHandler
31  * </p>
32  *
33  * @since 3.3
34  *
35  */

36 public class CloseOthersHandler extends AbstractEvaluationHandler {
37     private Expression enabledWhen;
38
39     public CloseOthersHandler() {
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             IEditorReference[] refArray = page.getEditorReferences();
49             if (refArray != null && refArray.length > 1) {
50                 IEditorReference[] otherEditors = new IEditorReference[refArray.length - 1];
51                 IEditorReference activeEditor = (IEditorReference) page
52                         .getReference(page.getActiveEditor());
53                 for (int i = 0; i < refArray.length; i++) {
54                     if (refArray[i] != activeEditor)
55                         continue;
56                     System.arraycopy(refArray, 0, otherEditors, 0, i);
57                     System.arraycopy(refArray, i + 1, otherEditors, i,
58                             refArray.length - 1 - i);
59                     break;
60                 }
61                 page.closeEditors(otherEditors, true);
62             }
63         }
64
65         return null;
66     }
67
68     /*
69      * (non-Javadoc)
70      *
71      * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
72      */

73     protected Expression getEnabledWhenExpression() {
74         if (enabledWhen == null) {
75             enabledWhen = new Expression() {
76                 public EvaluationResult evaluate(IEvaluationContext context)
77                         throws CoreException {
78                     IWorkbenchWindow window = InternalHandlerUtil
79                             .getActiveWorkbenchWindow(context);
80                     if (window != null) {
81                         IWorkbenchPage page = window.getActivePage();
82                         if (page != null) {
83                             IEditorReference[] refArray = page
84                                     .getEditorReferences();
85                             if (refArray != null && refArray.length > 1) {
86                                 return EvaluationResult.TRUE;
87
88                             }
89                         }
90                     }
91                     return EvaluationResult.FALSE;
92                 }
93
94                 /*
95                  * (non-Javadoc)
96                  *
97                  * @see org.eclipse.core.expressions.Expression#collectExpressionInfo(org.eclipse.core.expressions.ExpressionInfo)
98                  */

99                 public void collectExpressionInfo(ExpressionInfo info) {
100                     info
101                             .addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
102                 }
103             };
104         }
105         return enabledWhen;
106     }
107 }
108
Popular Tags