001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.core.runtime.Assert;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.jface.action.IMenuManager;
015: import org.eclipse.jface.action.IToolBarManager;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.ISelectionProvider;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.ui.IEditorPart;
022: import org.eclipse.ui.IViewPart;
023: import org.eclipse.ui.IWorkbenchPart;
024: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
025:
026: /**
027: * This class reads the registry for extensions that plug into
028: * 'popupMenus' extension point and deals only with the 'viewerContribution'
029: * elements.
030: */
031: public class ViewerActionBuilder extends PluginActionBuilder {
032:
033: private ISelectionProvider provider;
034:
035: private IWorkbenchPart part;
036:
037: /**
038: * Basic contstructor
039: */
040: public ViewerActionBuilder() {
041: }
042:
043: /* (non-Javadoc)
044: * Method declared on PluginActionBuilder.
045: */
046: protected ActionDescriptor createActionDescriptor(
047: IConfigurationElement element) {
048: if (part instanceof IViewPart) {
049: return new ActionDescriptor(element,
050: ActionDescriptor.T_VIEW, part);
051: }
052: return new ActionDescriptor(element, ActionDescriptor.T_EDITOR,
053: part);
054: }
055:
056: /* (non-Javadoc)
057: * Method declared on PluginActionBuilder.
058: */
059: protected BasicContribution createContribution() {
060: return new ViewerContribution(provider);
061: }
062:
063: /**
064: * Dispose of the action builder
065: */
066: public void dispose() {
067: if (cache != null) {
068: for (int i = 0; i < cache.size(); i++) {
069: ((BasicContribution) cache.get(i)).dispose();
070: }
071: cache = null;
072: }
073: }
074:
075: /* (non-Javadoc)
076: * Method declared on PluginActionBuilder.
077: */
078: protected boolean readElement(IConfigurationElement element) {
079: String tag = element.getName();
080:
081: // Found visibility sub-element
082: if (currentContribution != null
083: && tag
084: .equals(IWorkbenchRegistryConstants.TAG_VISIBILITY)) {
085: ((ViewerContribution) currentContribution)
086: .setVisibilityTest(element);
087: return true;
088: }
089:
090: return super .readElement(element);
091: }
092:
093: /**
094: * Reads the contributions for a viewer menu.
095: * This method is typically used in conjunction with <code>contribute</code> to read
096: * and then insert actions for a particular viewer menu.
097: *
098: * @param id the menu id
099: * @param prov the selection provider for the control containing the menu
100: * @param part the part containing the menu.
101: * @return <code>true</code> if 1 or more items were read.
102: */
103: public boolean readViewerContributions(String id,
104: ISelectionProvider prov, IWorkbenchPart part) {
105: Assert.isTrue(part instanceof IViewPart
106: || part instanceof IEditorPart);
107: provider = prov;
108: this .part = part;
109: readContributions(id,
110: IWorkbenchRegistryConstants.TAG_VIEWER_CONTRIBUTION,
111: IWorkbenchRegistryConstants.PL_POPUP_MENU);
112: return (cache != null);
113: }
114:
115: /**
116: * Helper class to collect the menus and actions defined within a
117: * contribution element.
118: */
119: private static class ViewerContribution extends BasicContribution
120: implements ISelectionChangedListener {
121: private ISelectionProvider selProvider;
122:
123: private ActionExpression visibilityTest;
124:
125: /**
126: * Create a new ViewerContribution.
127: *
128: * @param selProvider the selection provider
129: */
130: public ViewerContribution(ISelectionProvider selProvider) {
131: super ();
132: this .selProvider = selProvider;
133: if (selProvider != null) {
134: selProvider.addSelectionChangedListener(this );
135: }
136: }
137:
138: /**
139: * Set the visibility test.
140: *
141: * @param element the element
142: */
143: public void setVisibilityTest(IConfigurationElement element) {
144: visibilityTest = new ActionExpression(element);
145: }
146:
147: /* (non-Javadoc)
148: * Method declared on BasicContribution.
149: */
150: public void contribute(IMenuManager menu,
151: boolean menuAppendIfMissing, IToolBarManager toolbar,
152: boolean toolAppendIfMissing) {
153: boolean visible = true;
154:
155: if (visibilityTest != null) {
156: ISelection selection = selProvider.getSelection();
157: if (selection instanceof IStructuredSelection) {
158: visible = visibilityTest
159: .isEnabledFor((IStructuredSelection) selection);
160: } else {
161: visible = visibilityTest.isEnabledFor(selection);
162: }
163: }
164:
165: if (visible) {
166: super .contribute(menu, menuAppendIfMissing, toolbar,
167: toolAppendIfMissing);
168: }
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.ui.internal.PluginActionBuilder.BasicContribution#dispose()
173: */
174: public void dispose() {
175: if (selProvider != null) {
176: selProvider.removeSelectionChangedListener(this );
177: }
178: disposeActions();
179: super .dispose();
180: }
181:
182: /**
183: * Rather than hooking up each action as a selection listener,
184: * the contribution itself is added, and propagates
185: * the selection changed notification to all actions.
186: * This simplifies cleanup, in addition to potentially reducing the number of listeners.
187: *
188: * @see ISelectionChangedListener
189: * @since 3.1
190: */
191: public void selectionChanged(SelectionChangedEvent event) {
192: if (actions != null) {
193: if (actions != null) {
194: for (int i = 0; i < actions.size(); i++) {
195: PluginAction proxy = ((ActionDescriptor) actions
196: .get(i)).getAction();
197: proxy.selectionChanged(event);
198: }
199: }
200: }
201: }
202: }
203:
204: }
|