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.views.properties.tabbed;
011:
012: import org.eclipse.core.runtime.ISafeRunnable;
013: import org.eclipse.jface.util.SafeRunnable;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.layout.FillLayout;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.ui.IWorkbenchPart;
021:
022: /**
023: * A property tab is composed by one or more property sections and is used to
024: * categorize sections.
025: *
026: * @author Anthony Hunter
027: * @since 3.4
028: */
029: public final class TabContents {
030:
031: private ISection[] sections;
032:
033: private boolean controlsCreated;
034:
035: /**
036: *
037: */
038: public TabContents() {
039: controlsCreated = false;
040: }
041:
042: /**
043: * Retrieve a numbered index for the section.
044: * @param section the section.
045: * @return the section index.
046: */
047: public int getSectionIndex(ISection section) {
048: for (int i = 0; i < sections.length; i++) {
049: if (section == sections[i]) {
050: return i;
051: }
052: }
053: return -1;
054: }
055:
056: /**
057: * Retrieve the section at a numbered index.
058: * @param i a numbered index.
059: * @return the section.
060: */
061: public ISection getSectionAtIndex(int i) {
062: if (i >= 0 && i < sections.length) {
063: return sections[i];
064: }
065: return null;
066: }
067:
068: /**
069: * Retrieve the sections on the tab.
070: *
071: * @return the sections on the tab.
072: */
073: public ISection[] getSections() {
074: return sections;
075: }
076:
077: /**
078: * Creates page's sections controls.
079: *
080: * @param parent
081: * @param page
082: */
083: public void createControls(Composite parent,
084: final TabbedPropertySheetPage page) {
085: Composite pageComposite = page.getWidgetFactory()
086: .createComposite(parent, SWT.NO_FOCUS);
087: GridLayout layout = new GridLayout();
088: layout.marginWidth = 0;
089: layout.marginHeight = 0;
090: layout.verticalSpacing = 0;
091: pageComposite.setLayout(layout);
092:
093: for (int i = 0; i < sections.length; i++) {
094: final ISection section = sections[i];
095: final Composite sectionComposite = page.getWidgetFactory()
096: .createComposite(pageComposite, SWT.NO_FOCUS);
097: sectionComposite.setLayout(new FillLayout());
098: int style = (section.shouldUseExtraSpace()) ? GridData.FILL_BOTH
099: : GridData.FILL_HORIZONTAL;
100: GridData data = new GridData(style);
101: data.heightHint = section.getMinimumHeight();
102: sectionComposite.setLayoutData(data);
103:
104: ISafeRunnable runnable = new ISafeRunnable() {
105:
106: public void run() throws Exception {
107: section.createControls(sectionComposite, page);
108: }
109:
110: public void handleException(Throwable exception) {
111: /* not used */
112: }
113: };
114: SafeRunnable.run(runnable);
115: }
116: controlsCreated = true;
117: }
118:
119: /**
120: * Dispose of page's sections controls.
121: */
122: public void dispose() {
123: for (int i = 0; i < sections.length; i++) {
124: final ISection section = sections[i];
125: ISafeRunnable runnable = new ISafeRunnable() {
126:
127: public void run() throws Exception {
128: section.dispose();
129: }
130:
131: public void handleException(Throwable exception) {
132: /* not used */
133: }
134: };
135: SafeRunnable.run(runnable);
136: }
137: }
138:
139: /**
140: * Sends the lifecycle event to the page's sections.
141: */
142: public void aboutToBeShown() {
143: for (int i = 0; i < sections.length; i++) {
144: final ISection section = sections[i];
145: ISafeRunnable runnable = new ISafeRunnable() {
146:
147: public void run() throws Exception {
148: section.aboutToBeShown();
149: }
150:
151: public void handleException(Throwable exception) {
152: /* not used */
153: }
154: };
155: SafeRunnable.run(runnable);
156: }
157: }
158:
159: /**
160: * Sends the lifecycle event to the page's sections.
161: */
162: public void aboutToBeHidden() {
163: for (int i = 0; i < sections.length; i++) {
164: final ISection section = sections[i];
165: ISafeRunnable runnable = new ISafeRunnable() {
166:
167: public void run() throws Exception {
168: section.aboutToBeHidden();
169: }
170:
171: public void handleException(Throwable exception) {
172: /* not used */
173: }
174: };
175: SafeRunnable.run(runnable);
176: }
177: }
178:
179: /**
180: * Sets page's sections input objects.
181: *
182: * @param part
183: * @param selection
184: */
185: public void setInput(final IWorkbenchPart part,
186: final ISelection selection) {
187: for (int i = 0; i < sections.length; i++) {
188: final ISection section = sections[i];
189: ISafeRunnable runnable = new ISafeRunnable() {
190:
191: public void run() throws Exception {
192: section.setInput(part, selection);
193: }
194:
195: public void handleException(Throwable throwable) {
196: throwable.printStackTrace();
197: }
198: };
199: SafeRunnable.run(runnable);
200: }
201: }
202:
203: /**
204: * Set the sections for the tab.
205: *
206: * @param sections the sections for the tab.
207: */
208: public void setSections(ISection[] sections) {
209: this .sections = sections;
210: }
211:
212: /**
213: * Determine if the controls have been created.
214: *
215: * @return <code>true</code> if controls have been created.
216: */
217: public boolean controlsHaveBeenCreated() {
218: return controlsCreated;
219: }
220:
221: /**
222: * If controls have been created, refresh all sections on the page.
223: */
224: public void refresh() {
225: if (controlsCreated) {
226: for (int i = 0; i < sections.length; i++) {
227: final ISection section = sections[i];
228: ISafeRunnable runnable = new ISafeRunnable() {
229:
230: public void run() throws Exception {
231: section.refresh();
232: }
233:
234: public void handleException(Throwable throwable) {
235: throwable.printStackTrace();
236: }
237: };
238: SafeRunnable.run(runnable);
239: }
240: }
241: }
242: }
|