1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: GridBagHelper.java,v 1.2 2002/09/05 15:41:48 ludovicc Exp $
37 */
38 package samples.util;
39
40 import java.awt.*;
41
42 import javax.swing.*;
43
44 /***
45 * Helper for creating GridBagConstraints with convenience short methods. Fill
46 * and anchor are specified with single string containing letters 'n' (north),
47 * 'e' (east), 's' (south), 'w' (west) specifying in which direction from center
48 * span the component
49 *
50 * @author daniel.michalik
51 * @created 05 September 2002
52 */
53 public class GridBagHelper implements java.io.Serializable {
54 /***
55 * TODO: describe of the Field
56 */
57 public int x;
58 /***
59 * TODO: describe of the Field
60 */
61 public int y;
62
63 /***
64 * Constructor for the GridBagHelper object
65 */
66 public GridBagHelper() { }
67
68 /***
69 * TODO: document the method
70 *
71 * @param x TODO: Describe the Parameter
72 * @param y TODO: Describe the Parameter
73 * @return TODO: Describe the Return Value
74 */
75 public GridBagConstraints xy(int x, int y) {
76 GridBagConstraints c = new GridBagConstraints();
77 c.gridx = x;
78 c.gridy = y;
79 this.x = x;
80 this.y = y;
81 return c;
82 }
83
84 /***
85 * TODO: document the method
86 *
87 * @param x TODO: Describe the Parameter
88 * @param y TODO: Describe the Parameter
89 * @param fillanchor TODO: Describe the Parameter
90 * @return TODO: Describe the Return Value
91 */
92 public GridBagConstraints xy(int x, int y, String fillanchor) {
93 this.x = x;
94 this.y = y;
95 return setFillAnchor(xy(x, y), fillanchor);
96 }
97
98 /***
99 * TODO: document the method
100 *
101 * @param x TODO: Describe the Parameter
102 * @param fillanchor TODO: Describe the Parameter
103 * @return TODO: Describe the Return Value
104 */
105 public GridBagConstraints x(int x, String fillanchor) {
106 return xy(x, y, fillanchor);
107 }
108
109 /***
110 * TODO: document the method
111 *
112 * @param y TODO: Describe the Parameter
113 * @param fillanchor TODO: Describe the Parameter
114 * @return TODO: Describe the Return Value
115 */
116 public GridBagConstraints y(int y, String fillanchor) {
117 return xy(x, y, fillanchor);
118 }
119
120 /***
121 * TODO: document the method
122 *
123 * @param x TODO: Describe the Parameter
124 * @param w TODO: Describe the Parameter
125 * @param h TODO: Describe the Parameter
126 * @param fillanchor TODO: Describe the Parameter
127 * @return TODO: Describe the Return Value
128 */
129 public GridBagConstraints xwh(int x, int w, int h, String fillanchor) {
130 return xywh(x, y, w, h, fillanchor);
131 }
132
133 /***
134 * TODO: document the method
135 *
136 * @param y TODO: Describe the Parameter
137 * @param w TODO: Describe the Parameter
138 * @param h TODO: Describe the Parameter
139 * @param fillanchor TODO: Describe the Parameter
140 * @return TODO: Describe the Return Value
141 */
142 public GridBagConstraints ywh(int y, int w, int h, String fillanchor) {
143 return xywh(x, y, w, h, fillanchor);
144 }
145
146 /***
147 * TODO: document the method
148 *
149 * @param x TODO: Describe the Parameter
150 * @param y TODO: Describe the Parameter
151 * @param w TODO: Describe the Parameter
152 * @param h TODO: Describe the Parameter
153 * @return TODO: Describe the Return Value
154 */
155 public GridBagConstraints xywh(int x, int y, int w, int h) {
156 GridBagConstraints c = new GridBagConstraints();
157 c.gridx = x;
158 c.gridy = y;
159 c.gridwidth = w;
160 c.gridheight = h;
161 this.x = x;
162 this.y = y;
163 return c;
164 }
165
166 /***
167 * TODO: document the method
168 *
169 * @param x TODO: Describe the Parameter
170 * @param y TODO: Describe the Parameter
171 * @param w TODO: Describe the Parameter
172 * @param h TODO: Describe the Parameter
173 * @param fillanchor TODO: Describe the Parameter
174 * @return TODO: Describe the Return Value
175 */
176 public GridBagConstraints xywh(int x, int y, int w, int h, String fillanchor) {
177 this.x = x;
178 this.y = y;
179 return setFillAnchor(xywh(x, y, w, h), fillanchor);
180 }
181
182 /***
183 * Adds an element to the X2 attribute of the GridBagHelper object
184 */
185 public void addX2() {
186 x += 2;
187 }
188
189 /***
190 * Adds an element to the X1 attribute of the GridBagHelper object
191 */
192 public void addX1() {
193 x += 1;
194 }
195
196 /***
197 * Adds an element to the Y2 attribute of the GridBagHelper object
198 */
199 public void addY2() {
200 y += 2;
201 }
202
203 /***
204 * Adds an element to the Y1 attribute of the GridBagHelper object
205 */
206 public void addY1() {
207 y += 1;
208 }
209
210 private GridBagConstraints setFillAnchor(GridBagConstraints c, String a) {
211 String s = a.toLowerCase();
212 boolean b[] = {false, false, false, false};
213 b[0] = (s.indexOf('n') > -1);
214 b[1] = (s.indexOf('e') > -1);
215 b[2] = (s.indexOf('s') > -1);
216 b[3] = (s.indexOf('w') > -1);
217 if (b[0] && b[1] && b[2] && b[3]) {
218 c.fill = GridBagConstraints.BOTH;
219 return c;
220 } else if (b[0] && b[2]) {
221 c.fill = GridBagConstraints.VERTICAL;
222 b[0] = false;
223 b[2] = false;
224 } else if (b[1] && b[3]) {
225 c.fill = GridBagConstraints.HORIZONTAL;
226 b[1] = false;
227 b[3] = false;
228 }
229 if (b[0] && b[1]) {
230 c.anchor = GridBagConstraints.NORTHEAST;
231 } else if (b[1] && b[2]) {
232 c.anchor = GridBagConstraints.SOUTHEAST;
233 } else if (b[2] && b[3]) {
234 c.anchor = GridBagConstraints.SOUTHWEST;
235 } else if (b[3] && b[0]) {
236 c.anchor = GridBagConstraints.NORTHWEST;
237 } else if (b[0]) {
238 c.anchor = GridBagConstraints.NORTH;
239 } else if (b[1]) {
240 c.anchor = GridBagConstraints.EAST;
241 } else if (b[2]) {
242 c.anchor = GridBagConstraints.SOUTH;
243 } else if (b[3]) {
244 c.anchor = GridBagConstraints.WEST;
245 }
246
247 return c;
248 }
249
250 }
This page was automatically generated by Maven