Class DisplayLabel

java.lang.Object
java.awt.Component
java.awt.Label
edu.hws.jcm.awt.DisplayLabel
All Implemented Interfaces:
Computable, ImageObserver, MenuContainer, Serializable, Accessible

public class DisplayLabel extends Label implements Computable
A DisplayLabel is a label that can display numbers embedded in strings. The text for the label can include '#' characters, which are substituted by values of specified Value objects. (A doubled ## is interpreted as a single literal # to be displayed rather than substituted.) You should provide as many Values as there are #'s in the text. However, no errors are generated if this is not the case. Extra expressions are ignored; extra #'s are shown as "undefined" in the display. In fact, DisplayLabels do not ever generat JCMErrors. Note that Value objects include objects of type Constant, Variable, and Expression, for example. Value is just an interface defined in package edu.hws.jcm.data.

The values displayed in a DisplayLabel are recomputed when the Label's compute() method is called. Usually, this is done by a Controller that the DisplayLabel is registered with. See the Controller class for more information.

See Also:
  • Field Details

    • text

      protected String text
      Unsubstituted text for display.
    • numSize

      protected int numSize
      Desired maximum number of characters in displayed numbers.
    • values

      protected Value[] values
      Value objects whose values will be substituted for #'s in text.
  • Constructor Details

    • DisplayLabel

      public DisplayLabel()
      Create a label with no expressions set up to display a single number. Initial value is "undefined"; Use the setValue() method to set the value to be displayed.
    • DisplayLabel

      public DisplayLabel(String text, Value val)
      Convenience method for making a DisplayLabel with just one value to display.
      Parameters:
      text - Text to display. It shoud contain a single '#', which will be substituted by the value.
      val - a Value object whose value is substituted for the # in the text.
    • DisplayLabel

      public DisplayLabel(String text, Value[] vals)
      Create a DisplayLabel to display one or more values. Text and vals can be null. If not, text should have as many (single) #'s as there are expressions. The values of the Value objects are substituted for the #'s in the display.
      Parameters:
      text - The text to display. If this is null, it is set to "#".
      vals - The Value object(s) whose values are substituted for #'s in the text. If this is null, the values shoud be set later by calling the setValues() method.
  • Method Details

    • compute

      public void compute()
      The compute method recalculates the displayed Values and changes the text of the label to show the new values. This is usually called by a Controller.
      Specified by:
      compute in interface Computable
    • getValues

      public Value[] getValues()
      Get the array of Value objects whose values are displayed in this DisplayLabel.
    • setValue

      public void setValue(Value val)
      A convenience method that can be used when the display string contains just a single #. This sets the Value object whose value is substituted for that #.
    • setValues

      public void setValues(Value[] vals)
      Set the array of Value objects whose values are displayed in this DisplayLabel, and change the display to show the new values. (The contents of the array, vals, are copied into a newly created array.)
    • setNumSize

      public void setNumSize(int size)
      Set the desired maximum number of characters in displayed numbers. Actual size might be larger. Value is clamped to the range 6 to 25.
    • getNumSize

      public int getNumSize()
      Return the desired maximum number of characters in displayed numbers.
    • getBaseText

      public String getBaseText()
      Return the basic text, including the #'s where Values are inserted in the displayed text. Note that the getText() method from the Label class will return the actual displayed text, including the substitited values.
    • setText

      public void setText(String text)
      Set text for display -- text should include as many (single) #'s as there are values to display.
      Overrides:
      setText in class Label
    • getPreferredSize

      public Dimension getPreferredSize()
      Return the preferred size of this DisplayLabel. Allow space for up to numSize (or 8, whichever is larger) characters for each (single) # in the text. This is not meant to be called directly.
      Overrides:
      getPreferredSize in class Component