001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.views.properties.tabbed.override.tablist;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.ui.IWorkbenchPart;
016: import org.eclipse.ui.tests.views.properties.tabbed.override.OverrideTestsSelection;
017: import org.eclipse.ui.tests.views.properties.tabbed.override.OverrideTestsView;
018: import org.eclipse.ui.tests.views.properties.tabbed.model.Element;
019: import org.eclipse.ui.views.properties.tabbed.ISection;
020: import org.eclipse.ui.views.properties.tabbed.ITabItem;
021: import org.eclipse.ui.views.properties.tabbed.TabContents;
022: import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
023:
024: /**
025: * The content manager for the override tests example for the tabbed property
026: * sheet page.
027: * <p>
028: * The OverrideTestsView TabbedPropertySheetPage example is a look at the
029: * properties view after the migration of a TabFolder/TabItem framework to the
030: * tabbed properties view. In the case of a TabFolder, the folder (provider)
031: * knows both the tab labels and tab items. This aligns to the tabbed properties
032: * view, but the tab labels are tab descriptors and tab items are section
033: * descriptions. This does not work with the default framework as the tabs
034: * provide the sections. In this case, the IOverridableTabListContentProvider
035: * framework has been provided.
036: * <p>
037: * The overridable tab list is a content provider that provides both the
038: * sections and the tab labels.
039: *
040: * @author Anthony Hunter
041: * @since 3.4
042: */
043: public class OverrideTestsTabListsContentsManager {
044:
045: private Element activeElement;
046:
047: private IOverrideTestsTabList activeFolder;
048:
049: private ITabItem[] activeTabs;
050:
051: private Composite composite;
052:
053: private IOverrideTestsTabList emptyFolder;
054:
055: private IOverrideTestsTabList[] folders;
056:
057: private boolean processingUpdateTabs;
058:
059: private OverrideTestsView sampleView;
060:
061: private ISection section;
062:
063: private TabbedPropertySheetPage tabbedPropertySheetPage;
064:
065: public OverrideTestsTabListsContentsManager(Composite aComposite,
066: TabbedPropertySheetPage aTabbedPropertySheetPage,
067: ISection aSection) {
068: this .composite = aComposite;
069: this .section = aSection;
070: this .tabbedPropertySheetPage = aTabbedPropertySheetPage;
071: this .folders = new IOverrideTestsTabList[] {
072: new BasicTabList(), new AdvancedTabList() };
073: this .emptyFolder = new EmptyTabList();
074: }
075:
076: private boolean compareTab(OverrideTestsTabItem a,
077: OverrideTestsTabItem b) {
078: return a.getItem().equals(b.getItem())
079: && a.isSelected() == b.isSelected()
080: && a.getText().equals(b.getText());
081: }
082:
083: private boolean compareTabs(ITabItem[] a, ITabItem[] b) {
084: if (a != null && b != null) {
085: if (a.length != b.length) {
086: return false;
087: }
088: for (int i = 0; i < a.length; i++) {
089: if (!compareTab((OverrideTestsTabItem) a[i],
090: (OverrideTestsTabItem) b[i])) {
091: return false;
092: }
093: }
094: return true;
095: }
096: return a == null && b == null;
097: }
098:
099: public void dispose() {
100: if (activeFolder != null) {
101: activeFolder.dispose();
102: }
103: }
104:
105: public ITabItem[] getTabs() {
106: if (activeFolder != null) {
107: return activeFolder.getTabs();
108: }
109: return new ITabItem[] {};
110: }
111:
112: public void selectionChanged(IWorkbenchPart part,
113: ISelection selection) {
114: if (composite.isDisposed()) {
115: return;
116: }
117: if (processingUpdateTabs) {
118: return;
119: }
120: if (!validateSection()) {
121: return;
122: }
123: Assert.isTrue(part instanceof OverrideTestsView);
124: this .sampleView = (OverrideTestsView) part;
125: Assert.isTrue(selection instanceof OverrideTestsSelection);
126: Element newElement = ((OverrideTestsSelection) selection)
127: .getElement();
128: IOverrideTestsTabList newFolder = null;
129:
130: if (newElement == null) {
131: newFolder = emptyFolder;
132: } else {
133: for (int i = 0; i < folders.length; i++) {
134: if (folders[i].appliesTo(newElement)) {
135: newFolder = folders[i];
136: break;
137: }
138: }
139: }
140:
141: if (newFolder != activeFolder) {
142: if (activeFolder != null) {
143: activeFolder.dispose();
144: activeFolder = null;
145: }
146: activeFolder = newFolder;
147: if (newElement != null) {
148: activeFolder.selectionChanged(newElement);
149: }
150: newFolder.createControls(composite);
151: composite.layout(true);
152: } else if (newElement != activeElement) {
153: activeFolder.dispose();
154: if (newElement != null) {
155: activeFolder.selectionChanged(newElement);
156: }
157: activeFolder.createControls(composite);
158: composite.layout(true);
159: activeElement = newElement;
160: return;
161: }
162: ITabItem[] newTabs = activeFolder.getTabs();
163: if (!compareTabs(newTabs, activeTabs)) {
164: processingUpdateTabs = true;
165: tabbedPropertySheetPage.overrideTabs();
166: processingUpdateTabs = false;
167: activeTabs = newTabs;
168: }
169:
170: }
171:
172: public void selectTab(int index) {
173: if (activeTabs == null) {
174: return;
175: }
176: OverrideTestsTabItem selectedTab = (OverrideTestsTabItem) activeTabs[index];
177: sampleView.setSelection(selectedTab.getItem().getElement());
178:
179: OverrideTestsSelection selection = (OverrideTestsSelection) sampleView
180: .getSelection();
181: Element newElement = selection.getElement();
182:
183: activeFolder.dispose();
184: activeFolder.selectionChanged(newElement);
185: activeFolder.createControls(composite);
186: composite.layout(true);
187: activeElement = newElement;
188: activeTabs = getTabs();
189: }
190:
191: /**
192: * Returns true if the section is valid.
193: */
194: private boolean validateSection() {
195: TabContents tab = tabbedPropertySheetPage.getCurrentTab();
196: if (tab != null) {
197: ISection[] sections = tab.getSections();
198: if (sections != null) {
199: for (int i = 0; i < sections.length; i++) {
200: if (sections[i] == section) {
201: return true;
202: }
203: }
204: }
205: }
206: return false;
207: }
208: }
|