1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.jface.action.ContributionItem; 17 import org.eclipse.jface.action.IContributionItem; 18 import org.eclipse.jface.action.IContributionManager; 19 import org.eclipse.jface.action.ICoolBarManager; 20 import org.eclipse.jface.action.IMenuManager; 21 import org.eclipse.jface.action.IToolBarManager; 22 import org.eclipse.jface.action.SubMenuManager; 23 import org.eclipse.jface.action.SubToolBarManager; 24 import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem; 25 import org.eclipse.ui.IActionBars2; 26 import org.eclipse.ui.IWorkbenchActionConstants; 27 import org.eclipse.ui.SubActionBars2; 28 import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2; 29 import org.eclipse.ui.services.IServiceLocator; 30 31 34 public class ActionSetActionBars extends SubActionBars2 { 35 36 private IActionBarConfigurer2 actionBarConfigurer = null; 37 38 private String actionSetId; 39 40 private ArrayList adjunctContributions = new ArrayList (); 41 42 private IToolBarManager coolItemToolBarMgr = null; 43 44 private IToolBarContributionItem toolBarContributionItem = null; 45 46 49 public ActionSetActionBars(IActionBars2 parent, IServiceLocator serviceLocator, IActionBarConfigurer2 actionBarConfigurer, String actionSetId) { 50 super(parent, serviceLocator); 51 this.actionSetId = actionSetId; 52 this.actionBarConfigurer = actionBarConfigurer; 53 } 54 55 63 void addAdjunctContribution(IContributionItem item) { 64 adjunctContributions.add(item); 65 } 66 67 70 protected SubMenuManager createSubMenuManager(IMenuManager parent) { 71 return new ActionSetMenuManager(parent, actionSetId); 72 } 73 74 77 protected SubToolBarManager createSubToolBarManager(IToolBarManager parent) { 78 return null; 80 } 81 82 85 public void dispose() { 86 super.dispose(); 87 if (coolItemToolBarMgr == null) { 88 return; 89 } 90 IContributionItem[] items = coolItemToolBarMgr.getItems(); 91 for (int i = 0; i < items.length; i++) { 95 IContributionItem item = items[i]; 96 if (item instanceof PluginActionCoolBarContributionItem) { 97 PluginActionCoolBarContributionItem actionSetItem = (PluginActionCoolBarContributionItem) item; 98 if (actionSetItem.getActionSetId().equals(actionSetId)) { 99 coolItemToolBarMgr.remove(item); 100 item.dispose(); 101 } 102 } else { 103 } 107 } 108 109 for (int i = 0; i < adjunctContributions.size(); i++) { 111 ContributionItem item = (ContributionItem) adjunctContributions 112 .get(i); 113 IContributionManager parent = item.getParent(); 114 if (parent != null) { 115 parent.remove(item); 116 item.dispose(); 117 } 118 } 119 toolBarContributionItem = null; 120 coolItemToolBarMgr = null; 121 adjunctContributions = new ArrayList (); 122 } 123 124 139 private IContributionItem findAlphabeticalOrder(String startId, 140 String itemId, IContributionManager mgr) { 141 IContributionItem[] items = mgr.getItems(); 142 int insertIndex = 0; 143 144 while (insertIndex < items.length) { 146 IContributionItem item = items[insertIndex]; 147 if (item.getId() != null && item.getId().equals(startId)) { 148 break; 149 } 150 ++insertIndex; 151 } 152 153 for (int i = insertIndex + 1; i < items.length; i++) { 155 IContributionItem item = items[i]; 156 String testId = item.getId(); 157 158 if (item.isGroupMarker()) { 159 break; 160 } 161 162 if (itemId != null && testId != null) { 163 if (itemId.compareTo(testId) < 1) { 164 break; 165 } 166 } 167 insertIndex = i; 168 } 169 if (insertIndex >= items.length) { 171 return null; 172 } 173 return items[insertIndex]; 174 } 175 176 String getActionSetId() { 177 return actionSetId; 178 } 179 180 185 public IToolBarManager getToolBarManager() { 186 ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager(); 187 if (coolBarManager == null) { 188 return null; 189 } 190 return actionBarConfigurer.createToolBarManager(); 191 } 192 193 202 public IToolBarManager getToolBarManager(String actionId) { 203 String toolBarId = actionSetId; 205 boolean isAdjunctType = false; 206 if (!actionId.equals(actionSetId)) { 207 toolBarId = actionId; 209 isAdjunctType = true; 210 } 211 212 ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager(); 214 if (coolBarManager == null) { 215 return null; 216 } 217 218 if ((coolItemToolBarMgr != null) && (!isAdjunctType)) { 221 return coolItemToolBarMgr; 222 } 223 224 IContributionItem cbItem = coolBarManager.find(toolBarId); 226 if (cbItem instanceof IToolBarContributionItem) { 231 232 IToolBarContributionItem tbcbItem = (IToolBarContributionItem) cbItem; 233 coolItemToolBarMgr = tbcbItem.getToolBarManager(); 234 if (!isAdjunctType) { 237 toolBarContributionItem = tbcbItem; 238 } 239 } else { 240 241 coolItemToolBarMgr = actionBarConfigurer.createToolBarManager(); 242 243 248 IContributionItem toolBarContributionItem = actionBarConfigurer 249 .createToolBarContributionItem(coolItemToolBarMgr, 250 toolBarId); 251 252 toolBarContributionItem.setParent(coolItemToolBarMgr); 253 toolBarContributionItem.setVisible(getActive()); 254 coolItemToolBarMgr.markDirty(); 255 256 IContributionItem refItem = findAlphabeticalOrder( 258 IWorkbenchActionConstants.MB_ADDITIONS, toolBarId, 259 coolBarManager); 260 if (refItem != null) { 261 coolBarManager.insertAfter(refItem.getId(), 262 toolBarContributionItem); 263 } else { 264 coolBarManager.add(toolBarContributionItem); 265 } 266 } 267 return coolItemToolBarMgr; 268 } 269 270 void removeAdjunctContribution(ContributionItem item) { 272 adjunctContributions.remove(item); 273 } 274 275 278 protected void setActive(boolean set) { 279 super.setActive(set); 280 281 ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager(); 282 if (coolBarManager == null) { 283 return; 284 } 285 286 if (coolItemToolBarMgr != null) { 288 IContributionItem[] items = coolItemToolBarMgr.getItems(); 289 for (int i = 0; i < items.length; i++) { 290 IContributionItem item = items[i]; 291 if (item instanceof PluginActionCoolBarContributionItem) { 292 PluginActionCoolBarContributionItem actionSetItem = (PluginActionCoolBarContributionItem) item; 293 if (actionSetItem.getActionSetId().equals(actionSetId)) { 297 item.setVisible(set); 298 coolItemToolBarMgr.markDirty(); 299 if (!coolBarManager.isDirty()) { 300 coolBarManager.markDirty(); 301 } 302 } 303 } 304 } 305 coolItemToolBarMgr.update(false); 307 if (toolBarContributionItem != null) { 308 toolBarContributionItem.update(ICoolBarManager.SIZE); 309 } 310 } 311 312 if (adjunctContributions.size() > 0) { 314 for (Iterator i = adjunctContributions.iterator(); i.hasNext();) { 315 IContributionItem item = (IContributionItem) i.next(); 316 if (item instanceof ContributionItem) { 317 item.setVisible(set); 318 IContributionManager manager = ((ContributionItem) item) 319 .getParent(); 320 manager.markDirty(); 321 manager.update(false); 322 if (!coolBarManager.isDirty()) { 323 coolBarManager.markDirty(); 324 } 325 item.update(ICoolBarManager.SIZE); 326 } 327 328 } 329 330 } 331 } 332 333 } 334 | Popular Tags |