001: /*******************************************************************************
002: * Copyright (c) 2004, 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.internal.forms.widgets;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.graphics.Point;
014: import org.eclipse.swt.widgets.*;
015: import org.eclipse.ui.forms.widgets.ILayoutExtension;
016:
017: /**
018: * A pagebook is a composite control where only a single control is visible at
019: * a time. It is similar to a notebook, but without tabs.
020: * <p>
021: * This class may be instantiated; it is not intended to be subclassed.
022: * </p>
023: */
024: public class WrappedPageBook extends Composite {
025: class PageBookLayout extends Layout implements ILayoutExtension {
026: protected Point computeSize(Composite composite, int wHint,
027: int hHint, boolean flushCache) {
028: if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT)
029: return new Point(wHint, hHint);
030: Point result = null;
031: if (currentPage != null) {
032: result = currentPage.computeSize(wHint, hHint,
033: flushCache);
034: } else {
035: result = new Point(0, 0);
036: }
037: return result;
038: }
039:
040: protected void layout(Composite composite, boolean flushCache) {
041: if (currentPage != null) {
042: currentPage.setBounds(composite.getClientArea());
043: }
044: }
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see org.eclipse.ui.forms.widgets.ILayoutExtension#computeMaximumWidth(org.eclipse.swt.widgets.Composite,
050: * boolean)
051: */
052: public int computeMaximumWidth(Composite parent, boolean changed) {
053: return computeSize(parent, SWT.DEFAULT, SWT.DEFAULT,
054: changed).x;
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see org.eclipse.ui.forms.widgets.ILayoutExtension#computeMinimumWidth(org.eclipse.swt.widgets.Composite,
061: * boolean)
062: */
063: public int computeMinimumWidth(Composite parent, boolean changed) {
064: return computeSize(parent, 0, SWT.DEFAULT, changed).x;
065: }
066: }
067:
068: /**
069: * The current control; <code>null</code> if none.
070: */
071: private Control currentPage = null;
072:
073: /**
074: * Creates a new empty pagebook.
075: *
076: * @param parent
077: * the parent composite
078: * @param style
079: * the SWT style bits
080: */
081: public WrappedPageBook(Composite parent, int style) {
082: super (parent, style);
083: setLayout(new PageBookLayout());
084: }
085:
086: /**
087: * Shows the given page. This method has no effect if the given page is not
088: * contained in this pagebook.
089: *
090: * @param page
091: * the page to show
092: */
093: public void showPage(Control page) {
094: if (page == currentPage)
095: return;
096: if (page.getParent() != this )
097: return;
098: Control oldPage = currentPage;
099: currentPage = page;
100: // show new page
101: if (page != null) {
102: if (!page.isDisposed()) {
103: //page.setVisible(true);
104: layout(true);
105: page.setVisible(true);
106: }
107: }
108: // hide old *after* new page has been made visible in order to avoid
109: // flashing
110: if (oldPage != null && !oldPage.isDisposed())
111: oldPage.setVisible(false);
112: }
113:
114: public Point computeSize(int wHint, int hHint, boolean changed) {
115: return ((PageBookLayout) getLayout()).computeSize(this, wHint,
116: hHint, changed);
117: }
118: }
|