KickJava   Java API By Example, From Geeks To Geeks.

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


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.IViewReference;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchPartReference;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.handlers.HandlerUtil;
28
29 /**
30  * Activates the most recently used editor in the current window.
31  * <p>
32  * Replacement for: ActivateEditorAction
33  * </p>
34  *
35  * @since 3.3
36  */

37 public class ActivateEditorHandler extends AbstractEvaluationHandler {
38
39     private Expression enabledWhen;
40
41     public ActivateEditorHandler() {
42         registerEnablement();
43     }
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49      */

50     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
51         IWorkbenchWindow window = HandlerUtil
52                 .getActiveWorkbenchWindowChecked(event);
53         IWorkbenchPage page = window.getActivePage();
54         if (page != null) {
55             IEditorPart part = HandlerUtil.getActiveEditor(event);
56             if (part != null) {
57                 page.activate(part);
58                 part.setFocus();
59             } else {
60                 IWorkbenchPartReference ref = page.getActivePartReference();
61                 if (ref instanceof IViewReference) {
62                     if (((WorkbenchPage) page).isFastView((IViewReference) ref)) {
63                         ((WorkbenchPage) page)
64                                 .toggleFastView((IViewReference) ref);
65                     }
66                 }
67             }
68         }
69         return null;
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
76      */

77     protected Expression getEnabledWhenExpression() {
78         if (enabledWhen == null) {
79             enabledWhen = new Expression() {
80                 public EvaluationResult evaluate(IEvaluationContext context)
81                         throws CoreException {
82                     IWorkbenchWindow window = InternalHandlerUtil
83                             .getActiveWorkbenchWindow(context);
84                     if (window != null) {
85                         if (window.getActivePage() != null) {
86                             return EvaluationResult.TRUE;
87                         }
88                     }
89                     return EvaluationResult.FALSE;
90                 }
91
92                 /*
93                  * (non-Javadoc)
94                  *
95                  * @see org.eclipse.core.expressions.Expression#collectExpressionInfo(org.eclipse.core.expressions.ExpressionInfo)
96                  */

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