Debian Codegraph @

freehep-graphicsbase/src/main/java/org/freehep/graphicsbase/swing/layout/TableLayout.java

raw
1 // Copyright 2003, FreeHEP.
2 package org.freehep.graphicsbase.swing.layout;
3
4 import java.awt.Component;
5 import java.awt.GridBagConstraints;
6 import java.awt.GridBagLayout;
7 import java.awt.Insets;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 /**
12 * This layoutmanager uses all of the GridBagLayout's managers functionality
13 * and power but allows you to set the options easily. It will construct
14 * the GridBagConstraints object for you.
15 *
16 * To use it, set the layout manager for your Container to TableLayout and
17 * add each component with a name string for which the format is specified
18 * below:
19 *
20 * "gridx gridy gridwidth gridheight ( ipadx ipady ) [ top left bottom right ] { weightx weighty} options"
21 *
22 * you may use spaces or commas as separators.
23 *
24 * gridx and gridy are mandatory, can be * for RELATIVE
25 * gridwidth and gridheight are optional, can be * for REMAINDER
26 * ipadx and ipady are optional
27 * insets (top...right) are optional
28 * weights are optional and override any settings made to them by the options
29 * options are optional
30 *
31 * Options may contain a set of characters which will set the corresponding
32 * flag to true. If not set, the flag is false.
33 *
34 * "r" right align
35 * "l" left align
36 * "t" top align
37 * "b" bottom align
38 * if none of these are set the component is placed in the center.
39 *
40 * "w" resize both cell and component in width
41 * "W" resize only cell in width
42 * "h" resize both cell and component in height
43 * "H" resize only cell in height
44 * if none of these are set neither the component nor the cell is not resized.
45 * and the extra space is put on the outside of the table.
46 *
47 * The weight is calculated as follows, if not set explicitly by the {weight} section:
48 * 1 if character set, fill is set if lowercase.
49 *
50 * You may construct a TableLayout with some default name string.
51 *
52 * @author Mark Donszelmann
53 * @version $Id: TableLayout.java 8584 2006-08-10 23:06:37Z duns $
54 */
55 public class TableLayout extends GridBagLayout {
56
57 // defaults are relative, single row and column, no padding or insets, center
58 // resizing is done for both components and cells.
59 public static final String NO_RESIZE = "* * 1 1 (0, 0) [0, 0, 0, 0] {0.0, 0.0}";
60 public static final String RESIZE_WIDTH = "* * 1 1 (0, 0) [0, 0, 0, 0] {1.0, 0.0} w";
61 public static final String RESIZE_HEIGHT = "* * 1 1 (0, 0) [0, 0, 0, 0] {0.0, 1.0} h";
62 public static final String RESIZE_BOTH = "* * 1 1 (0, 0) [0, 0, 0, 0] {1.0, 1.0} wh";
63
64 public static final String LEFT = "0 * [5 15 5 15] r";
65 public static final String RIGHT = "1 * [5 15 5 15] lw";
66 public static final String VERY_LEFT = "0 * [5 0 5 15] r";
67 public static final String VERY_RIGHT = "1 * [5 15 5 0] lw";
68 public static final String FULL = "0 * * 1 [5 15 5 15] w";
69 public static final String COLUMN = "0 * wt";
70 public static final String COLUMN_FILL = "0 * wh";
71
72 private GridBagConstraints defaults;
73
74 public TableLayout() {
75 this(NO_RESIZE);
76 }
77
78 public TableLayout(String defaultOptions) {
79 super();
80 defaults = getGridBagConstraints(defaultOptions);
81 if (defaults == null) {
82 usage();
83 defaults = new GridBagConstraints();
84 System.err.println("Using Default GridBagConstraints.");
85 }
86 }
87
88 public void usage() {
89 System.err.println("Usage: gridx gridy gridwidth gridheight ( ipadx ipady ) [ top left bottom right ] { weightx weighty} options");
90 System.err.println();
91 System.err.println("you may use spaces or commas as separators.");
92 System.err.println();
93 System.err.println(" gridx and gridy are mandatory, can be * for RELATIVE");
94 System.err.println(" gridwidth and gridheight are optional, can be * for REMAINDER");
95 System.err.println(" ipadx and ipady are optional");
96 System.err.println(" insets (top...right) are optional");
97 System.err.println(" weights are optional and override any settings made to them by the options");
98 System.err.println(" options are optional");
99 System.err.println();
100 System.err.println("Options may contain a set of characters which will set the corresponding");
101 System.err.println("flag to true. If not set, the flag is false.");
102 System.err.println();
103 System.err.println(" \"r\" right align");
104 System.err.println(" \"l\" left align");
105 System.err.println(" \"t\" top align");
106 System.err.println(" \"b\" bottom align");
107 System.err.println("if none of these are set the component is placed in the center.");
108 System.err.println();
109 System.err.println(" \"w\" resize both cell and component in width");
110 System.err.println(" \"W\" resize only cell in width");
111 System.err.println(" \"h\" resize both cell and component in height");
112 System.err.println(" \"H\" resize only cell in height");
113 System.err.println("if none of these are set neither the component nor the cell is not resized.");
114 System.err.println("and the extra space is put on the outside of the table.");
115 System.err.println();
116 System.err.println("The weight is calculated as follows, if not set explicitly by the {weight} section:");
117 System.err.println("1.0 if character set, fill is set if lowercase.");
118 System.err.println();
119 }
120
121 public void addLayoutComponent(String name, Component component) {
122 addLayoutComponent(component, name);
123 }
124
125 public void addLayoutComponent(Component component, Object constraints) {
126 if (constraints instanceof String) {
127 constraints = getGridBagConstraints((String)constraints);
128 if (constraints == null) {
129 usage();
130 constraints = new GridBagConstraints();
131 System.err.println("Using Default GridBagConstraints.");
132 }
133 }
134 super.addLayoutComponent(component, constraints);
135 }
136
137 public String toString() {
138 return "[TableLayout: "+super.toString()+"]";
139 }
140
141 protected GridBagConstraints getGridBagConstraints(String name) {
142 return getGridBagConstraints(name, defaults);
143 }
144
145 public static GridBagConstraints getGridBagConstraints(String name, GridBagConstraints def) {
146
147 // optional whitespace
148 String ws = "\\s*";
149
150 // whitespace or comma
151 String s = "[\\s,]+";
152
153 // integer
154 String d = "[+-]?\\d+";
155
156 // grouped integer
157 String gd = "([+-]?\\d+)";
158
159 // grouped row column span
160 String gdstar = "((?:"+d+")|\\*)";
161
162 // grouped float
163 String gf = "([+-]?\\d*(?:\\.\\d*)?)";
164
165 // gouped options
166 String go = "([tlbrWwHh]*)";
167
168 // gridx gridy gridwidth gridheight
169 // 0 1 2 3
170 String coordPattern = ws+gdstar+s+gdstar+"(?:"+s+gdstar+s+gdstar+")?";
171
172 // ( ipadx ipady )
173 // 4 5
174 String ipadPattern = ws+"(?:\\("+ws+gd+s+gd+ws+"\\))?";
175
176 // [ top left bottom right ]
177 // 6 7 8 9
178 String insetsPattern = ws+"(?:\\["+ws+gd+s+gd+s+gd+s+gd+ws+"\\])?";
179
180 // { weightx weigthy }
181 // 10 11
182 String weightPattern = ws+"(?:\\{"+ws+gf+s+gf+ws+"\\})?";
183
184 // options
185 // 12
186 String optionPattern = ws+go;
187
188 // rest
189 String restPattern = ws+"(.*)";
190
191 Pattern pattern = Pattern.compile(coordPattern + ipadPattern + insetsPattern + weightPattern +optionPattern + restPattern);
192 Matcher matcher = pattern.matcher(name);
193
194 int nArgs = 14;
195 if (!matcher.find()) {
196 if (matcher.groupCount() != nArgs) {
197 System.err.println("Expected "+nArgs+" arguments, but got "+matcher.groupCount()+" in '"+name+"'.");
198 } else {
199 System.err.println("Cannot parse '"+name+"'.");
200 }
201 return null;
202 }
203
204 try {
205 // non optional
206 int gridx = getPosition(matcher.group(1));
207 int gridy = getPosition(matcher.group(2));
208
209 // optional
210 int gridwidth = matcher.group(3) != null ?
211 getSpan(matcher.group(3)) :
212 def.gridwidth;
213 int gridheight = matcher.group(4) != null ?
214 getSpan(matcher.group(4)) :
215 def.gridheight;
216
217 int ipadx = matcher.group(5) != null ?
218 Integer.parseInt(matcher.group(5)) :
219 def.ipadx;
220 int ipady = matcher.group(6) != null ?
221 Integer.parseInt(matcher.group(6)) :
222 def.ipady;
223
224 Insets insets;
225 if (matcher.group(7) != null) {
226 insets = new Insets(Integer.parseInt(matcher.group(7)),
227 Integer.parseInt(matcher.group(8)),
228 Integer.parseInt(matcher.group(9)),
229 Integer.parseInt(matcher.group(10)));
230 } else {
231 insets = def.insets;
232 }
233
234 double weightx = matcher.group(11) != null ?
235 Double.parseDouble(matcher.group(11)) :
236 def.weightx;
237 double weighty = matcher.group(12) != null ?
238 Double.parseDouble(matcher.group(12)) :
239 def.weighty;
240
241 String options = matcher.group(13);
242 int anchor = (def != null) ? def.anchor : GridBagConstraints.CENTER;
243 int fill = (def != null) ? def.fill : GridBagConstraints.NONE ;
244 if (options != null) {
245 int position = 0x00;
246 if ((options.indexOf('l') >= 0) && (options.indexOf('r') < 0)) {
247 position |= 0x10;
248 }
249
250 if ((options.indexOf('r') >= 0) && (options.indexOf('l') < 0)) {
251 position |= 0x20;
252 }
253
254 if ((options.indexOf('t') >= 0) && (options.indexOf('b') < 0)) {
255 position |= 0x01;
256 }
257
258 if ((options.indexOf('b') >= 0) && (options.indexOf('t') < 0)) {
259 position |= 0x02;
260 }
261
262 switch(position) {
263 default:
264 case 0x00: break;
265 case 0x01: anchor = GridBagConstraints.NORTH; break;
266 case 0x02: anchor = GridBagConstraints.SOUTH; break;
267 case 0x10: anchor = GridBagConstraints.WEST; break;
268 case 0x11: anchor = GridBagConstraints.NORTHWEST; break;
269 case 0x12: anchor = GridBagConstraints.SOUTHWEST; break;
270 case 0x20: anchor = GridBagConstraints.EAST; break;
271 case 0x21: anchor = GridBagConstraints.NORTHEAST; break;
272 case 0x22: anchor = GridBagConstraints.SOUTHEAST; break;
273 }
274
275 boolean fillWidth, fillHeight;
276 switch(fill) {
277 default:
278 case GridBagConstraints.NONE: fillWidth = false; fillHeight = false; break;
279 case GridBagConstraints.HORIZONTAL: fillWidth = true; fillHeight = false; break;
280 case GridBagConstraints.VERTICAL: fillWidth = false; fillHeight = true; break;
281 case GridBagConstraints.BOTH: fillWidth = true; fillHeight = true; break;
282 }
283
284 if (options.indexOf('w') >= 0) {
285 fillWidth = true;
286 if (matcher.group(11) == null) {
287 weightx = 1.0;
288 }
289 }
290
291 if (options.indexOf('W') >= 0) {
292 if (matcher.group(11) == null) {
293 weightx = 1.0;
294 }
295 }
296
297 if (options.indexOf('h') >= 0) {
298 fillHeight = true;
299 if (matcher.group(12) == null) {
300 weighty = 1.0;
301 }
302 }
303
304 if (options.indexOf('H') >= 0) {
305 if (matcher.group(12) == null) {
306 weighty = 1.0;
307 }
308 }
309
310 if (fillWidth) {
311 if (fillHeight) {
312 fill = GridBagConstraints.BOTH;
313 } else {
314 fill = GridBagConstraints.HORIZONTAL;
315 }
316 } else if (fillHeight) {
317 fill = GridBagConstraints.VERTICAL;
318 }
319
320 if ((matcher.group(14) != null) && !matcher.group(14).equals("")) {
321 System.err.println("Cannot parse: '"+matcher.group(14)+"' in '"+name+"'.");
322 return null;
323 }
324 }
325 return new TableConstraints(
326 gridx, gridy,
327 gridwidth, gridheight,
328 weightx, weighty,
329 anchor, fill, insets,
330 ipadx, ipady
331 );
332 } catch (NumberFormatException nfe) {
333 System.err.println("Problem "+nfe.getMessage()+" in '"+name+"'.");
334 return null;
335 }
336 }
337
338 private static int getPosition(String position) {
339 if (position.equals("*")) {
340 return GridBagConstraints.RELATIVE;
341 }
342
343 return Integer.parseInt(position);
344 }
345
346 private static int getSpan(String span) {
347 if (span.equals("*")) {
348 return GridBagConstraints.REMAINDER;
349 }
350
351 return Integer.parseInt(span);
352 }
353
354 public static String toString(GridBagConstraints c) {
355 StringBuffer s = new StringBuffer();
356 s.append(c.gridx == TableConstraints.RELATIVE ? "*" : String.valueOf(c.gridx));
357 s.append(" ");
358 s.append(c.gridy == TableConstraints.RELATIVE ? "*" : String.valueOf(c.gridy));
359 s.append(" ");
360 s.append(c.gridwidth == TableConstraints.REMAINDER ? "*" : String.valueOf(c.gridwidth));
361 s.append(" ");
362 s.append(c.gridheight == TableConstraints.REMAINDER ? "*" : String.valueOf(c.gridheight));
363 s.append(" (");
364 s.append(String.valueOf(c.ipadx));
365 s.append(" ");
366 s.append(String.valueOf(c.ipady));
367 s.append(") [");
368 s.append(String.valueOf(c.insets.top));
369 s.append(" ");
370 s.append(String.valueOf(c.insets.left));
371 s.append(" ");
372 s.append(String.valueOf(c.insets.bottom));
373 s.append(" ");
374 s.append(String.valueOf(c.insets.right));
375 s.append("] {");
376 s.append(String.valueOf(c.weightx));
377 s.append(" ");
378 s.append(String.valueOf(c.weighty));
379 s.append("} ");
380 switch (c.anchor) {
381 default: break;
382 case GridBagConstraints.CENTER: break;
383 case GridBagConstraints.NORTH: s.append("t"); break;
384 case GridBagConstraints.NORTHWEST: s.append("tl"); break;
385 case GridBagConstraints.WEST: s.append("l"); break;
386 case GridBagConstraints.SOUTHWEST: s.append("bl"); break;
387 case GridBagConstraints.SOUTH: s.append("b"); break;
388 case GridBagConstraints.SOUTHEAST: s.append("br"); break;
389 case GridBagConstraints.EAST: s.append("r"); break;
390 case GridBagConstraints.NORTHEAST: s.append("tr"); break;
391 }
392 switch (c.fill) {
393 default: break;
394 case GridBagConstraints.NONE:
395 if (c.weightx == 1.0) s.append("W");
396 if (c.weighty == 1.0) s.append("H");
397 break;
398 case GridBagConstraints.HORIZONTAL:
399 s.append("w");
400 if (c.weighty == 1.0) s.append("H");
401 break;
402 case GridBagConstraints.VERTICAL:
403 if (c.weightx == 1.0) s.append("W");
404 s.append("h");
405 break;
406 case GridBagConstraints.BOTH:
407 s.append("wh");
408 break;
409 }
410 return s.toString();
411 }
412
413 public static class TableConstraints extends GridBagConstraints {
414
415 public TableConstraints() {
416 super();
417 }
418
419 public TableConstraints(int gridx, int gridy,
420 int gridwidth, int gridheight,
421 double weightx, double weighty,
422 int anchor, int fill, Insets insets,
423 int ipadx, int ipady) {
424 super(gridx, gridy, gridwidth, gridheight, weightx, weighty,
425 anchor, fill, insets, ipadx, ipady);
426 }
427
428 public String toString() {
429 return "TableConstraints: "+TableLayout.toString(this);
430 }
431 }
432
433 public static void main(String args[]) {
434 TableLayout layout = new TableLayout();
435 System.out.println(layout.getGridBagConstraints("1 2 3 4 (5 6) [7 8 9 10] {11 12} tlwh"));
436 System.out.println(layout.getGridBagConstraints("1 2 3 4 [7 8 9 10] {11 12} trWh"));
437 System.out.println(layout.getGridBagConstraints("1 2 3 4 (5 6) {11 12} tlwH"));
438 System.out.println(layout.getGridBagConstraints("1 2 3 4 (5 6) [7 8 9 10] brwh"));
439 System.out.println(layout.getGridBagConstraints("1 2 3 4 (5 6) tlWh"));
440 System.out.println(layout.getGridBagConstraints("1 2 3 4"));
441 System.out.println(layout.getGridBagConstraints("1 2"));
442 System.out.println(layout.getGridBagConstraints("* * 2 4"));
443 System.out.println(layout.getGridBagConstraints("* 0 2 4"));
444 System.out.println(layout.getGridBagConstraints("0 * * *"));
445 System.out.println(layout.getGridBagConstraints("0 0 * 4"));
446 }
447 }