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 java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
021: import org.eclipse.ui.SubActionBars;
022: import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2;
023: import org.eclipse.ui.internal.registry.IActionSet;
024: import org.eclipse.ui.internal.registry.IActionSetDescriptor;
025:
026: /**
027: * Manage the configurable actions for one window.
028: */
029: public class ActionPresentation {
030: private WorkbenchWindow window;
031:
032: private HashMap mapDescToRec = new HashMap(3);
033:
034: private HashMap invisibleBars = new HashMap(3);
035:
036: private class SetRec {
037: public SetRec(IActionSetDescriptor desc, IActionSet set,
038: SubActionBars bars) {
039: this .desc = desc;
040: this .set = set;
041: this .bars = bars;
042: }
043:
044: public IActionSetDescriptor desc;
045:
046: public IActionSet set;
047:
048: public SubActionBars bars;
049: }
050:
051: /**
052: * ActionPresentation constructor comment.
053: */
054: public ActionPresentation(WorkbenchWindow window) {
055: super ();
056: this .window = window;
057: }
058:
059: /**
060: * Remove all action sets.
061: */
062: public void clearActionSets() {
063: // Get all of the action sets -- both visible and invisible.
064: final List oldList = new ArrayList();
065: oldList.addAll(mapDescToRec.keySet());
066: oldList.addAll(invisibleBars.keySet());
067:
068: Iterator iter = oldList.iterator();
069: while (iter.hasNext()) {
070: IActionSetDescriptor desc = (IActionSetDescriptor) iter
071: .next();
072: removeActionSet(desc);
073: }
074: }
075:
076: /**
077: * Destroy an action set.
078: */
079: public void removeActionSet(IActionSetDescriptor desc) {
080: SetRec rec = (SetRec) mapDescToRec.remove(desc);
081: if (rec == null) {
082: rec = (SetRec) invisibleBars.remove(desc);
083: }
084: if (rec != null) {
085: IActionSet set = rec.set;
086: SubActionBars bars = rec.bars;
087: if (bars != null) {
088: bars.dispose();
089: }
090: if (set != null) {
091: set.dispose();
092: }
093: }
094: }
095:
096: /**
097: * Sets the list of visible action set.
098: */
099: public void setActionSets(IActionSetDescriptor[] newArray) {
100: // Convert array to list.
101: HashSet newList = new HashSet();
102:
103: for (int i = 0; i < newArray.length; i++) {
104: IActionSetDescriptor descriptor = newArray[i];
105:
106: newList.add(descriptor);
107: }
108: List oldList = new ArrayList(mapDescToRec.keySet());
109:
110: // Remove obsolete actions.
111: Iterator iter = oldList.iterator();
112: while (iter.hasNext()) {
113: IActionSetDescriptor desc = (IActionSetDescriptor) iter
114: .next();
115: if (!newList.contains(desc)) {
116: SetRec rec = (SetRec) mapDescToRec.get(desc);
117: if (rec != null) {
118: mapDescToRec.remove(desc);
119: IActionSet set = rec.set;
120: SubActionBars bars = rec.bars;
121: if (bars != null) {
122: SetRec invisibleRec = new SetRec(desc, set,
123: bars);
124: invisibleBars.put(desc, invisibleRec);
125: bars.deactivate();
126: }
127: }
128: }
129: }
130:
131: // Add new actions.
132: ArrayList sets = new ArrayList();
133:
134: for (int i = 0; i < newArray.length; i++) {
135: IActionSetDescriptor desc = newArray[i];
136:
137: if (!mapDescToRec.containsKey(desc)) {
138: try {
139: SetRec rec;
140: // If the action bars and sets have already been created
141: // then
142: // reuse those action sets
143: if (invisibleBars.containsKey(desc)) {
144: rec = (SetRec) invisibleBars.get(desc);
145: if (rec.bars != null) {
146: rec.bars.activate();
147: }
148: invisibleBars.remove(desc);
149: } else {
150: IActionSet set = desc.createActionSet();
151: SubActionBars bars = new ActionSetActionBars(
152: window.getActionBars(), window,
153: (IActionBarConfigurer2) window
154: .getWindowConfigurer()
155: .getActionBarConfigurer(), desc
156: .getId());
157: rec = new SetRec(desc, set, bars);
158: set.init(window, bars);
159: sets.add(set);
160:
161: // only register against the tracker once - check for
162: // other registrations against the provided extension
163: Object[] existingRegistrations = window
164: .getExtensionTracker()
165: .getObjects(
166: desc
167: .getConfigurationElement()
168: .getDeclaringExtension());
169: if (existingRegistrations.length == 0
170: || !containsRegistration(
171: existingRegistrations, desc)) {
172: //register the set with the page tracker
173: //this will be cleaned up by WorkbenchWindow listener
174: window
175: .getExtensionTracker()
176: .registerObject(
177: desc
178: .getConfigurationElement()
179: .getDeclaringExtension(),
180: desc,
181: IExtensionTracker.REF_WEAK);
182: }
183: }
184: mapDescToRec.put(desc, rec);
185: } catch (CoreException e) {
186: WorkbenchPlugin
187: .log(
188: "Unable to create ActionSet: " + desc.getId(), e);//$NON-NLS-1$
189: }
190: }
191: }
192: // We process action sets in two passes for coolbar purposes. First we
193: // process base contributions
194: // (i.e., actions that the action set contributes to its toolbar), then
195: // we process adjunct contributions
196: // (i.e., actions that the action set contributes to other toolbars).
197: // This type of processing is
198: // necessary in order to maintain group order within a coolitem.
199: PluginActionSetBuilder.processActionSets(sets, window);
200:
201: iter = sets.iterator();
202: while (iter.hasNext()) {
203: PluginActionSet set = (PluginActionSet) iter.next();
204: set.getBars().activate();
205: }
206: }
207:
208: /**
209: * Return whether the array contains the given action set.
210: *
211: * @param existingRegistrations the array to check
212: * @param set the set to look for
213: * @return whether the set is in the array
214: * @since 3.1
215: */
216: private boolean containsRegistration(
217: Object[] existingRegistrations, IActionSetDescriptor set) {
218: for (int i = 0; i < existingRegistrations.length; i++) {
219: if (existingRegistrations[i] == set) {
220: return true;
221: }
222: }
223: return false;
224: }
225:
226: /**
227: */
228: public IActionSet[] getActionSets() {
229: Collection setRecCollection = mapDescToRec.values();
230: IActionSet result[] = new IActionSet[setRecCollection.size()];
231: int i = 0;
232: for (Iterator iterator = setRecCollection.iterator(); iterator
233: .hasNext(); i++) {
234: result[i] = ((SetRec) iterator.next()).set;
235: }
236: return result;
237: }
238: }
|