Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5e62f75f8b5f47552d52768b000dd3d8950c3e68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*******************************************************************************
 * Copyright (c) 2001, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Obeo - Contribution in the project EEF
 *******************************************************************************/
package org.eclipse.eef.properties.ui.api;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.eef.properties.ui.internal.page.EEFPartListenerAdapter;
import org.eclipse.eef.properties.ui.internal.page.EEFTabbedPropertyComposite;
import org.eclipse.eef.properties.ui.internal.page.EEFTabbedPropertyListContentProvider;
import org.eclipse.eef.properties.ui.internal.page.EEFTabbedPropertySheetPageLabelProvider;
import org.eclipse.eef.properties.ui.internal.page.EEFTabbedPropertyViewer;
import org.eclipse.eef.properties.ui.internal.registry.EEFTabbedPropertyRegistry;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.part.IContributedContentsView;
import org.eclipse.ui.part.Page;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.PropertySheet;

/**
 * A property sheet page that provides a tabbed UI.
 *
 * @author Anthony Hunter
 * @author Stephane Begaudeau
 * @since 1.6.0
 */
public class EEFTabbedPropertySheetPage extends Page implements IPropertySheetPage {

	/**
	 * The widget factory.
	 */
	private EEFTabbedPropertySheetWidgetFactory widgetFactory;

	/**
	 * The composite holding all the widgets.
	 */
	private EEFTabbedPropertyComposite tabbedPropertyComposite;

	/**
	 * The viewer.
	 */
	private EEFTabbedPropertyViewer tabbedPropertyViewer;

	/**
	 * The workbench part requesting the use of the property page.
	 */
	private IEEFTabbedPropertySheetPageContributor contributor;

	/**
	 * The content provider of the viewer.
	 */
	private EEFTabbedPropertyListContentProvider contentProvider;

	/**
	 * The currently active tab.
	 */
	private EEFTabContents currentTab;

	/**
	 * The map of the tab descriptor to their instantiated tab.
	 */
	private Map<IEEFTabDescriptor, EEFTabContents> descriptorToTab = new HashMap<IEEFTabDescriptor, EEFTabContents>();

	/**
	 * The map of the tab to their main composite.
	 */
	private Map<EEFTabContents, Composite> tabToComposite = new HashMap<EEFTabContents, Composite>();

	/**
	 * This queue will contain the list of the label of the selected tabs.
	 */
	private List<String> selectionQueue = new ArrayList<String>();

	/**
	 * Indicates that the selection queue is locked by the user.
	 */
	private boolean selectionQueueLocked;

	/**
	 * The tab selection listener.
	 */
	private List<IEEFTabSelectionListener> tabSelectionListeners = new ArrayList<IEEFTabSelectionListener>();

	/**
	 * The workbench window.
	 */
	private IWorkbenchWindow cachedWorkbenchWindow;

	/**
	 * The part activation listener used to manage a part of the lifecycle of the property sheet page.
	 */
	private IPartListener partActivationListener;

	/**
	 * Indicates if we should activate the property sheet.
	 */
	private boolean activePropertySheet;

	/**
	 * The current selection.
	 */
	private ISelection currentSelection;

	/**
	 * The registry.
	 */
	private EEFTabbedPropertyRegistry registry = EEFTabbedPropertyRegistry.getDefault();

	/**
	 * The form used to contain the all the widgets.
	 */
	private Form form;

	/**
	 * Wrapper for contributors who want to use this version of the framework but can not have a hard dependency (via
	 * inheritance) towards IEEFTabbedPropertySheetPageContributor.
	 *
	 * @author pcdavid
	 */
	private static class ContributorWrapper implements IEEFTabbedPropertySheetPageContributor {
		/**
		 * The original contributor object.
		 */
		private final Object realContributor;
		/**
		 * The contributor id.
		 */
		private final String contributorId;

		/**
		 * Creates a wrapper.
		 *
		 * @param realContributor
		 *            the original contributor object.
		 * @param contributorId
		 *            the contributor id.
		 */
		public ContributorWrapper(Object realContributor, String contributorId) {
			this.realContributor = realContributor;
			this.contributorId = contributorId;
		}

		@Override
		public String getContributorId() {
			return contributorId;
		}

		/**
		 * Return the original (wrapped) Contributor.
		 *
		 * @return the original (wrapped) Contributor.
		 */
		public Object getRealContributor() {
			return this.realContributor;
		}
	}

	/**
	 * The constructor.
	 *
	 * @param contributor
	 *            the contributor.
	 * @param contributorId
	 *            the contributor id.
	 */
	public EEFTabbedPropertySheetPage(Object contributor, String contributorId) {
		if (contributor instanceof IEEFTabbedPropertySheetPageContributor) {
			this.contributor = (IEEFTabbedPropertySheetPageContributor) contributor;
		} else {
			this.contributor = new ContributorWrapper(contributor, contributorId);
		}
		this.initContributor(this.contributor.getContributorId());
	}

	/**
	 * Initialize the contributor with the provided contributor id.
	 *
	 * @param contributorId
	 *            The contributor id
	 */
	private void initContributor(String contributorId) {
		this.contentProvider = new EEFTabbedPropertyListContentProvider(this.registry);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		this.widgetFactory = new EEFTabbedPropertySheetWidgetFactory();

		this.form = this.widgetFactory.createForm(parent);
		form.setText("HAL 9000"); //$NON-NLS-1$
		this.widgetFactory.decorateFormHeading(form);

		form.getBody().setLayout(new FormLayout());

		this.tabbedPropertyComposite = new EEFTabbedPropertyComposite(form.getBody(), this.widgetFactory);
		this.widgetFactory.paintBordersFor(this.tabbedPropertyComposite);
		this.tabbedPropertyComposite.setLayout(new FormLayout());

		FormData formData = new FormData();
		formData.left = new FormAttachment(0, 0);
		formData.right = new FormAttachment(100, 0);
		formData.top = new FormAttachment(0, 0);
		formData.bottom = new FormAttachment(100, 0);
		this.tabbedPropertyComposite.setLayoutData(formData);

		this.form.setLayoutData(formData);
		this.widgetFactory.paintBordersFor(form);

		this.tabbedPropertyViewer = new EEFTabbedPropertyViewer(this.tabbedPropertyComposite.getTabbedPropertyList());
		this.tabbedPropertyViewer.setContentProvider(this.contentProvider);
		this.tabbedPropertyViewer.setLabelProvider(new EEFTabbedPropertySheetPageLabelProvider());
		this.tabbedPropertyViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				EEFTabbedPropertySheetPage.this.processSelectionChanged(event);
			}
		});

		this.tabbedPropertyComposite.getScrolledComposite().addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				EEFTabbedPropertySheetPage.this.resizeScrolledComposite();
			}
		});

		this.partActivationListener = new EEFPartListenerAdapter() {
			@Override
			public void partOpened(IWorkbenchPart part) {
				EEFTabbedPropertySheetPage.this.handlePartActivated(part);
			}
		};

		this.cachedWorkbenchWindow = this.getSite().getWorkbenchWindow();
		this.cachedWorkbenchWindow.getPartService().addPartListener(this.partActivationListener);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		this.setInput(part, selection);
	}

	/**
	 * Set the input of the page.
	 *
	 * @param part
	 *            The current workbench part
	 * @param selection
	 *            The current selection
	 */
	private void setInput(IWorkbenchPart part, ISelection selection) {
		if (selection.equals(currentSelection)) {
			return;
		}

		this.currentSelection = selection;

		// see if the selection provides a new contributor
		// validateRegistry(selection);
		List<IEEFTabDescriptor> descriptors = EEFTabbedPropertyRegistry.getDefault().getTabDescriptors(part, selection);
		// If there are no descriptors for the given input we do not need to
		// touch the tab objects. We might reuse them for the next valid
		// input.
		if (descriptors.size() > 0) {
			this.updateTabs(descriptors);
		}
		// update tabs list
		this.tabbedPropertyViewer.setInput(part, currentSelection);
		int lastTabSelectionIndex = this.getLastTabSelection(part, currentSelection);
		Object selectedTab = this.tabbedPropertyViewer.getElementAt(lastTabSelectionIndex);
		this.selectionQueueLocked = true;
		try {
			if (selectedTab == null) {
				this.tabbedPropertyViewer.setSelection(null);
			} else {
				this.tabbedPropertyViewer.setSelection(new StructuredSelection(selectedTab));
			}
		} finally {
			this.selectionQueueLocked = false;
		}
	}

	/**
	 * Update the current tabs to represent the given input object. When tabs apply for both the old and new input they
	 * are reused otherwise they are disposed. If the current visible tab will not be reused (i.e. will be disposed) we
	 * have to send it an aboutToBeHidden() message.
	 *
	 * @param descriptors
	 *            The tab descriptors.
	 */
	protected void updateTabs(List<IEEFTabDescriptor> descriptors) {
		Map<IEEFTabDescriptor, EEFTabContents> newTabs = new HashMap<IEEFTabDescriptor, EEFTabContents>(descriptors.size() * 2);
		boolean disposingCurrentTab = this.currentTab != null;

		for (IEEFTabDescriptor descriptor : descriptors) {
			EEFTabContents tab = this.descriptorToTab.remove(descriptor);
			if (tab != null && tab.controlsHaveBeenCreated()) {
				if (tab == this.currentTab) {
					disposingCurrentTab = false;
				}
			} else {
				tab = this.createTab(descriptor);
			}

			newTabs.put(descriptor, tab);

		}

		if (disposingCurrentTab) {
			/**
			 * If the current tab is about to be disposed we have to call aboutToBeHidden
			 */
			this.currentTab.aboutToBeHidden();
			this.currentTab = null;
		}
		this.disposeTabs(this.descriptorToTab.values());
		this.descriptorToTab = newTabs;
	}

	/**
	 * Create the tab contents for the provided tab descriptor.
	 *
	 * @param tabDescriptor
	 *            the tab descriptor.
	 * @return the tab contents.
	 */
	protected EEFTabContents createTab(IEEFTabDescriptor tabDescriptor) {
		return tabDescriptor.createTab();
	}

	/**
	 * Returns the last known selected tab for the given input.
	 *
	 * @param part
	 *            The current workbench part
	 * @param input
	 *            The current selection
	 * @return The index of the currently selected tab (0, the first tab, by default)
	 */
	private int getLastTabSelection(IWorkbenchPart part, ISelection input) {
		List<IEEFTabDescriptor> descriptors = registry.getTabDescriptors(part, input);
		if (descriptors.size() != 0) {
			for (String text : this.selectionQueue) {
				int i = 0;
				for (IEEFTabDescriptor descriptor : descriptors) {
					if (text.equals(descriptor.getLabel())) {
						return i;
					}

					i = i + 1;
				}
			}
		}
		return 0;
	}

	/**
	 * Disposes the TabContents objects passed to this method. If the 'currentTab' is going to be disposed, then the
	 * caller should call aboutToBeHidden() on the currentTab and set it to null before calling this method. Also, the
	 * caller needs to ensure that descriptorToTab map entries corresponding to the disposed TabContents objects are
	 * also removed.
	 *
	 * @param tabs
	 *            The list of tabs
	 */
	protected void disposeTabs(Collection<EEFTabContents> tabs) {
		for (EEFTabContents tab : tabs) {
			Composite composite = tabToComposite.remove(tab);
			tab.dispose();
			if (composite != null) {
				composite.dispose();
			}
		}
	}

	/**
	 * Handle the part activated event.
	 *
	 * @param part
	 *            the new activated part.
	 */
	private void handlePartActivated(IWorkbenchPart part) {
		/*
		 * The properties view has been activated and the current page is this instance of TabbedPropertySheetPage
		 */
		boolean thisActivated = part instanceof PropertySheet && ((PropertySheet) part).getCurrentPage() == this;

		/*
		 * When the active part changes and the part does not provide a selection that affects this property sheet page,
		 * the PropertySheet does not send us a selectionChanged() event. We need to be informed of these events since
		 * we want to send aboutToBeHidden() and aboutToBeShown() when the property sheet is hidden or shown.
		 */
		IContributedContentsView view = null;
		if (!thisActivated && !matchesContributor(part) && !part.getSite().getId().equals(contributor.getContributorId())) {
			/*
			 * Is the part is a IContributedContentsView for the contributor, for example, outline view.
			 */
			view = part.getAdapter(IContributedContentsView.class);
		}

		if (view == null || (view.getContributingPart() != null && !view.getContributingPart().equals(contributor))) {
			if (this.activePropertySheet) {
				if (currentTab != null) {
					currentTab.aboutToBeHidden();
				}
				this.activePropertySheet = false;
			}
			return;
		}

		if (!this.activePropertySheet && currentTab != null) {
			currentTab.aboutToBeShown();
			currentTab.refresh();
		}
		this.activePropertySheet = true;
	}

	/**
	 * Tests whether the specified part is the same as the contributor we represent.
	 *
	 * @param part
	 *            a workbench part.
	 * @return <code>true</code> if the specified part is the same as the contributor we represent.
	 */
	private boolean matchesContributor(IWorkbenchPart part) {
		if (contributor instanceof ContributorWrapper) {
			ContributorWrapper wrapper = (ContributorWrapper) contributor;
			return part.equals(wrapper.getRealContributor());
		} else {
			return part.equals(contributor);
		}
	}

	/**
	 * Handle the selection changed event.
	 *
	 * @param event
	 *            The selection changed event
	 */
	private void processSelectionChanged(SelectionChangedEvent event) {
		if (event.getSelection() instanceof IStructuredSelection
				&& ((IStructuredSelection) event.getSelection()).getFirstElement() instanceof IEEFTabDescriptor) {
			IEEFTabDescriptor descriptor = (IEEFTabDescriptor) ((IStructuredSelection) event.getSelection()).getFirstElement();
			EEFTabContents tab = null;

			if (descriptor == null) {
				// pretend the tab is empty.
				this.hideTab(this.currentTab);
			} else {
				// create tab if necessary
				// can not cache based on the id - tabs may have the same id,
				// but different section depending on the selection
				tab = this.descriptorToTab.get(descriptor);

				if (tab != this.currentTab) {
					this.hideTab(this.currentTab);
				}

				Composite tabComposite = this.tabToComposite.get(tab);
				if (tabComposite == null) {
					tabComposite = this.createTabComposite();
					tab.createControls(tabComposite, this);
					// tabAreaComposite.layout(true);
					this.tabToComposite.put(tab, tabComposite);
				}
				// force widgets to be resized
				tab.setInput(tabbedPropertyViewer.getWorkbenchPart(), (ISelection) tabbedPropertyViewer.getInput());

				// store tab selection
				this.storeCurrentTabSelection(descriptor.getLabel());

				if (tab != this.currentTab) {
					this.showTab(tab);
				}

				tab.refresh();
			}

			// TODO this should be removed
			form.getMessageManager().addMessage("key", "I am sorry Dave. I'm afraid I can't do that", "data", IMessageProvider.WARNING); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

			tabbedPropertyComposite.getTabComposite().layout(true);

			this.currentTab = tab;
			this.resizeScrolledComposite();

			if (descriptor != null) {
				this.handleTabSelection(descriptor);
			}
		}
	}

	/**
	 * Show the given tab.
	 *
	 * @param tab
	 *            The tab to show
	 */
	private void showTab(EEFTabContents tab) {
		if (tab != null) {
			Composite tabComposite = tabToComposite.get(tab);
			if (tabComposite != null) {
				/**
				 * the following method call order is important - do not change it or the widgets might be drawn
				 * incorrectly
				 */
				tabComposite.moveAbove(null);
				tab.aboutToBeShown();
				tabComposite.setVisible(true);
			}
		}
	}

	/**
	 * Hide the given tab.
	 *
	 * @param tab
	 *            The tab to hide
	 */
	private void hideTab(EEFTabContents tab) {
		if (tab != null) {
			Composite tabComposite = tabToComposite.get(tab);
			if (tabComposite != null) {
				tab.aboutToBeHidden();
				tabComposite.setVisible(false);
			}
		}
	}

	/**
	 * Create the composite for the tab.
	 *
	 * @return The composite to use for the new tab
	 */
	private Composite createTabComposite() {
		Composite result = this.widgetFactory.createComposite(tabbedPropertyComposite.getTabComposite(), SWT.NO_FOCUS);
		result.setVisible(false);
		result.setLayout(new FillLayout());
		FormData data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		result.setLayoutData(data);
		return result;
	}

	/**
	 * Stores the current tab label in the selection queue. Tab labels are used to carry the tab context from one input
	 * object to another. The queue specifies the selection priority. So if the first tab in the queue is not available
	 * for the input we try the second tab and so on. If none of the tabs are available we default to the first tab
	 * available for the input.
	 *
	 * @param label
	 *            The label of the currently selected tab.
	 */
	private void storeCurrentTabSelection(String label) {
		if (!selectionQueueLocked) {
			selectionQueue.remove(label);
			selectionQueue.add(0, label);
		}
	}

	/**
	 * Handle the tab selection.
	 *
	 * @param descriptor
	 *            The tab descriptor
	 */
	private void handleTabSelection(IEEFTabDescriptor descriptor) {
		if (selectionQueueLocked) {
			/*
			 * don't send tab selection events for non user changes.
			 */
			return;
		}
		for (IEEFTabSelectionListener listener : this.tabSelectionListeners) {
			listener.tabSelected(descriptor);
		}
	}

	/**
	 * Add a tab selection listener.
	 *
	 * @param listener
	 *            a tab selection listener.
	 */
	public void addTabSelectionListener(IEEFTabSelectionListener listener) {
		tabSelectionListeners.add(listener);
	}

	/**
	 * Remove a tab selection listener.
	 *
	 * @param listener
	 *            a tab selection listener.
	 */
	public void removeTabSelectionListener(IEEFTabSelectionListener listener) {
		tabSelectionListeners.remove(listener);
	}

	/**
	 * Resize the scrolled composite.
	 */
	public void resizeScrolledComposite() {
		Point currentTabSize = new Point(0, 0);
		if (this.currentTab != null) {
			Composite sizeReference = this.tabToComposite.get(this.currentTab);
			if (sizeReference != null) {
				currentTabSize = sizeReference.computeSize(SWT.DEFAULT, SWT.DEFAULT);
			}
		}
		this.tabbedPropertyComposite.getScrolledComposite().setMinSize(currentTabSize);

		ScrollBar verticalScrollBar = this.tabbedPropertyComposite.getScrolledComposite().getVerticalBar();
		if (verticalScrollBar != null) {
			Rectangle clientArea = this.tabbedPropertyComposite.getScrolledComposite().getClientArea();
			int increment = clientArea.height - 5;
			verticalScrollBar.setPageIncrement(increment);
		}

		ScrollBar horizontalScrollBar = this.tabbedPropertyComposite.getScrolledComposite().getHorizontalBar();
		if (horizontalScrollBar != null) {
			Rectangle clientArea = this.tabbedPropertyComposite.getScrolledComposite().getClientArea();
			int increment = clientArea.width - 5;
			horizontalScrollBar.setPageIncrement(increment);
		}
	}

	/**
	 * Set the currently selected tab to be that of the provided tab id.
	 *
	 * @param id
	 *            The string id of the tab to select.
	 */
	public void setSelectedTab(String id) {
		List<IEEFTabDescriptor> elements = this.tabbedPropertyViewer.getElements();
		for (IEEFTabDescriptor descriptor : elements) {
			if (descriptor.getId() != null && descriptor.getId().equals(id)) {
				this.tabbedPropertyViewer.setSelection(new StructuredSelection(descriptor), true);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.part.IPage#dispose()
	 */
	@Override
	public void dispose() {
		this.disposeContributor();
		this.widgetFactory.dispose();
	}

	/**
	 * Dispose the contributor with the provided contributor id. This happens on part close as well as when contributors
	 * switch between the workbench part and contributor from a selection.
	 */
	private void disposeContributor() {
		if (this.currentTab != null) {
			this.currentTab.aboutToBeHidden();
		}

		Collection<EEFTabContents> tabs = this.descriptorToTab.values();
		for (EEFTabContents tab : tabs) {
			Composite composite = this.tabToComposite.remove(tab);
			tab.dispose();
			if (composite != null) {
				composite.dispose();
			}
		}
		this.descriptorToTab = new HashMap<IEEFTabDescriptor, EEFTabContents>();
	}

	/**
	 * Returns the list of currently active tabs.
	 *
	 * @return the currently active tabs.
	 */
	public List<IEEFTabDescriptor> getActiveTabs() {
		return this.tabbedPropertyViewer.getElements();
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.part.IPage#getControl()
	 */
	@Override
	public Control getControl() {
		return this.form;
	}

	/**
	 * Return the currentTab.
	 *
	 * @return the currentTab
	 */
	public EEFTabContents getCurrentTab() {
		return this.currentTab;
	}

	/**
	 * Returns the currently selected tab.
	 *
	 * @return the currently selected tab or <code>null</code> if there is no tab selected.
	 */
	public IEEFTabDescriptor getSelectedTab() {
		int selectedTab = tabbedPropertyViewer.getSelectionIndex();
		if (selectedTab != -1) {
			return tabbedPropertyViewer.getElementAt(selectedTab);
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.part.IPage#setActionBars(org.eclipse.ui.IActionBars)
	 */
	@Override
	public void setActionBars(IActionBars actionBars) {
		// do nothing for now
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.ui.part.IPage#setFocus()
	 */
	@Override
	public void setFocus() {
		this.getControl().setFocus();
	}

	/**
	 * Return the widgetFactory.
	 *
	 * @return the widgetFactory
	 */
	public EEFTabbedPropertySheetWidgetFactory getWidgetFactory() {
		return this.widgetFactory;
	}

	/**
	 * Refresh the currently active tab.
	 */
	public void refresh() {
		this.currentTab.refresh();
	}

	/**
	 * Return the contributor.
	 *
	 * @return the contributor
	 */
	public IEEFTabbedPropertySheetPageContributor getContributor() {
		return this.contributor;
	}

}

Back to the top