KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > CloseOthersAction


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
19  * Closes all editors except the one that is active.
20  */

21 public class CloseOthersAction extends PageEventAction {
22
23     /**
24      * Create an instance of this class.
25      *
26      * @param window the window
27      */

28     public CloseOthersAction(IWorkbenchWindow window) {
29         super(WorkbenchMessages.CloseOthersAction_text, window);
30         setToolTipText(WorkbenchMessages.CloseOthersAction_toolTip);
31         setEnabled(false);
32         setId("closeOthers"); //$NON-NLS-1$
33
updateState();
34         window.getWorkbench().getHelpSystem().setHelp(this,
35                 IWorkbenchHelpContextIds.CLOSE_OTHERS_ACTION);
36         setActionDefinitionId("org.eclipse.ui.file.closeOthers"); //$NON-NLS-1$
37
}
38
39     /* (non-Javadoc)
40      * Method declared on PageEventAction.
41      */

42     public void pageActivated(IWorkbenchPage page) {
43         super.pageActivated(page);
44         updateState();
45     }
46
47     /* (non-Javadoc)
48      * Method declared on PageEventAction.
49      */

50     public void pageClosed(IWorkbenchPage page) {
51         super.pageClosed(page);
52         updateState();
53     }
54
55     /* (non-Javadoc)
56      * Method declared on PartEventAction.
57      */

58     public void partClosed(IWorkbenchPart part) {
59         super.partClosed(part);
60         updateState();
61     }
62
63     /* (non-Javadoc)
64      * Method declared on PartEventAction.
65      */

66     public void partOpened(IWorkbenchPart part) {
67         super.partOpened(part);
68         updateState();
69     }
70
71     /* (non-Javadoc)
72      * Method declared on Action.
73      */

74     public void run() {
75         if (getWorkbenchWindow() == null) {
76             // action has been disposed
77
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     /**
98      * Enable the action if there is more than one editor open.
99      */

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