KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > properties > tabbed > TabbedPropertySheetPage


1 /*******************************************************************************
2  * Copyright (c) 2001, 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.views.properties.tabbed;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.viewers.ILabelProviderListener;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredContentProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.layout.FillLayout;
36 import org.eclipse.swt.layout.FormAttachment;
37 import org.eclipse.swt.layout.FormData;
38 import org.eclipse.swt.layout.FormLayout;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.IEditorPart;
43 import org.eclipse.ui.IPartListener;
44 import org.eclipse.ui.IViewPart;
45 import org.eclipse.ui.IWorkbenchPart;
46 import org.eclipse.ui.IWorkbenchWindow;
47 import org.eclipse.ui.actions.ActionFactory;
48 import org.eclipse.ui.internal.views.properties.tabbed.view.Tab;
49 import org.eclipse.ui.internal.views.properties.tabbed.view.TabDescriptor;
50 import org.eclipse.ui.internal.views.properties.tabbed.view.TabListContentProvider;
51 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyComposite;
52 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyRegistry;
53 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyRegistryFactory;
54 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyTitle;
55 import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyViewer;
56 import org.eclipse.ui.part.IContributedContentsView;
57 import org.eclipse.ui.part.Page;
58 import org.eclipse.ui.views.properties.IPropertySheetPage;
59 import org.eclipse.ui.views.properties.PropertySheet;
60
61 /**
62  * A property sheet page that provides a tabbed UI.
63  *
64  * @author Anthony Hunter
65  */

66 public class TabbedPropertySheetPage
67     extends Page
68     implements IPropertySheetPage, ILabelProviderListener {
69
70     // widgets
71
private TabbedPropertyComposite tabbedPropertyComposite;
72
73     private TabbedPropertySheetWidgetFactory widgetFactory;
74
75     private ITabbedPropertySheetPageContributor contributor;
76
77     private TabbedPropertyRegistry registry;
78
79     private ITabbedPropertySheetPageContributor selectionContributor = null;
80
81     /**
82      * The currently active contributor id, which may not match the contributor
83      * id from the workbench part that created this instance.
84      */

85     private String JavaDoc currentContributorId;
86
87     protected IStructuredContentProvider tabListContentProvider;
88
89     private ISelection currentSelection;
90
91     private boolean activePropertySheet;
92
93     private TabbedPropertyViewer tabbedPropertyViewer;
94
95     private Tab currentTab;
96
97     private Map JavaDoc descriptorToTab;
98
99     private Map JavaDoc tabToComposite;
100
101     private List JavaDoc selectionQueue;
102
103     private boolean selectionQueueLocked;
104
105     private List JavaDoc tabSelectionListeners;
106
107     private IWorkbenchWindow cachedWorkbenchWindow;
108
109     private boolean hasTitleBar;
110
111     /**
112      * a listener that is interested in part activation events.
113      */

114     private IPartListener partActivationListener = new IPartListener() {
115
116         public void partActivated(IWorkbenchPart part) {
117             handlePartActivated(part);
118         }
119
120         public void partBroughtToTop(IWorkbenchPart part) {
121         }
122
123         public void partClosed(IWorkbenchPart part) {
124         }
125
126         public void partDeactivated(IWorkbenchPart part) {
127         }
128
129         public void partOpened(IWorkbenchPart part) {
130         }
131     };
132
133     private class TabbedPropertySheetPageContributorFromSelection
134         implements ITabbedPropertySheetPageContributor {
135
136         private String JavaDoc contributorId;
137
138         /**
139          * Constructor that takes in a contributor id taken from a selection.
140          *
141          * @param contributorId
142          * the contributor id.
143          */

144         public TabbedPropertySheetPageContributorFromSelection(
145                 String JavaDoc contributorId) {
146             super();
147             this.contributorId = contributorId;
148         }
149
150         /**
151          * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor#getContributorId()
152          */

153         public String JavaDoc getContributorId() {
154             return contributorId;
155         }
156
157     }
158
159     /**
160      * Label provider for the ListViewer.
161      */

162     class TabbedPropertySheetPageLabelProvider
163         extends LabelProvider {
164
165         public String JavaDoc getText(Object JavaDoc element) {
166             if (element instanceof TabDescriptor) {
167                 return ((TabDescriptor) element).getLabel();
168             }
169             return null;
170         }
171     }
172
173     /**
174      * SelectionChangedListener for the ListViewer.
175      */

176     class SelectionChangedListener
177         implements ISelectionChangedListener {
178
179         /**
180          * Shows the tab associated with the selection.
181          */

182         public void selectionChanged(SelectionChangedEvent event) {
183             IStructuredSelection selection = (IStructuredSelection) event
184                 .getSelection();
185             Tab tab = null;
186             TabDescriptor descriptor = (TabDescriptor) selection
187                 .getFirstElement();
188
189             if (descriptor == null) {
190                 // pretend the tab is empty.
191
hideTab(currentTab);
192             } else {
193                 // create tab if necessary
194
// can not cache based on the id - tabs may have the same id,
195
// but different section depending on the selection
196
tab = (Tab) descriptorToTab.get(descriptor);
197
198                 if (tab != currentTab) {
199                     hideTab(currentTab);
200                 }
201
202                 Composite tabComposite = (Composite) tabToComposite.get(tab);
203                 if (tabComposite == null) {
204                     tabComposite = createTabComposite();
205                     tab.createControls(tabComposite,
206                         TabbedPropertySheetPage.this);
207                     // tabAreaComposite.layout(true);
208
tabToComposite.put(tab, tabComposite);
209                 }
210                 // force widgets to be resized
211
tab.setInput(tabbedPropertyViewer.getWorkbenchPart(),
212                     (ISelection) tabbedPropertyViewer.getInput());
213
214                 // store tab selection
215
storeCurrentTabSelection(descriptor.getLabel());
216
217                 if (tab != currentTab) {
218                     showTab(tab);
219                 }
220
221                 tab.refresh();
222             }
223             tabbedPropertyComposite.getTabComposite().layout(true);
224             currentTab = tab;
225             resizeScrolledComposite();
226
227             if (descriptor != null) {
228                 handleTabSelection(descriptor);
229             }
230         }
231
232         /**
233          * Shows the given tab.
234          */

235         private void showTab(Tab target) {
236             if (target != null) {
237                 Composite tabComposite = (Composite) tabToComposite.get(target);
238                 if (tabComposite != null) {
239                     /**
240                      * the following method call order is important - do not
241                      * change it or the widgets might be drawn incorrectly
242                      */

243                     tabComposite.moveAbove(null);
244                     target.aboutToBeShown();
245                     tabComposite.setVisible(true);
246                 }
247             }
248         }
249
250         /**
251          * Hides the given tab.
252          */

253         private void hideTab(Tab target) {
254             if (target != null) {
255                 Composite tabComposite = (Composite) tabToComposite.get(target);
256                 if (tabComposite != null) {
257                     target.aboutToBeHidden();
258                     tabComposite.setVisible(false);
259                 }
260             }
261         }
262
263     }
264
265     /**
266      * create a new tabbed property sheet page.
267      *
268      * @param tabbedPropertySheetPageContributor
269      * the tabbed property sheet page contributor.
270      */

271     public TabbedPropertySheetPage(
272             ITabbedPropertySheetPageContributor tabbedPropertySheetPageContributor) {
273         contributor = tabbedPropertySheetPageContributor;
274         tabToComposite = new HashMap JavaDoc();
275         selectionQueue = new ArrayList JavaDoc(10);
276         tabSelectionListeners = new ArrayList JavaDoc();
277         initContributor(contributor.getContributorId());
278     }
279
280     /**
281      * Handle the part activated event.
282      *
283      * @param part
284      * the new activated part.
285      */

286     protected void handlePartActivated(IWorkbenchPart part) {
287         /*
288          * The properties view has been activated and the current page is this
289          * instance of TabbedPropertySheetPage
290          */

291         boolean thisActivated = part instanceof PropertySheet
292             && ((PropertySheet) part).getCurrentPage() == this;
293
294         /*
295          * When the active part changes and the part does not provide a
296          * selection that affects this property sheet page, the PropertySheet
297          * does not send us a selectionChanged() event. We need to be informed
298          * of these events since we want to send aboutToBeHidden() and
299          * aboutToBeShown() when the property sheet is hidden or shown.
300          */

301         if (!thisActivated && !part.equals(contributor)
302                 && !part.getSite().getId().equals(contributor.getContributorId())) {
303             /*
304              * Is the part is a IContributedContentsView for the contributor,
305              * for example, outline view.
306              */

307             IContributedContentsView view = (IContributedContentsView) part
308                 .getAdapter(IContributedContentsView.class);
309             if (view == null
310                 || (view.getContributingPart() != null && !view
311                     .getContributingPart().equals(contributor))) {
312                 if (activePropertySheet) {
313                     if (currentTab != null) {
314                         currentTab.aboutToBeHidden();
315                     }
316                     activePropertySheet = false;
317                 }
318                 return;
319             }
320         }
321         if (!activePropertySheet && currentTab != null) {
322             currentTab.aboutToBeShown();
323             currentTab.refresh();
324         }
325         activePropertySheet = true;
326     }
327
328     /**
329      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
330      */

331     public void createControl(Composite parent) {
332         widgetFactory = new TabbedPropertySheetWidgetFactory();
333         tabbedPropertyComposite = new TabbedPropertyComposite(parent,
334             widgetFactory, hasTitleBar);
335         widgetFactory.paintBordersFor(tabbedPropertyComposite);
336         tabbedPropertyComposite.setLayout(new FormLayout());
337         FormData formData = new FormData();
338         formData.left = new FormAttachment(0, 0);
339         formData.right = new FormAttachment(100, 0);
340         formData.top = new FormAttachment(0, 0);
341         formData.bottom = new FormAttachment(100, 0);
342         tabbedPropertyComposite.setLayoutData(formData);
343
344         tabbedPropertyViewer = new TabbedPropertyViewer(tabbedPropertyComposite
345             .getList());
346         tabbedPropertyViewer.setContentProvider(tabListContentProvider);
347         tabbedPropertyViewer
348             .setLabelProvider(new TabbedPropertySheetPageLabelProvider());
349         tabbedPropertyViewer
350             .addSelectionChangedListener(new SelectionChangedListener());
351
352         /**
353          * Add a part activation listener.
354          */

355         cachedWorkbenchWindow = getSite().getWorkbenchWindow();
356         cachedWorkbenchWindow.getPartService().addPartListener(
357             partActivationListener);
358
359         /**
360          * Add a label provider change listener.
361          */

362         if (hasTitleBar) {
363             registry.getLabelProvider().addListener(this);
364         }
365     }
366
367     /**
368      * Initialize the contributor with the provided contributor id.
369      *
370      * @param contributorId
371      * the contributor id.
372      */

373     private void initContributor(String JavaDoc contributorId) {
374         descriptorToTab = new HashMap JavaDoc();
375         if (contributor.getContributorId().equals(contributorId)) {
376             /**
377              * default contributor from the workbench part.
378              */

379             registry = TabbedPropertyRegistryFactory.getInstance()
380                 .createRegistry(contributor);
381         } else {
382             /**
383              * selection contributor.
384              */

385             selectionContributor = new TabbedPropertySheetPageContributorFromSelection(
386                 contributorId);
387             registry = TabbedPropertyRegistryFactory.getInstance()
388                 .createRegistry(selectionContributor);
389         }
390         currentContributorId = contributorId;
391         tabListContentProvider = getTabListContentProvider();
392         hasTitleBar = registry.getLabelProvider() != null;
393
394         if (tabbedPropertyViewer != null) {
395             tabbedPropertyViewer.setContentProvider(tabListContentProvider);
396         }
397
398         /**
399          * Add a label provider change listener.
400          */

401         if (hasTitleBar) {
402             registry.getLabelProvider().addListener(this);
403         }
404
405     }
406
407     /**
408      * Gets the tab list content provider for the contributor.
409      *
410      * @return the tab list content provider for the contributor.
411      */

412     protected IStructuredContentProvider getTabListContentProvider() {
413         return new TabListContentProvider(registry);
414     }
415
416     /**
417      * Dispose the contributor with the provided contributor id. This happens on
418      * part close as well as when contributiors switch between the workbench
419      * part and contributor from a selection.
420      *
421      * @param contributorId
422      * the contributor id.
423      */

424     private void disposeContributor() {
425         /**
426          * If the current tab is about to be disposed we have to call
427          * aboutToBeHidden
428          */

429         if (currentTab != null) {
430             currentTab.aboutToBeHidden();
431             currentTab = null;
432         }
433
434         disposeTabs(descriptorToTab.values());
435         descriptorToTab = new HashMap JavaDoc();
436
437         /**
438          * Remove the label provider change listener.
439          */

440         if (hasTitleBar && registry != null) {
441             registry.getLabelProvider().removeListener(this);
442         }
443
444         if (selectionContributor != null) {
445             /**
446              * remove the selection contributed registry.
447              */

448             TabbedPropertyRegistryFactory.getInstance().disposeRegistry(
449                 selectionContributor);
450             selectionContributor = null;
451         }
452     }
453
454     /**
455      * @see org.eclipse.ui.part.IPage#dispose()
456      */

457     public void dispose() {
458
459         disposeContributor();
460
461         if (widgetFactory != null) {
462             widgetFactory.dispose();
463             widgetFactory = null;
464         }
465         /**
466          * Remove the part activation listener.
467          */

468         if (cachedWorkbenchWindow != null) {
469             cachedWorkbenchWindow.getPartService().removePartListener(
470                 partActivationListener);
471             cachedWorkbenchWindow = null;
472         }
473
474         if (registry != null) {
475             TabbedPropertyRegistryFactory.getInstance().disposeRegistry(
476                 contributor);
477             registry = null;
478         }
479         
480         contributor = null;
481         currentSelection = null;
482     }
483
484     /**
485      * @see org.eclipse.ui.part.IPage#getControl()
486      */

487     public Control getControl() {
488         return tabbedPropertyComposite;
489     }
490
491     /**
492      * @see org.eclipse.ui.part.IPage#setActionBars(org.eclipse.ui.IActionBars)
493      */

494     public void setActionBars(IActionBars actionBars) {
495         // Override the undo and redo global action handlers
496
// to use the contributor action handlers
497
IActionBars partActionBars = null;
498         if (contributor instanceof IEditorPart) {
499             IEditorPart editorPart = (IEditorPart) contributor;
500             partActionBars = editorPart.getEditorSite().getActionBars();
501         } else if (contributor instanceof IViewPart) {
502             IViewPart viewPart = (IViewPart) contributor;
503             partActionBars = viewPart.getViewSite().getActionBars();
504         }
505         
506         if (partActionBars != null) {
507             IAction action = partActionBars.getGlobalActionHandler(ActionFactory.UNDO
508                 .getId());
509             if (action != null) {
510                 actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), action);
511             }
512             action = partActionBars.getGlobalActionHandler(ActionFactory.REDO
513                 .getId());
514             if (action != null) {
515                 actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), action);
516             }
517         }
518     }
519
520     /**
521      * @see org.eclipse.ui.part.IPage#setFocus()
522      */

523     public void setFocus() {
524         getControl().setFocus();
525     }
526
527     /**
528      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
529      * org.eclipse.jface.viewers.ISelection)
530      */

531     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
532         setInput(part, selection);
533     }
534
535     /**
536      * Stores the current tab label in the selection queue. Tab labels are used
537      * to carry the tab context from one input object to another. The queue
538      * specifies the selection priority. So if the first tab in the queue is not
539      * available for the input we try the second tab and so on. If none of the
540      * tabs are available we default to the first tab available for the input.
541      */

542     private void storeCurrentTabSelection(String JavaDoc label) {
543         if (!selectionQueueLocked) {
544             selectionQueue.remove(label);
545             selectionQueue.add(0, label);
546         }
547     }
548
549     private void resizeScrolledComposite() {
550         Point currentTabSize = new Point(0, 0);
551         if (currentTab != null) {
552             Composite sizeReference = (Composite) tabToComposite
553                 .get(currentTab);
554             if (sizeReference != null) {
555                 currentTabSize = sizeReference.computeSize(SWT.DEFAULT, SWT.DEFAULT);
556             }
557         }
558         tabbedPropertyComposite.getScrolledComposite().setMinSize(
559             currentTabSize.x, currentTabSize.y);
560     }
561
562     private void disposeTabs(Collection JavaDoc tabs) {
563         for (Iterator JavaDoc iter = tabs.iterator(); iter.hasNext();) {
564             Tab tab = (Tab) iter.next();
565             Composite composite = (Composite) tabToComposite.remove(tab);
566             tab.dispose();
567             if (composite != null) {
568                 composite.dispose();
569             }
570         }
571     }
572
573     /**
574      * Returns the last known selected tab for the given input.
575      */

576     private int getLastTabSelection(IWorkbenchPart part, ISelection input) {
577         TabDescriptor[] descriptors = registry.getTabDescriptors(part, input);
578         if (descriptors.length != 0) {
579             for (Iterator JavaDoc iter = selectionQueue.iterator(); iter.hasNext();) {
580                 String JavaDoc text = (String JavaDoc) iter.next();
581                 for (int i = 0; i < descriptors.length; i++) {
582                     if (text.equals(descriptors[i].getLabel())) {
583                         return i;
584                     }
585                 }
586             }
587         }
588         return 0;
589     }
590
591     /**
592      * Update the current tabs to represent the given input object. When tabs
593      * apply for both the old and new input they are reused otherwise they are
594      * disposed. If the current visible tab will not be reused (i.e. will be
595      * disposed) we have to send it an aboutToBeHidden() message.
596      */

597     protected void updateTabs(TabDescriptor[] descriptors) {
598         Map JavaDoc newTabs = new HashMap JavaDoc(descriptors.length * 2);
599         boolean disposingCurrentTab = (currentTab != null);
600         for (int i = 0; i < descriptors.length; i++) {
601             Tab tab = (Tab) descriptorToTab.remove(descriptors[i]);
602
603             if (tab != null && tab.controlsHaveBeenCreated()) {
604                 if (tab == currentTab) {
605                     disposingCurrentTab = false;
606                 }
607             } else {
608                 tab = (descriptors[i]).createTab();
609             }
610
611             newTabs.put(descriptors[i], tab);
612         }
613         if (disposingCurrentTab) {
614             /**
615              * If the current tab is about to be disposed we have to call
616              * aboutToBeHidden
617              */

618             currentTab.aboutToBeHidden();
619             currentTab = null;
620         }
621         disposeTabs(descriptorToTab.values());
622         descriptorToTab = newTabs;
623     }
624
625     /**
626      * Helper method for creating property tab composites.
627      */

628     private Composite createTabComposite() {
629         Composite result = widgetFactory.createComposite(
630             tabbedPropertyComposite.getTabComposite(), SWT.NO_FOCUS);
631         result.setVisible(false);
632         result.setLayout(new FillLayout());
633         FormData data = new FormData();
634         if (hasTitleBar) {
635             data.top = new FormAttachment(tabbedPropertyComposite.getTitle(), 0);
636         } else {
637             data.top = new FormAttachment(0, 0);
638         }
639         data.bottom = new FormAttachment(100, 0);
640         data.left = new FormAttachment(0, 0);
641         data.right = new FormAttachment(100, 0);
642         result.setLayoutData(data);
643         return result;
644     }
645
646     private void setInput(IWorkbenchPart part, ISelection selection) {
647         if (selection.equals(currentSelection)) {
648             return;
649         }
650
651         this.currentSelection = selection;
652
653         // see if the selection provides a new contributor
654
validateRegistry(selection);
655         TabDescriptor[] descriptors = registry.getTabDescriptors(part,
656             currentSelection);
657         // If there are no descriptors for the given input we do not need to
658
// touch the tab objects. We might reuse them for the next valid
659
// input.
660
if (descriptors.length > 0) {
661             updateTabs(descriptors);
662         }
663         // update tabs list
664
tabbedPropertyViewer.setInput(part, currentSelection);
665         int lastTabSelectionIndex = getLastTabSelection(part, currentSelection);
666         Object JavaDoc selectedTab = tabbedPropertyViewer
667             .getElementAt(lastTabSelectionIndex);
668         selectionQueueLocked = true;
669         try {
670             if (selectedTab == null) {
671                 tabbedPropertyViewer.setSelection(null);
672             } else {
673                 tabbedPropertyViewer.setSelection(new StructuredSelection(
674                     selectedTab));
675             }
676         } finally {
677             selectionQueueLocked = false;
678         }
679         refreshTitleBar();
680     }
681
682     /**
683      * Refresh the currently active tab.
684      */

685     public void refresh() {
686         currentTab.refresh();
687     }
688
689     /**
690      * Get the currently active tab.
691      *
692      * @return the currently active tab.
693      */

694     public Tab getCurrentTab() {
695         return currentTab;
696     }
697
698     /**
699      * Handle the tab selected change event.
700      *
701      * @param tabDescriptor
702      * the new selected tab.
703      */

704     private void handleTabSelection(TabDescriptor tabDescriptor) {
705         if (selectionQueueLocked) {
706             /*
707              * don't send tab selection events for non user changes.
708              */

709             return;
710         }
711         for (Iterator JavaDoc i = tabSelectionListeners.iterator(); i.hasNext();) {
712             ITabSelectionListener listener = (ITabSelectionListener) i.next();
713             listener.tabSelected(tabDescriptor);
714         }
715     }
716
717     /**
718      * Add a tab selection listener.
719      *
720      * @param listener
721      * a tab selection listener.
722      */

723     public void addTabSelectionListener(ITabSelectionListener listener) {
724         tabSelectionListeners.add(listener);
725     }
726
727     /**
728      * Remove a tab selection listener.
729      *
730      * @param listener
731      * a tab selection listener.
732      */

733     public void removeTabSelectionListener(ITabSelectionListener listener) {
734         tabSelectionListeners.remove(listener);
735     }
736
737     /**
738      * Get the widget factory.
739      *
740      * @return the widget factory.
741      */

742     public TabbedPropertySheetWidgetFactory getWidgetFactory() {
743         return widgetFactory;
744     }
745
746     /**
747      * Update the title bar of the contributor has a label provider.
748      */

749     private void refreshTitleBar() {
750         if (hasTitleBar) {
751             TabbedPropertyTitle title = tabbedPropertyComposite.getTitle();
752             if (currentTab == null) {
753                 /**
754                  * No tabs are shown so hide the title bar, otherwise you see
755                  * "No properties available" and a title bar for the selection.
756                  */

757                 title.setTitle(null, null);
758             } else {
759                 String JavaDoc text = registry.getLabelProvider().getText(
760                     currentSelection);
761                 Image image = registry.getLabelProvider().getImage(
762                     currentSelection);
763                 title.setTitle(text, image);
764             }
765         }
766     }
767
768     /**
769      * @see org.eclipse.jface.viewers.ILabelProviderListener#labelProviderChanged(org.eclipse.jface.viewers.LabelProviderChangedEvent)
770      */

771     public void labelProviderChanged(LabelProviderChangedEvent event) {
772         refreshTitleBar();
773     }
774
775     /**
776      * Retrieve the contributor from the selection.
777      *
778      * @param object -
779      * the selected element
780      * @return the TabbedPropertySheetPageContributor or null if not applicable
781      */

782     private ITabbedPropertySheetPageContributor getTabbedPropertySheetPageContributor(
783             Object JavaDoc object) {
784         if (object instanceof ITabbedPropertySheetPageContributor) {
785             return (ITabbedPropertySheetPageContributor) object;
786         }
787
788         if (object instanceof IAdaptable
789             && ((IAdaptable) object)
790                 .getAdapter(ITabbedPropertySheetPageContributor.class) != null) {
791             return (ITabbedPropertySheetPageContributor) (((IAdaptable) object)
792                 .getAdapter(ITabbedPropertySheetPageContributor.class));
793         }
794
795         if (Platform.getAdapterManager().hasAdapter(object,
796             ITabbedPropertySheetPageContributor.class.getName())) {
797             return (ITabbedPropertySheetPageContributor) Platform
798                 .getAdapterManager().loadAdapter(object,
799                     ITabbedPropertySheetPageContributor.class.getName());
800         }
801
802         return null;
803     }
804
805     /**
806      * The workbench part creates this instance of the TabbedPropertySheetPage
807      * and implements ITabbedPropertySheetPageContributor which is unique
808      * contributor id. This unique contributor id is used to load a registry
809      * with the extension point This id matches the registry.
810      * <p>
811      * It is possible for elements in a selection to implement
812      * ITabbedPropertySheetPageContributor to provide a different contributor id
813      * and thus a differenent registry.
814      *
815      * @param selection
816      * the current selection in the active workbench part.
817      */

818     private void validateRegistry(ISelection selection) {
819         if (selection == null) {
820             return;
821         }
822
823         if (!(selection instanceof IStructuredSelection)) {
824             return;
825         }
826
827         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
828         if (structuredSelection.size() == 0) {
829             return;
830         }
831
832         ITabbedPropertySheetPageContributor newContributor = getTabbedPropertySheetPageContributor(structuredSelection.getFirstElement());
833         
834         if (newContributor == null) {
835             /**
836              * selection does not implement or adapt ITabbedPropertySheetPageContributor.
837              */

838             newContributor = contributor;
839         }
840         
841         String JavaDoc selectionContributorId = newContributor.getContributorId();
842         if (selectionContributorId.equals(currentContributorId)) {
843             /**
844              * selection has the same contributor id as current, so leave
845              * existing registry.
846              */

847             return;
848         }
849
850         /**
851          * Selection implements ITabbedPropertySheetPageContributor different
852          * than current contributor id, so make sure all elements implement the
853          * new id. If all contributor id do not match, then fall back to default
854          * contributor from the workbench part.
855          */

856         Iterator JavaDoc i = structuredSelection.iterator();
857         i.next();
858         while (i.hasNext()) {
859             newContributor = getTabbedPropertySheetPageContributor(i.next());
860             if (newContributor == null || !newContributor.getContributorId().equals(selectionContributorId)) {
861                 /**
862                  * fall back to use the default contributor id from the
863                  * workbench part.
864                  */

865                 if (selectionContributor != null) {
866                     disposeContributor();
867                     currentContributorId = contributor.getContributorId();
868                     initContributor(currentContributorId);
869                 }
870                 return;
871             }
872         }
873
874         /**
875          * All the elements in the selection implement a new contributor id, so
876          * use that id.
877          */

878         disposeContributor();
879         currentContributorId = selectionContributorId;
880         initContributor(currentContributorId);
881         overrideActionBars();
882     }
883
884     /**
885      * Override the action bars for the selection based contributor.
886      */

887     private void overrideActionBars() {
888         if (registry.getActionProvider() != null ) {
889             IActionProvider actionProvider = registry.getActionProvider();
890             actionProvider.setActionBars(contributor, getSite().getActionBars());
891         }
892     }
893
894 }
Popular Tags