KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > awt > Constrain


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.awt;
32
33 import java.awt.*;
34
35 public abstract class Constrain {
36
37     public static void add (Container container, Component comp, Object JavaDoc constraints) {
38         container.add (comp);
39         GridBagLayout layout = (GridBagLayout)container.getLayout ();
40         GridBagConstraints c = (GridBagConstraints)constraints;
41         layout.setConstraints (comp, c);
42     }
43
44     public static GridBagConstraints labelLike (int x, int y) {
45         GridBagConstraints c = new GridBagConstraints();
46         c.gridx = x;
47         c.gridy = y;
48         c.weightx = 0;
49         c.weighty = 0;
50         c.anchor = GridBagConstraints.NORTHWEST;
51         c.fill = GridBagConstraints.NONE;
52         return c;
53     }
54         
55     public static GridBagConstraints labelLike (int x, int y, int w) {
56         GridBagConstraints c = labelLike (x, y);
57         c.gridwidth = w;
58         return c;
59     }
60         
61     public static GridBagConstraints fieldLike (int x, int y) {
62         GridBagConstraints c = new GridBagConstraints();
63         c.gridx = x;
64         c.gridy = y;
65         c.weightx = 1.0;
66         c.weighty = 0.0;
67         c.anchor = GridBagConstraints.NORTHWEST;
68         c.fill = GridBagConstraints.HORIZONTAL;
69         return c;
70     }
71
72     public static GridBagConstraints fieldLike (int x, int y, int w) {
73         GridBagConstraints c = fieldLike (x, y);
74         c.gridwidth = w;
75         return c;
76     }
77
78     public static GridBagConstraints areaLike (int x, int y) {
79         GridBagConstraints c = new GridBagConstraints();
80         c.gridx = x;
81         c.gridy = y;
82         c.weightx = 1.0;
83         c.weighty = 1.0;
84         c.anchor = GridBagConstraints.NORTHWEST;
85         c.fill = GridBagConstraints.BOTH;
86         return c;
87     }
88
89     public static GridBagConstraints areaLike (int x, int y, int w) {
90         GridBagConstraints c = areaLike (x, y);
91         c.gridwidth = w;
92         return c;
93     }
94
95     public static GridBagConstraints rightJustify (GridBagConstraints c) {
96         c.anchor = GridBagConstraints.NORTHEAST;
97         return c;
98     }
99
100     public static GridBagConstraints centered (GridBagConstraints c) {
101         c.anchor = GridBagConstraints.CENTER;
102         return c;
103     }
104
105     public static GridBagConstraints anchor (GridBagConstraints c,
106                                                int anchor) {
107         c.anchor = anchor;
108         return c;
109     }
110
111     public static Panel makeConstrainedPanel () {
112         Panel panel = new Panel ();
113         panel.setLayout (new GridBagLayout ());
114         return panel;
115     }
116
117     public static Panel makeConstrainedPanel (int w, int h) {
118         Panel panel = makeConstrainedPanel ();
119         
120         GridBagConstraints c = new GridBagConstraints();
121         c.gridx = 0;
122         c.gridy = h;
123         c.gridwidth = w;
124         c.weightx = 1.0;
125         c.weighty = 0.0;
126         c.anchor = GridBagConstraints.NORTHWEST;
127         c.fill = GridBagConstraints.VERTICAL;
128         add (panel, new Panel(), c);
129         
130         c = new GridBagConstraints();
131         c.gridx = w;
132         c.gridy = 0;
133         c.gridheight = h;
134         c.weightx = 0.0;
135         c.weighty = 1.0;
136         c.anchor = GridBagConstraints.NORTHWEST;
137         c.fill = GridBagConstraints.HORIZONTAL;
138         add (panel, new Panel(), c);
139         
140         return panel;
141     }
142
143 }
144
Popular Tags