KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
22 import org.eclipse.ui.SubActionBars;
23 import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2;
24 import org.eclipse.ui.internal.registry.IActionSet;
25 import org.eclipse.ui.internal.registry.IActionSetDescriptor;
26
27 /**
28  * Manage the configurable actions for one window.
29  */

30 public class ActionPresentation {
31     private WorkbenchWindow window;
32
33     private HashMap JavaDoc mapDescToRec = new HashMap JavaDoc(3);
34
35     private HashMap JavaDoc invisibleBars = new HashMap JavaDoc(3);
36
37     private class SetRec {
38         public SetRec(IActionSetDescriptor desc, IActionSet set,
39                 SubActionBars bars) {
40             this.desc = desc;
41             this.set = set;
42             this.bars = bars;
43         }
44
45         public IActionSetDescriptor desc;
46
47         public IActionSet set;
48
49         public SubActionBars bars;
50     }
51
52     /**
53      * ActionPresentation constructor comment.
54      */

55     public ActionPresentation(WorkbenchWindow window) {
56         super();
57         this.window = window;
58     }
59
60     /**
61      * Remove all action sets.
62      */

63     public void clearActionSets() {
64         // Get all of the action sets -- both visible and invisible.
65
final List JavaDoc oldList = new ArrayList JavaDoc();
66         oldList.addAll(mapDescToRec.keySet());
67         oldList.addAll(invisibleBars.keySet());
68
69         Iterator JavaDoc iter = oldList.iterator();
70         while (iter.hasNext()) {
71             IActionSetDescriptor desc = (IActionSetDescriptor) iter.next();
72             removeActionSet(desc);
73         }
74     }
75
76     /**
77      * Destroy an action set.
78      */

79     public void removeActionSet(IActionSetDescriptor desc) {
80         SetRec rec = (SetRec) mapDescToRec.remove(desc);
81         if (rec == null) {
82             rec = (SetRec) invisibleBars.remove(desc);
83         }
84         if (rec != null) {
85             IActionSet set = rec.set;
86             SubActionBars bars = rec.bars;
87             if (bars != null) {
88                 bars.dispose();
89             }
90             if (set != null) {
91                 set.dispose();
92             }
93         }
94     }
95
96     /**
97      * Sets the list of visible action set.
98      */

99     public void setActionSets(IActionSetDescriptor[] newArray) {
100         // Convert array to list.
101
HashSet JavaDoc newList = new HashSet JavaDoc();
102         
103         for (int i = 0; i < newArray.length; i++) {
104             IActionSetDescriptor descriptor = newArray[i];
105             
106             newList.add(descriptor);
107         }
108         List JavaDoc oldList = new ArrayList JavaDoc(mapDescToRec.keySet());
109
110         // Remove obsolete actions.
111
Iterator JavaDoc iter = oldList.iterator();
112         while (iter.hasNext()) {
113             IActionSetDescriptor desc = (IActionSetDescriptor) iter.next();
114             if (!newList.contains(desc)) {
115                 SetRec rec = (SetRec) mapDescToRec.get(desc);
116                 if (rec != null) {
117                     mapDescToRec.remove(desc);
118                     IActionSet set = rec.set;
119                     SubActionBars bars = rec.bars;
120                     if (bars != null) {
121                         SetRec invisibleRec = new SetRec(desc, set, bars);
122                         invisibleBars.put(desc, invisibleRec);
123                         bars.deactivate();
124                     }
125                 }
126             }
127         }
128
129         // Add new actions.
130
ArrayList JavaDoc sets = new ArrayList JavaDoc();
131         
132         for (int i = 0; i < newArray.length; i++) {
133             IActionSetDescriptor desc = newArray[i];
134
135             if (!mapDescToRec.containsKey(desc)) {
136                 try {
137                     SetRec rec;
138                     // If the action bars and sets have already been created
139
// then
140
// reuse those action sets
141
if (invisibleBars.containsKey(desc)) {
142                         rec = (SetRec) invisibleBars.get(desc);
143                         if (rec.bars != null) {
144                             rec.bars.activate();
145                         }
146                         invisibleBars.remove(desc);
147                     } else {
148                         IActionSet set = desc.createActionSet();
149                         SubActionBars bars = new ActionSetActionBars(window
150                                 .getActionBars(), window,
151                                 (IActionBarConfigurer2) window.getWindowConfigurer()
152                                         .getActionBarConfigurer(), desc.getId());
153                         rec = new SetRec(desc, set, bars);
154                         set.init(window, bars);
155                         sets.add(set);
156
157                         // only register against the tracker once - check for
158
// other registrations against the provided extension
159
Object JavaDoc[] existingRegistrations = window
160                                 .getExtensionTracker().getObjects(
161                                         desc.getConfigurationElement()
162                                                 .getDeclaringExtension());
163                         if (existingRegistrations.length == 0
164                                 || !containsRegistration(existingRegistrations,
165                                         desc)) {
166                             //register the set with the page tracker
167
//this will be cleaned up by WorkbenchWindow listener
168
window.getExtensionTracker().registerObject(
169                                     desc.getConfigurationElement().getDeclaringExtension(),
170                                     desc, IExtensionTracker.REF_WEAK);
171                         }
172                     }
173                     mapDescToRec.put(desc, rec);
174                 } catch (CoreException e) {
175                     WorkbenchPlugin
176                             .log("Unable to create ActionSet: " + desc.getId(), e);//$NON-NLS-1$
177
}
178             }
179         }
180         // We process action sets in two passes for coolbar purposes. First we
181
// process base contributions
182
// (i.e., actions that the action set contributes to its toolbar), then
183
// we process adjunct contributions
184
// (i.e., actions that the action set contributes to other toolbars).
185
// This type of processing is
186
// necessary in order to maintain group order within a coolitem.
187
PluginActionSetBuilder.processActionSets(sets, window);
188
189         iter = sets.iterator();
190         while (iter.hasNext()) {
191             PluginActionSet set = (PluginActionSet) iter.next();
192             set.getBars().activate();
193         }
194     }
195
196     /**
197      * Return whether the array contains the given action set.
198      *
199      * @param existingRegistrations the array to check
200      * @param set the set to look for
201      * @return whether the set is in the array
202      * @since 3.1
203      */

204     private boolean containsRegistration(Object JavaDoc[] existingRegistrations, IActionSetDescriptor set) {
205         for (int i = 0; i < existingRegistrations.length; i++) {
206             if (existingRegistrations[i] == set) {
207                 return true;
208             }
209         }
210         return false;
211     }
212
213     /**
214      */

215     public IActionSet[] getActionSets() {
216         Collection JavaDoc setRecCollection = mapDescToRec.values();
217         IActionSet result[] = new IActionSet[setRecCollection.size()];
218         int i = 0;
219         for (Iterator JavaDoc iterator = setRecCollection.iterator(); iterator
220                 .hasNext(); i++) {
221             result[i] = ((SetRec) iterator.next()).set;
222         }
223         return result;
224     }
225 }
226
Popular Tags