1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.ui.IEditorReference; 14 import org.eclipse.ui.IWorkbenchPage; 15 import org.eclipse.ui.IWorkbenchPart; 16 import org.eclipse.ui.IWorkbenchWindow; 17 18 21 public class CloseOthersAction extends PageEventAction { 22 23 28 public CloseOthersAction(IWorkbenchWindow window) { 29 super(WorkbenchMessages.CloseOthersAction_text, window); 30 setToolTipText(WorkbenchMessages.CloseOthersAction_toolTip); 31 setEnabled(false); 32 setId("closeOthers"); updateState(); 34 window.getWorkbench().getHelpSystem().setHelp(this, 35 IWorkbenchHelpContextIds.CLOSE_OTHERS_ACTION); 36 setActionDefinitionId("org.eclipse.ui.file.closeOthers"); } 38 39 42 public void pageActivated(IWorkbenchPage page) { 43 super.pageActivated(page); 44 updateState(); 45 } 46 47 50 public void pageClosed(IWorkbenchPage page) { 51 super.pageClosed(page); 52 updateState(); 53 } 54 55 58 public void partClosed(IWorkbenchPart part) { 59 super.partClosed(part); 60 updateState(); 61 } 62 63 66 public void partOpened(IWorkbenchPart part) { 67 super.partOpened(part); 68 updateState(); 69 } 70 71 74 public void run() { 75 if (getWorkbenchWindow() == null) { 76 return; 78 } 79 IWorkbenchPage page = getActivePage(); 80 if (page != null) { 81 IEditorReference[] refArray = page.getEditorReferences(); 82 if (refArray != null && refArray.length > 1) { 83 IEditorReference[] otherEditors = new IEditorReference[refArray.length - 1]; 84 IEditorReference activeEditor = (IEditorReference) page.getReference(page.getActiveEditor()); 85 for(int i = 0; i < refArray.length; i++) { 86 if(refArray[i] != activeEditor) 87 continue; 88 System.arraycopy(refArray, 0, otherEditors, 0, i); 89 System.arraycopy(refArray, i+1, otherEditors, i, refArray.length - 1 - i); 90 break; 91 } 92 page.closeEditors(otherEditors, true); 93 } 94 } 95 } 96 97 100 private void updateState() { 101 IWorkbenchPage page = getActivePage(); 102 if (page != null) { 103 setEnabled(page.getEditorReferences().length > 1); 104 } else { 105 setEnabled(false); 106 } 107 } 108 } 109 | Popular Tags |