001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.action.ActionContributionItem;
013: import org.eclipse.jface.action.IContributionManager;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.ui.IPluginContribution;
016: import org.eclipse.ui.PlatformUI;
017: import org.eclipse.ui.activities.ActivityManagerEvent;
018: import org.eclipse.ui.activities.IActivityManagerListener;
019: import org.eclipse.ui.activities.IIdentifier;
020: import org.eclipse.ui.activities.IIdentifierListener;
021: import org.eclipse.ui.activities.IWorkbenchActivitySupport;
022: import org.eclipse.ui.activities.IdentifierEvent;
023: import org.eclipse.ui.activities.WorkbenchActivityHelper;
024:
025: /**
026: * Contribution item for actions provided by plugins via workbench action
027: * extension points.
028: */
029: public class PluginActionContributionItem extends
030: ActionContributionItem implements IIdentifierListener,
031: IActivityManagerListener {
032:
033: private IIdentifier identifier = null;
034:
035: /**
036: * Creates a new contribution item from the given action. The id of the
037: * action is used as the id of the item.
038: *
039: * @param action
040: * the action
041: */
042: public PluginActionContributionItem(PluginAction action) {
043: // dynamic UI (DDW) - this constructor has changed since 1113
044: super (action);
045: }
046:
047: /**
048: * Hook the activity and identifier listener (if necessary);
049: *
050: * @since 3.1
051: */
052: private void hookListeners() {
053: PlatformUI.getWorkbench().getActivitySupport()
054: .getActivityManager().addActivityManagerListener(this );
055: // set up the identifier if necessary
056: IIdentifier id = getIdentifier();
057: if (id != null) {
058: id.addIdentifierListener(this );
059: }
060: }
061:
062: /**
063: * Unhook the activity and identifier listener (if necessary);
064: *
065: * @since 3.1
066: */
067: private void unhookListeners() {
068: PlatformUI.getWorkbench().getActivitySupport()
069: .getActivityManager().removeActivityManagerListener(
070: this );
071:
072: IIdentifier id = getIdentifier();
073: if (id != null) {
074: id.removeIdentifierListener(this );
075: }
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.jface.action.IContributionItem#setParent(org.eclipse.jface.action.IContributionManager)
080: */
081: public void setParent(IContributionManager parent) {
082: IContributionManager oldParent = getParent();
083: super .setParent(parent);
084: if (oldParent == parent) {
085: return;
086: }
087:
088: if (parent == null) {
089: unhookListeners();
090: } else {
091: hookListeners();
092: }
093: }
094:
095: /**
096: * Create the IIdentifier reference for this item.
097: *
098: * @since 3.0
099: */
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(
173: ActivityManagerEvent activityManagerEvent) {
174: // ensure that if we're going from a non-filtering state that we get an identifier
175: // and vice versa.
176: if (WorkbenchActivityHelper.isFiltering() && identifier == null) {
177: hookListeners();
178: invalidateParent();
179: } else if (!WorkbenchActivityHelper.isFiltering()
180: && identifier != null) {
181: unhookListeners();
182: disposeIdentifier();
183: invalidateParent();
184: }
185: }
186:
187: /*
188: * For testing purposes only
189: */
190: public ISelection getSelection() {
191: return ((PluginAction) getAction()).getSelection();
192: }
193: }
|