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