KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ViewReference


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     /**
46      *
47      */

48     private final ViewFactory factory;
49
50     String JavaDoc secondaryId;
51
52     private IMemento memento;
53
54     private ViewActionBuilder actionBuilder;
55
56     public ViewReference(ViewFactory factory, String JavaDoc id, String JavaDoc 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 JavaDoc title = null;
64         if (desc != null) {
65             iDesc = desc.getImageDescriptor();
66             title = desc.getLabel();
67         }
68
69         String JavaDoc 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, ""); //$NON-NLS-1$//$NON-NLS-2$
87
this.secondaryId = secondaryId;
88     }
89
90     protected PartPane createPane() {
91         return new ViewPane(this, this.factory.page);
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.eclipse.ui.internal.WorkbenchPartReference#dispose()
98      */

99     protected void doDisposePart() {
100         IViewPart view = (IViewPart) part;
101         if (view != null) {
102             // Free action bars, pane, etc.
103
PartSite site = (PartSite) view.getSite();
104             ViewActionBars actionBars = (ViewActionBars) site.getActionBars();
105             //
106
// 3.3 start
107
//
108
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             // 3.3 end
115
actionBars.dispose();
116             
117             // and now dispose the delegates since the
118
// PluginActionContributionItem
119
// can no longer do that
120
actionBuilder.dispose();
121             actionBuilder = null;
122
123             // Free the site.
124
site.dispose();
125         }
126
127         super.doDisposePart();
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.eclipse.ui.IWorkbenchPartReference#getPage()
134      */

135     public IWorkbenchPage getPage() {
136         return this.factory.page;
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.eclipse.ui.internal.WorkbenchPartReference#getRegisteredName()
143      */

144     public String JavaDoc 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 JavaDoc computePartName() {
158         if (part instanceof IWorkbenchPart2) {
159             return super.computePartName();
160         } else {
161             return getRegisteredName();
162         }
163     }
164
165     protected String JavaDoc computeContentDescription() {
166         if (part instanceof IWorkbenchPart2) {
167             return super.computeContentDescription();
168         } else {
169             String JavaDoc rawTitle = getRawTitle();
170
171             if (!Util.equals(rawTitle, getRegisteredName())) {
172                 return rawTitle;
173             }
174
175             return ""; //$NON-NLS-1$
176
}
177     }
178
179     /*
180      * (non-Javadoc)
181      *
182      * @see org.eclipse.ui.IViewReference
183      */

184     public String JavaDoc getSecondaryId() {
185         return secondaryId;
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see org.eclipse.ui.IViewReference#getView(boolean)
192      */

193     public IViewPart getView(boolean restore) {
194         return (IViewPart) getPart(restore);
195     }
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see org.eclipse.ui.IViewReference#isFastView()
201      */

202     public boolean isFastView() {
203         return this.factory.page.isFastView(this);
204     }
205
206     /**
207      * Wrapper for restoring the view. First, this delegates to
208      * busyRestoreViewHelper to do the real work of restoring the view. If
209      * unable to restore the view, this method tries to substitute an error part
210      * and return success.
211      *
212      * @param factory
213      * TODO
214      * @return
215      */

216     protected IWorkbenchPart createPart() {
217
218         // Check the status of this part
219

220         IWorkbenchPart result = null;
221         PartInitException exception = null;
222
223         // Try to restore the view -- this does the real work of restoring the
224
// view
225
//
226
try {
227             result = createPartHelper();
228         } catch (PartInitException e) {
229             exception = e;
230         }
231
232         // If unable to create the part, create an error part instead
233
// and pass the error to the status handling facility
234
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())); //$NON-NLS-1$
244

245             // Pass the error to the status handling facility
246
StatusManager.getManager().handle(logStatus);
247             StatusManager.getManager()
248                     .handle(displayStatus, StatusManager.SHOW);
249
250             IViewDescriptor desc = factory.viewReg.find(getId());
251             String JavaDoc 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 JavaDoc 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         // Create the part pane
307
PartPane pane = getPane();
308
309         // Create the pane's top-level control
310
pane.createControl(factory.page.getClientComposite());
311
312         String JavaDoc label = desc.getLabel(); // debugging only
313

314         // Things that will need to be disposed if an exception occurs (they are
315
// listed here
316
// in the order they should be disposed)
317
Composite content = null;
318         IViewPart initializedView = null;
319         ViewSite site = null;
320         ViewActionBars actionBars = null;
321         // End of things that need to be explicitly disposed from the try block
322

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             // Create site
336
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                 // Once we've called init, we MUST dispose the view. Remember
345
// the fact that
346
// we've initialized the view in case an exception is thrown.
347
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             // Create the top-level composite
363
{
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             // Install the part's tools and menu
379
{
380                 //
381
// 3.3 start
382
//
383
IMenuService menuService = (IMenuService) site
384                         .getService(IMenuService.class);
385                 menuService.populateContributionManager(
386                         (ContributionManager) site.getActionBars()
387                                 .getMenuManager(), "menu:" //$NON-NLS-1$
388
+ site.getId());
389                 menuService
390                         .populateContributionManager((ContributionManager) site
391                                 .getActionBars().getToolBarManager(),
392                                 "toolbar:" + site.getId()); //$NON-NLS-1$
393
// 3.3 end
394

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             // The editor should now be fully created. Exercise its public
421
// interface, and sanity-check
422
// it wherever possible. If it's going to throw exceptions or behave
423
// badly, it's much better
424
// that it does so now while we can still cancel creation of the
425
// part.
426
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 JavaDoc e) {
438             if ((e instanceof Error JavaDoc) && !(e instanceof LinkageError JavaDoc)) {
439                 throw (Error JavaDoc) e;
440             }
441             
442             // An exception occurred. First deallocate anything we've allocated
443
// in the try block (see the top
444
// of the try block for a list of objects that need to be explicitly
445
// disposed)
446
if (content != null) {
447                 try {
448                     content.dispose();
449                 } catch (RuntimeException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
493      * The memento is that last view state saved by the workbench.
494      *
495      * @return the last state that was saved by the workbench. It can return
496      * <code>null</code>.
497      * @since 3.1.1
498      */

499     public IMemento getMemento() {
500         return memento;
501     }
502 }
503
Popular Tags