KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > views > properties > tabbed > view > Tab


1 /*******************************************************************************
2  * Copyright (c) 2001, 2006 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.internal.views.properties.tabbed.view;
12
13 import org.eclipse.core.runtime.ISafeRunnable;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.FillLayout;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.views.properties.tabbed.ISection;
23 import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
24
25 /**
26  * A property tab is composed by one or more property sections and is used to
27  * categorize sections.
28  *
29  * @author Anthony Hunter
30  */

31 public class Tab {
32
33     private ISection[] sections;
34
35     private boolean controlsCreated;
36
37     Tab() {
38         controlsCreated = false;
39     }
40
41     /**
42      * Retrieve a numbered index for the section.
43      * @param section the section.
44      * @return the section index.
45      */

46     public int getSectionIndex(ISection section) {
47         for (int i = 0; i < sections.length; i++) {
48             if (section == sections[i]) {
49                 return i;
50             }
51         }
52         return -1;
53     }
54
55     /**
56      * Retrieve the section at a numbered index.
57      * @param i a numbered index.
58      * @return the section.
59      */

60     public ISection getSectionAtIndex(int i) {
61         if (i >= 0 && i < sections.length) {
62             return sections[i];
63         }
64         return null;
65     }
66
67     /**
68      * Retrieve the sections on the tab.
69      *
70      * @return the sections on the tab.
71      */

72     public ISection[] getSections() {
73         return sections;
74     }
75
76     /**
77      * Creates page's sections controls.
78      *
79      * @param parent
80      * @param page
81      */

82     public void createControls(Composite parent,
83             final TabbedPropertySheetPage page) {
84         Composite pageComposite = page.getWidgetFactory().createComposite(
85             parent, SWT.NO_FOCUS);
86         GridLayout layout = new GridLayout();
87         layout.marginWidth = 0;
88         layout.marginHeight = 0;
89         layout.verticalSpacing = 0;
90         pageComposite.setLayout(layout);
91
92         for (int i = 0; i < sections.length; i++) {
93             final ISection section = sections[i];
94             final Composite sectionComposite = page.getWidgetFactory()
95                 .createComposite(pageComposite, SWT.NO_FOCUS);
96             sectionComposite.setLayout(new FillLayout());
97             int style = (section.shouldUseExtraSpace()) ? GridData.FILL_BOTH
98                 : GridData.FILL_HORIZONTAL;
99             GridData data = new GridData(style);
100             data.heightHint = section.getMinimumHeight();
101             sectionComposite.setLayoutData(data);
102
103             ISafeRunnable runnable = new ISafeRunnable() {
104
105                 public void run()
106                     throws Exception JavaDoc {
107                     section.createControls(sectionComposite, page);
108                 }
109
110                 public void handleException(Throwable JavaDoc exception) {
111                     /* not used */
112                 }
113             };
114             Platform.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()
128                     throws Exception JavaDoc {
129                     section.dispose();
130                 }
131
132                 public void handleException(Throwable JavaDoc exception) {
133                     /* not used */
134                 }
135             };
136             Platform.run(runnable);
137         }
138     }
139
140     /**
141      * Sends the lifecycle event to the page's sections.
142      */

143     public void aboutToBeShown() {
144         for (int i = 0; i < sections.length; i++) {
145             final ISection section = sections[i];
146             ISafeRunnable runnable = new ISafeRunnable() {
147
148                 public void run()
149                     throws Exception JavaDoc {
150                     section.aboutToBeShown();
151                 }
152
153                 public void handleException(Throwable JavaDoc exception) {
154                     /* not used */
155                 }
156             };
157             Platform.run(runnable);
158         }
159     }
160
161     /**
162      * Sends the lifecycle event to the page's sections.
163      */

164     public void aboutToBeHidden() {
165         for (int i = 0; i < sections.length; i++) {
166             final ISection section = sections[i];
167             ISafeRunnable runnable = new ISafeRunnable() {
168
169                 public void run()
170                     throws Exception JavaDoc {
171                     section.aboutToBeHidden();
172                 }
173
174                 public void handleException(Throwable JavaDoc exception) {
175                     /* not used */
176                 }
177             };
178             Platform.run(runnable);
179         }
180     }
181
182     /**
183      * Sets page's sections input objects.
184      *
185      * @param part
186      * @param selection
187      */

188     public void setInput(final IWorkbenchPart part, final ISelection selection) {
189         for (int i = 0; i < sections.length; i++) {
190             final ISection section = sections[i];
191             ISafeRunnable runnable = new ISafeRunnable() {
192
193                 public void run()
194                     throws Exception JavaDoc {
195                     section.setInput(part, selection);
196                 }
197
198                 public void handleException(Throwable JavaDoc throwable) {
199                     throwable.printStackTrace();
200                 }
201             };
202             Platform.run(runnable);
203         }
204     }
205
206     void setSections(ISection[] sections) {
207         this.sections = sections;
208     }
209
210     /**
211      *
212      * @return <code>true</code> if controls have been created.
213      */

214     public boolean controlsHaveBeenCreated() {
215         return controlsCreated;
216     }
217
218     /**
219      * If controls have been created, refresh all sections on the page.
220      */

221     public void refresh() {
222         if (controlsCreated) {
223             for (int i = 0; i < sections.length; i++) {
224                 final ISection section = sections[i];
225                 ISafeRunnable runnable = new ISafeRunnable() {
226
227                     public void run()
228                         throws Exception JavaDoc {
229                         section.refresh();
230                     }
231
232                     public void handleException(Throwable JavaDoc throwable) {
233                         throwable.printStackTrace();
234                     }
235                 };
236                 Platform.run(runnable);
237             }
238         }
239     }
240 }
241
Popular Tags