1 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 36 public class CloseOthersHandler extends AbstractEvaluationHandler { 37 private Expression enabledWhen; 38 39 public CloseOthersHandler() { 40 registerEnablement(); 41 } 42 43 public Object 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 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 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 |