KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtension;
19 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.ui.IWorkbenchPart;
25
26 /**
27  * This manager is used to populate a popup menu manager with actions
28  * for a given type.
29  */

30 public class ObjectActionContributorManager extends ObjectContributorManager {
31     private static ObjectActionContributorManager sharedInstance;
32
33     /**
34      * PopupMenuManager constructor.
35      */

36     public ObjectActionContributorManager() {
37         super();
38         loadContributors();
39     }
40
41     /**
42      * Contributes submenus and/or actions applicable to the selection in the
43      * provided viewer into the provided popup menu.
44      *
45      * @param part the part being contributed to
46      * @param popupMenu the menu being contributed to
47      * @param selProv the selection provider
48      * @return whether anything was contributed
49      */

50     public boolean contributeObjectActions(IWorkbenchPart part,
51             IMenuManager popupMenu, ISelectionProvider selProv) {
52         // Get a selection.
53
ISelection selection = selProv.getSelection();
54         if (selection == null) {
55             return false;
56         }
57
58         // Convert the selection into an element vector.
59
// According to the dictionary, a selection is "one that
60
// is selected", or "a collection of selected things".
61
// In reflection of this, we deal with one or a collection.
62
List JavaDoc elements = null;
63         if (selection instanceof IStructuredSelection) {
64             elements = ((IStructuredSelection) selection).toList();
65         } else {
66             elements = new ArrayList JavaDoc(1);
67             elements.add(selection);
68         }
69
70         List JavaDoc contributors = getContributors(elements);
71        
72         if (contributors.isEmpty()) {
73             return false;
74         }
75
76         // First pass, add the menus and collect the overrides. Prune from the
77
// list any non-applicable contributions.
78
boolean actualContributions = false;
79         ArrayList JavaDoc overrides = new ArrayList JavaDoc(4);
80         for (Iterator JavaDoc it = contributors.iterator(); it.hasNext();) {
81             IObjectActionContributor contributor = (IObjectActionContributor) it.next();
82             if (!isApplicableTo(elements, contributor)) {
83                 it.remove();
84                 continue;
85             }
86             if (contributor.contributeObjectMenus(popupMenu, selProv)) {
87                 actualContributions = true;
88             }
89             contributor.contributeObjectActionIdOverrides(overrides);
90         }
91         
92         // Second pass, add the contributions that are applicable to
93
// the selection.
94
for (Iterator JavaDoc it = contributors.iterator(); it.hasNext();) {
95             IObjectActionContributor contributor = (IObjectActionContributor) it.next();
96             if (contributor.contributeObjectActions(part, popupMenu, selProv,
97                     overrides)) {
98                 actualContributions = true;
99             }
100         }
101         return actualContributions;
102     }
103
104     /**
105      * Returns the shared instance of this manager.
106      * @return the shared instance of this manager
107      */

108     public static ObjectActionContributorManager getManager() {
109         if (sharedInstance == null) {
110             sharedInstance = new ObjectActionContributorManager();
111         }
112         return sharedInstance;
113     }
114
115     /**
116      * Loads the contributors from the workbench's registry.
117      */

118     private void loadContributors() {
119         ObjectActionContributorReader reader = new ObjectActionContributorReader();
120         reader.readPopupContributors(this);
121     }
122     
123     /* (non-Javadoc)
124      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
125      */

126     public void addExtension(IExtensionTracker tracker, IExtension addedExtension) {
127         IConfigurationElement[] addedElements = addedExtension.getConfigurationElements();
128         for (int i = 0; i < addedElements.length; i++) {
129             ObjectActionContributorReader reader = new ObjectActionContributorReader();
130             reader.setManager(this);
131             reader.readElement(addedElements[i]);
132         }
133     }
134 }
135
Popular Tags