1 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 19 import org.eclipse.core.expressions.Expression; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.action.MenuManager; 24 import org.eclipse.jface.viewers.ISelectionProvider; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.ui.IActionBars; 28 import org.eclipse.ui.IKeyBindingService; 29 import org.eclipse.ui.IWorkbenchPage; 30 import org.eclipse.ui.IWorkbenchPart; 31 import org.eclipse.ui.IWorkbenchPartReference; 32 import org.eclipse.ui.IWorkbenchPartSite; 33 import org.eclipse.ui.IWorkbenchWindow; 34 import org.eclipse.ui.SubActionBars; 35 import org.eclipse.ui.commands.ICommandService; 36 import org.eclipse.ui.contexts.IContextService; 37 import org.eclipse.ui.handlers.IHandlerService; 38 import org.eclipse.ui.internal.commands.SlaveCommandService; 39 import org.eclipse.ui.internal.contexts.SlaveContextService; 40 import org.eclipse.ui.internal.expressions.ActivePartExpression; 41 import org.eclipse.ui.internal.handlers.SlaveHandlerService; 42 import org.eclipse.ui.internal.progress.WorkbenchSiteProgressService; 43 import org.eclipse.ui.internal.services.ServiceLocator; 44 import org.eclipse.ui.internal.testing.WorkbenchPartTestable; 45 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 46 import org.eclipse.ui.services.IServiceLocator; 47 import org.eclipse.ui.services.IServiceScopes; 48 import org.eclipse.ui.testing.IWorkbenchPartTestable; 49 50 70 public abstract class PartSite implements IWorkbenchPartSite { 71 72 94 public static final void registerContextMenu(final String menuId, 95 final MenuManager menuManager, 96 final ISelectionProvider selectionProvider, 97 final boolean includeEditorInput, final IWorkbenchPart part, 98 final Collection menuExtenders) { 99 104 final Iterator extenderItr = menuExtenders.iterator(); 105 boolean foundMatch = false; 106 while (extenderItr.hasNext()) { 107 final PopupMenuExtender existingExtender = (PopupMenuExtender) extenderItr 108 .next(); 109 if (existingExtender.matches(menuManager, selectionProvider, part)) { 110 existingExtender.addMenuId(menuId); 111 foundMatch = true; 112 break; 113 } 114 } 115 116 if (!foundMatch) { 117 menuExtenders.add(new PopupMenuExtender(menuId, menuManager, 118 selectionProvider, part, includeEditorInput)); 119 } 120 } 121 122 private IWorkbenchPartReference partReference; 123 124 private IWorkbenchPart part; 125 126 private IWorkbenchPage page; 127 128 private String extensionID; 129 130 private String pluginID; 131 132 private String extensionName; 133 134 private ISelectionProvider selectionProvider; 135 136 private SubActionBars actionBars; 137 138 private KeyBindingService keyBindingService; 139 140 protected ArrayList menuExtenders; 141 142 private WorkbenchSiteProgressService progressService; 143 144 protected final ServiceLocator serviceLocator; 145 146 156 public PartSite(IWorkbenchPartReference ref, IWorkbenchPart part, 157 IWorkbenchPage page) { 158 this.partReference = ref; 159 this.part = part; 160 this.page = page; 161 extensionID = "org.eclipse.ui.UnknownID"; extensionName = "Unknown Name"; 164 final IServiceLocator parentServiceLocator = page.getWorkbenchWindow(); 166 this.serviceLocator = new ServiceLocator(parentServiceLocator); 167 168 initializeDefaultServices(); 169 } 170 171 174 private void initializeDefaultServices() { 175 serviceLocator.registerService(IWorkbenchPartSite.class, this); 176 final IHandlerService parentService = (IHandlerService) serviceLocator 177 .getService(IHandlerService.class); 178 final Expression defaultExpression = new ActivePartExpression(part); 179 final IHandlerService slave = new SlaveHandlerService(parentService, 180 defaultExpression); 181 serviceLocator.registerService(IHandlerService.class, slave); 182 183 final IContextService parentContextService = (IContextService) serviceLocator 184 .getService(IContextService.class); 185 final IContextService contextService = new SlaveContextService( 186 parentContextService, defaultExpression); 187 serviceLocator.registerService(IContextService.class, contextService); 188 189 final ICommandService parentCommandService = (ICommandService) serviceLocator 190 .getService(ICommandService.class); 191 final ICommandService commandService = new SlaveCommandService( 192 parentCommandService, IServiceScopes.PARTSITE_SCOPE, 193 this); 194 serviceLocator.registerService(ICommandService.class, commandService); 195 } 196 197 200 public void dispose() { 201 if (menuExtenders != null) { 202 HashSet managers = new HashSet (menuExtenders.size()); 203 for (int i = 0; i < menuExtenders.size(); i++) { 204 PopupMenuExtender ext = (PopupMenuExtender) menuExtenders.get(i); 205 managers.add(ext.getManager()); 206 ext.dispose(); 207 } 208 if (managers.size()>0) { 209 for (Iterator iterator = managers.iterator(); iterator 210 .hasNext();) { 211 MenuManager mgr = (MenuManager) iterator.next(); 212 mgr.dispose(); 213 } 214 } 215 menuExtenders = null; 216 } 217 218 if (keyBindingService != null) { 219 keyBindingService.dispose(); 220 } 221 222 if (progressService != null) { 223 progressService.dispose(); 224 } 225 226 if (serviceLocator != null) { 227 serviceLocator.dispose(); 228 } 229 } 230 231 237 public IActionBars getActionBars() { 238 return actionBars; 239 } 240 241 246 public String getId() { 247 return extensionID; 248 } 249 250 255 public IWorkbenchPage getPage() { 256 return page; 257 } 258 259 262 public PartPane getPane() { 263 return ((WorkbenchPartReference) partReference).getPane(); 264 } 265 266 269 public IWorkbenchPart getPart() { 270 return part; 271 } 272 273 276 public IWorkbenchPartReference getPartReference() { 277 return partReference; 278 } 279 280 285 public String getPluginId() { 286 return pluginID; 287 } 288 289 292 public String getRegisteredName() { 293 return extensionName; 294 } 295 296 299 public ISelectionProvider getSelectionProvider() { 300 return selectionProvider; 301 } 302 303 308 public Shell getShell() { 309 PartPane pane = getPane(); 310 311 Display currentDisplay = Display.getCurrent(); 317 if (currentDisplay == null 318 || currentDisplay != getWorkbenchWindow().getWorkbench() 319 .getDisplay()) { 320 326 return getWorkbenchWindow().getShell(); 327 } 328 329 if (pane == null) { 330 return getWorkbenchWindow().getShell(); 331 } 332 333 Shell s = pane.getShell(); 334 335 if (s == null) { 336 return getWorkbenchWindow().getShell(); 337 } 338 339 return s; 340 } 341 342 347 public IWorkbenchWindow getWorkbenchWindow() { 348 return page.getWorkbenchWindow(); 349 } 350 351 354 public void registerContextMenu(String menuID, MenuManager menuMgr, 355 ISelectionProvider selProvider) { 356 if (menuExtenders == null) { 357 menuExtenders = new ArrayList (1); 358 } 359 360 registerContextMenu(menuID, menuMgr, selProvider, true, getPart(), 361 menuExtenders); 362 } 363 364 367 public void registerContextMenu(MenuManager menuMgr, 368 ISelectionProvider selProvider) { 369 registerContextMenu(getId(), menuMgr, selProvider); 370 } 371 372 376 public String [] getContextMenuIds() { 377 if (menuExtenders == null) { 378 return new String [0]; 379 } 380 ArrayList menuIds = new ArrayList (menuExtenders.size()); 381 for (Iterator iter = menuExtenders.iterator(); iter.hasNext();) { 382 final PopupMenuExtender extender = (PopupMenuExtender) iter.next(); 383 menuIds.addAll(extender.getMenuIds()); 384 } 385 return (String []) menuIds.toArray(new String [menuIds.size()]); 386 } 387 388 391 public void setActionBars(SubActionBars bars) { 392 actionBars = bars; 393 } 394 395 398 public void setConfigurationElement(IConfigurationElement configElement) { 399 400 extensionID = configElement.getAttribute("id"); 403 pluginID = configElement.getNamespace(); 405 406 String name = configElement.getAttribute("name"); if (name != null) { 409 extensionName = name; 410 } 411 } 412 413 protected void setPluginId(String pluginId) { 414 this.pluginID = pluginId; 415 } 416 417 423 protected void setId(String id) { 424 extensionID = id; 425 } 426 427 430 public void setPart(IWorkbenchPart newPart) { 431 part = newPart; 432 } 433 434 440 protected void setRegisteredName(String name) { 441 extensionName = name; 442 } 443 444 447 public void setSelectionProvider(ISelectionProvider provider) { 448 selectionProvider = provider; 449 } 450 451 454 public IKeyBindingService getKeyBindingService() { 455 if (keyBindingService == null) { 456 keyBindingService = new KeyBindingService(this); 457 458 if (this instanceof EditorSite) { 461 EditorActionBuilder.ExternalContributor contributor = (EditorActionBuilder.ExternalContributor) ((EditorSite) this) 462 .getExtensionActionBarContributor(); 463 464 if (contributor != null) { 465 ActionDescriptor[] actionDescriptors = contributor 466 .getExtendedActions(); 467 468 if (actionDescriptors != null) { 469 for (int i = 0; i < actionDescriptors.length; i++) { 470 ActionDescriptor actionDescriptor = actionDescriptors[i]; 471 472 if (actionDescriptor != null) { 473 IAction action = actionDescriptors[i] 474 .getAction(); 475 476 if (action != null 477 && action.getActionDefinitionId() != null) { 478 keyBindingService.registerAction(action); 479 } 480 } 481 } 482 } 483 } 484 } 485 } 486 487 return keyBindingService; 488 } 489 490 protected String getInitialScopeId() { 491 return null; 492 } 493 494 500 public final Object getAdapter(Class adapter) { 501 502 if (IWorkbenchSiteProgressService.class == adapter) { 503 return getSiteProgressService(); 504 } 505 506 if (IWorkbenchPartTestable.class == adapter) { 507 return new WorkbenchPartTestable(this); 508 } 509 510 return Platform.getAdapterManager().getAdapter(this, adapter); 511 } 512 513 public void activateActionBars(boolean forceVisibility) { 514 if (actionBars != null) { 515 actionBars.activate(forceVisibility); 516 } 517 } 518 519 public void deactivateActionBars(boolean forceHide) { 520 if (actionBars != null) { 521 actionBars.deactivate(forceHide); 522 } 523 } 524 525 530 private WorkbenchSiteProgressService getSiteProgressService() { 531 if (progressService == null) { 532 progressService = new WorkbenchSiteProgressService(this); 533 } 534 return progressService; 535 } 536 537 public final Object getService(final Class key) { 538 return serviceLocator.getService(key); 539 } 540 541 public final boolean hasService(final Class key) { 542 return serviceLocator.hasService(key); 543 } 544 545 551 public String toString() { 552 final StringBuffer buffer = new StringBuffer (); 553 buffer.append("PartSite(id="); buffer.append(getId()); 555 buffer.append(",pluginId="); buffer.append(getPluginId()); 557 buffer.append(",registeredName="); buffer.append(getRegisteredName()); 559 buffer.append(",hashCode="); buffer.append(hashCode()); 561 buffer.append(')'); 562 return buffer.toString(); 563 } 564 } 565 | Popular Tags |