KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.ISelectionProvider;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
26
27 /**
28  * This class reads the registry for extensions that plug into
29  * 'popupMenus' extension point and deals only with the 'viewerContribution'
30  * elements.
31  */

32 public class ViewerActionBuilder extends PluginActionBuilder {
33     
34
35     private ISelectionProvider provider;
36
37     private IWorkbenchPart part;
38
39     /**
40      * Basic contstructor
41      */

42     public ViewerActionBuilder() {
43     }
44
45     /* (non-Javadoc)
46      * Method declared on PluginActionBuilder.
47      */

48     protected ActionDescriptor createActionDescriptor(
49             IConfigurationElement element) {
50         if (part instanceof IViewPart) {
51             return new ActionDescriptor(element, ActionDescriptor.T_VIEW, part);
52         }
53         return new ActionDescriptor(element, ActionDescriptor.T_EDITOR, part);
54     }
55
56     /* (non-Javadoc)
57      * Method declared on PluginActionBuilder.
58      */

59     protected BasicContribution createContribution() {
60         return new ViewerContribution(provider);
61     }
62
63     /**
64      * Dispose of the action builder
65      */

66     public void dispose() {
67         if (cache != null) {
68             for (int i = 0; i < cache.size(); i++) {
69                 ((BasicContribution) cache.get(i)).dispose();
70             }
71             cache = null;
72         }
73     }
74
75     /* (non-Javadoc)
76      * Method declared on PluginActionBuilder.
77      */

78     protected boolean readElement(IConfigurationElement element) {
79         String JavaDoc tag = element.getName();
80
81         // Found visibility sub-element
82
if (currentContribution != null && tag.equals(IWorkbenchRegistryConstants.TAG_VISIBILITY)) {
83             ((ViewerContribution) currentContribution)
84                     .setVisibilityTest(element);
85             return true;
86         }
87
88         return super.readElement(element);
89     }
90
91     /**
92      * Reads the contributions for a viewer menu.
93      * This method is typically used in conjunction with <code>contribute</code> to read
94      * and then insert actions for a particular viewer menu.
95      *
96      * @param id the menu id
97      * @param prov the selection provider for the control containing the menu
98      * @param part the part containing the menu.
99      * @return <code>true</code> if 1 or more items were read.
100      */

101     public boolean readViewerContributions(String JavaDoc id, ISelectionProvider prov,
102             IWorkbenchPart part) {
103         Assert.isTrue(part instanceof IViewPart || part instanceof IEditorPart);
104         provider = prov;
105         this.part = part;
106         readContributions(id, IWorkbenchRegistryConstants.TAG_VIEWER_CONTRIBUTION,
107                 IWorkbenchRegistryConstants.PL_POPUP_MENU);
108         return (cache != null);
109     }
110
111     /**
112      * Helper class to collect the menus and actions defined within a
113      * contribution element.
114      */

115     private static class ViewerContribution extends BasicContribution implements ISelectionChangedListener {
116         private ISelectionProvider selProvider;
117
118         private ActionExpression visibilityTest;
119
120         /**
121          * Create a new ViewerContribution.
122          *
123          * @param selProvider the selection provider
124          */

125         public ViewerContribution(ISelectionProvider selProvider) {
126             super();
127             this.selProvider = selProvider;
128             if (selProvider != null) {
129                 selProvider.addSelectionChangedListener(this);
130             }
131         }
132
133         /**
134          * Set the visibility test.
135          *
136          * @param element the element
137          */

138         public void setVisibilityTest(IConfigurationElement element) {
139             visibilityTest = new ActionExpression(element);
140         }
141
142         /* (non-Javadoc)
143          * Method declared on BasicContribution.
144          */

145         public void contribute(IMenuManager menu, boolean menuAppendIfMissing,
146                 IToolBarManager toolbar, boolean toolAppendIfMissing) {
147             boolean visible = true;
148
149             if (visibilityTest != null) {
150                 ISelection selection = selProvider.getSelection();
151                 if (selection instanceof IStructuredSelection) {
152                     visible = visibilityTest
153                             .isEnabledFor((IStructuredSelection) selection);
154                 } else {
155                     visible = visibilityTest.isEnabledFor(selection);
156                 }
157             }
158
159             if (visible) {
160                 super.contribute(menu, menuAppendIfMissing, toolbar,
161                         toolAppendIfMissing);
162             }
163         }
164         
165         /* (non-Javadoc)
166          * @see org.eclipse.ui.internal.PluginActionBuilder.BasicContribution#dispose()
167          */

168         public void dispose() {
169             if (selProvider != null) {
170                 selProvider.removeSelectionChangedListener(this);
171             }
172             disposeActions();
173             super.dispose();
174         }
175
176         /**
177          * Rather than hooking up each action as a selection listener,
178          * the contribution itself is added, and propagates
179          * the selection changed notification to all actions.
180          * This simplifies cleanup, in addition to potentially reducing the number of listeners.
181          *
182          * @see ISelectionChangedListener
183          * @since 3.1
184          */

185         public void selectionChanged(SelectionChangedEvent event) {
186             if (actions != null) {
187                 if (actions != null) {
188                     for (int i = 0; i < actions.size(); i++) {
189                         PluginAction proxy = ((ActionDescriptor) actions.get(i))
190                                 .getAction();
191                         proxy.selectionChanged(event);
192                     }
193                 }
194             }
195         }
196     }
197
198 }
199
Popular Tags