1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 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 30 public class ActionPresentation { 31 private WorkbenchWindow window; 32 33 private HashMap mapDescToRec = new HashMap (3); 34 35 private HashMap invisibleBars = new HashMap (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 55 public ActionPresentation(WorkbenchWindow window) { 56 super(); 57 this.window = window; 58 } 59 60 63 public void clearActionSets() { 64 final List oldList = new ArrayList (); 66 oldList.addAll(mapDescToRec.keySet()); 67 oldList.addAll(invisibleBars.keySet()); 68 69 Iterator iter = oldList.iterator(); 70 while (iter.hasNext()) { 71 IActionSetDescriptor desc = (IActionSetDescriptor) iter.next(); 72 removeActionSet(desc); 73 } 74 } 75 76 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 99 public void setActionSets(IActionSetDescriptor[] newArray) { 100 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 Iterator 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 ArrayList sets = new ArrayList (); 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 (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 Object [] existingRegistrations = window 160 .getExtensionTracker().getObjects( 161 desc.getConfigurationElement() 162 .getDeclaringExtension()); 163 if (existingRegistrations.length == 0 164 || !containsRegistration(existingRegistrations, 165 desc)) { 166 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); } 178 } 179 } 180 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 204 private boolean containsRegistration(Object [] 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 215 public IActionSet[] getActionSets() { 216 Collection setRecCollection = mapDescToRec.values(); 217 IActionSet result[] = new IActionSet[setRecCollection.size()]; 218 int i = 0; 219 for (Iterator iterator = setRecCollection.iterator(); iterator 220 .hasNext(); i++) { 221 result[i] = ((SetRec) iterator.next()).set; 222 } 223 return result; 224 } 225 } 226 | Popular Tags |