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.IEditorReference;
020: import org.eclipse.ui.ISources;
021: import org.eclipse.ui.IWorkbenchPage;
022: import org.eclipse.ui.IWorkbenchWindow;
023: import org.eclipse.ui.handlers.HandlerUtil;
024:
025: /**
026: * Closes all editors except the one that is active.
027: * <p>
028: * Replacement for CloseOthersHandler
029: * </p>
030: *
031: * @since 3.3
032: *
033: */
034: public class CloseOthersHandler extends AbstractEvaluationHandler {
035: private Expression enabledWhen;
036:
037: public CloseOthersHandler() {
038: registerEnablement();
039: }
040:
041: public Object execute(ExecutionEvent event)
042: throws ExecutionException {
043: IWorkbenchWindow window = HandlerUtil
044: .getActiveWorkbenchWindowChecked(event);
045: IWorkbenchPage page = window.getActivePage();
046: if (page != null) {
047: IEditorReference[] refArray = page.getEditorReferences();
048: if (refArray != null && refArray.length > 1) {
049: IEditorReference[] otherEditors = new IEditorReference[refArray.length - 1];
050: IEditorReference activeEditor = (IEditorReference) page
051: .getReference(page.getActiveEditor());
052: for (int i = 0; i < refArray.length; i++) {
053: if (refArray[i] != activeEditor)
054: continue;
055: System.arraycopy(refArray, 0, otherEditors, 0, i);
056: System.arraycopy(refArray, i + 1, otherEditors, i,
057: refArray.length - 1 - i);
058: break;
059: }
060: page.closeEditors(otherEditors, true);
061: }
062: }
063:
064: return null;
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see org.eclipse.ui.internal.AbstractEvaluationHandler#getEnabledWhenExpression()
071: */
072: protected Expression getEnabledWhenExpression() {
073: if (enabledWhen == null) {
074: enabledWhen = new Expression() {
075: public EvaluationResult evaluate(
076: IEvaluationContext context)
077: throws CoreException {
078: IWorkbenchWindow window = InternalHandlerUtil
079: .getActiveWorkbenchWindow(context);
080: if (window != null) {
081: IWorkbenchPage page = window.getActivePage();
082: if (page != null) {
083: IEditorReference[] refArray = page
084: .getEditorReferences();
085: if (refArray != null && refArray.length > 1) {
086: return EvaluationResult.TRUE;
087:
088: }
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: }
|