source: josm/src/org/openstreetmap/josm/tools/GBC.java@ 71

Last change on this file since 71 was 71, checked in by imi, 18 years ago
  • refactored GpsPoint to be immutable and added LatLon and NorthEast
  • refactored Bounding Box calculations
  • various other renames
File size: 2.7 KB
Line 
1package org.openstreetmap.josm.tools;
2
3import java.awt.Component;
4import java.awt.Dimension;
5import java.awt.GridBagConstraints;
6import java.awt.Insets;
7
8import javax.swing.Box;
9
10/**
11 * A wrapper for GridBagConstraints which has sane default static creators and
12 * member functions to chain calling.
13 *
14 * @author imi
15 */
16public class GBC extends GridBagConstraints {
17
18 /**
19 * Use public static creator functions to create an GBC.
20 */
21 private GBC() {}
22
23 /**
24 * Create a standard constraint (which is not the last).
25 * @return A standard constraint with no filling.
26 */
27 public static GBC std() {
28 GBC c = new GBC();
29 c.anchor = WEST;
30 return c;
31 }
32
33 /**
34 * Create the constraint for the last elements on a line.
35 * @return A constraint which indicates the last item on a line.
36 */
37 public static GBC eol() {
38 GBC c = std();
39 c.gridwidth = REMAINDER;
40 return c;
41 }
42
43 /**
44 * Create the constraint for the last elements on a line and on a paragraph.
45 * This is merely a shortcut for eol().insets(0,0,0,10)
46 * @return A constraint which indicates the last item on a line.
47 */
48 public static GBC eop() {
49 return eol().insets(0,0,0,10);
50 }
51
52 /**
53 * Try to fill both, horizontal and vertical
54 * @return This constraint for chaining.
55 */
56 public GBC fill() {
57 return fill(BOTH);
58 }
59
60 /**
61 * Set fill to the given value
62 * @param value The filling value, either NONE, HORIZONTAL, VERTICAL or BOTH
63 * @return This constraint for chaining.
64 */
65 public GBC fill(int value) {
66 fill = value;
67 if (value == HORIZONTAL || value == BOTH)
68 weightx = 1.0;
69 if (value == VERTICAL || value == BOTH)
70 weighty = 1.0;
71 return this;
72 }
73
74 /**
75 * Set the anchor of this GBC to a.
76 * @param a The new anchor, e.g. GBC.CENTER or GBC.EAST.
77 * @return This constraint for chaining.
78 */
79 public GBC anchor(int a) {
80 anchor = a;
81 return this;
82 }
83
84 /**
85 * Adds insets to this GBC.
86 * @param left The left space of the insets
87 * @param top The top space of the insets
88 * @param right The right space of the insets
89 * @param bottom The bottom space of the insets
90 * @return This constraint for chaining.
91 */
92 public GBC insets(int left, int top, int right, int bottom) {
93 insets = new Insets(top, left, bottom, right);
94 return this;
95 }
96
97 /**
98 * This is a helper to easily create a glue with a minimum default value.
99 * @param x If higher than 0, this will be a horizontal glue with x as minimum
100 * horizontal strut.
101 * @param y If higher than 0, this will be a vertical glue with y as minimum
102 * vertical strut.
103 */
104 public static Component glue(int x, int y) {
105 short maxx = x > 0 ? Short.MAX_VALUE : 0;
106 short maxy = y > 0 ? Short.MAX_VALUE : 0;
107 return new Box.Filler(new Dimension(x,y), new Dimension(x,y), new Dimension(maxx,maxy));
108 }
109}
Note: See TracBrowser for help on using the repository browser.