KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > PageBookView


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.part;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.commands.common.EventManager;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.jface.util.SafeRunnable;
25 import org.eclipse.jface.viewers.IPostSelectionProvider;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.ui.IActionBars;
35 import org.eclipse.ui.IPartListener;
36 import org.eclipse.ui.IViewSite;
37 import org.eclipse.ui.IWorkbenchPart;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.SubActionBars;
40 import org.eclipse.ui.internal.WorkbenchPlugin;
41 import org.eclipse.ui.internal.util.Util;
42
43 /**
44  * Abstract superclass of all multi-page workbench views.
45  * <p>
46  * Within the workbench there are many views which track the active part. If a
47  * part is activated these views display some properties for the active part. A
48  * simple example is the <code>Outline View</code>, which displays the
49  * outline for the active editor. To avoid loss of context when part activation
50  * changes, these views may implement a multi-page approach. A separate page is
51  * maintained within the view for each source view. If a part is activated the
52  * associated page for the part is brought to top. If a part is closed the
53  * associated page is disposed. <code>PageBookView</code> is a base
54  * implementation for multi page views.
55  * </p>
56  * <p>
57  * <code>PageBookView</code>s provide an <code>IPageSite</code> for each of
58  * their pages. This site is supplied during the page's initialization. The page
59  * may supply a selection provider for this site. <code>PageBookView</code>s
60  * deal with these selection providers in a similar way to a workbench page's
61  * <code>SelectionService</code>. When a page is made visible, if its site
62  * has a selection provider, then changes in the selection are listened for and
63  * the current selection is obtained and fired as a selection change event.
64  * Selection changes are no longer listened for when a page is made invisible.
65  * </p>
66  * <p>
67  * This class should be subclassed by clients wishing to define new multi-page
68  * views.
69  * </p>
70  * <p>
71  * When a <code>PageBookView</code> is created the following methods are
72  * invoked. Subclasses must implement these.
73  * <ul>
74  * <li><code>createDefaultPage</code> - called to create a default page for
75  * the view. This page is displayed when the active part in the workbench does
76  * not have a page.</li>
77  * <li><code>getBootstrapPart</code> - called to determine the active part in
78  * the workbench. A page will be created for this part</li>
79  * </ul>
80  * </p>
81  * <p>
82  * When a part is activated the base implementation does not know if a page
83  * should be created for the part. Therefore, it delegates creation to the
84  * subclass.
85  * <ul>
86  * <li><code>isImportant</code> - called when a workbench part is activated.
87  * Subclasses return whether a page should be created for the new part.</li>
88  * <li><code>doCreatePage</code> - called to create a page for a particular
89  * part in the workbench. This is only invoked when <code>isImportant</code>
90  * returns </code>true</code>.</li>
91  * </ul>
92  * </p>
93  * <p>
94  * When a part is closed the base implementation will destroy the page
95  * associated with the particular part. The page was created by a subclass, so
96  * the subclass must also destroy it. Subclasses must implement these.
97  * <ul>
98  * <li><code>doDestroyPage</code> - called to destroy a page for a particular
99  * part in the workbench.</li>
100  * </ul>
101  * </p>
102  */

103 public abstract class PageBookView extends ViewPart implements IPartListener {
104     /**
105      * The pagebook control, or <code>null</code> if not initialized.
106      */

107     private PageBook book;
108
109     /**
110      * The page record for the default page.
111      */

112     private PageRec defaultPageRec;
113
114     /**
115      * Map from parts to part records (key type: <code>IWorkbenchPart</code>;
116      * value type: <code>PartRec</code>).
117      */

118     private Map JavaDoc mapPartToRec = new HashMap JavaDoc();
119
120     /**
121      * Map from pages to view sites Note that view sites were not added to page
122      * recs to avoid breaking binary compatibility with previous builds
123      */

124     private Map JavaDoc mapPageToSite = new HashMap JavaDoc();
125
126     /**
127      * Map from pages to the number of pageRecs actively associated with a page.
128      */

129     private Map JavaDoc mapPageToNumRecs = new HashMap JavaDoc();
130
131     /**
132      * The page rec which provided the current page or <code>null</code>
133      */

134     private PageRec activeRec;
135
136     /**
137      * The action bar property listener.
138      */

139     private IPropertyChangeListener actionBarPropListener = new IPropertyChangeListener() {
140         public void propertyChange(PropertyChangeEvent event) {
141             if (event.getProperty().equals(SubActionBars.P_ACTION_HANDLERS)
142                     && activeRec != null
143                     && event.getSource() == activeRec.subActionBars) {
144                 refreshGlobalActionHandlers();
145             }
146         }
147     };
148
149     /**
150      * Selection change listener to listen for page selection changes
151      */

152     private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() {
153         public void selectionChanged(SelectionChangedEvent event) {
154             pageSelectionChanged(event);
155         }
156     };
157
158     /**
159      * Selection change listener to listen for page selection changes
160      */

161     private ISelectionChangedListener postSelectionListener = new ISelectionChangedListener() {
162         public void selectionChanged(SelectionChangedEvent event) {
163             postSelectionChanged(event);
164         }
165     };
166
167     /**
168      * Selection provider for this view's site
169      */

170     private SelectionProvider selectionProvider = new SelectionProvider();
171
172     /**
173      * A data structure used to store the information about a single page within
174      * a pagebook view.
175      */

176     protected static class PageRec {
177
178         /**
179          * The part.
180          */

181         public IWorkbenchPart part;
182
183         /**
184          * The page.
185          */

186         public IPage page;
187
188         /**
189          * The page's action bars
190          */

191         public SubActionBars subActionBars;
192
193         /**
194          * Creates a new page record initialized to the given part and page.
195          *
196          * @param part
197          * @param page
198          */

199         public PageRec(IWorkbenchPart part, IPage page) {
200             this.part = part;
201             this.page = page;
202         }
203
204         /**
205          * Disposes of this page record by <code>null</code>ing its fields.
206          */

207         public void dispose() {
208             part = null;
209             page = null;
210         }
211     }
212
213     private static class SelectionManager extends EventManager {
214         /**
215          *
216          * @param listener
217          * listen
218          */

219         public void addSelectionChangedListener(
220                 ISelectionChangedListener listener) {
221             addListenerObject(listener);
222         }
223
224         /**
225          *
226          * @param listener
227          * listen
228          */

229         public void removeSelectionChangedListener(
230                 ISelectionChangedListener listener) {
231             removeListenerObject(listener);
232         }
233
234         /**
235          *
236          * @param event
237          * the event
238          */

239         public void selectionChanged(final SelectionChangedEvent event) {
240             // pass on the notification to listeners
241
Object JavaDoc[] listeners = getListeners();
242             for (int i = 0; i < listeners.length; ++i) {
243                 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
244                 Platform.run(new SafeRunnable() {
245                     public void run() {
246                         l.selectionChanged(event);
247                     }
248                 });
249             }
250         }
251
252     }
253
254     /**
255      * A selection provider/listener for this view. It is a selection provider
256      * for this view's site.
257      */

258     protected class SelectionProvider implements IPostSelectionProvider {
259
260         private SelectionManager fSelectionListener = new SelectionManager();
261
262         private SelectionManager fPostSelectionListeners = new SelectionManager();
263
264         /*
265          * (non-Javadoc) Method declared on ISelectionProvider.
266          */

267         public void addSelectionChangedListener(
268                 ISelectionChangedListener listener) {
269             fSelectionListener.addSelectionChangedListener(listener);
270         }
271
272         /*
273          * (non-Javadoc) Method declared on ISelectionProvider.
274          */

275         public ISelection getSelection() {
276             // get the selection provider from the current page
277
IPage currentPage = getCurrentPage();
278             // during workbench startup we may be in a state when
279
// there is no current page
280
if (currentPage == null) {
281                 return StructuredSelection.EMPTY;
282             }
283             IPageSite site = getPageSite(currentPage);
284             if (site == null) {
285                 return StructuredSelection.EMPTY;
286             }
287             ISelectionProvider selProvider = site.getSelectionProvider();
288             if (selProvider != null) {
289                 return selProvider.getSelection();
290             }
291             return StructuredSelection.EMPTY;
292         }
293
294         /*
295          * (non-Javadoc) Method declared on ISelectionProvider.
296          */

297         public void removeSelectionChangedListener(
298                 ISelectionChangedListener listener) {
299             fSelectionListener.removeSelectionChangedListener(listener);
300         }
301
302         /**
303          * The selection has changed. Process the event, notifying selection
304          * listeners and post selection listeners.
305          *
306          * @param event
307          * the change
308          */

309         public void selectionChanged(final SelectionChangedEvent event) {
310             fSelectionListener.selectionChanged(event);
311         }
312
313         /**
314          * The selection has changed, so notify any post-selection listeners.
315          *
316          * @param event
317          * the change
318          */

319         public void postSelectionChanged(final SelectionChangedEvent event) {
320             fPostSelectionListeners.selectionChanged(event);
321         }
322
323         /*
324          * (non-Javadoc) Method declared on ISelectionProvider.
325          */

326         public void setSelection(ISelection selection) {
327             // get the selection provider from the current page
328
IPage currentPage = getCurrentPage();
329             // during workbench startup we may be in a state when
330
// there is no current page
331
if (currentPage == null) {
332                 return;
333             }
334             IPageSite site = getPageSite(currentPage);
335             if (site == null) {
336                 return;
337             }
338             ISelectionProvider selProvider = site.getSelectionProvider();
339             // and set its selection
340
if (selProvider != null) {
341                 selProvider.setSelection(selection);
342             }
343         }
344
345         /*
346          * (non-Javadoc)
347          *
348          * @see org.eclipse.jface.viewers.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
349          */

350         public void addPostSelectionChangedListener(
351                 ISelectionChangedListener listener) {
352             fPostSelectionListeners.addSelectionChangedListener(listener);
353         }
354
355         /*
356          * (non-Javadoc)
357          *
358          * @see org.eclipse.jface.viewers.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
359          */

360         public void removePostSelectionChangedListener(
361                 ISelectionChangedListener listener) {
362             fPostSelectionListeners.removeSelectionChangedListener(listener);
363         }
364     }
365
366     /**
367      * Creates a new pagebook view.
368      */

369     protected PageBookView() {
370         super();
371     }
372
373     /**
374      * Creates and returns the default page for this view.
375      * <p>
376      * Subclasses must implement this method.
377      * </p>
378      * <p>
379      * Subclasses must call initPage with the new page (if it is an
380      * <code>IPageBookViewPage</code>) before calling createControl on the
381      * page.
382      * </p>
383      *
384      * @param book
385      * the pagebook control
386      * @return the default page
387      */

388     protected abstract IPage createDefaultPage(PageBook book);
389
390     /**
391      * Creates a page for a given part. Adds it to the pagebook but does not
392      * show it.
393      *
394      * @param part
395      * The part we are making a page for.
396      * @return IWorkbenchPart
397      */

398     private PageRec createPage(IWorkbenchPart part) {
399         PageRec rec = doCreatePage(part);
400         if (rec != null) {
401             mapPartToRec.put(part, rec);
402             preparePage(rec);
403         }
404         return rec;
405     }
406
407     /**
408      * Prepares the page in the given page rec for use in this view.
409      *
410      * @param rec
411      */

412     private void preparePage(PageRec rec) {
413         IPageSite site = null;
414         Integer JavaDoc count;
415
416         if (!doesPageExist(rec.page)) {
417             if (rec.page instanceof IPageBookViewPage) {
418                 site = ((IPageBookViewPage) rec.page).getSite();
419             }
420             if (site == null) {
421                 // We will create a site for our use
422
site = new PageSite(getViewSite());
423             }
424             mapPageToSite.put(rec.page, site);
425
426             rec.subActionBars = (SubActionBars) site.getActionBars();
427             rec.subActionBars.addPropertyChangeListener(actionBarPropListener);
428             // for backward compability with IPage
429
rec.page.setActionBars(rec.subActionBars);
430
431             count = new Integer JavaDoc(0);
432         } else {
433             site = (IPageSite) mapPageToSite.get(rec.page);
434             rec.subActionBars = (SubActionBars) site.getActionBars();
435             count = ((Integer JavaDoc) mapPageToNumRecs.get(rec.page));
436         }
437
438         mapPageToNumRecs.put(rec.page, new Integer JavaDoc(count.intValue() + 1));
439     }
440
441     /**
442      * Initializes the given page with a page site.
443      * <p>
444      * Subclasses should call this method after the page is created but before
445      * creating its controls.
446      * </p>
447      * <p>
448      * Subclasses may override
449      * </p>
450      *
451      * @param page
452      * The page to initialize
453      */

454     protected void initPage(IPageBookViewPage page) {
455         try {
456             page.init(new PageSite(getViewSite()));
457         } catch (PartInitException e) {
458             WorkbenchPlugin.log(getClass(), "initPage", e); //$NON-NLS-1$
459
}
460     }
461
462     /**
463      * The <code>PageBookView</code> implementation of this
464      * <code>IWorkbenchPart</code> method creates a <code>PageBook</code>
465      * control with its default page showing. Subclasses may extend.
466      */

467     public void createPartControl(Composite parent) {
468
469         // Create the page book.
470
book = new PageBook(parent, SWT.NONE);
471
472         // Create the default page rec.
473
IPage defaultPage = createDefaultPage(book);
474         defaultPageRec = new PageRec(null, defaultPage);
475         preparePage(defaultPageRec);
476
477         // Show the default page
478
showPageRec(defaultPageRec);
479
480         // Listen to part activation events.
481
getSite().getPage().addPartListener(this);
482         showBootstrapPart();
483     }
484
485     /**
486      * The <code>PageBookView</code> implementation of this
487      * <code>IWorkbenchPart</code> method cleans up all the pages. Subclasses
488      * may extend.
489      */

490     public void dispose() {
491         // stop listening to part activation
492
getSite().getPage().removePartListener(this);
493
494         // Deref all of the pages.
495
activeRec = null;
496         if (defaultPageRec != null) {
497             // check for null since the default page may not have
498
// been created (ex. perspective never visible)
499
defaultPageRec.page.dispose();
500             defaultPageRec = null;
501         }
502         Map JavaDoc clone = (Map JavaDoc) ((HashMap JavaDoc) mapPartToRec).clone();
503         Iterator JavaDoc itr = clone.values().iterator();
504         while (itr.hasNext()) {
505             PageRec rec = (PageRec) itr.next();
506             removePage(rec);
507         }
508
509         // Run super.
510
super.dispose();
511     }
512
513     /**
514      * Creates a new page in the pagebook for a particular part. This page will
515      * be made visible whenever the part is active, and will be destroyed with a
516      * call to <code>doDestroyPage</code>.
517      * <p>
518      * Subclasses must implement this method.
519      * </p>
520      * <p>
521      * Subclasses must call initPage with the new page (if it is an
522      * <code>IPageBookViewPage</code>) before calling createControl on the
523      * page.
524      * </p>
525      *
526      * @param part
527      * the input part
528      * @return the record describing a new page for this view
529      * @see #doDestroyPage
530      */

531     protected abstract PageRec doCreatePage(IWorkbenchPart part);
532
533     /**
534      * Destroys a page in the pagebook for a particular part. This page was
535      * returned as a result from <code>doCreatePage</code>.
536      * <p>
537      * Subclasses must implement this method.
538      * </p>
539      *
540      * @param part
541      * the input part
542      * @param pageRecord
543      * a page record for the part
544      * @see #doCreatePage
545      */

546     protected abstract void doDestroyPage(IWorkbenchPart part,
547             PageRec pageRecord);
548
549     /**
550      * Returns true if the page has already been created.
551      *
552      * @param page
553      * the page to test
554      * @return true if this page has already been created.
555      */

556     protected boolean doesPageExist(IPage page) {
557         return mapPageToNumRecs.containsKey(page);
558     }
559
560     /**
561      * The <code>PageBookView</code> implementation of this
562      * <code>IAdaptable</code> method delegates to the current page, if it
563      * implements <code>IAdaptable</code>.
564      */

565     public Object JavaDoc getAdapter(Class JavaDoc key) {
566         // delegate to the current page, if supported
567
IPage page = getCurrentPage();
568         Object JavaDoc adapter = Util.getAdapter(page, key);
569         if (adapter != null) {
570             return adapter;
571         }
572         // if the page did not find the adapter, look for one provided by
573
// this view before delegating to super.
574
adapter = getViewAdapter(key);
575         if (adapter != null) {
576             return adapter;
577         }
578         // delegate to super
579
return super.getAdapter(key);
580     }
581
582     /**
583      * Returns an adapter of the specified type, as provided by this view (not
584      * the current page), or <code>null</code> if this view does not provide
585      * an adapter of the specified adapter.
586      * <p>
587      * The default implementation returns <code>null</code>. Subclasses may
588      * override.
589      * </p>
590      *
591      * @param adapter
592      * the adapter class to look up
593      * @return a object castable to the given class, or <code>null</code> if
594      * this object does not have an adapter for the given class
595      * @since 3.2
596      */

597     protected Object JavaDoc getViewAdapter(Class JavaDoc adapter) {
598         return null;
599     }
600
601     /**
602      * Returns the active, important workbench part for this view.
603      * <p>
604      * When the page book view is created it has no idea which part within the
605      * workbook should be used to generate the first page. Therefore, it
606      * delegates the choice to subclasses of <code>PageBookView</code>.
607      * </p>
608      * <p>
609      * Implementors of this method should return an active, important part in
610      * the workbench or <code>null</code> if none found.
611      * </p>
612      * <p>
613      * Subclasses must implement this method.
614      * </p>
615      *
616      * @return the active important part, or <code>null</code> if none
617      */

618     protected abstract IWorkbenchPart getBootstrapPart();
619
620     /**
621      * Returns the part which contributed the current page to this view.
622      *
623      * @return the part which contributed the current page or <code>null</code>
624      * if no part contributed the current page
625      */

626     protected IWorkbenchPart getCurrentContributingPart() {
627         if (activeRec == null) {
628             return null;
629         }
630         return activeRec.part;
631     }
632
633     /**
634      * Returns the currently visible page for this view or <code>null</code>
635      * if no page is currently visible.
636      *
637      * @return the currently visible page
638      */

639     public IPage getCurrentPage() {
640         if (activeRec == null) {
641             return null;
642         }
643         return activeRec.page;
644     }
645
646     /**
647      * Returns the view site for the given page of this view.
648      *
649      * @param page
650      * the page
651      * @return the corresponding site, or <code>null</code> if not found
652      */

653     protected PageSite getPageSite(IPage page) {
654         return (PageSite) mapPageToSite.get(page);
655     }
656
657     /**
658      * Returns the default page for this view.
659      *
660      * @return the default page
661      */

662     public IPage getDefaultPage() {
663         return defaultPageRec.page;
664     }
665
666     /**
667      * Returns the pagebook control for this view.
668      *
669      * @return the pagebook control, or <code>null</code> if not initialized
670      */

671     protected PageBook getPageBook() {
672         return book;
673     }
674
675     /**
676      * Returns the page record for the given part.
677      *
678      * @param part
679      * the part
680      * @return the corresponding page record, or <code>null</code> if not
681      * found
682      */

683     protected PageRec getPageRec(IWorkbenchPart part) {
684         return (PageRec) mapPartToRec.get(part);
685     }
686
687     /**
688      * Returns the page record for the given page of this view.
689      *
690      * @param page
691      * the page
692      * @return the corresponding page record, or <code>null</code> if not
693      * found
694      */

695     protected PageRec getPageRec(IPage page) {
696         Iterator JavaDoc itr = mapPartToRec.values().iterator();
697         while (itr.hasNext()) {
698             PageRec rec = (PageRec) itr.next();
699             if (rec.page == page) {
700                 return rec;
701             }
702         }
703         return null;
704     }
705
706     /**
707      * Returns whether the given part should be added to this view.
708      * <p>
709      * Subclasses must implement this method.
710      * </p>
711      *
712      * @param part
713      * the input part
714      * @return <code>true</code> if the part is relevant, and
715      * <code>false</code> otherwise
716      */

717     protected abstract boolean isImportant(IWorkbenchPart part);
718
719     /*
720      * (non-Javadoc) Method declared on IViewPart.
721      */

722     public void init(IViewSite site) throws PartInitException {
723         site.setSelectionProvider(selectionProvider);
724         super.init(site);
725     }
726
727     /**
728      * The <code>PageBookView</code> implementation of this
729      * <code>IPartListener</code> method shows the page when the given part is
730      * activated. Subclasses may extend.
731      */

732     public void partActivated(IWorkbenchPart part) {
733         // Is this an important part? If not just return.
734
if (!isImportant(part)) {
735             return;
736         }
737
738         // Create a page for the part.
739
PageRec rec = getPageRec(part);
740         if (rec == null) {
741             rec = createPage(part);
742         }
743
744         // Show the page.
745
if (rec != null) {
746             showPageRec(rec);
747         } else {
748             showPageRec(defaultPageRec);
749         }
750     }
751
752     /**
753      * The <code>PageBookView</code> implementation of this
754      * <code>IPartListener</code> method does nothing. Subclasses may extend.
755      */

756     public void partBroughtToTop(IWorkbenchPart part) {
757         // Do nothing by default
758
}
759
760     /**
761      * The <code>PageBookView</code> implementation of this
762      * <code>IPartListener</code> method deal with the closing of the active
763      * part. Subclasses may extend.
764      */

765     public void partClosed(IWorkbenchPart part) {
766         // Update the active part.
767
if (activeRec != null && activeRec.part == part) {
768             showPageRec(defaultPageRec);
769         }
770
771         // Find and remove the part page.
772
PageRec rec = getPageRec(part);
773         if (rec != null) {
774             removePage(rec);
775         }
776     }
777
778     /**
779      * The <code>PageBookView</code> implementation of this
780      * <code>IPartListener</code> method does nothing. Subclasses may extend.
781      */

782     public void partDeactivated(IWorkbenchPart part) {
783         // Do nothing.
784
}
785
786     /*
787      * (non-Javadoc)
788      *
789      * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
790      */

791     public void partOpened(IWorkbenchPart part) {
792         // Do nothing by default.
793
}
794
795     /**
796      * Refreshes the global actions for the active page.
797      */

798     private void refreshGlobalActionHandlers() {
799         // Clear old actions.
800
IActionBars bars = getViewSite().getActionBars();
801         bars.clearGlobalActionHandlers();
802
803         // Set new actions.
804
Map JavaDoc newActionHandlers = activeRec.subActionBars
805                 .getGlobalActionHandlers();
806         if (newActionHandlers != null) {
807             Set JavaDoc keys = newActionHandlers.entrySet();
808             Iterator JavaDoc iter = keys.iterator();
809             while (iter.hasNext()) {
810                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
811                 bars.setGlobalActionHandler((String JavaDoc) entry.getKey(),
812                         (IAction) entry.getValue());
813             }
814         }
815     }
816
817     /**
818      * Removes a page record. If it is the last reference to the page dispose of
819      * it - otherwise just decrement the reference count.
820      *
821      * @param rec
822      */

823     private void removePage(PageRec rec) {
824         mapPartToRec.remove(rec.part);
825
826         int newCount = ((Integer JavaDoc) mapPageToNumRecs.get(rec.page)).intValue() - 1;
827
828         if (newCount == 0) {
829             Object JavaDoc site = mapPageToSite.remove(rec.page);
830             mapPageToNumRecs.remove(rec.page);
831
832             if (rec.subActionBars != null) {
833                 rec.subActionBars.dispose();
834             }
835
836             Control control = rec.page.getControl();
837             if (control != null && !control.isDisposed()) {
838                 // Dispose the page's control so pages don't have to do this in
839
// their
840
// dispose method.
841
// The page's control is a child of this view's control so if
842
// this view
843
// is closed, the page's control will already be disposed.
844
control.dispose();
845             }
846
847             if (site instanceof PageSite) {
848                 ((PageSite) site).dispose();
849             }
850
851             // free the page
852
doDestroyPage(rec.part, rec);
853         } else {
854             mapPageToNumRecs.put(rec.page, new Integer JavaDoc(newCount));
855         }
856     }
857
858     /*
859      * (non-Javadoc) Method declared on IWorkbenchPart.
860      */

861     public void setFocus() {
862         // first set focus on the page book, in case the page
863
// doesn't properly handle setFocus
864
if (book != null) {
865             book.setFocus();
866         }
867         // then set focus on the page, if any
868
if (activeRec != null) {
869             activeRec.page.setFocus();
870         }
871     }
872
873     /**
874      * Handle page selection changes.
875      *
876      * @param event
877      */

878     private void pageSelectionChanged(SelectionChangedEvent event) {
879         // forward this change from a page to our site's selection provider
880
SelectionProvider provider = (SelectionProvider) getSite()
881                 .getSelectionProvider();
882         if (provider != null) {
883             provider.selectionChanged(event);
884         }
885     }
886
887     /**
888      * Handle page selection changes.
889      *
890      * @param event
891      */

892     private void postSelectionChanged(SelectionChangedEvent event) {
893         // forward this change from a page to our site's selection provider
894
SelectionProvider provider = (SelectionProvider) getSite()
895                 .getSelectionProvider();
896         if (provider != null) {
897             provider.postSelectionChanged(event);
898         }
899     }
900
901     /**
902      * Shows a page for the active workbench part.
903      */

904     private void showBootstrapPart() {
905         IWorkbenchPart part = getBootstrapPart();
906         if (part != null) {
907             partActivated(part);
908         }
909     }
910
911     /**
912      * Shows page contained in the given page record in this view. The page
913      * record must be one from this pagebook view.
914      * <p>
915      * The <code>PageBookView</code> implementation of this method asks the
916      * pagebook control to show the given page's control, and records that the
917      * given page is now current. Subclasses may extend.
918      * </p>
919      *
920      * @param pageRec
921      * the page record containing the page to show
922      */

923     protected void showPageRec(PageRec pageRec) {
924         // If already showing do nothing
925
if (activeRec == pageRec) {
926             return;
927         }
928         // If the page is the same, just set activeRec to pageRec
929
if (activeRec != null && pageRec != null
930                 && activeRec.page == pageRec.page) {
931             activeRec = pageRec;
932             return;
933         }
934
935         // Hide old page.
936
if (activeRec != null) {
937             PageSite pageSite = (PageSite) mapPageToSite.get(activeRec.page);
938
939             activeRec.subActionBars.deactivate();
940
941             // deactivate the nested services
942
pageSite.deactivate();
943
944             // remove our selection listener
945
ISelectionProvider provider = pageSite.getSelectionProvider();
946             if (provider != null) {
947                 provider
948                         .removeSelectionChangedListener(selectionChangedListener);
949                 if (provider instanceof IPostSelectionProvider) {
950                     ((IPostSelectionProvider) provider)
951                             .removePostSelectionChangedListener(postSelectionListener);
952                 }
953             }
954         }
955
956         // Show new page.
957
activeRec = pageRec;
958         Control pageControl = activeRec.page.getControl();
959         if (pageControl != null && !pageControl.isDisposed()) {
960             PageSite pageSite = (PageSite) mapPageToSite.get(activeRec.page);
961
962             // Verify that the page control is not disposed
963
// If we are closing, it may have already been disposed
964
book.showPage(pageControl);
965             activeRec.subActionBars.activate();
966             refreshGlobalActionHandlers();
967
968             // activate the nested services
969
pageSite.activate();
970
971             // add our selection listener
972
ISelectionProvider provider = pageSite.getSelectionProvider();
973             if (provider != null) {
974                 provider.addSelectionChangedListener(selectionChangedListener);
975                 if (provider instanceof IPostSelectionProvider) {
976                     ((IPostSelectionProvider) provider)
977                             .addPostSelectionChangedListener(postSelectionListener);
978                 }
979             }
980             // Update action bars.
981
getViewSite().getActionBars().updateActionBars();
982         }
983     }
984
985     /**
986      * Returns the selectionProvider for this page book view.
987      *
988      * @return a SelectionProvider
989      */

990     protected SelectionProvider getSelectionProvider() {
991         return selectionProvider;
992     }
993 }
994
Popular Tags