001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.core.commands.ExecutionEvent;
013: import org.eclipse.core.commands.ExecutionException;
014: import org.eclipse.core.expressions.EvaluationResult;
015: import org.eclipse.core.expressions.Expression;
016: import org.eclipse.core.expressions.ExpressionInfo;
017: import org.eclipse.core.expressions.IEvaluationContext;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.ui.IEditorPart;
020: import org.eclipse.ui.ISources;
021: import org.eclipse.ui.IViewReference;
022: import org.eclipse.ui.IWorkbenchPage;
023: import org.eclipse.ui.IWorkbenchPartReference;
024: import org.eclipse.ui.IWorkbenchWindow;
025: import org.eclipse.ui.handlers.HandlerUtil;
026:
027: /**
028: * Activates the most recently used editor in the current window.
029: * <p>
030: * Replacement for: ActivateEditorAction
031: * </p>
032: *
033: * @since 3.3
034: */
035: public class ActivateEditorHandler extends AbstractEvaluationHandler {
036:
037: private Expression enabledWhen;
038:
039: public ActivateEditorHandler() {
040: registerEnablement();
041: }
042:
043: /*
044: * (non-Javadoc)
045: *
046: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
047: */
048: public Object execute(ExecutionEvent event)
049: throws ExecutionException {
050: IWorkbenchWindow window = HandlerUtil
051: .getActiveWorkbenchWindowChecked(event);
052: IWorkbenchPage page = window.getActivePage();
053: if (page != null) {
054: IEditorPart part = HandlerUtil.getActiveEditor(event);
055: if (part != null) {
056: page.activate(part);
057: part.setFocus();
058: } else {
059: IWorkbenchPartReference ref = page
060: .getActivePartReference();
061: if (ref instanceof IViewReference) {
062: if (((WorkbenchPage) page)
063: .isFastView((IViewReference) ref)) {
064: ((WorkbenchPage) page)
065: .toggleFastView((IViewReference) ref);
066: }
067: }
068: }
069: }
070: return null;
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
077: */
078: protected Expression getEnabledWhenExpression() {
079: if (enabledWhen == null) {
080: enabledWhen = new Expression() {
081: public EvaluationResult evaluate(
082: IEvaluationContext context)
083: throws CoreException {
084: IWorkbenchWindow window = InternalHandlerUtil
085: .getActiveWorkbenchWindow(context);
086: if (window != null) {
087: if (window.getActivePage() != null) {
088: return EvaluationResult.TRUE;
089: }
090: }
091: return EvaluationResult.FALSE;
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.eclipse.core.expressions.Expression#collectExpressionInfo(org.eclipse.core.expressions.ExpressionInfo)
098: */
099: public void collectExpressionInfo(ExpressionInfo info) {
100: info
101: .addVariableNameAccess(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
102: }
103: };
104: }
105: return enabledWhen;
106: }
107: }
|