Mercurial > hg > jslab
comparison src/samer/core_/util/VLayout.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 * VLayout.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 Things are layed out horizontally. Everyone gets to | |
17 be their preferred size, except for the filler, which | |
18 is stretched horizontally (but not vertically) to fill | |
19 the space available. The whole thing is centered vertically. | |
20 */ | |
21 | |
22 public class VLayout implements LayoutManager, java.io.Serializable | |
23 { | |
24 private int gap=4; | |
25 private int filler=1; // index of filler component | |
26 private int fillerHeight=10; | |
27 | |
28 public VLayout( int f, int g) { gap=g; filler=f; } | |
29 public VLayout( int f) { gap=0; filler=f; } | |
30 | |
31 public void setGap(int a) { gap=a; } | |
32 public int getGap() { return gap; } | |
33 public void setFiller(int f) { filler=f; } | |
34 public int getFiller() { return filler; } | |
35 public void addLayoutComponent(String name, Component comp) {} | |
36 public void removeLayoutComponent(Component comp) {} | |
37 | |
38 public Dimension preferredLayoutSize(Container parent) | |
39 { | |
40 Component[] c=parent.getComponents(); | |
41 Dimension d1=new Dimension(0,0), d2; | |
42 | |
43 int n=c.length; | |
44 for (int i=0; i<n; i++) { | |
45 d2 = c[i].getPreferredSize(); | |
46 d1.width += d2.width; | |
47 d1.height = Math.max(d1.height,d2.height); | |
48 } | |
49 | |
50 if (n>1) d1.width += (n-1)*gap; | |
51 | |
52 Insets insets=parent.getInsets(); | |
53 d1.width += insets.left + insets.right; | |
54 d1.height += insets.top + insets.bottom; | |
55 | |
56 return d1; | |
57 } | |
58 | |
59 public Dimension minimumLayoutSize(Container parent) | |
60 { | |
61 Component[] c=parent.getComponents(); | |
62 Dimension d1=new Dimension(0,0), d2; | |
63 | |
64 int n=c.length; | |
65 for (int i=0; i<n; i++) { | |
66 d2 = c[i].getMinimumSize(); | |
67 d1.width += d2.width; | |
68 d1.height = Math.max(d1.height,d2.height); | |
69 } | |
70 | |
71 if (n>1) d1.width += (n-1)*gap; | |
72 | |
73 Insets insets=parent.getInsets(); | |
74 d1.width += insets.left + insets.right; | |
75 d1.height += insets.top + insets.bottom; | |
76 | |
77 return d1; | |
78 } | |
79 | |
80 public void layoutContainer(Container parent) | |
81 { | |
82 Component[] c=parent.getComponents(); | |
83 Insets insets=parent.getInsets(); | |
84 int xl, xr, y, havail, h; | |
85 Dimension p=parent.getSize(), d; | |
86 | |
87 havail = p.height - (insets.top+insets.bottom); | |
88 | |
89 int n=c.length; | |
90 | |
91 // first layout components left of filler | |
92 | |
93 xl = insets.left; | |
94 for (int i=0; i<filler && i<n; i++) { | |
95 d = c[i].getPreferredSize(); | |
96 if (d.height>havail) { h=havail; y=insets.bottom; } | |
97 else { h=d.height; y=(p.height-h)/2; } | |
98 c[i].setBounds(xl,y,d.width,h); | |
99 xl += d.width + gap; | |
100 } | |
101 // xl is now position of left hand edge of filler | |
102 | |
103 // next, layout components right of filler | |
104 // x is now position of RIGHTHAND edge | |
105 | |
106 xr = p.width-insets.right; | |
107 for (int i=n-1; i>filler && i>=0; i--) { | |
108 d = c[i].getPreferredSize(); | |
109 if (d.height>havail) { h=havail; y=insets.bottom; } | |
110 else { h=d.height; y=(p.height-h)/2; } | |
111 c[i].setBounds(xr-d.width,y,d.width,h); | |
112 xr -= d.width + gap; | |
113 } | |
114 // xr is now position of right hand edge of filler | |
115 | |
116 // now we can position the filler component | |
117 if (filler>=0 && filler<n) { | |
118 d = c[filler].getPreferredSize(); | |
119 if (d.height>havail) { h=havail; y=insets.bottom; } | |
120 else { h=d.height; y=(p.height-h)/2; } | |
121 c[filler].setBounds(xl,y,xr-xl,h); | |
122 } | |
123 } | |
124 } |