acm.gui.TableConstraints Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javakarel Show documentation
Show all versions of javakarel Show documentation
This the original Stanford Karel for Java, packaged for Maven. ACM Library is included. See also https://cs.stanford.edu/people/eroberts/karel-the-robot-learns-java.pdf
The newest version!
/*
* @(#)TableConstraints.java 1.99.1 08/12/08
*/
// ************************************************************************
// * Copyright (c) 2008 by the Association for Computing Machinery *
// * *
// * The Java Task Force seeks to impose few restrictions on the use of *
// * these packages so that users have as much freedom as possible to *
// * use this software in constructive ways and can make the benefits of *
// * that work available to others. In view of the legal complexities *
// * of software development, however, it is essential for the ACM to *
// * maintain its copyright to guard against attempts by others to *
// * claim ownership rights. The full text of the JTF Software License *
// * is available at the following URL: *
// * *
// * http://www.acm.org/jtf/jtf-software-license.pdf *
// * *
// ************************************************************************
// REVISION HISTORY
//
// -- V2.0 --
// Code cleanup 28-May-07 (ESR)
// 1. Added generic type tags.
//
// Feature enhancement 26-May-08 (ESR)
// 1. Added support for serialization.
package acm.gui;
import acm.util.*;
import java.awt.*;
import java.util.*;
/* Class: TableConstraints */
/**
* This class specifies a set of constraints appropriate to a
* TableLayout
or GridBagLayout
. It has
* the following advantages over the GridBagConstraints
* class on which it is based:
*
*
* - The constraints can be specified as an easily readable string
* instead of by initializing the individual fields.
*
- The class includes new
width
and height
* fields that can be used with TableLayout
to specify
* minimum heights and widths for rows and columns.
* - The class provides accessor methods (
getGridX
* and so forth) so that clients can operate as if the fields
* are private.
* - The class includes a
toString
method that
* displays nondefault values of the fields in a readable way.
*
*
*
To create a TableConstraints
object, use the
* constructor with a string argument to set the fields of the
* underlying GridBagConstraints
object. For example,
* suppose you wanted to achieve the effect of the traditional code
*
*
* GridBagConstraints gbc = new GridBagConstraints();
* gbc.gridx = 2;
* gbc.gridy = 3;
* gbc.fill = GridBagConstraints.BOTH;
*
*
*
Using TableConstraints
, you can do all of this
* with the constructor, as follows:
*
*
* new TableConstraints("gridx=2 gridy=3 fill=BOTH");
*
*/
public class TableConstraints extends GridBagConstraints {
/* Field: width */
/**
* Specifies the desired width of this cell. The width of a column
* is taken to be the maximum of the specified cell widths. If this
* field has its default value of 0, the width is taken from the
* preferred size of the component.
*/
public int width;
/* Field: height */
/**
* Specifies the desired height of this cell. The height of a row
* is taken to be the maximum of the specified cell heights. If this
* field has its default value of 0, the height is taken from the
* preferred size of the component.
*/
public int height;
/* Constructor: TableConstraints() */
/**
* Creates a new TableConstraints
object with default
* values for each of the fields.
*
* Example: TableConstraints constraints = new TableConstraints();
*/
public TableConstraints() {
this("");
}
/* Constructor: TableConstraints(str) */
/**
* Creates a new TableConstraints
object whose components
* are initialized according from the specified string. Each field is
* initialized by specifying a binding in the form
*
*
* key=value
*
*
*
where key is the name of one of the public fields
* in the TableConstraints
class and value is the
* corresponding value, which can be expressed either as an integer
* or as one of the constant names appropriate to that field. For
* example, the string
*
*
* "width=20 fill=BOTH"
*
*
*
would create a TableConstraints
object whose
* width
field was set to 20 and whose fill
* field was set the the constant GridBagConstraints.BOTH
.
*
*
As a special case, the four elements of the insets
* field can be set using the key names left
, right
,
* top
, and bottom
. Also, because the names
* are more likely to indicate their purposes to novices, the HTML names
* rowspan
and colspan
can be used in place
* of gridwidth
and gridheight
.
*
* Example: TableConstraints constraints = new TableConstraints(str);
* @param str The constraint string as a series of key/value pairs
*/
public TableConstraints(String str) {
this(new OptionTable(str.toLowerCase(), LEGAL_KEYS).getMap());
}
/* Constructor: TableConstraints(map) */
/**
* Creates a new TableConstraints
object whose components
* are the key/value pairs in the map.
*
* Example: TableConstraints constraints = new TableConstraints(map);
* @param map A map containing the key/value pairs
*
*/
public TableConstraints(Map map) {
OptionTable constraintTable = new OptionTable(map);
gridx = parseXYConstraint(constraintTable.getOption("gridx"));
gridy = parseXYConstraint(constraintTable.getOption("gridy"));
String rowSpan = constraintTable.getOption("gridwidth");
if (rowSpan == null) rowSpan = constraintTable.getOption("rowspan");
String colSpan = constraintTable.getOption("gridheight");
if (colSpan == null) colSpan = constraintTable.getOption("colspan");
gridwidth = parseSpanConstraint(rowSpan);
gridheight = parseSpanConstraint(colSpan);
fill = parseFillConstraint(constraintTable.getOption("fill"));
anchor = parseAnchorConstraint(constraintTable.getOption("anchor"));
ipadx = constraintTable.getIntOption("ipadx", 0);
ipady = constraintTable.getIntOption("ipady", 0);
weightx = constraintTable.getDoubleOption("weightx", 0.0);
weighty = constraintTable.getDoubleOption("weighty", 0.0);
insets.left = constraintTable.getIntOption("left", 0);
insets.right = constraintTable.getIntOption("right", 0);
insets.top = constraintTable.getIntOption("top", 0);
insets.bottom = constraintTable.getIntOption("bottom", 0);
width = constraintTable.getIntOption("width", -1);
if (width == -1) {
width = 0;
} else {
width += insets.left + insets.right;
}
height = constraintTable.getIntOption("height", -1);
if (height == -1) {
height = 0;
} else {
height += insets.top + insets.bottom;
}
if (gridwidth != 1 && width != 0) {
throw new ErrorException("TableConstraints: Cannot specify both width and gridwidth");
}
if (gridheight != 1 && height != 0) {
throw new ErrorException("TableConstraints: Cannot specify both height and gridheight");
}
}
/* Constructor: TableConstraints(gbc) */
/**
* Creates a new TableConstraints
object whose fields match those
* of the specified GridBagConstraints
object. Clients will not
* ordinarily need to call this version of the constructor.
*
* Example: TableConstraints constraints = new TableConstraints(gbc);
* @param gbc The GridBagConstraints
object to copy
*
*/
public TableConstraints(GridBagConstraints gbc) {
gridx = gbc.gridx;
gridy = gbc.gridy;
gridwidth = gbc.gridwidth;
gridheight = gbc.gridheight;
fill = gbc.fill;
anchor = gbc.anchor;
ipadx = gbc.ipadx;
ipady = gbc.ipady;
weightx = gbc.weightx;
weighty = gbc.weighty;
insets.left = gbc.insets.left;
insets.right = gbc.insets.right;
insets.top = gbc.insets.top;
insets.bottom = gbc.insets.bottom;
if (gbc instanceof TableConstraints) {
TableConstraints tc = (TableConstraints) gbc;
width = tc.width;
height = tc.height;
}
}
/* Method: getAnchor() */
/**
* Returns the anchor
field from the constraint.
*
* Example: int anchor = constraint.getAnchor();
* @return The anchor
field from the constraint
*/
public int getAnchor() {
return anchor;
}
/* Method: getFill() */
/**
* Returns the fill
field from the constraint.
*
* Example: int fill = constraint.getFill();
* @return The fill
field from the constraint
*/
public int getFill() {
return fill;
}
/* Method: getGridX() */
/**
* Returns the gridx
field from the constraint.
*
* Example: int gridx = constraint.getGridX();
* @return The gridx
field from the constraint
*/
public int getGridX() {
return gridx;
}
/* Method: getGridY() */
/**
* Returns the gridy
field from the constraint.
*
* Example: int gridy = constraint.getGridY();
* @return The gridy
field from the constraint
*/
public int getGridY() {
return gridy;
}
/* Method: getGridWidth() */
/**
* Returns the gridwidth
field from the constraint.
*
* Example: int gridwidth = constraint.getGridWidth();
* @return The gridwidth
field from the constraint
*/
public int getGridWidth() {
return gridwidth;
}
/* Method: getGridHeight() */
/**
* Returns the gridheight
field from the constraint.
*
* Example: int gridheight = constraint.getGridHeight();
* @return The gridheight
field from the constraint
*/
public int getGridHeight() {
return gridheight;
}
/* Method: getIPadX() */
/**
* Returns the ipadx
field from the constraint.
*
* Example: int ipadx = constraint.getIPadX();
* @return The ipadx
field from the constraint
*/
public int getIPadX() {
return ipadx;
}
/* Method: getIPadY() */
/**
* Returns the ipady
field from the constraint.
*
* Example: int ipady = constraint.getIPadY();
* @return The ipady
field from the constraint
*/
public int getIPadY() {
return ipady;
}
/* Method: getInsets() */
/**
* Returns the insets
field from the constraint.
*
* Example: Insets insets = constraint.getInsets();
* @return The insets
field from the constraint
*/
public Insets getInsets() {
return insets;
}
/* Method: getWeightX() */
/**
* Returns the weightx
field from the constraint.
*
* Example: double weightx = constraint.getWeightX();
* @return The weightx
field from the constraint
*/
public double getWeightX() {
return weightx;
}
/* Method: getWeightY() */
/**
* Returns the weighty
field from the constraint.
*
* Example: double weighty = constraint.getWeightY();
* @return The weighty
field from the constraint
*/
public double getWeightY() {
return weighty;
}
/* Method: getWidth() */
/**
* Returns the width
field from the constraint.
*
* Example: int width = constraint.getWidth();
* @return The width
field from the constraint
*/
public int getWidth() {
return width;
}
/* Method: getHeight() */
/**
* Returns the height
field from the constraint.
*
* Example: int height = constraint.getHeight();
* @return The height
field from the constraint
*/
public int getHeight() {
return height;
}
/* Override method: toString() */
/**
* Converts the constraint into a readable string.
*
* Example: String str = constraint.toString();
* @return A readable string version of the constraint
*
*/
public String toString() {
String str = getClass().getName();
str += "[gridx=" + gridx + ",gridy=" + gridy;
switch (fill) {
case VERTICAL: str += ",fill=VERTICAL"; break;
case HORIZONTAL: str += ",fill=HORIZONTAL"; break;
case BOTH: str += ",fill=BOTH"; break;
}
switch (anchor) {
case NORTH: str += ",anchor=NORTH"; break;
case SOUTH: str += ",anchor=SOUTH"; break;
case EAST: str += ",anchor=EAST"; break;
case WEST: str += ",anchor=WEST"; break;
case NORTHEAST: str += ",anchor=NORTHEAST"; break;
case NORTHWEST: str += ",anchor=NORTHWEST"; break;
case SOUTHEAST: str += ",anchor=SOUTHEAST"; break;
case SOUTHWEST: str += ",anchor=SOUTHWEST"; break;
case MY_PAGE_START: str += ",anchor=PAGE_START"; break;
case MY_PAGE_END: str += ",anchor=PAGE_END"; break;
case MY_LINE_START: str += ",anchor=LINE_START"; break;
case MY_LINE_END: str += ",anchor=LINE_END"; break;
case MY_FIRST_LINE_START: str += ",anchor=FIRST_LINE_START"; break;
case MY_FIRST_LINE_END: str += ",anchor=FIRST_LINE_END"; break;
case MY_LAST_LINE_START: str += ",anchor=LAST_LINE_START"; break;
case MY_LAST_LINE_END: str += ",anchor=LAST_LINE_END"; break;
}
if (gridwidth != 1) str += ",gridwidth=" + gridwidth;
if (gridheight != 1) str += ",gridheight=" + gridheight;
if (ipadx != 0) str += ",ipadx=" + ipadx;
if (ipady != 0) str += ",ipady=" + ipady;
if (insets.left != 0) str += ",left=" + insets.left;
if (insets.right != 0) str += ",right=" + insets.right;
if (insets.top != 0) str += ",top=" + insets.top;
if (insets.bottom != 0) str += ",bottom=" + insets.bottom;
if (width != 0) str += ",width=" + width;
if (height != 0) str += ",height=" + height;
str += "]";
return str;
}
/* Private method: parseXYConstraint(str) */
/**
* Reads the gridx
and gridy
values.
*/
private int parseXYConstraint(String str) {
if (str == null) return RELATIVE;
if (str.equals("relative")) return RELATIVE;
try {
return Integer.decode(str).intValue();
} catch (NumberFormatException ex) {
throw new ErrorException("TableConstraints: Illegal grid coordinate");
}
}
/* Private method: parseSpanConstraint(str) */
/**
* Reads the gridwidth
and gridheight
values.
*/
private int parseSpanConstraint(String str) {
if (str == null) return 1;
if (str.equals("relative")) return RELATIVE;
if (str.equals("remainder")) return REMAINDER;
try {
return Integer.decode(str).intValue();
} catch (NumberFormatException ex) {
throw new ErrorException("TableConstraints: Illegal span constraint");
}
}
/* Private method: parseAnchorConstraint(str) */
/**
* Reads the value of an anchor
constraint.
*/
private int parseAnchorConstraint(String str) {
if (str == null) return CENTER;
if (str.equals("center")) return CENTER;
if (str.equals("north")) return NORTH;
if (str.equals("south")) return SOUTH;
if (str.equals("east")) return EAST;
if (str.equals("west")) return WEST;
if (str.equals("northeast") || str.equals("ne")) return NORTHEAST;
if (str.equals("northwest") || str.equals("nw")) return NORTHWEST;
if (str.equals("southeast") || str.equals("se")) return SOUTHEAST;
if (str.equals("southwest") || str.equals("sw")) return SOUTHWEST;
if (str.equals("page_start")) return MY_PAGE_START;
if (str.equals("page_end")) return MY_PAGE_END;
if (str.equals("line_start")) return MY_LINE_START;
if (str.equals("line_end")) return MY_LINE_END;
if (str.equals("first_line_start")) return MY_FIRST_LINE_START;
if (str.equals("first_line_end")) return MY_FIRST_LINE_END;
if (str.equals("last_line_start")) return MY_LAST_LINE_START;
if (str.equals("last_line_end")) return MY_LAST_LINE_END;
throw new ErrorException("TableConstraints: Illegal anchor specification");
}
/* Private method: parseFillConstraint(str) */
/**
* Reads the value of an fill
constraint.
*/
private int parseFillConstraint(String str) {
if (str == null || str.equals("none")) return NONE;
if (str.equals("horizontal")) return HORIZONTAL;
if (str.equals("vertical")) return VERTICAL;
if (str.equals("both")) return BOTH;
throw new ErrorException("TableConstraints: Illegal fill specification");
}
/* Constants that allow this file to be compiled with old libraries */
private static final int MY_PAGE_START = 19;
private static final int MY_PAGE_END = 20;
private static final int MY_LINE_START = 21;
private static final int MY_LINE_END = 22;
private static final int MY_FIRST_LINE_START = 23;
private static final int MY_FIRST_LINE_END = 24;
private static final int MY_LAST_LINE_START = 25;
private static final int MY_LAST_LINE_END = 26;
/* Array of legal keys */
protected static final String[] LEGAL_KEYS = {
"anchor",
"bottom",
"colspan",
"fill",
"gridwidth",
"gridheight",
"gridx",
"gridy",
"height",
"ipadx",
"ipady",
"left",
"right",
"rowspan",
"top",
"weightx",
"weighty",
"width"
};
/* Serial version UID */
/**
* The serialization code for this class. This value should be incremented
* whenever you change the structure of this class in an incompatible way,
* typically by adding a new instance variable.
*/
static final long serialVersionUID = 1L;
}