KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.jface.action.ActionContributionItem;
15 import org.eclipse.jface.action.IContributionManager;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.ui.IPluginContribution;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.activities.ActivityManagerEvent;
20 import org.eclipse.ui.activities.IActivityManagerListener;
21 import org.eclipse.ui.activities.IIdentifier;
22 import org.eclipse.ui.activities.IIdentifierListener;
23 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
24 import org.eclipse.ui.activities.IdentifierEvent;
25 import org.eclipse.ui.activities.WorkbenchActivityHelper;
26
27 /**
28  * Contribution item for actions provided by plugins via workbench action
29  * extension points.
30  */

31 public class PluginActionContributionItem extends ActionContributionItem
32         implements IIdentifierListener, IActivityManagerListener {
33
34     private IIdentifier identifier = null;
35
36     /**
37      * Creates a new contribution item from the given action. The id of the
38      * action is used as the id of the item.
39      *
40      * @param action
41      * the action
42      */

43     public PluginActionContributionItem(PluginAction action) {
44         // dynamic UI (DDW) - this constructor has changed since 1113
45
super(action);
46     }
47
48     /**
49      * Hook the activity and identifier listener (if necessary);
50      *
51      * @since 3.1
52      */

53     private void hookListeners() {
54         PlatformUI.getWorkbench().getActivitySupport().getActivityManager()
55                 .addActivityManagerListener(this);
56         // set up the identifier if necessary
57
IIdentifier id = getIdentifier();
58         if (id != null) {
59             id.addIdentifierListener(this);
60         }
61     }
62     
63     /**
64      * Unhook the activity and identifier listener (if necessary);
65      *
66      * @since 3.1
67      */

68     private void unhookListeners() {
69         PlatformUI.getWorkbench().getActivitySupport().getActivityManager()
70                 .removeActivityManagerListener(this);
71
72         IIdentifier id = getIdentifier();
73         if (id != null) {
74             id.removeIdentifierListener(this);
75         }
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.jface.action.IContributionItem#setParent(org.eclipse.jface.action.IContributionManager)
80      */

81     public void setParent(IContributionManager parent) {
82         IContributionManager oldParent = getParent();
83         super.setParent(parent);
84         if (oldParent == parent) {
85             return;
86         }
87         
88         if (parent == null) {
89             unhookListeners();
90         } else {
91             hookListeners();
92         }
93     }
94     
95     /**
96      * Create the IIdentifier reference for this item.
97      *
98      * @since 3.0
99      */

100     private IIdentifier getIdentifier() {
101         if (!WorkbenchActivityHelper.isFiltering()) {
102             return null;
103         }
104         
105         if (identifier == null) {
106             IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
107                     .getWorkbench().getActivitySupport();
108             IPluginContribution contribution = (IPluginContribution) getAction();
109             // no need to check if contribution.getPluginId() == null - plugin
110
// actions are always from plugins.
111
identifier = workbenchActivitySupport.getActivityManager()
112                     .getIdentifier(
113                             WorkbenchActivityHelper
114                                     .createUnifiedId(contribution));
115         }
116         return identifier;
117     }
118
119     /**
120      * Dispose of the IIdentifier if necessary.
121      *
122      * @since 3.0
123      */

124     private void disposeIdentifier() {
125         identifier = null;
126     }
127
128     /**
129      * The default implementation of this <code>IContributionItem</code>
130      * method notifies the delegate if loaded and implements the <code>IActionDelegate2</code>
131      * interface.
132      */

133     public void dispose() {
134         unhookListeners();
135         disposeIdentifier();
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * @see org.eclipse.jface.action.ActionContributionItem#isVisible()
142      */

143     public boolean isVisible() {
144         if (identifier != null && !identifier.isEnabled()) {
145             return false;
146         }
147         return super.isVisible();
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.ui.activities.IIdentifierListener#identifierChanged(org.eclipse.ui.activities.IdentifierEvent)
152      */

153     public void identifierChanged(IdentifierEvent identifierEvent) {
154         invalidateParent();
155     }
156
157     /**
158      * Mark the parent dirty if we have a parent.
159      *
160      * @since 3.1
161      */

162     private void invalidateParent() {
163         IContributionManager parent = getParent();
164         if (parent != null) {
165             parent.markDirty();
166         }
167     }
168
169     /* (non-Javadoc)
170      * @see org.eclipse.ui.activities.IActivityManagerListener#activityManagerChanged(org.eclipse.ui.activities.ActivityManagerEvent)
171      */

172     public void activityManagerChanged(ActivityManagerEvent activityManagerEvent) {
173         // ensure that if we're going from a non-filtering state that we get an identifier
174
// and vice versa.
175
if (WorkbenchActivityHelper.isFiltering() && identifier == null) {
176             hookListeners();
177             invalidateParent();
178         } else if (!WorkbenchActivityHelper.isFiltering() && identifier != null) {
179             unhookListeners();
180             disposeIdentifier();
181             invalidateParent();
182         }
183     }
184     
185     /*
186      * For testing purposes only
187      */

188     public ISelection getSelection() {
189         return ((PluginAction)getAction()).getSelection();
190     }
191 }
192
Popular Tags