KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > PartEventAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.ui.IPartListener;
15 import org.eclipse.ui.IWorkbenchPart;
16
17 /**
18  * The abstract superclass for actions that listen to part activation and
19  * open/close events. This implementation tracks the active part (see
20  * <code>getActivePart</code>) and provides a convenient place to monitor
21  * part lifecycle events that could affect the availability of the action.
22  * <p>
23  * Subclasses must implement the following <code>IAction</code> method:
24  * <ul>
25  * <li><code>run</code> - to do the action's work</li>
26  * </ul>
27  * </p>
28  * <p>
29  * Subclasses may extend any of the <code>IPartListener</code> methods if the
30  * action availablity needs to be recalculated:
31  * <ul>
32  * <li><code>partActivated</code></li>
33  * <li><code>partDeactivated</code></li>
34  * <li><code>partOpened</code></li>
35  * <li><code>partClosed</code></li>
36  * <li><code>partBroughtToTop</code></li>
37  * </ul>
38  * </p>
39  * <p>
40  * Although this method implements the <code>IPartListener</code> interface,
41  * it does NOT register itself.
42  * </p>
43  */

44 public abstract class PartEventAction extends Action implements IPartListener {
45
46     /**
47      * The active part, or <code>null</code> if none.
48      */

49     private IWorkbenchPart activePart;
50
51     /**
52      * Creates a new action with the given text.
53      *
54      * @param text the action's text, or <code>null</code> if there is no text
55      */

56     protected PartEventAction(String JavaDoc text) {
57         super(text);
58     }
59
60     /**
61      * Creates a new action with the given text and style.
62      *
63      * @param text the action's text, or <code>null</code> if there is no text
64      * @param style one of <code>AS_PUSH_BUTTON</code>, <code>AS_CHECK_BOX</code>,
65      * <code>AS_DROP_DOWN_MENU</code>, <code>AS_RADIO_BUTTON</code>, and
66      * <code>AS_UNSPECIFIED</code>
67      * @since 3.0
68      */

69     protected PartEventAction(String JavaDoc text, int style) {
70         super(text, style);
71     }
72
73     /**
74      * Returns the currently active part in the workbench.
75      *
76      * @return currently active part in the workbench, or <code>null</code> if none
77      */

78     public IWorkbenchPart getActivePart() {
79         return activePart;
80     }
81
82     /**
83      * The <code>PartEventAction</code> implementation of this
84      * <code>IPartListener</code> method records that the given part is active.
85      * Subclasses may extend this method if action availability has to be
86      * recalculated.
87      */

88     public void partActivated(IWorkbenchPart part) {
89         activePart = part;
90     }
91
92     /**
93      * The <code>PartEventAction</code> implementation of this
94      * <code>IPartListener</code> method does nothing. Subclasses should extend
95      * this method if action availability has to be recalculated.
96      */

97     public void partBroughtToTop(IWorkbenchPart part) {
98         // do nothing
99
}
100
101     /**
102      * The <code>PartEventAction</code> implementation of this
103      * <code>IPartListener</code> method clears the active part if it just closed.
104      * Subclasses may extend this method if action availability has to be
105      * recalculated.
106      */

107     public void partClosed(IWorkbenchPart part) {
108         if (part == activePart) {
109             activePart = null;
110         }
111     }
112
113     /**
114      * The <code>PartEventAction</code> implementation of this
115      * <code>IPartListener</code> method records that there is no active part.
116      * Subclasses may extend this method if action availability has to be
117      * recalculated.
118      */

119     public void partDeactivated(IWorkbenchPart part) {
120         activePart = null;
121     }
122
123     /**
124      * The <code>PartEventAction</code> implementation of this
125      * <code>IPartListener</code> method does nothing. Subclasses should extend
126      * this method if action availability has to be recalculated.
127      */

128     public void partOpened(IWorkbenchPart part) {
129         // do nothing
130
}
131 }
132
Popular Tags