1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.IConfigurationElement; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 16 import org.eclipse.jface.action.ContributionManager; 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.layout.FillLayout; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.ui.IKeyBindingService; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.IViewPart; 26 import org.eclipse.ui.IViewReference; 27 import org.eclipse.ui.IWorkbenchPage; 28 import org.eclipse.ui.IWorkbenchPart; 29 import org.eclipse.ui.IWorkbenchPart2; 30 import org.eclipse.ui.IWorkbenchPart3; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.internal.misc.StatusUtil; 34 import org.eclipse.ui.internal.misc.UIStats; 35 import org.eclipse.ui.internal.registry.ViewDescriptor; 36 import org.eclipse.ui.internal.util.Util; 37 import org.eclipse.ui.menus.IMenuService; 38 import org.eclipse.ui.part.IWorkbenchPartOrientation; 39 import org.eclipse.ui.statushandlers.StatusManager; 40 import org.eclipse.ui.views.IViewDescriptor; 41 import org.eclipse.ui.views.IViewRegistry; 42 43 class ViewReference extends WorkbenchPartReference implements IViewReference { 44 45 48 private final ViewFactory factory; 49 50 String secondaryId; 51 52 private IMemento memento; 53 54 private ViewActionBuilder actionBuilder; 55 56 public ViewReference(ViewFactory factory, String id, String secondaryId, 57 IMemento memento) { 58 super(); 59 this.memento = memento; 60 this.factory = factory; 61 ViewDescriptor desc = (ViewDescriptor) this.factory.viewReg.find(id); 62 ImageDescriptor iDesc = null; 63 String title = null; 64 if (desc != null) { 65 iDesc = desc.getImageDescriptor(); 66 title = desc.getLabel(); 67 } 68 69 String name = null; 70 71 if (memento != null) { 72 name = memento.getString(IWorkbenchConstants.TAG_PART_NAME); 73 IMemento propBag = memento.getChild(IWorkbenchConstants.TAG_PROPERTIES); 74 if (propBag != null) { 75 IMemento[] props = propBag 76 .getChildren(IWorkbenchConstants.TAG_PROPERTY); 77 for (int i = 0; i < props.length; i++) { 78 propertyCache.put(props[i].getID(), props[i].getTextData()); 79 } 80 } 81 } 82 if (name == null) { 83 name = title; 84 } 85 86 init(id, title, "", iDesc, name, ""); this.secondaryId = secondaryId; 88 } 89 90 protected PartPane createPane() { 91 return new ViewPane(this, this.factory.page); 92 } 93 94 99 protected void doDisposePart() { 100 IViewPart view = (IViewPart) part; 101 if (view != null) { 102 PartSite site = (PartSite) view.getSite(); 104 ViewActionBars actionBars = (ViewActionBars) site.getActionBars(); 105 IMenuService menuService = (IMenuService) site 109 .getService(IMenuService.class); 110 menuService.releaseContributions((ContributionManager) site.getActionBars() 111 .getMenuManager()); 112 menuService.releaseContributions((ContributionManager) site.getActionBars() 113 .getToolBarManager()); 114 actionBars.dispose(); 116 117 actionBuilder.dispose(); 121 actionBuilder = null; 122 123 site.dispose(); 125 } 126 127 super.doDisposePart(); 128 } 129 130 135 public IWorkbenchPage getPage() { 136 return this.factory.page; 137 } 138 139 144 public String getRegisteredName() { 145 if (part != null && part.getSite() != null) { 146 return part.getSite().getRegisteredName(); 147 } 148 149 IViewRegistry reg = this.factory.viewReg; 150 IViewDescriptor desc = reg.find(getId()); 151 if (desc != null) { 152 return desc.getLabel(); 153 } 154 return getTitle(); 155 } 156 157 protected String computePartName() { 158 if (part instanceof IWorkbenchPart2) { 159 return super.computePartName(); 160 } else { 161 return getRegisteredName(); 162 } 163 } 164 165 protected String computeContentDescription() { 166 if (part instanceof IWorkbenchPart2) { 167 return super.computeContentDescription(); 168 } else { 169 String rawTitle = getRawTitle(); 170 171 if (!Util.equals(rawTitle, getRegisteredName())) { 172 return rawTitle; 173 } 174 175 return ""; } 177 } 178 179 184 public String getSecondaryId() { 185 return secondaryId; 186 } 187 188 193 public IViewPart getView(boolean restore) { 194 return (IViewPart) getPart(restore); 195 } 196 197 202 public boolean isFastView() { 203 return this.factory.page.isFastView(this); 204 } 205 206 216 protected IWorkbenchPart createPart() { 217 218 220 IWorkbenchPart result = null; 221 PartInitException exception = null; 222 223 try { 227 result = createPartHelper(); 228 } catch (PartInitException e) { 229 exception = e; 230 } 231 232 if (exception != null) { 235 IStatus partStatus = exception.getStatus(); 236 IStatus displayStatus = StatusUtil.newStatus(partStatus, 237 WorkbenchMessages.ViewFactory_initException); 238 IStatus logStatus = StatusUtil 239 .newStatus( 240 partStatus, 241 NLS 242 .bind( 243 "Unable to create view ID {0}: {1}", getId(), partStatus.getMessage())); 245 StatusManager.getManager().handle(logStatus); 247 StatusManager.getManager() 248 .handle(displayStatus, StatusManager.SHOW); 249 250 IViewDescriptor desc = factory.viewReg.find(getId()); 251 String label = getId(); 252 if (desc != null) { 253 label = desc.getLabel(); 254 } 255 256 ErrorViewPart part = new ErrorViewPart(displayStatus); 257 258 PartPane pane = getPane(); 259 ViewSite site = new ViewSite(this, part, factory.page, getId(), 260 PlatformUI.PLUGIN_ID, label); 261 site.setActionBars(new ViewActionBars(factory.page.getActionBars(), 262 site, (ViewPane) pane)); 263 try { 264 part.init(site); 265 } catch (PartInitException e) { 266 StatusUtil.handleStatus(e, StatusManager.SHOW 267 | StatusManager.LOG); 268 return null; 269 } 270 part.setPartName(label); 271 272 Composite parent = (Composite) pane.getControl(); 273 Composite content = new Composite(parent, SWT.NONE); 274 content.setLayout(new FillLayout()); 275 276 try { 277 part.createPartControl(content); 278 } catch (Exception e) { 279 content.dispose(); 280 StatusUtil.handleStatus(e, StatusManager.SHOW 281 | StatusManager.LOG); 282 return null; 283 } 284 285 result = part; 286 } 287 288 return result; 289 } 290 291 private IWorkbenchPart createPartHelper() throws PartInitException { 292 293 IWorkbenchPart result = null; 294 295 IMemento stateMem = null; 296 if (memento != null) { 297 stateMem = memento.getChild(IWorkbenchConstants.TAG_VIEW_STATE); 298 } 299 300 IViewDescriptor desc = factory.viewReg.find(getId()); 301 if (desc == null) { 302 throw new PartInitException( 303 WorkbenchMessages.ViewFactory_couldNotCreate); 304 } 305 306 PartPane pane = getPane(); 308 309 pane.createControl(factory.page.getClientComposite()); 311 312 String label = desc.getLabel(); 314 Composite content = null; 318 IViewPart initializedView = null; 319 ViewSite site = null; 320 ViewActionBars actionBars = null; 321 323 try { 324 IViewPart view = null; 325 try { 326 UIStats.start(UIStats.CREATE_PART, label); 327 view = desc.createView(); 328 } finally { 329 UIStats.end(UIStats.CREATE_PART, view, label); 330 } 331 332 if (view instanceof IWorkbenchPart3) { 333 createPartProperties((IWorkbenchPart3)view); 334 } 335 site = new ViewSite(this, view, factory.page, desc); 337 actionBars = new ViewActionBars(factory.page.getActionBars(), site, 338 (ViewPane) pane); 339 site.setActionBars(actionBars); 340 341 try { 342 UIStats.start(UIStats.INIT_PART, label); 343 view.init(site, stateMem); 344 initializedView = view; 348 349 } finally { 350 UIStats.end(UIStats.INIT_PART, view, label); 351 } 352 353 if (view.getSite() != site) { 354 throw new PartInitException( 355 WorkbenchMessages.ViewFactory_siteException, null); 356 } 357 int style = SWT.NONE; 358 if (view instanceof IWorkbenchPartOrientation) { 359 style = ((IWorkbenchPartOrientation) view).getOrientation(); 360 } 361 362 { 364 Composite parent = (Composite) pane.getControl(); 365 content = new Composite(parent, style); 366 content.setLayout(new FillLayout()); 367 368 try { 369 UIStats.start(UIStats.CREATE_PART_CONTROL, label); 370 view.createPartControl(content); 371 372 parent.layout(true); 373 } finally { 374 UIStats.end(UIStats.CREATE_PART_CONTROL, view, label); 375 } 376 } 377 378 { 380 IMenuService menuService = (IMenuService) site 384 .getService(IMenuService.class); 385 menuService.populateContributionManager( 386 (ContributionManager) site.getActionBars() 387 .getMenuManager(), "menu:" + site.getId()); 389 menuService 390 .populateContributionManager((ContributionManager) site 391 .getActionBars().getToolBarManager(), 392 "toolbar:" + site.getId()); 395 actionBuilder = new ViewActionBuilder(); 396 actionBuilder.readActionExtensions(view); 397 ActionDescriptor[] actionDescriptors = actionBuilder 398 .getExtendedActions(); 399 IKeyBindingService keyBindingService = view.getSite() 400 .getKeyBindingService(); 401 402 if (actionDescriptors != null) { 403 for (int i = 0; i < actionDescriptors.length; i++) { 404 ActionDescriptor actionDescriptor = actionDescriptors[i]; 405 406 if (actionDescriptor != null) { 407 IAction action = actionDescriptors[i].getAction(); 408 409 if (action != null 410 && action.getActionDefinitionId() != null) { 411 keyBindingService.registerAction(action); 412 } 413 } 414 } 415 } 416 417 site.getActionBars().updateActionBars(); 418 } 419 420 PartTester.testView(view); 427 428 result = view; 429 430 IConfigurationElement element = (IConfigurationElement) Util.getAdapter(desc, 431 IConfigurationElement.class); 432 if (element != null) { 433 factory.page.getExtensionTracker().registerObject( 434 element.getDeclaringExtension(), view, 435 IExtensionTracker.REF_WEAK); 436 } 437 } catch (Throwable e) { 438 if ((e instanceof Error ) && !(e instanceof LinkageError )) { 439 throw (Error ) e; 440 } 441 442 if (content != null) { 447 try { 448 content.dispose(); 449 } catch (RuntimeException re) { 450 StatusManager.getManager().handle( 451 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 452 re)); 453 } 454 } 455 456 if (initializedView != null) { 457 try { 458 initializedView.dispose(); 459 } catch (RuntimeException re) { 460 StatusManager.getManager().handle( 461 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 462 re)); 463 } 464 } 465 466 if (site != null) { 467 try { 468 site.dispose(); 469 } catch (RuntimeException re) { 470 StatusManager.getManager().handle( 471 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 472 re)); 473 } 474 } 475 476 if (actionBars != null) { 477 try { 478 actionBars.dispose(); 479 } catch (RuntimeException re) { 480 StatusManager.getManager().handle( 481 StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, 482 re)); 483 } 484 } 485 486 throw new PartInitException(WorkbenchPlugin.getStatus(e)); 487 } 488 489 return result; 490 } 491 492 499 public IMemento getMemento() { 500 return memento; 501 } 502 } 503 | Popular Tags |