001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney;
024:
025: import org.eclipse.core.runtime.Assert;
026: import org.eclipse.swt.graphics.Point;
027: import org.eclipse.swt.graphics.Rectangle;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.Layout;
031:
032: /**
033: * A layout in which one of the child controls is visible
034: * and fills the entire parent control and all the other
035: * child controls are not visible.
036: * <P>
037: * This layout has no corressponding data associated with
038: * each child control. The child control to be shown is
039: * selected by calling the <code>show</code> method on this
040: * layout. This method may be called multiple times to
041: * change the visible child control.
042: *
043: * @author Nigel Westbury
044: */
045: public class AlternativeContentLayout extends Layout {
046: Control currentlyShowing = null;
047:
048: @Override
049: public void layout(Composite editor, boolean force) {
050: Rectangle bounds = editor.getClientArea();
051:
052: Control[] children = editor.getChildren();
053: for (int i = 0; i < children.length; i++) {
054: if (children[i].equals(currentlyShowing)) {
055: children[i]
056: .setBounds(0, 0, bounds.width, bounds.height);
057: } else {
058: children[i].setBounds(0, 0, 0, 0);
059: }
060: }
061:
062: // TODO: check if this following code is correct
063: if (force && currentlyShowing instanceof Composite) {
064: ((Composite) currentlyShowing).layout(force);
065: }
066: }
067:
068: @Override
069: public Point computeSize(Composite editor, int wHint, int hHint,
070: boolean force) {
071: Control[] children = editor.getChildren();
072: int maxWidth = 0;
073: int maxHeight = 0;
074: for (int i = 0; i < children.length; i++) {
075: Point contentsSize = children[i].computeSize(wHint, hHint,
076: force);
077: if (contentsSize.x > maxWidth) {
078: maxWidth = contentsSize.x;
079: }
080: if (contentsSize.y > maxHeight) {
081: maxHeight = contentsSize.y;
082: }
083: }
084:
085: return new Point(maxWidth, maxHeight);
086: }
087:
088: public void show(Control childControl) {
089: Assert.isTrue(childControl.getParent().getLayout() == this );
090:
091: if (currentlyShowing != null) {
092: currentlyShowing.setSize(0, 0);
093: }
094: currentlyShowing = childControl;
095: if (currentlyShowing != null) {
096: currentlyShowing
097: .setSize(childControl.getParent().getSize());
098: }
099: }
100: }
|