/* * @(#)Point2DView.java 1.0 14 July 2001 * * Copyright 2001 * College of Computer Science * Northeastern University * Boston, MA 02115 * * This software may be used for educational purposes as long as * this copyright notice is retained intact at the top of all files. * * Should this software be modified, the words "Modified from * Original" must be included as a comment below this notice. * * All publication rights are retained. This software or its * documentation may not be published in any media either in whole * or in part without explicit permission. * * Contact information: * Richard Rasala rasala@ccs.neu.edu * Viera Proulx vkp@ccs.neu.edu * Jeff Raab jmr@ccs.neu.edu * * Telephone: 617-373-2462 * * This software was created with support from Northeastern * University and from NSF grant DUE-9950829. */ import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.codec.*; import edu.neu.ccs.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.geom.*; import java.text.NumberFormat; import java.text.DecimalFormat; import javax.swing.*; /** *

View for input of a Point2D.Double object * or an XPoint2D object.

* * @author Viera Proulx * @author Richard Rasala * @version 14 July 2001 */ public final class Point2DView extends Display implements TypedView, JPTConstants { /////////////// // Constants // /////////////// /** Default value for each TextFieldView in the Point2DView. */ public static final String DEFAULT_VALUE = null; /** Default title for the Point2DView. */ public static final String DEFAULT_TITLE = null; /** Default prefix for the Point2DView for horizontal layout only */ public static final String DEFAULT_PREFIX = null; /** Default label for the x coordinate text field view */ public static final String DEFAULT_LABELx = "X:"; /** Default label for the y coordinate text field view */ public static final String DEFAULT_LABELy = "Y:"; /** Default layout orientation for the Point2DView . */ public static final int DEFAULT_ORIENTATION = VERTICAL; ///////////////// // Member Data // ///////////////// /** Input field for the x coordinate of a point. */ protected TextFieldView xcoord; /** Input field for the y coordinate of a point. */ protected TextFieldView ycoord; /** Property list for this view object. */ protected InputProperties properties = new InputProperties(); ////////////////// // Constructors // ////////////////// /** * Default constructor. * * @see #Point2DView(int) * @see #Point2DView(String, String) * @see #Point2DView(String, String, String, String, int) * @see #Point2DView(String, String, String, String, String, String, int) */ public Point2DView() { this(DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_LABELx, DEFAULT_LABELy, DEFAULT_TITLE, DEFAULT_PREFIX, DEFAULT_ORIENTATION); } /** * Constructor with the desired orientation. * * @param int orientation the desired VERTICAL or HORIZONTAL orientation * * @see #Point2DView() * @see #Point2DView(String, String) * @see #Point2DView(String, String, String, String, int) * @see #Point2DView(String, String, String, String, String, String, int) */ public Point2DView(int orientation) { this(DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_LABELx, DEFAULT_LABELy, DEFAULT_TITLE, DEFAULT_PREFIX, orientation); } /** * Constructor with the desired default values for the x and y views. * * @param String xDefault the default value for the x coordinate view * @param String yDefault the default value for the y coordinate view * * @see #Point2DView() * @see #Point2DView(int) * @see #Point2DView(String, String, String, String, int) * @see #Point2DView(String, String, String, String, String, String, int) */ public Point2DView(String xDefault, String yDefault) { this(xDefault, yDefault, DEFAULT_LABELx, DEFAULT_LABELy, DEFAULT_TITLE, DEFAULT_PREFIX, DEFAULT_ORIENTATION); } /** * Constructor with the desired default values for the x and y views, * the desired labels for these x and y views, * and the desired orientation. * * @param String xDefault the default value for the x coordinate view * @param String yDefault the default value for the y coordinate view * @param String labelX the label for the x coordinate text field view * @param String labelY the label for the y coordinate text field view * @param int orientation the desired VERTICAL or HORIZONTAL orientation * * @see #Point2DView() * @see #Point2DView(int) * @see #Point2DView(String, String) * @see #Point2DView(String, String, String, String, String, String, int) */ public Point2DView( String xDefault, String yDefault, String labelX, String labelY, int orientation) { this(xDefault, yDefault, labelX, labelY, DEFAULT_TITLE, DEFAULT_PREFIX, orientation); } /** * Constructor with the desired default values for the x and y views, * the desired labels for these x and y views, * the desired display title if any, * the desired annotation prefix if any, * and the desired orientation. * * @param String xDefault the default value for the x coordinate view * @param String yDefault the default value for the y coordinate view * @param String labelX the label for the x coordinate text field view * @param String labelY the label for the y coordinate text field view * @param String title the title for the view * @param String prefix the prefix for the view * @param int orientation the desired VERTICAL or HORIZONTAL orientation * * @see #Point2DView() * @see #Point2DView(int) * @see #Point2DView(String, String) * @see #Point2DView(String, String, String, String, int) */ public Point2DView( String xDefault, String yDefault, String labelX, String labelY, String title, String prefix, int orientation) { super(); // add the internal panel to the Display add(createDisplayPair (xDefault, yDefault, labelX, labelY, orientation)); // set the title and annotation for the Display setTitleText(title); setAnnotationText(prefix); } ////////////////////////////////// // Helpers for the Constructors // ////////////////////////////////// /** * Helper function that constructs a display collection containing * two text field views with the specified default settings, * labels, and orientation * * @param String xDefault the default value for the x coordinate view * @param String yDefault the default value for the y coordinate view * @param String labelX the label for the x coordinate text field view * @param String labelY the label for the y coordinate text field view * @param int orientation the desired VERTICAL or HORIZONTAL orientation */ protected DisplayCollection createDisplayPair( String xDefault, String yDefault, String labelX, String labelY, int orientation) { createTFVPair(xDefault, yDefault); DisplayCollection pointDisplay = new DisplayCollection(); pointDisplay.setOrientation(orientation); pointDisplay.add( new DisplayWrapper( new Display(xcoord, labelX, null))); pointDisplay.add( new DisplayWrapper( new Display(ycoord, labelY, null))); return pointDisplay; } /** * Creates two text field views with the desired default values * * @param String xDefault the default value for the x coordinate view * @param String yDefault the default value for the y coordinate view */ protected void createTFVPair(String xDefault, String yDefault) { xcoord = new TextFieldView( xDefault, "Please enter a real number:", "Point2D: x coordinate error"); ycoord = new TextFieldView( yDefault, "Please enter a real number:", "Point2D: y coordinate error"); xcoord.setDefaultViewState(xDefault); ycoord.setDefaultViewState(yDefault); // set width for input fields xcoord.setPreferredWidth(120); ycoord.setPreferredWidth(120); } /////////////// // TypedView // /////////////// /** * Returns a new instance of the appropriate type of object for this * view whose state is set from the data state of this view. * * @see #requestObject() * @see #demandPoint2D() * @see #requestPoint2D() */ public Stringable demandObject() { return new XPoint2D( xcoord.demandDouble(), ycoord.demandDouble()); } /** * Returns a new instance of the appropriate type of object for this * view whose state is set from the data state of this view, * or throws an exception if the input operation is cancelled. * * @throws CancelledException if the user cancels the input operation * @see #demandObject() * @see #demandPoint2D() * @see #requestPoint2D() */ public Stringable requestObject() throws CancelledException { return new XPoint2D( xcoord.requestDouble(), ycoord.requestDouble()); } /** * Sets the input properties for this view * to the provided input properties. * * @param properties the desired input properties for the view * @see #getInputProperties() */ public void setInputProperties(InputProperties p) { properties = p; } /** * Returns the input properties associated with this view. * * @see #setInputProperties(InputProperties) */ public InputProperties getInputProperties() { return properties; } /** * Returns the current class of objects of which a new instance * is created when an object is requested. */ public Class getDataType() { return XPoint2D.class; } ///////////////// // Displayable // ///////////////// /** * Sets the data state for this view to the data encoded in the * given String. Note that this data need not * represent a well-formed point data. * * @param data the desired encoded data String */ public void setViewState(String data) { String[] terms = CodecUtilities.decode(data); xcoord.setViewState(terms[0]); ycoord.setViewState(terms[1]); } /** * Returns the data state for this view as an encoded * String. Note that the data state for this view * need not represent a well-formed point data. */ public String getViewState() { return CodecUtilities.encode( new String[] { xcoord.getViewState(), ycoord.getViewState()}); } /** * Sets the default value for this view to the data encoded in * the given String. Note that this data need not * represent a well-formed point data. * * @param data the desired encoded data String */ public void setDefaultViewState(String data) { String[] terms = CodecUtilities.decode(data); xcoord.setDefaultViewState(terms[0]); ycoord.setDefaultViewState(terms[1]); } /** * Returns the default view state for this view as an encoded * String. Note that the data state for this view * need not represent a well-formed point data. */ public String getDefaultViewState() { return CodecUtilities.encode( new String[] { xcoord.getDefaultViewState(), ycoord.getDefaultViewState()}); } ////////////////////////////////////////////////// // Variations of demandObject and requestObject // ////////////////////////////////////////////////// /** * Gets the data state for this view as a Point2D.Double object * * @see #demandObject() * @see #requestObject() * @see #requestPoint2D() */ public Point2D.Double demandPoint2D(){ return new Point2D.Double( xcoord.demandDouble(), ycoord.demandDouble()); } /** * Gets the data state for this view as a Point2D.Double object * or throws an exception if the input operation is cancelled. * * @throws CancelledException if the user cancels the input operation * @see #demandObject() * @see #requestObject() * @see #demandPoint2D() */ public Point2D.Double requestPoint2D() throws CancelledException { return new Point2D.Double( xcoord.requestDouble(), ycoord.requestDouble()); } //////////////////////////////// // Variations of setViewState // //////////////////////////////// /** * Sets the data state for this view to the data stored in * the pair of strings passed. Note that this data need * not represent a well-formed point data. * * @param x the string for the x coordinate * @param y the string for the y coordinate */ public void setViewState(String x, String y) { xcoord.setViewState(x); ycoord.setViewState(y); } /** * Sets the default view state for this view to the data stored in * the pair of strings passed. Note that this data need * not represent a well-formed point data. * * @param x the string for the x coordinate * @param y the string for the y coordinate */ public void setDefaultViewState(String x, String y) { xcoord.setDefaultViewState(x); ycoord.setDefaultViewState(y); } /** * Sets the data state for this view to the given * Point2D.Double object. * * @param p the desired Point2D.Double object */ public void setViewState(Point2D.Double p){ if (p != null) setViewState(p.x, p.y); } /** * Sets the data state for this view to the given * x, y pair of doubles. * * @param x the desired x coordinate value * @param y the desired y coordinate value */ public void setViewState(double x, double y){ setViewState(x + "", y + ""); } /** * Sets the data state for this view to the given * Point2D.Double object * using the specified number format. * * @param p the desired Point2D.Double object * @param f the desired number format object */ public void setViewState(Point2D.Double p, NumberFormat f) { if (p != null) setViewState(p.x, p.y, f); } /** * Sets the data state for this view to the given * x, y, coordinates * using the specified number format. * * @param x the desired x coordinate value * @param y the desired y coordinate value * @param f the desired number format object */ public void setViewState(double x, double y, NumberFormat f) { setViewState(f.format(x), f.format(y)); } /////////////////// // Other Methods // /////////////////// /** * Returns the two coordinate views in an array. */ public TextFieldView[] getViews() { return new TextFieldView[] {xcoord, ycoord}; } /** * Sets the preferred width for the two coordinate views * to the desired value * * @param width the desired width */ public void setPreferredWidth(int width){ xcoord.setPreferredWidth(width); ycoord.setPreferredWidth(width); } }