001: /*******************************************************************************
002: * Copyright (c) 2000, 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.forms.widgets;
011:
012: import java.util.Hashtable;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.graphics.*;
015: import org.eclipse.swt.layout.GridLayout;
016: import org.eclipse.swt.widgets.*;
017: import org.eclipse.ui.internal.forms.widgets.WrappedPageBook;
018:
019: /**
020: * ScrolledPageBook is a class that is capable of stacking several composites
021: * (pages), while showing one at a time. The content is scrolled if there is
022: * not enough space to fit it in the client area.
023: *
024: * @since 3.0
025: */
026: public class ScrolledPageBook extends SharedScrolledComposite {
027: private WrappedPageBook pageBook;
028: private Hashtable pages;
029: private Composite emptyPage;
030: private Control currentPage;
031:
032: /**
033: * Creates a new instance in the provided parent
034: *
035: * @param parent
036: */
037: public ScrolledPageBook(Composite parent) {
038: this (parent, SWT.H_SCROLL | SWT.V_SCROLL);
039: }
040:
041: /**
042: * Creates a new instance in the provided parent and with the provided
043: * style.
044: *
045: * @param parent
046: * the control parent
047: * @param style
048: * the style to use
049: */
050: public ScrolledPageBook(Composite parent, int style) {
051: super (parent, style);
052: pageBook = new WrappedPageBook(this , SWT.NULL);
053: setContent(pageBook);
054: pages = new Hashtable();
055: setExpandHorizontal(true);
056: setExpandVertical(true);
057: this .addListener(SWT.Traverse, new Listener() {
058: public void handleEvent(Event e) {
059: switch (e.detail) {
060: case SWT.TRAVERSE_ESCAPE:
061: case SWT.TRAVERSE_RETURN:
062: case SWT.TRAVERSE_TAB_NEXT:
063: case SWT.TRAVERSE_TAB_PREVIOUS:
064: e.doit = true;
065: break;
066: }
067: }
068: });
069: }
070:
071: /**
072: * Removes the default size of the composite, allowing the control to
073: * shrink to the trim.
074: *
075: * @param wHint
076: * the width hint
077: * @param hHint
078: * the height hint
079: * @param changed
080: * if <code>true</code>, do not use cached values
081: */
082: public Point computeSize(int wHint, int hHint, boolean changed) {
083: Rectangle trim = computeTrim(0, 0, 10, 10);
084: return new Point(trim.width, trim.height);
085: }
086:
087: /**
088: * Tests if the page under the provided key is currently in the book.
089: *
090: * @param key
091: * the page key
092: * @return <code>true</code> if page exists, <code>false</code>
093: * otherwise.
094: */
095: public boolean hasPage(Object key) {
096: return pages.containsKey(key);
097: }
098:
099: /**
100: * Creates a new page for the provided key. Use the returned composite to
101: * create children in it.
102: *
103: * @param key
104: * the page key
105: * @return the newly created page composite
106: */
107: public Composite createPage(Object key) {
108: Composite page = createPage();
109: pages.put(key, page);
110: return page;
111: }
112:
113: /**
114: * Returns the page book container.
115: *
116: * @return the page book container
117: */
118: public Composite getContainer() {
119: return pageBook;
120: }
121:
122: /**
123: * Registers a page under the privided key to be managed by the page book.
124: * The page must be a direct child of the page book container.
125: *
126: * @param key
127: * the page key
128: * @param page
129: * the page composite to register
130: * @see #createPage(Object)
131: * @see #getContainer
132: */
133: public void registerPage(Object key, Control page) {
134: pages.put(key, page);
135: }
136:
137: /**
138: * Removes the page under the provided key from the page book. Does nothing
139: * if page with that key does not exist.
140: *
141: * @param key
142: * the page key.
143: */
144: public void removePage(Object key) {
145: removePage(key, true);
146: }
147:
148: /**
149: * Removes the page under the provided key from the page book. Does nothing
150: * if page with that key does not exist.
151: *
152: * @param key
153: * the page key.
154: * @param showEmptyPage
155: * if <code>true</code>, shows the empty page
156: * after page removal.
157: */
158: public void removePage(Object key, boolean showEmptyPage) {
159: Control page = (Control) pages.get(key);
160: if (page != null) {
161: pages.remove(key);
162: page.dispose();
163: if (showEmptyPage)
164: showEmptyPage();
165: }
166: }
167:
168: /**
169: * Shows the page with the provided key and hides the page previously
170: * showing. Does nothing if the page with that key does not exist.
171: *
172: * @param key
173: * the page key
174: */
175: public void showPage(Object key) {
176: Control page = (Control) pages.get(key);
177: if (page != null) {
178: pageBook.showPage(page);
179: if (currentPage != null && currentPage != page) {
180: // switching pages - force layout
181: if (page instanceof Composite)
182: ((Composite) page).layout(false);
183: }
184: currentPage = page;
185: } else {
186: showEmptyPage();
187: }
188: reflow(true);
189: }
190:
191: /**
192: * Shows a page with no children to be used if the desire is to not show
193: * any registered page.
194: */
195: public void showEmptyPage() {
196: if (emptyPage == null) {
197: emptyPage = createPage();
198: emptyPage.setLayout(new GridLayout());
199: }
200: pageBook.showPage(emptyPage);
201: currentPage = emptyPage;
202: reflow(true);
203: }
204:
205: /**
206: * Sets focus on the current page if shown.
207: */
208: public boolean setFocus() {
209: if (currentPage != null)
210: return currentPage.setFocus();
211: return super .setFocus();
212: }
213:
214: /**
215: * Returns the page currently showing.
216: *
217: * @return the current page
218: */
219: public Control getCurrentPage() {
220: return currentPage;
221: }
222:
223: private Composite createPage() {
224: Composite page = new LayoutComposite(pageBook, SWT.NULL);
225: page.setBackground(getBackground());
226: page.setForeground(getForeground());
227: page.setMenu(pageBook.getMenu());
228: return page;
229: }
230: }
|