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