KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > ScrolledPageBook


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.forms.widgets;
12 import java.util.Hashtable JavaDoc;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.*;
17 import org.eclipse.ui.internal.forms.widgets.WrappedPageBook;
18 /**
19  * ScrolledPageBook is a class that is capable of stacking several composites
20  * (pages), while showing one at a time. The content is scrolled if there is
21  * not enough space to fit it in the client area.
22  *
23  * @since 3.0
24  */

25 public class ScrolledPageBook extends SharedScrolledComposite {
26     private WrappedPageBook pageBook;
27     private Hashtable JavaDoc pages;
28     private Composite emptyPage;
29     private Control currentPage;
30     /**
31      * Creates a new instance in the provided parent
32      *
33      * @param parent
34      */

35     public ScrolledPageBook(Composite parent) {
36         this(parent, SWT.H_SCROLL | SWT.V_SCROLL);
37     }
38     /**
39      * Creates a new instance in the provided parent and with the provided
40      * style.
41      *
42      * @param parent
43      * the control parent
44      * @param style
45      * the style to use
46      */

47     public ScrolledPageBook(Composite parent, int style) {
48         super(parent, style);
49         pageBook = new WrappedPageBook(this, SWT.NULL);
50         setContent(pageBook);
51         pages = new Hashtable JavaDoc();
52         setExpandHorizontal(true);
53         setExpandVertical(true);
54         this.addListener(SWT.Traverse, new Listener() {
55             public void handleEvent(Event e) {
56                 switch (e.detail) {
57                     case SWT.TRAVERSE_ESCAPE :
58                     case SWT.TRAVERSE_RETURN :
59                     case SWT.TRAVERSE_TAB_NEXT :
60                     case SWT.TRAVERSE_TAB_PREVIOUS :
61                         e.doit = true;
62                         break;
63                 }
64             }
65         });
66     }
67     /**
68      * Removes the default size of the composite, allowing the control to
69      * shrink to the trim.
70      *
71      * @param wHint
72      * the width hint
73      * @param hHint
74      * the height hint
75      * @param changed
76      * if <code>true</code>, do not use cached values
77      */

78     public Point computeSize(int wHint, int hHint, boolean changed) {
79         Rectangle trim = computeTrim(0, 0, 10, 10);
80         return new Point(trim.width, trim.height);
81     }
82     /**
83      * Tests if the page under the provided key is currently in the book.
84      *
85      * @param key
86      * the page key
87      * @return <code>true</code> if page exists, <code>false</code>
88      * otherwise.
89      */

90     public boolean hasPage(Object JavaDoc key) {
91         return pages.containsKey(key);
92     }
93     /**
94      * Creates a new page for the provided key. Use the returned composite to
95      * create children in it.
96      *
97      * @param key
98      * the page key
99      * @return the newly created page composite
100      */

101     public Composite createPage(Object JavaDoc key) {
102         Composite page = createPage();
103         pages.put(key, page);
104         return page;
105     }
106     /**
107      * Returns the page book container.
108      *
109      * @return the page book container
110      */

111     public Composite getContainer() {
112         return pageBook;
113     }
114     /**
115      * Registers a page under the privided key to be managed by the page book.
116      * The page must be a direct child of the page book container.
117      *
118      * @param key
119      * the page key
120      * @param page
121      * the page composite to register
122      * @see #createPage(Object)
123      * @see #getContainer
124      */

125     public void registerPage(Object JavaDoc key, Control page) {
126         pages.put(key, page);
127     }
128     /**
129      * Removes the page under the provided key from the page book. Does nothing
130      * if page with that key does not exist.
131      *
132      * @param key
133      * the page key.
134      */

135     public void removePage(Object JavaDoc key) {
136         removePage(key, true);
137     }
138     /**
139      * Removes the page under the provided key from the page book. Does nothing
140      * if page with that key does not exist.
141      *
142      * @param key
143      * the page key.
144      * @param showEmptyPage
145      * if <code>true</code>, shows the empty page
146      * after page removal.
147      */

148     public void removePage(Object JavaDoc key, boolean showEmptyPage) {
149         Control page = (Control) pages.get(key);
150         if (page != null) {
151             pages.remove(key);
152             page.dispose();
153             if (showEmptyPage)
154                 showEmptyPage();
155         }
156     }
157     /**
158      * Shows the page with the provided key and hides the page previously
159      * showing. Does nothing if the page with that key does not exist.
160      *
161      * @param key
162      * the page key
163      */

164     public void showPage(Object JavaDoc key) {
165         Control page = (Control) pages.get(key);
166         if (page != null) {
167             pageBook.showPage(page);
168             if (currentPage != null && currentPage != page) {
169                 // switching pages - force layout
170
if (page instanceof Composite)
171                     ((Composite) page).layout(false);
172             }
173             currentPage = page;
174         } else {
175             showEmptyPage();
176         }
177         reflow(true);
178     }
179     /**
180      * Shows a page with no children to be used if the desire is to not show
181      * any registered page.
182      */

183     public void showEmptyPage() {
184         if (emptyPage == null) {
185             emptyPage = createPage();
186             emptyPage.setLayout(new GridLayout());
187         }
188         pageBook.showPage(emptyPage);
189         currentPage = emptyPage;
190         reflow(true);
191     }
192     /**
193      * Sets focus on the current page if shown.
194      */

195     public boolean setFocus() {
196         if (currentPage != null)
197             return currentPage.setFocus();
198         return super.setFocus();
199     }
200     /**
201      * Returns the page currently showing.
202      *
203      * @return the current page
204      */

205     public Control getCurrentPage() {
206         return currentPage;
207     }
208     private Composite createPage() {
209         Composite page = new LayoutComposite(pageBook, SWT.NULL);
210         page.setBackground(getBackground());
211         page.setForeground(getForeground());
212         page.setMenu(pageBook.getMenu());
213         return page;
214     }
215 }
216
Popular Tags