1 11 package org.eclipse.ui.part; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.expressions.Expression; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.jface.action.MenuManager; 18 import org.eclipse.jface.viewers.ILabelDecorator; 19 import org.eclipse.jface.viewers.IPostSelectionProvider; 20 import org.eclipse.jface.viewers.ISelectionChangedListener; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 import org.eclipse.jface.viewers.SelectionChangedEvent; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.ui.IActionBars; 25 import org.eclipse.ui.IEditorActionBarContributor; 26 import org.eclipse.ui.IEditorPart; 27 import org.eclipse.ui.IEditorSite; 28 import org.eclipse.ui.IKeyBindingService; 29 import org.eclipse.ui.INestableKeyBindingService; 30 import org.eclipse.ui.IWorkbenchPage; 31 import org.eclipse.ui.IWorkbenchPart; 32 import org.eclipse.ui.IWorkbenchWindow; 33 import org.eclipse.ui.commands.ICommandService; 34 import org.eclipse.ui.contexts.IContextService; 35 import org.eclipse.ui.handlers.IHandlerService; 36 import org.eclipse.ui.internal.PartSite; 37 import org.eclipse.ui.internal.PopupMenuExtender; 38 import org.eclipse.ui.internal.WorkbenchPlugin; 39 import org.eclipse.ui.internal.commands.SlaveCommandService; 40 import org.eclipse.ui.internal.contexts.NestableContextService; 41 import org.eclipse.ui.internal.expressions.ActivePartExpression; 42 import org.eclipse.ui.internal.handlers.NestableHandlerService; 43 import org.eclipse.ui.internal.services.INestable; 44 import org.eclipse.ui.internal.services.ServiceLocator; 45 import org.eclipse.ui.services.IServiceLocator; 46 import org.eclipse.ui.services.IServiceScopes; 47 48 57 public class MultiPageEditorSite implements IEditorSite, INestable { 58 59 62 private IEditorPart editor; 63 64 67 private ArrayList menuExtenders; 68 69 72 private MultiPageEditorPart multiPageEditor; 73 74 77 private ISelectionChangedListener postSelectionChangedListener = null; 78 79 83 private ISelectionChangedListener selectionChangedListener = null; 84 85 90 private ISelectionProvider selectionProvider = null; 91 92 97 private IKeyBindingService service = null; 98 99 103 private final ServiceLocator serviceLocator; 104 105 114 public MultiPageEditorSite(MultiPageEditorPart multiPageEditor, 115 IEditorPart editor) { 116 Assert.isNotNull(multiPageEditor); 117 Assert.isNotNull(editor); 118 this.multiPageEditor = multiPageEditor; 119 this.editor = editor; 120 121 final IServiceLocator parentServiceLocator = multiPageEditor.getSite(); 122 serviceLocator = new ServiceLocator(parentServiceLocator); 123 124 initializeDefaultServices(); 125 } 126 127 130 private void initializeDefaultServices() { 131 final Expression defaultExpression = new ActivePartExpression( 132 multiPageEditor); 133 134 final IHandlerService parentService = (IHandlerService) serviceLocator 135 .getService(IHandlerService.class); 136 final IHandlerService slave = new NestableHandlerService(parentService, 137 defaultExpression); 138 serviceLocator.registerService(IHandlerService.class, slave); 139 140 final IContextService parentContext = (IContextService) serviceLocator 141 .getService(IContextService.class); 142 final IContextService context = new NestableContextService( 143 parentContext, defaultExpression); 144 serviceLocator.registerService(IContextService.class, context); 145 146 final ICommandService parentCommandService = (ICommandService) serviceLocator 147 .getService(ICommandService.class); 148 final ICommandService commandService = new SlaveCommandService( 149 parentCommandService, IServiceScopes.MPESITE_SCOPE, 150 this); 151 serviceLocator.registerService(ICommandService.class, commandService); 152 153 } 154 155 161 public final void activate() { 162 serviceLocator.activate(); 163 } 164 165 171 public final void deactivate() { 172 serviceLocator.deactivate(); 173 } 174 175 178 public void dispose() { 179 if (menuExtenders != null) { 180 for (int i = 0; i < menuExtenders.size(); i++) { 181 ((PopupMenuExtender) menuExtenders.get(i)).dispose(); 182 } 183 menuExtenders = null; 184 } 185 186 if (service != null) { 188 IKeyBindingService parentService = getEditor().getSite() 189 .getKeyBindingService(); 190 if (parentService instanceof INestableKeyBindingService) { 191 INestableKeyBindingService nestableParent = (INestableKeyBindingService) parentService; 192 nestableParent.removeKeyBindingService(this); 193 } 194 service = null; 195 } 196 197 if (serviceLocator != null) { 198 serviceLocator.dispose(); 199 } 200 } 201 202 209 public IEditorActionBarContributor getActionBarContributor() { 210 return null; 211 } 212 213 220 public IActionBars getActionBars() { 221 return multiPageEditor.getEditorSite().getActionBars(); 222 } 223 224 229 public Object getAdapter(Class adapter) { 230 return null; 231 } 232 233 241 public ILabelDecorator getDecoratorManager() { 242 return getWorkbenchWindow().getWorkbench().getDecoratorManager() 243 .getLabelDecorator(); 244 } 245 246 251 public IEditorPart getEditor() { 252 return editor; 253 } 254 255 262 public String getId() { 263 return ""; } 265 266 269 public IKeyBindingService getKeyBindingService() { 270 if (service == null) { 271 service = getMultiPageEditor().getEditorSite() 272 .getKeyBindingService(); 273 if (service instanceof INestableKeyBindingService) { 274 INestableKeyBindingService nestableService = (INestableKeyBindingService) service; 275 service = nestableService.getKeyBindingService(this); 276 277 } else { 278 283 WorkbenchPlugin 284 .log("MultiPageEditorSite.getKeyBindingService() Parent key binding service was not an instance of INestableKeyBindingService. It was an instance of " + service.getClass().getName() + " instead."); } 286 } 287 return service; 288 } 289 290 295 public MultiPageEditorPart getMultiPageEditor() { 296 return multiPageEditor; 297 } 298 299 306 public IWorkbenchPage getPage() { 307 return getMultiPageEditor().getSite().getPage(); 308 } 309 310 315 public IWorkbenchPart getPart() { 316 return editor; 317 } 318 319 326 public String getPluginId() { 327 return ""; } 329 330 336 private ISelectionChangedListener getPostSelectionChangedListener() { 337 if (postSelectionChangedListener == null) { 338 postSelectionChangedListener = new ISelectionChangedListener() { 339 public void selectionChanged(SelectionChangedEvent event) { 340 MultiPageEditorSite.this.handlePostSelectionChanged(event); 341 } 342 }; 343 } 344 return postSelectionChangedListener; 345 } 346 347 354 public String getRegisteredName() { 355 return ""; } 357 358 364 private ISelectionChangedListener getSelectionChangedListener() { 365 if (selectionChangedListener == null) { 366 selectionChangedListener = new ISelectionChangedListener() { 367 public void selectionChanged(SelectionChangedEvent event) { 368 MultiPageEditorSite.this.handleSelectionChanged(event); 369 } 370 }; 371 } 372 return selectionChangedListener; 373 } 374 375 382 public ISelectionProvider getSelectionProvider() { 383 return selectionProvider; 384 } 385 386 public final Object getService(final Class key) { 387 return serviceLocator.getService(key); 388 } 389 390 397 public Shell getShell() { 398 return getMultiPageEditor().getSite().getShell(); 399 } 400 401 408 public IWorkbenchWindow getWorkbenchWindow() { 409 return getMultiPageEditor().getSite().getWorkbenchWindow(); 410 } 411 412 421 protected void handlePostSelectionChanged(SelectionChangedEvent event) { 422 ISelectionProvider parentProvider = getMultiPageEditor().getSite() 423 .getSelectionProvider(); 424 if (parentProvider instanceof MultiPageSelectionProvider) { 425 SelectionChangedEvent newEvent = new SelectionChangedEvent( 426 parentProvider, event.getSelection()); 427 MultiPageSelectionProvider prov = (MultiPageSelectionProvider) parentProvider; 428 prov.firePostSelectionChanged(newEvent); 429 } 430 } 431 432 445 protected void handleSelectionChanged(SelectionChangedEvent event) { 446 ISelectionProvider parentProvider = getMultiPageEditor().getSite() 447 .getSelectionProvider(); 448 if (parentProvider instanceof MultiPageSelectionProvider) { 449 SelectionChangedEvent newEvent = new SelectionChangedEvent( 450 parentProvider, event.getSelection()); 451 MultiPageSelectionProvider prov = (MultiPageSelectionProvider) parentProvider; 452 prov.fireSelectionChanged(newEvent); 453 } 454 } 455 456 public final boolean hasService(final Class key) { 457 return serviceLocator.hasService(key); 458 } 459 460 470 public void registerContextMenu(MenuManager menuManager, 471 ISelectionProvider selProvider) { 472 getMultiPageEditor().getSite().registerContextMenu(menuManager, 473 selProvider); 474 } 475 476 public final void registerContextMenu(final MenuManager menuManager, 477 final ISelectionProvider selectionProvider, 478 final boolean includeEditorInput) { 479 registerContextMenu(getId(), menuManager, selectionProvider, 480 includeEditorInput); 481 } 482 483 495 public void registerContextMenu(String menuID, MenuManager menuMgr, 496 ISelectionProvider selProvider) { 497 if (menuExtenders == null) { 498 menuExtenders = new ArrayList (1); 499 } 500 PartSite.registerContextMenu(menuID, menuMgr, selProvider, true, 501 editor, menuExtenders); 502 } 503 504 public final void registerContextMenu(final String menuId, 505 final MenuManager menuManager, 506 final ISelectionProvider selectionProvider, 507 final boolean includeEditorInput) { 508 if (menuExtenders == null) { 509 menuExtenders = new ArrayList (1); 510 } 511 PartSite.registerContextMenu(menuId, menuManager, selectionProvider, 512 includeEditorInput, editor, menuExtenders); 513 } 514 515 526 public void setSelectionProvider(ISelectionProvider provider) { 527 ISelectionProvider oldSelectionProvider = selectionProvider; 528 selectionProvider = provider; 529 if (oldSelectionProvider != null) { 530 oldSelectionProvider 531 .removeSelectionChangedListener(getSelectionChangedListener()); 532 if (oldSelectionProvider instanceof IPostSelectionProvider) { 533 ((IPostSelectionProvider) oldSelectionProvider) 534 .removePostSelectionChangedListener(getPostSelectionChangedListener()); 535 } 536 } 537 if (selectionProvider != null) { 538 selectionProvider 539 .addSelectionChangedListener(getSelectionChangedListener()); 540 if (selectionProvider instanceof IPostSelectionProvider) { 541 ((IPostSelectionProvider) selectionProvider) 542 .addPostSelectionChangedListener(getPostSelectionChangedListener()); 543 } 544 } 545 } 546 } 547 | Popular Tags |