View Javadoc
1 package jrre.gui; 2 3 import java.awt.*; 4 import java.util.Hashtable; 5 6 /*** 7 * The <code>GraphPaperLayout</code> class is a layout manager that 8 * lays out a container's components in a rectangular grid, similar 9 * to GridLayout. Unlike GridLayout, however, components can take 10 * up multiple rows and/or columns. The layout manager acts as a 11 * sheet of graph paper. When a component is added to the layout 12 * manager, the location and relative size of the component are 13 * simply supplied by the constraints as a Rectangle. 14 * <p><code><pre> 15 * import java.awt.*; 16 * import java.applet.Applet; 17 * public class ButtonGrid extends Applet { 18 * public void init() { 19 * setLayout(new GraphPaperLayout(new Dimension(5,5))); 20 * // Add a 1x1 Rect at (0,0) 21 * add(new Button("1"), new Rectangle(0,0,1,1)); 22 * // Add a 2x1 Rect at (2,0) 23 * add(new Button("2"), new Rectangle(2,0,2,1)); 24 * // Add a 1x2 Rect at (1,1) 25 * add(new Button("3"), new Rectangle(1,1,1,2)); 26 * // Add a 2x2 Rect at (3,2) 27 * add(new Button("4"), new Rectangle(3,2,2,2)); 28 * // Add a 1x1 Rect at (0,4) 29 * add(new Button("5"), new Rectangle(0,4,1,1)); 30 * // Add a 1x2 Rect at (2,3) 31 * add(new Button("6"), new Rectangle(2,3,1,2)); 32 * } 33 * } 34 * </pre></code> 35 * 36 * @author Michael Martak 37 */ 38 39 public class GraphPaperLayout implements LayoutManager2 { 40 int hgap; //horizontal gap 41 int vgap; //vertical gap 42 Dimension gridSize; //grid size in logical units (n x m) 43 Hashtable compTable; //constraints (Rectangles) 44 45 /*** 46 * Creates a graph paper layout with a default of a 1 x 1 graph, with no 47 * vertical or horizontal padding. 48 */ 49 public GraphPaperLayout() { 50 this(new Dimension(1,1)); 51 } 52 53 /*** 54 * Creates a graph paper layout with the given grid size, with no vertical 55 * or horizontal padding. 56 */ 57 public GraphPaperLayout(Dimension gridSize) { 58 this(gridSize, 0, 0); 59 } 60 61 /*** 62 * Creates a graph paper layout with the given grid size and padding. 63 * @param gridSize size of the graph paper in logical units (n x m) 64 * @param hgap horizontal padding 65 * @param vgap vertical padding 66 */ 67 public GraphPaperLayout(Dimension gridSize, int hgap, int vgap) { 68 if ((gridSize.width <= 0) || (gridSize.height <= 0)) { 69 throw new IllegalArgumentException( 70 "dimensions must be greater than zero"); 71 } 72 this.gridSize = new Dimension(gridSize); 73 this.hgap = hgap; 74 this.vgap = vgap; 75 compTable = new Hashtable(); 76 } 77 78 /*** 79 * @return the size of the graph paper in logical units (n x m) 80 */ 81 public Dimension getGridSize() { 82 return new Dimension( gridSize ); 83 } 84 85 /*** 86 * Set the size of the graph paper in logical units (n x m) 87 */ 88 public void setGridSize( Dimension d ) { 89 setGridSize( d.width, d.height ); 90 } 91 92 /*** 93 * Set the size of the graph paper in logical units (n x m) 94 */ 95 public void setGridSize( int width, int height ) { 96 gridSize = new Dimension( width, height ); 97 } 98 99 public void setConstraints(Component comp, Rectangle constraints) { 100 compTable.put(comp, new Rectangle(constraints)); 101 } 102 103 /*** 104 * Adds the specified component with the specified name to 105 * the layout. This does nothing in GraphPaperLayout, since constraints 106 * are required. 107 */ 108 public void addLayoutComponent(String name, Component comp) { 109 } 110 111 /*** 112 * Removes the specified component from the layout. 113 * @param comp the component to be removed 114 */ 115 public void removeLayoutComponent(Component comp) { 116 compTable.remove(comp); 117 } 118 119 /*** 120 * Calculates the preferred size dimensions for the specified 121 * panel given the components in the specified parent container. 122 * @param parent the component to be laid out 123 * 124 * @see #minimumLayoutSize 125 */ 126 public Dimension preferredLayoutSize(Container parent) { 127 return getLayoutSize(parent, true); 128 } 129 130 /*** 131 * Calculates the minimum size dimensions for the specified 132 * panel given the components in the specified parent container. 133 * @param parent the component to be laid out 134 * @see #preferredLayoutSize 135 */ 136 public Dimension minimumLayoutSize(Container parent) { 137 return getLayoutSize(parent, false); 138 } 139 140 /*** 141 * Algorithm for calculating layout size (minimum or preferred). 142 * <p> 143 * The width of a graph paper layout is the largest cell width 144 * (calculated in <code>getLargestCellSize()</code> times the number of 145 * columns, plus the horizontal padding times the number of columns 146 * plus one, plus the left and right insets of the target container. 147 * <p> 148 * The height of a graph paper layout is the largest cell height 149 * (calculated in <code>getLargestCellSize()</code> times the number of 150 * rows, plus the vertical padding times the number of rows 151 * plus one, plus the top and bottom insets of the target container. 152 * 153 * @param parent the container in which to do the layout. 154 * @param isPreferred true for calculating preferred size, false for 155 * calculating minimum size. 156 * @return the dimensions to lay out the subcomponents of the specified 157 * container. 158 */ 159 protected Dimension getLayoutSize(Container parent, boolean isPreferred) { 160 Dimension largestSize = getLargestCellSize(parent, isPreferred); 161 Insets insets = parent.getInsets(); 162 largestSize.width = ( largestSize.width * gridSize.width ) + 163 ( hgap * ( gridSize.width + 1 ) ) + insets.left + insets.right; 164 largestSize.height = ( largestSize.height * gridSize.height ) + 165 ( vgap * ( gridSize.height + 1 ) ) + insets.top + insets.bottom; 166 return largestSize; 167 } 168 169 /*** 170 * Algorithm for calculating the largest minimum or preferred cell size. 171 * <p> 172 * Largest cell size is calculated by getting the applicable size of each 173 * component and keeping the maximum value, dividing the component's width 174 * by the number of columns it is specified to occupy and dividing the 175 * component's height by the number of rows it is specified to occupy. 176 * 177 * @param parent the container in which to do the layout. 178 * @param isPreferred true for calculating preferred size, false for 179 * calculating minimum size. 180 * @return the largest cell size required. 181 */ 182 protected Dimension getLargestCellSize(Container parent, 183 boolean isPreferred) { 184 int ncomponents = parent.getComponentCount(); 185 Dimension maxCellSize = new Dimension(0,0); 186 for ( int i = 0; i < ncomponents; i++ ) { 187 Component c = parent.getComponent(i); 188 Rectangle rect = (Rectangle)compTable.get(c); 189 if ( c != null && rect != null ) { 190 Dimension componentSize; 191 if ( isPreferred ) { 192 componentSize = c.getPreferredSize(); 193 } else { 194 componentSize = c.getMinimumSize(); 195 } 196 // Note: rect dimensions are already asserted to be > 0 when the 197 // component is added with constraints 198 maxCellSize.width = Math.max(maxCellSize.width, 199 componentSize.width / rect.width); 200 maxCellSize.height = Math.max(maxCellSize.height, 201 componentSize.height / rect.height); 202 } 203 } 204 return maxCellSize; 205 } 206 207 /*** 208 * Lays out the container in the specified container. 209 * @param parent the component which needs to be laid out 210 */ 211 public void layoutContainer(Container parent) { 212 synchronized (parent.getTreeLock()) { 213 Insets insets = parent.getInsets(); 214 int ncomponents = parent.getComponentCount(); 215 216 if (ncomponents == 0) { 217 return; 218 } 219 220 // Total parent dimensions 221 Dimension size = parent.getSize(); 222 int totalW = size.width - (insets.left + insets.right); 223 int totalH = size.height - (insets.top + insets.bottom); 224 225 // Cell dimensions, including padding 226 int totalCellW = totalW / gridSize.width; 227 int totalCellH = totalH / gridSize.height; 228 229 // Cell dimensions, without padding 230 int cellW = (totalW - ( (gridSize.width + 1) * hgap) ) 231 / gridSize.width; 232 int cellH = (totalH - ( (gridSize.height + 1) * vgap) ) 233 / gridSize.height; 234 235 for ( int i = 0; i < ncomponents; i++ ) { 236 Component c = parent.getComponent(i); 237 Rectangle rect = (Rectangle)compTable.get(c); 238 if ( rect != null ) { 239 int x = insets.left + ( totalCellW * rect.x ) + hgap; 240 int y = insets.top + ( totalCellH * rect.y ) + vgap; 241 int w = ( cellW * rect.width ) - hgap; 242 int h = ( cellH * rect.height ) - vgap; 243 c.setBounds(x, y, w, h); 244 } 245 } 246 } 247 } 248 249 // LayoutManager2 ///////////////////////////////////////////////////////// 250 251 /*** 252 * Adds the specified component to the layout, using the specified 253 * constraint object. 254 * @param comp the component to be added 255 * @param constraints where/how the component is added to the layout. 256 */ 257 public void addLayoutComponent(Component comp, Object constraints) { 258 if (constraints instanceof Rectangle) { 259 Rectangle rect = (Rectangle)constraints; 260 if ( rect.width <= 0 || rect.height <= 0 ) { 261 throw new IllegalArgumentException( 262 "cannot add to layout: rectangle must have positive width and height"); 263 } 264 if ( rect.x < 0 || rect.y < 0 ) { 265 throw new IllegalArgumentException( 266 "cannot add to layout: rectangle x and y must be >= 0"); 267 } 268 setConstraints(comp, rect); 269 } else if (constraints != null) { 270 throw new IllegalArgumentException( 271 "cannot add to layout: constraint must be a Rectangle"); 272 } 273 } 274 275 /*** 276 * Returns the maximum size of this component. 277 * @see java.awt.Component#getMinimumSize() 278 * @see java.awt.Component#getPreferredSize() 279 * @see LayoutManager 280 */ 281 public Dimension maximumLayoutSize(Container target) { 282 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); 283 } 284 285 /*** 286 * Returns the alignment along the x axis. This specifies how 287 * the component would like to be aligned relative to other 288 * components. The value should be a number between 0 and 1 289 * where 0 represents alignment along the origin, 1 is aligned 290 * the furthest away from the origin, 0.5 is centered, etc. 291 */ 292 public float getLayoutAlignmentX(Container target) { 293 return 0.5f; 294 } 295 296 /*** 297 * Returns the alignment along the y axis. This specifies how 298 * the component would like to be aligned relative to other 299 * components. The value should be a number between 0 and 1 300 * where 0 represents alignment along the origin, 1 is aligned 301 * the furthest away from the origin, 0.5 is centered, etc. 302 */ 303 public float getLayoutAlignmentY(Container target) { 304 return 0.5f; 305 } 306 307 /*** 308 * Invalidates the layout, indicating that if the layout manager 309 * has cached information it should be discarded. 310 */ 311 public void invalidateLayout(Container target) { 312 // Do nothing 313 } 314 }

This page was automatically generated by Maven