KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBPanel


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 /**
7  * Java layout managers are frustrating, badly written
8  * pieces of garbage that are difficult to use, erratic,
9  * throw strange errors, and are generally junk. The
10  * only one worth spitting on is GridBagLayout, but it's
11  * clumsy and inconvenient to use.<p>
12  * <p/>
13  * This class wraps up JPanel with an intrinsic GridBagLayout
14  * and constraints object, and provides a bunch of new
15  * add() ftns to make stuff faster and easier. For me, anyway :-).<p>
16  * <p/>
17  * This works nicely for adding single height objects of
18  * different widths, using ftns such as add(), addline(),
19  * newline(), or addwide(). High objects can be placed
20  * explicitly using add(x,y,width,height).<p>
21  * <p/>
22  * The concept of a 'cursor' position is used, with cells
23  * being added to the next 'cursor' position, and the cursor
24  * being adjusted with 'newline()', or a fixed positional
25  * placement.<p>
26  * <p/>
27  * <pre>
28  * e.g.
29  * <p/>
30  * add(a); add(b); add(c); newLine();
31  * addLine(d);
32  * add(e); addLine(f);
33  * addWide(g,2); addLine(h);
34  * addBig(i,2);
35  * add(j,1,2,3,8)
36  * <p/>
37  * should produce something like:
38  * <p/>
39  * [ a ] [ b ] [ c ]
40  * [ d ]
41  * [ e ] [ f ]
42  * [ g ] [ h ]
43  * [ ]
44  * [ i ]
45  * ... ... ...
46  * ... [ j ]
47  * <p/>
48  * (YMMV :-) )
49  * </pre>
50  * - Chris
51  */

52
53 public class CBPanel extends JPanel
54 {
55     GridBagLayout gridbag;
56     protected GridBagConstraints c;
57
58     protected int xpos = 0;
59     protected int ypos = 0;
60
61     /**
62      * Constructor calls the JPanel superclass constructor,
63      * and initialises the GridBagLayout and GridBagConstraints
64      * objects.
65      */

66     public CBPanel()
67     {
68         super(true); // set double buffering on.
69
gridbag = new GridBagLayout();
70         setLayout(gridbag);
71         c = new GridBagConstraints();
72         c.fill = GridBagConstraints.BOTH;
73         c.insets = new Insets(1, 1, 1, 1);
74         c.weightx = 0;
75         c.weighty = 0;
76     }
77
78     /**
79      * Makes all components added <i>after</i> this call
80      * expand to fill all available space.
81      */

82     public void makeHeavy()
83     {
84         c.weightx = 1;
85         c.weighty = 1;
86     }
87
88     /**
89      * Makes all components added <i>after</i> this call
90      * expand to fill all available width (but not height).
91      */

92     public void makeWide()
93     {
94         c.weightx = 1;
95         c.weighty = 0;
96     }
97
98
99     /*
100      * Makes all components added <i>after</i> this call
101      * stay their original size, and <i>not</i> fill up any spare space.
102      */

103     public void makeLight()
104     {
105         c.weightx = 0;
106         c.weighty = 0;
107     }
108
109
110     /**
111      * Makes all components added <i>after</i> this call
112      * expand to fill all available height (but not width).
113      */

114
115
116     public void makeHigh()
117     {
118         c.weightx = 0;
119         c.weighty = 1;
120     }
121
122
123     /**
124      * Used to set the constraint objects x, y, width and
125      * height objects.
126      *
127      * @param X the X cell position
128      * @param Y the Y cell position
129      * @param width the number of cells spanned in width
130      * @param height the number of cells of height
131      */

132     private void setXYWH(int X, int Y, int width, int height)
133     {
134         c.gridx = X;
135         c.gridy = Y;
136         c.gridwidth = width;
137         c.gridheight = height;
138     }
139
140     /**
141      * Add the next component in a straight line, automatically
142      * incrementing the X position 'cursor'.
143      *
144      * @param comp the component to add
145      */

146     public Component add(Component comp)
147     {
148         return add(comp, xpos, ypos, 1, 1);
149 /*
150         setXYWH(xpos, ypos, 1, 1);
151         add(comp, c);
152         xpos++;
153         return comp;
154 */

155     }
156
157     /**
158      * Add a unit cell object to a specific x and y
159      * position. The 'cursor' is set to the adjacent
160      * x position.
161      *
162      * @param comp the component to add
163      */

164     public Component add(Component comp, int x, int y)
165     {
166         return add(comp, x, y, 1, 1);
167 /*
168         xpos = x;
169         ypos = y;
170         setXYWH(xpos,ypos,1,1);
171         add(comp, c);
172         xpos++;
173         return comp;
174 */

175     }
176
177     /**
178      * Adds a component to the specified x,y cell position, with
179      * a size spanning (width, height) cells. Cursor resets to
180      * the lowest y position (i.e. height), and the adjacent x
181      * position (i.e. x+width);
182      *
183      * @param comp the component to add
184      */

185
186     public Component add(Component comp, int x, int y, int width, int height)
187     {
188 //System.out.println("add constraints: " + c.weightx + ", " + c.weighty + " " + comp.toString());
189
xpos = x;
190         ypos = y;
191         setXYWH(xpos, ypos, width, height);
192         add(comp, c);
193         ypos = y + height - 1;
194         xpos = x + width;
195         return comp;
196     }
197
198     /**
199      * Adjusts the cursor to the
200      * next line down. (i.e. set the x cursor to zero,
201      * and increments the y cursor).
202      */

203
204
205     public void newLine()
206     {
207         xpos = 0;
208         ypos++;
209     }
210
211     /**
212      * Adds a component taking up the remainder of the
213      * line (using GridBagConstraints.REMAINDER), and
214      * increments the cursor to the next line.
215      *
216      * @param comp the component to add
217      */

218
219
220     public void addLine(Component comp)
221     {
222         setXYWH(xpos, ypos, GridBagConstraints.REMAINDER, 1);
223         add(comp, c);
224         newLine();
225     }
226
227     /**
228      * Adds a component taking up the remainder of the
229      * line (using GridBagConstraints.REMAINDER), and
230      * increments the cursor to the next line.
231      *
232      * @param comp the component to add
233      */

234
235
236     public void addln(Component comp)
237     {
238         addLine(comp);
239     }
240
241     /**
242      * Adds a large multi - line component at the next cursor position,
243      * increments the cursor to the start of the next line
244      * immediately below the object.
245      *
246      * @param comp the component to add
247      * @param height the number of cells height the component is.
248      */

249
250
251     public void addLines(Component comp, int height)
252     {
253         setXYWH(xpos, ypos, GridBagConstraints.REMAINDER, height);
254         add(comp, c);
255         xpos = 0;
256         ypos += height;
257     }
258
259     /**
260      * Adds a wider-than-usual component, updating the
261      * cursor as appropriate.
262      */

263
264     public void addWide(Component comp, int width)
265     {
266 //System.out.println("add wide constraints: " + c.weightx + ", " + c.weighty + " comp: " + comp.toString());
267
setXYWH(xpos, ypos, width, 1);
268         add(comp, c);
269         xpos += width;
270     }
271
272     public void addGreedyWide(Component comp)
273     {
274         addGreedyWide(comp, 1);
275     }
276
277     public void addGreedyWide(Component comp, int width)
278     {
279         double oldx = c.weightx;
280         c.weightx = 1;
281         addWide(comp, width);
282         c.weightx = oldx;
283     }
284
285     /**
286      * for debugging... gets current state...
287      */

288
289     public String JavaDoc toString()
290     {
291         return ("CBPanel x,y pos: " + xpos + "," + ypos + " constraint weight x,y " + c.weightx + "," + c.weighty);
292     }
293 }
Popular Tags