1 /*******************************************************************************2 * Copyright (c) 2007 IBM Corporation and others.3 * All rights reserved. This program and the accompanying materials4 * are made available under the terms of the Eclipse Public License v1.05 * which accompanies this distribution, and is available at6 * http://www.eclipse.org/legal/epl-v10.html7 *8 * Contributors:9 * IBM Corporation - initial API and implementation10 ******************************************************************************/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 editors29 * <p>30 * Replacement for CloseAllAction31 * </p>32 * 33 * @since 3.334 * 35 */36 public class CloseAllHandler extends AbstractEvaluationHandler {37 private Expression enabledWhen;38 39 public CloseAllHandler() {40 registerEnablement();41 }42 43 public Object execute(ExecutionEvent event) throws ExecutionException {44 IWorkbenchWindow window = HandlerUtil45 .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 = InternalHandlerUtil65 .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