001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.swt.custom;
011:
012: import org.eclipse.swt.*;
013: import org.eclipse.swt.graphics.*;
014: import org.eclipse.swt.widgets.*;
015:
016: /**
017: * This Layout stacks all the controls one on top of the other and resizes all controls
018: * to have the same size and location.
019: * The control specified in topControl is visible and all other controls are not visible.
020: * Users must set the topControl value to flip between the visible items and then call
021: * layout() on the composite which has the StackLayout.
022: *
023: * <p> Here is an example which places ten buttons in a stack layout and
024: * flips between them:
025: *
026: * <pre><code>
027: * public static void main(String[] args) {
028: * Display display = new Display();
029: * Shell shell = new Shell(display);
030: * shell.setLayout(new GridLayout());
031: *
032: * final Composite parent = new Composite(shell, SWT.NONE);
033: * parent.setLayoutData(new GridData(GridData.FILL_BOTH));
034: * final StackLayout layout = new StackLayout();
035: * parent.setLayout(layout);
036: * final Button[] bArray = new Button[10];
037: * for (int i = 0; i < 10; i++) {
038: * bArray[i] = new Button(parent, SWT.PUSH);
039: * bArray[i].setText("Button "+i);
040: * }
041: * layout.topControl = bArray[0];
042: *
043: * Button b = new Button(shell, SWT.PUSH);
044: * b.setText("Show Next Button");
045: * final int[] index = new int[1];
046: * b.addListener(SWT.Selection, new Listener(){
047: * public void handleEvent(Event e) {
048: * index[0] = (index[0] + 1) % 10;
049: * layout.topControl = bArray[index[0]];
050: * parent.layout();
051: * }
052: * });
053: *
054: * shell.open();
055: * while (shell != null && !shell.isDisposed()) {
056: * if (!display.readAndDispatch())
057: * display.sleep();
058: * }
059: * }
060: * </code></pre>
061: */
062:
063: public class StackLayout extends Layout {
064:
065: /**
066: * marginWidth specifies the number of pixels of horizontal margin
067: * that will be placed along the left and right edges of the layout.
068: *
069: * The default value is 0.
070: */
071: public int marginWidth = 0;
072: /**
073: * marginHeight specifies the number of pixels of vertical margin
074: * that will be placed along the top and bottom edges of the layout.
075: *
076: * The default value is 0.
077: */
078: public int marginHeight = 0;
079:
080: /**
081: * topControl the Control that is displayed at the top of the stack.
082: * All other controls that are children of the parent composite will not be visible.
083: */
084: public Control topControl;
085:
086: protected Point computeSize(Composite composite, int wHint,
087: int hHint, boolean flushCache) {
088: Control children[] = composite.getChildren();
089: int maxWidth = 0;
090: int maxHeight = 0;
091: for (int i = 0; i < children.length; i++) {
092: Point size = children[i].computeSize(wHint, hHint,
093: flushCache);
094: maxWidth = Math.max(size.x, maxWidth);
095: maxHeight = Math.max(size.y, maxHeight);
096: }
097: int width = maxWidth + 2 * marginWidth;
098: int height = maxHeight + 2 * marginHeight;
099: if (wHint != SWT.DEFAULT)
100: width = wHint;
101: if (hHint != SWT.DEFAULT)
102: height = hHint;
103: return new Point(width, height);
104: }
105:
106: protected boolean flushCache(Control control) {
107: return true;
108: }
109:
110: protected void layout(Composite composite, boolean flushCache) {
111: Control children[] = composite.getChildren();
112: Rectangle rect = composite.getClientArea();
113: rect.x += marginWidth;
114: rect.y += marginHeight;
115: rect.width -= 2 * marginWidth;
116: rect.height -= 2 * marginHeight;
117: for (int i = 0; i < children.length; i++) {
118: children[i].setBounds(rect);
119: children[i].setVisible(children[i] == topControl);
120:
121: }
122: }
123:
124: String getName() {
125: String string = getClass().getName();
126: int index = string.lastIndexOf('.');
127: if (index == -1)
128: return string;
129: return string.substring(index + 1, string.length());
130: }
131:
132: /**
133: * Returns a string containing a concise, human-readable
134: * description of the receiver.
135: *
136: * @return a string representation of the layout
137: */
138: public String toString() {
139: String string = getName() + " {";
140: if (marginWidth != 0)
141: string += "marginWidth=" + marginWidth + " ";
142: if (marginHeight != 0)
143: string += "marginHeight=" + marginHeight + " ";
144: if (topControl != null)
145: string += "topControl=" + topControl + " ";
146: string = string.trim();
147: string += "}";
148: return string;
149: }
150: }
|