KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > StackLayout


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.swt.custom;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.*;
17
18 /**
19  * This Layout stacks all the controls one on top of the other and resizes all controls
20  * to have the same size and location.
21  * The control specified in topControl is visible and all other controls are not visible.
22  * Users must set the topControl value to flip between the visible items and then call
23  * layout() on the composite which has the StackLayout.
24  *
25  * <p> Here is an example which places ten buttons in a stack layout and
26  * flips between them:
27  *
28  * <pre><code>
29  * public static void main(String[] args) {
30  * Display display = new Display();
31  * Shell shell = new Shell(display);
32  * shell.setLayout(new GridLayout());
33  *
34  * final Composite parent = new Composite(shell, SWT.NONE);
35  * parent.setLayoutData(new GridData(GridData.FILL_BOTH));
36  * final StackLayout layout = new StackLayout();
37  * parent.setLayout(layout);
38  * final Button[] bArray = new Button[10];
39  * for (int i = 0; i &lt; 10; i++) {
40  * bArray[i] = new Button(parent, SWT.PUSH);
41  * bArray[i].setText("Button "+i);
42  * }
43  * layout.topControl = bArray[0];
44  *
45  * Button b = new Button(shell, SWT.PUSH);
46  * b.setText("Show Next Button");
47  * final int[] index = new int[1];
48  * b.addListener(SWT.Selection, new Listener(){
49  * public void handleEvent(Event e) {
50  * index[0] = (index[0] + 1) % 10;
51  * layout.topControl = bArray[index[0]];
52  * parent.layout();
53  * }
54  * });
55  *
56  * shell.open();
57  * while (shell != null && !shell.isDisposed()) {
58  * if (!display.readAndDispatch())
59  * display.sleep();
60  * }
61  * }
62  * </code></pre>
63  */

64
65 public class StackLayout extends Layout {
66     
67     /**
68      * marginWidth specifies the number of pixels of horizontal margin
69      * that will be placed along the left and right edges of the layout.
70      *
71      * The default value is 0.
72      */

73     public int marginWidth = 0;
74     /**
75      * marginHeight specifies the number of pixels of vertical margin
76      * that will be placed along the top and bottom edges of the layout.
77      *
78      * The default value is 0.
79      */

80     public int marginHeight = 0;
81
82     /**
83      * topControl the Control that is displayed at the top of the stack.
84      * All other controls that are children of the parent composite will not be visible.
85      */

86     public Control topControl;
87
88 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
89     Control children[] = composite.getChildren();
90     int maxWidth = 0;
91     int maxHeight = 0;
92     for (int i = 0; i < children.length; i++) {
93         Point size = children[i].computeSize(wHint, hHint, flushCache);
94         maxWidth = Math.max(size.x, maxWidth);
95         maxHeight = Math.max(size.y, maxHeight);
96     }
97     int width = maxWidth + 2 * marginWidth;
98     int height = maxHeight + 2 * marginHeight;
99     if (wHint != SWT.DEFAULT) width = wHint;
100     if (hHint != SWT.DEFAULT) height = hHint;
101     return new Point(width, height);
102 }
103
104 protected boolean flushCache(Control control) {
105     return true;
106 }
107
108 protected void layout(Composite composite, boolean flushCache) {
109     Control children[] = composite.getChildren();
110     Rectangle rect = composite.getClientArea();
111     rect.x += marginWidth;
112     rect.y += marginHeight;
113     rect.width -= 2 * marginWidth;
114     rect.height -= 2 * marginHeight;
115     for (int i = 0; i < children.length; i++) {
116         children[i].setBounds(rect);
117         children[i].setVisible(children[i] == topControl);
118             
119     }
120 }
121
122 String JavaDoc getName () {
123     String JavaDoc string = getClass ().getName ();
124     int index = string.lastIndexOf ('.');
125     if (index == -1) return string;
126     return string.substring (index + 1, string.length ());
127 }
128
129 /**
130  * Returns a string containing a concise, human-readable
131  * description of the receiver.
132  *
133  * @return a string representation of the layout
134  */

135 public String JavaDoc toString () {
136     String JavaDoc string = getName ()+" {";
137     if (marginWidth != 0) string += "marginWidth="+marginWidth+" ";
138     if (marginHeight != 0) string += "marginHeight="+marginHeight+" ";
139     if (topControl != null) string += "topControl="+topControl+" ";
140     string = string.trim();
141     string += "}";
142     return string;
143 }
144 }
145
Popular Tags