samer@0
|
1 /*
|
samer@0
|
2 * StackLayout.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@0
|
12 package samer.core.util;
|
samer@0
|
13 import java.awt.*;
|
samer@0
|
14
|
samer@0
|
15 /**
|
samer@0
|
16 This layout manager stacks everything up vertically
|
samer@0
|
17 letting each component assume it's preferred height,
|
samer@0
|
18 but stretching them all out to fill the width available.
|
samer@0
|
19 The preferred width is the height of the widest
|
samer@0
|
20 component
|
samer@0
|
21 */
|
samer@0
|
22
|
samer@0
|
23 public class StackLayout implements LayoutManager, java.io.Serializable
|
samer@0
|
24 {
|
samer@0
|
25 private int gap;
|
samer@0
|
26
|
samer@0
|
27 public StackLayout() { gap=0; }
|
samer@0
|
28 public StackLayout(int a) { gap=a; }
|
samer@0
|
29 public void setGap(int a) { gap=a; }
|
samer@0
|
30 public int getGap(int a) { return gap; }
|
samer@0
|
31
|
samer@0
|
32 public void addLayoutComponent(String name, Component comp) {}
|
samer@0
|
33 public void removeLayoutComponent(Component comp) {}
|
samer@0
|
34
|
samer@0
|
35 public Dimension preferredLayoutSize(Container parent)
|
samer@0
|
36 {
|
samer@0
|
37 Component[] c=parent.getComponents();
|
samer@0
|
38 Dimension d1=new Dimension(0,0), d2;
|
samer@0
|
39
|
samer@0
|
40 int n=c.length;
|
samer@0
|
41 for (int i=0; i<n; i++) {
|
samer@0
|
42 d2 = c[i].getPreferredSize();
|
samer@0
|
43 d1.height += d2.height;
|
samer@0
|
44 d1.width = Math.max(d1.width,d2.width);
|
samer@0
|
45 }
|
samer@0
|
46
|
samer@0
|
47 if (n>1) d1.height += (n-1)*gap;
|
samer@0
|
48
|
samer@0
|
49 Insets insets=parent.getInsets();
|
samer@0
|
50 d1.width += insets.left + insets.right;
|
samer@0
|
51 d1.height += insets.top + insets.bottom;
|
samer@0
|
52
|
samer@0
|
53 return d1;
|
samer@0
|
54 }
|
samer@0
|
55
|
samer@0
|
56 public Dimension minimumLayoutSize(Container parent)
|
samer@0
|
57 {
|
samer@0
|
58 Component[] c=parent.getComponents();
|
samer@0
|
59 Dimension d1=new Dimension(0,0), d2;
|
samer@0
|
60
|
samer@0
|
61 int n=c.length;
|
samer@0
|
62 for (int i=0; i<n; i++) {
|
samer@0
|
63 d2 = c[i].getMinimumSize();
|
samer@0
|
64 d1.height += d2.height;
|
samer@0
|
65 d1.width = Math.max(d1.width,d2.width);
|
samer@0
|
66 }
|
samer@0
|
67
|
samer@0
|
68 if (n>1) d1.height += (n-1)*gap;
|
samer@0
|
69
|
samer@0
|
70 Insets insets=parent.getInsets();
|
samer@0
|
71 d1.width += insets.left + insets.right;
|
samer@0
|
72 d1.height += insets.top + insets.bottom;
|
samer@0
|
73
|
samer@0
|
74 return d1;
|
samer@0
|
75 }
|
samer@0
|
76
|
samer@0
|
77 public void layoutContainer(Container parent)
|
samer@0
|
78 {
|
samer@0
|
79 Component[] c=parent.getComponents();
|
samer@0
|
80 Insets insets=parent.getInsets();
|
samer@0
|
81 int x, y, w;
|
samer@0
|
82 Dimension p=parent.getSize(), d;
|
samer@0
|
83
|
samer@0
|
84 w = p.width - (insets.left+insets.right);
|
samer@0
|
85 x = insets.left;
|
samer@0
|
86 y = insets.top;
|
samer@0
|
87
|
samer@0
|
88 int n=c.length;
|
samer@0
|
89 for (int i=0; i<n; i++) {
|
samer@0
|
90 d = c[i].getPreferredSize();
|
samer@0
|
91 c[i].setBounds(x,y,w,d.height);
|
samer@0
|
92 y += d.height + gap;
|
samer@0
|
93 }
|
samer@0
|
94 }
|
samer@0
|
95 } |