1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.expressions.Expression; 14 import org.eclipse.jface.action.ActionContributionItem; 15 import org.eclipse.jface.action.ContributionManager; 16 import org.eclipse.jface.action.GroupMarker; 17 import org.eclipse.jface.action.IContributionItem; 18 import org.eclipse.jface.action.IContributionManager; 19 import org.eclipse.jface.action.IContributionManagerOverrides; 20 import org.eclipse.jface.action.ICoolBarManager; 21 import org.eclipse.jface.action.IMenuManager; 22 import org.eclipse.jface.action.IToolBarManager; 23 import org.eclipse.jface.action.SubContributionManager; 24 import org.eclipse.jface.action.SubMenuManager; 25 import org.eclipse.jface.action.SubStatusLineManager; 26 import org.eclipse.jface.action.SubToolBarManager; 27 import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem; 28 import org.eclipse.ui.IActionBars2; 29 import org.eclipse.ui.IEditorActionBarContributor; 30 import org.eclipse.ui.IEditorPart; 31 import org.eclipse.ui.IWorkbenchActionConstants; 32 import org.eclipse.ui.IWorkbenchPart; 33 import org.eclipse.ui.SubActionBars2; 34 import org.eclipse.ui.actions.RetargetAction; 35 import org.eclipse.ui.internal.expressions.LegacyEditorActionBarExpression; 36 import org.eclipse.ui.internal.misc.Policy; 37 import org.eclipse.ui.internal.provisional.presentations.IActionBarPresentationFactory; 38 import org.eclipse.ui.services.IServiceLocator; 39 40 43 public class EditorActionBars extends SubActionBars2 { 44 45 private class Overrides implements IContributionManagerOverrides { 46 47 public Integer getAccelerator(IContributionItem item) { 48 return null; 49 } 50 51 public String getAcceleratorText(IContributionItem item) { 52 return null; 53 } 54 55 public Boolean getEnabled(IContributionItem item) { 56 if (((item instanceof ActionContributionItem) && (((ActionContributionItem) item) 57 .getAction() instanceof RetargetAction)) 58 || enabledAllowed) { 59 return null; 60 } else { 61 return Boolean.FALSE; 62 } 63 } 64 65 public String getText(IContributionItem item) { 66 return null; 67 } 68 } 69 70 76 private static final boolean DEBUG_TOOLBAR_DISPOSAL = Policy.DEBUG_TOOLBAR_DISPOSAL; 77 78 private IToolBarManager coolItemToolBarMgr = null; 79 80 private IEditorActionBarContributor editorContributor; 81 82 private boolean enabledAllowed = false; 83 84 private IEditorActionBarContributor extensionContributor; 85 86 private int refCount; 87 88 private IToolBarContributionItem toolBarContributionItem = null; 89 90 private String type; 91 92 private IActionBarPresentationFactory actionBarPresentation; 93 94 95 98 public EditorActionBars(WorkbenchPage page, 99 final IServiceLocator serviceLocator, String type) { 100 super((IActionBars2)page.getActionBars(), serviceLocator); 101 this.type = type; 102 this.actionBarPresentation = ((WorkbenchWindow) page 103 .getWorkbenchWindow()).getActionBarPresentationFactory(); 104 } 105 106 109 public void activate(boolean forceVisibility) { 110 setActive(true, forceVisibility); 111 } 112 113 116 public void addRef() { 117 ++refCount; 118 } 119 120 123 protected SubMenuManager createSubMenuManager(IMenuManager parent) { 124 return new EditorMenuManager(parent); 125 } 126 127 130 protected SubToolBarManager createSubToolBarManager(IToolBarManager parent) { 131 return null; 133 } 134 135 138 public void deactivate(boolean forceVisibility) { 139 setActive(false, forceVisibility); 140 } 141 142 145 public void dispose() { 146 super.dispose(); 147 if (editorContributor != null) { 148 editorContributor.dispose(); 149 } 150 if (extensionContributor != null) { 151 extensionContributor.dispose(); 152 } 153 154 160 if (toolBarContributionItem != null) { 161 ICoolBarManager coolBarManager = getCoolBarManager(); 163 if (coolBarManager instanceof SubContributionManager) { 164 SubContributionManager subManager = (SubContributionManager) coolBarManager; 165 IContributionManager manager = subManager.getParent(); 166 if (manager instanceof ContributionManager) { 167 final IContributionItem replacementItem = new PlaceholderContributionItem( 168 toolBarContributionItem); 169 boolean succeeded = ((ContributionManager) manager) 170 .replaceItem(replacementItem.getId(), 171 replacementItem); 172 if (!succeeded && DEBUG_TOOLBAR_DISPOSAL) { 173 System.out 174 .println("FAILURE WHILE DISPOSING EditorActionBars"); System.out 176 .println("Could not replace " + replacementItem.getId() + " in the contribution manager."); } 178 } else if (DEBUG_TOOLBAR_DISPOSAL) { 179 System.out 180 .println("FAILURE WHILE DISPOSING EditorActionBars"); System.out 182 .println("The manager was not a ContributionManager."); System.out 184 .println("It was a " + manager.getClass().getName()); } 186 } else if (DEBUG_TOOLBAR_DISPOSAL) { 187 System.out.println("FAILURE WHILE DISPOSING EditorActionBars"); System.out 189 .println("The coolBarManager was not a SubContributionManager"); System.out 191 .println("It was a " + coolBarManager.getClass().getName()); } 193 194 toolBarContributionItem.dispose(); 196 } 197 toolBarContributionItem = null; 198 if (coolItemToolBarMgr != null) { 200 coolItemToolBarMgr.removeAll(); 201 } 202 coolItemToolBarMgr = null; 203 editorHandlerExpression = null; 204 } 205 206 209 public IEditorActionBarContributor getEditorContributor() { 210 return editorContributor; 211 } 212 213 216 public String getEditorType() { 217 return type; 218 } 219 220 223 public IEditorActionBarContributor getExtensionContributor() { 224 return extensionContributor; 225 } 226 227 230 public int getRef() { 231 return refCount; 232 } 233 234 241 public IToolBarManager getToolBarManager() { 242 243 ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager(); 245 if (coolBarManager == null) { 246 return null; 247 } 248 249 if (coolBarManager.find(IWorkbenchActionConstants.GROUP_EDITOR) == null) { 252 coolBarManager.add(new GroupMarker( 253 IWorkbenchActionConstants.GROUP_EDITOR)); 254 } 255 if (toolBarContributionItem == null) { 256 IContributionItem foundItem = coolBarManager.find(type); 257 if ((foundItem instanceof IToolBarContributionItem)) { 258 toolBarContributionItem = (IToolBarContributionItem) foundItem; 259 coolItemToolBarMgr = toolBarContributionItem.getToolBarManager(); 260 if (coolItemToolBarMgr == null) { 261 coolItemToolBarMgr = actionBarPresentation.createToolBarManager(); 262 toolBarContributionItem = actionBarPresentation.createToolBarContributionItem( 263 coolItemToolBarMgr, type); 264 coolBarManager.prependToGroup( 266 IWorkbenchActionConstants.GROUP_EDITOR, 267 toolBarContributionItem); 268 } 269 } else { 270 coolItemToolBarMgr = actionBarPresentation.createToolBarManager(); 271 if ((coolBarManager instanceof ContributionManager) 272 && (foundItem instanceof PlaceholderContributionItem)) { 273 PlaceholderContributionItem placeholder = (PlaceholderContributionItem) foundItem; 274 toolBarContributionItem = createToolBarContributionItem(coolItemToolBarMgr, placeholder); 275 ((ContributionManager) coolBarManager).replaceItem(type, 277 toolBarContributionItem); 278 } else { 279 toolBarContributionItem = actionBarPresentation.createToolBarContributionItem( 280 coolItemToolBarMgr, type); 281 coolBarManager.prependToGroup( 283 IWorkbenchActionConstants.GROUP_EDITOR, 284 toolBarContributionItem); 285 } 286 } 287 ((ContributionManager)coolItemToolBarMgr).setOverrides(new Overrides()); 288 toolBarContributionItem.setVisible(getActive()); 289 coolItemToolBarMgr.markDirty(); 290 } 291 292 return coolItemToolBarMgr; 293 } 294 295 299 IToolBarContributionItem createToolBarContributionItem( 300 final IToolBarManager manager, PlaceholderContributionItem item) { 301 IToolBarContributionItem toolBarContributionItem = actionBarPresentation 302 .createToolBarContributionItem(manager, item.getId()); 303 toolBarContributionItem.setCurrentHeight(item.getHeight()); 304 toolBarContributionItem.setCurrentWidth(item.getWidth()); 305 toolBarContributionItem.setMinimumItemsToShow(item.getMinimumItemsToShow()); 306 toolBarContributionItem.setUseChevron(item.getUseChevron()); 307 return toolBarContributionItem; 308 } 309 310 317 private boolean isVisible() { 318 if (toolBarContributionItem != null) { 319 return toolBarContributionItem.isVisible(); 320 } 321 return false; 322 } 323 324 330 public void partChanged(IWorkbenchPart part) { 331 super.partChanged(part); 332 if (part instanceof IEditorPart) { 333 IEditorPart editor = (IEditorPart) part; 334 if (editorContributor != null) { 335 editorContributor.setActiveEditor(editor); 336 } 337 if (extensionContributor != null) { 338 extensionContributor.setActiveEditor(editor); 339 } 340 } 341 } 342 343 346 public void removeRef() { 347 --refCount; 348 } 349 350 358 private void setActive(boolean set, boolean forceVisibility) { 359 basicSetActive(set); 360 if (isSubMenuManagerCreated()) { 361 ((EditorMenuManager) getMenuManager()).setVisible(set, 362 forceVisibility); 363 } 364 365 if (isSubStatusLineManagerCreated()) { 366 ((SubStatusLineManager) getStatusLineManager()).setVisible(set); 367 } 368 369 setVisible(set, forceVisibility); 370 } 371 372 375 public void setEditorContributor(IEditorActionBarContributor c) { 376 editorContributor = c; 377 } 378 379 386 private void setEnabledAllowed(boolean enabledAllowed) { 387 if (this.enabledAllowed == enabledAllowed) { 388 return; 389 } 390 this.enabledAllowed = enabledAllowed; 391 if (coolItemToolBarMgr != null) { 392 IContributionItem[] items = coolItemToolBarMgr.getItems(); 393 for (int i = 0; i < items.length; i++) { 394 IContributionItem item = items[i]; 395 item.update(IContributionManagerOverrides.P_ENABLED); 396 } 397 } 398 } 399 400 403 public void setExtensionContributor(IEditorActionBarContributor c) { 404 extensionContributor = c; 405 } 406 407 415 private void setVisible(boolean visible) { 416 if (toolBarContributionItem != null) { 417 toolBarContributionItem.setVisible(visible); 418 if (toolBarContributionItem.getParent() != null) { 419 toolBarContributionItem.getParent().markDirty(); 420 } 421 } 422 } 423 424 442 private void setVisible(boolean visible, boolean forceVisibility) { 443 if (visible) { 444 setEnabledAllowed(true); 445 if (!isVisible()) { 446 setVisible(true); 447 } 448 } else { 449 if (forceVisibility) { 450 setVisible(false); 452 } else { 453 setEnabledAllowed(false); 455 } 456 } 457 458 ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager(); 459 if ((coolItemToolBarMgr != null) && (coolBarManager != null)) { 460 IContributionItem[] items = coolItemToolBarMgr.getItems(); 461 for (int i = 0; i < items.length; i++) { 462 IContributionItem item = items[i]; 463 item.setVisible(visible || !forceVisibility); 464 coolItemToolBarMgr.markDirty(); 465 if (!coolBarManager.isDirty()) { 466 coolBarManager.markDirty(); 467 } 468 } 469 coolItemToolBarMgr.update(false); 471 if (toolBarContributionItem != null) { 472 toolBarContributionItem.setVisible(visible || !forceVisibility); 473 } 474 } 475 } 476 477 private LegacyEditorActionBarExpression editorHandlerExpression = null; 478 479 483 public Expression getHandlerExpression() { 484 if (editorHandlerExpression == null) { 485 editorHandlerExpression = new LegacyEditorActionBarExpression(type); 486 } 487 return editorHandlerExpression; 488 } 489 } 490 | Popular Tags |