| 1 | /* BorderLayout.java -- A layout manager class
|
|---|
| 2 | Copyright (C) 1999, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * This class implements a layout manager that positions components
|
|---|
| 43 | * in certain sectors of the parent container.
|
|---|
| 44 | *
|
|---|
| 45 | * @author Aaron M. Renn ([email protected])
|
|---|
| 46 | * @author Rolf W. Rasmussen <[email protected]>
|
|---|
| 47 | */
|
|---|
| 48 | public class BorderLayout implements LayoutManager2, java.io.Serializable
|
|---|
| 49 | {
|
|---|
| 50 |
|
|---|
| 51 | /*
|
|---|
| 52 | * Static Variables
|
|---|
| 53 | */
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Constant indicating the top of the container
|
|---|
| 57 | */
|
|---|
| 58 | public static final String NORTH = "North";
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Constant indicating the bottom of the container
|
|---|
| 62 | */
|
|---|
| 63 | public static final String SOUTH = "South";
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Constant indicating the right side of the container
|
|---|
| 67 | */
|
|---|
| 68 | public static final String EAST = "East";
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Constant indicating the left side of the container
|
|---|
| 72 | */
|
|---|
| 73 | public static final String WEST = "West";
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Constant indicating the center of the container
|
|---|
| 77 | */
|
|---|
| 78 | public static final String CENTER = "Center";
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * The constant indicating the position before the first line of the
|
|---|
| 83 | * layout. The exact position depends on the writing system: For a
|
|---|
| 84 | * top-to-bottom orientation, it is the same as {@link #NORTH}, for
|
|---|
| 85 | * a bottom-to-top orientation, it is the same as {@link #SOUTH}.
|
|---|
| 86 | *
|
|---|
| 87 | * <p>This constant is an older name for {@link #PAGE_START} which
|
|---|
| 88 | * has exactly the same value.
|
|---|
| 89 | *
|
|---|
| 90 | * @since 1.2
|
|---|
| 91 | */
|
|---|
| 92 | public static final String BEFORE_FIRST_LINE = "First";
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * The constant indicating the position after the last line of the
|
|---|
| 97 | * layout. The exact position depends on the writing system: For a
|
|---|
| 98 | * top-to-bottom orientation, it is the same as {@link #SOUTH}, for
|
|---|
| 99 | * a bottom-to-top orientation, it is the same as {@link #NORTH}.
|
|---|
| 100 | *
|
|---|
| 101 | * <p>This constant is an older name for {@link #PAGE_END} which
|
|---|
| 102 | * has exactly the same value.
|
|---|
| 103 | *
|
|---|
| 104 | * @since 1.2
|
|---|
| 105 | */
|
|---|
| 106 | public static final String AFTER_LAST_LINE = "Last";
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * The constant indicating the position before the first item of the
|
|---|
| 111 | * layout. The exact position depends on the writing system: For a
|
|---|
| 112 | * left-to-right orientation, it is the same as {@link #WEST}, for
|
|---|
| 113 | * a right-to-left orientation, it is the same as {@link #EAST}.
|
|---|
| 114 | *
|
|---|
| 115 | * <p>This constant is an older name for {@link #LINE_START} which
|
|---|
| 116 | * has exactly the same value.
|
|---|
| 117 | *
|
|---|
| 118 | * @since 1.2
|
|---|
| 119 | */
|
|---|
| 120 | public static final String BEFORE_LINE_BEGINS = "Before";
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * The constant indicating the position after the last item of the
|
|---|
| 125 | * layout. The exact position depends on the writing system: For a
|
|---|
| 126 | * left-to-right orientation, it is the same as {@link #EAST}, for
|
|---|
| 127 | * a right-to-left orientation, it is the same as {@link #WEST}.
|
|---|
| 128 | *
|
|---|
| 129 | * <p>This constant is an older name for {@link #LINE_END} which
|
|---|
| 130 | * has exactly the same value.
|
|---|
| 131 | *
|
|---|
| 132 | * @since 1.2
|
|---|
| 133 | */
|
|---|
| 134 | public static final String AFTER_LINE_ENDS = "After";
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * The constant indicating the position before the first line of the
|
|---|
| 139 | * layout. The exact position depends on the writing system: For a
|
|---|
| 140 | * top-to-bottom orientation, it is the same as {@link #NORTH}, for
|
|---|
| 141 | * a bottom-to-top orientation, it is the same as {@link #SOUTH}.
|
|---|
| 142 | *
|
|---|
| 143 | * @since 1.4
|
|---|
| 144 | */
|
|---|
| 145 | public static final String PAGE_START = BEFORE_FIRST_LINE;
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * The constant indicating the position after the last line of the
|
|---|
| 150 | * layout. The exact position depends on the writing system: For a
|
|---|
| 151 | * top-to-bottom orientation, it is the same as {@link #SOUTH}, for
|
|---|
| 152 | * a bottom-to-top orientation, it is the same as {@link #NORTH}.
|
|---|
| 153 | *
|
|---|
| 154 | * @since 1.4
|
|---|
| 155 | */
|
|---|
| 156 | public static final String PAGE_END = AFTER_LAST_LINE;
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * The constant indicating the position before the first item of the
|
|---|
| 161 | * layout. The exact position depends on the writing system: For a
|
|---|
| 162 | * left-to-right orientation, it is the same as {@link #WEST}, for
|
|---|
| 163 | * a right-to-left orientation, it is the same as {@link #EAST}.
|
|---|
| 164 | *
|
|---|
| 165 | * @since 1.4
|
|---|
| 166 | */
|
|---|
| 167 | public static final String LINE_START = BEFORE_LINE_BEGINS;
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | /**
|
|---|
| 171 | * The constant indicating the position after the last item of the
|
|---|
| 172 | * layout. The exact position depends on the writing system: For a
|
|---|
| 173 | * left-to-right orientation, it is the same as {@link #EAST}, for
|
|---|
| 174 | * a right-to-left orientation, it is the same as {@link #WEST}.
|
|---|
| 175 | *
|
|---|
| 176 | * @since 1.4
|
|---|
| 177 | */
|
|---|
| 178 | public static final String LINE_END = AFTER_LINE_ENDS;
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 | // Serialization constant
|
|---|
| 183 | private static final long serialVersionUID = -8658291919501921765L;
|
|---|
| 184 |
|
|---|
| 185 | /*************************************************************************/
|
|---|
| 186 |
|
|---|
| 187 | /*
|
|---|
| 188 | * Instance Variables
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * @serial
|
|---|
| 193 | */
|
|---|
| 194 | private Component north;
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * @serial
|
|---|
| 198 | */
|
|---|
| 199 | private Component south;
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * @serial
|
|---|
| 203 | */
|
|---|
| 204 | private Component east;
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * @serial
|
|---|
| 208 | */
|
|---|
| 209 | private Component west;
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * @serial
|
|---|
| 213 | */
|
|---|
| 214 | private Component center;
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | * @serial
|
|---|
| 218 | */
|
|---|
| 219 | private Component firstLine;
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * @serial
|
|---|
| 223 | */
|
|---|
| 224 | private Component lastLine;
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * @serial
|
|---|
| 228 | */
|
|---|
| 229 | private Component firstItem;
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * @serial
|
|---|
| 233 | */
|
|---|
| 234 | private Component lastItem;
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * @serial The horizontal gap between components
|
|---|
| 238 | */
|
|---|
| 239 | private int hgap;
|
|---|
| 240 |
|
|---|
| 241 | /**
|
|---|
| 242 | * @serial The vertical gap between components
|
|---|
| 243 | */
|
|---|
| 244 | private int vgap;
|
|---|
| 245 |
|
|---|
| 246 | /*************************************************************************/
|
|---|
| 247 |
|
|---|
| 248 | /*
|
|---|
| 249 | * Constructors
|
|---|
| 250 | */
|
|---|
| 251 |
|
|---|
| 252 | /**
|
|---|
| 253 | * Initializes a new instance of <code>BorderLayout</code> with no
|
|---|
| 254 | * horiztonal or vertical gaps between components.
|
|---|
| 255 | */
|
|---|
| 256 | public
|
|---|
| 257 | BorderLayout()
|
|---|
| 258 | {
|
|---|
| 259 | this(0,0);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /*************************************************************************/
|
|---|
| 263 |
|
|---|
| 264 | /**
|
|---|
| 265 | * Initializes a new instance of <code>BorderLayout</code> with the
|
|---|
| 266 | * specified horiztonal and vertical gaps between components.
|
|---|
| 267 | *
|
|---|
| 268 | * @param hgap The horizontal gap between components.
|
|---|
| 269 | * @param vgap The vertical gap between components.
|
|---|
| 270 | */
|
|---|
| 271 | public
|
|---|
| 272 | BorderLayout(int hgap, int vgap)
|
|---|
| 273 | {
|
|---|
| 274 | this.hgap = hgap;
|
|---|
| 275 | this.vgap = vgap;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /*************************************************************************/
|
|---|
| 279 |
|
|---|
| 280 | /*
|
|---|
| 281 | * Instance Variables
|
|---|
| 282 | */
|
|---|
| 283 |
|
|---|
| 284 | /**
|
|---|
| 285 | * Returns the horitzontal gap value.
|
|---|
| 286 | *
|
|---|
| 287 | * @return The horitzontal gap value.
|
|---|
| 288 | */
|
|---|
| 289 | public int
|
|---|
| 290 | getHgap()
|
|---|
| 291 | {
|
|---|
| 292 | return(hgap);
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /*************************************************************************/
|
|---|
| 296 |
|
|---|
| 297 | /**
|
|---|
| 298 | * Sets the horizontal gap to the specified value.
|
|---|
| 299 | *
|
|---|
| 300 | * @param hgap The new horizontal gap.
|
|---|
| 301 | */
|
|---|
| 302 | public void
|
|---|
| 303 | setHgap(int hgap)
|
|---|
| 304 | {
|
|---|
| 305 | this.hgap = hgap;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /*************************************************************************/
|
|---|
| 309 |
|
|---|
| 310 | /**
|
|---|
| 311 | * Returns the vertical gap value.
|
|---|
| 312 | *
|
|---|
| 313 | * @return The vertical gap value.
|
|---|
| 314 | */
|
|---|
| 315 | public int
|
|---|
| 316 | getVgap()
|
|---|
| 317 | {
|
|---|
| 318 | return(vgap);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /*************************************************************************/
|
|---|
| 322 |
|
|---|
| 323 | /**
|
|---|
| 324 | * Sets the vertical gap to the specified value.
|
|---|
| 325 | *
|
|---|
| 326 | * @param vgap The new vertical gap value.
|
|---|
| 327 | */
|
|---|
| 328 | public void
|
|---|
| 329 | setVgap(int vgap)
|
|---|
| 330 | {
|
|---|
| 331 | this.vgap = vgap;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /*************************************************************************/
|
|---|
| 335 |
|
|---|
| 336 | /**
|
|---|
| 337 | * Adds a component to the layout in the specified constraint position,
|
|---|
| 338 | * which must be one of the string constants defined in this class.
|
|---|
| 339 | *
|
|---|
| 340 | * @param component The component to add.
|
|---|
| 341 | * @param constraints The constraint string.
|
|---|
| 342 | *
|
|---|
| 343 | * @exception IllegalArgumentException If the constraint object is not
|
|---|
| 344 | * a string, or is not one of the specified constants in this class.
|
|---|
| 345 | */
|
|---|
| 346 | public void
|
|---|
| 347 | addLayoutComponent(Component component, Object constraints)
|
|---|
| 348 | {
|
|---|
| 349 | if (constraints != null && ! (constraints instanceof String))
|
|---|
| 350 | throw new IllegalArgumentException("Constraint must be a string");
|
|---|
| 351 |
|
|---|
| 352 | String str = (String)constraints;
|
|---|
| 353 |
|
|---|
| 354 | if (str == null || str.equals(CENTER))
|
|---|
| 355 | center = component;
|
|---|
| 356 | else if (str.equals(NORTH))
|
|---|
| 357 | north = component;
|
|---|
| 358 | else if (str.equals(SOUTH))
|
|---|
| 359 | south = component;
|
|---|
| 360 | else if (str.equals(EAST))
|
|---|
| 361 | east = component;
|
|---|
| 362 | else if (str.equals(WEST))
|
|---|
| 363 | west = component;
|
|---|
| 364 | else if (str.equals(BEFORE_FIRST_LINE))
|
|---|
| 365 | firstLine = component;
|
|---|
| 366 | else if (str.equals(AFTER_LAST_LINE))
|
|---|
| 367 | lastLine = component;
|
|---|
| 368 | else if (str.equals(BEFORE_LINE_BEGINS))
|
|---|
| 369 | firstItem = component;
|
|---|
| 370 | else if (str.equals(AFTER_LINE_ENDS))
|
|---|
| 371 | lastItem = component;
|
|---|
| 372 | else
|
|---|
| 373 | throw new IllegalArgumentException("Constraint value not valid: " + str);
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /*************************************************************************/
|
|---|
| 377 |
|
|---|
| 378 | /**
|
|---|
| 379 | * Adds a component to the layout in the specified constraint position,
|
|---|
| 380 | * which must be one of the string constants defined in this class.
|
|---|
| 381 | *
|
|---|
| 382 | * @param constraints The constraint string.
|
|---|
| 383 | * @param component The component to add.
|
|---|
| 384 | *
|
|---|
| 385 | * @exception IllegalArgumentException If the constraint object is not
|
|---|
| 386 | * one of the specified constants in this class.
|
|---|
| 387 | *
|
|---|
|
|---|