Package mars.venus

Class NumberDisplayBaseChooser

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, SwingConstants

public class NumberDisplayBaseChooser extends JCheckBox
Use to select base for displaying numbers. Initially the choices are only 10 (decimal) and 16 (hex), so I'm using a check box where checked means hex. If base 8 (octal) is added later, the Component will need to change.
See Also:
  • Field Details

  • Constructor Details

    • NumberDisplayBaseChooser

      public NumberDisplayBaseChooser(String text, boolean displayInHex)
      Constructor. It assumes the text will be worded so that a checked box means hexadecimal!
      Parameters:
      text - Text to accompany the check box.
      displayInHex - Whether to display in hexadecimal or decimal.
  • Method Details

    • getBase

      public int getBase()
      Retrieve the current number base.
      Returns:
      current number base, currently DECIMAL or HEXADECIMAL
    • setBase

      public void setBase(int newBase)
      Set the current number base.
      Parameters:
      newBase - The new number base. Currently, if it is neither DECIMAL nor HEXADECIMAL, the base will not be changed.
    • formatUnsignedInteger

      public static String formatUnsignedInteger(int value, int base)
      Produces a string form of an unsigned given the value and the numerical base to convert it to. This class method can be used by anyone anytime. If base is 16, result is same as for formatNumber(). If base is 10, will produce string version of unsigned value. E.g. 0xffffffff will produce "4294967295" instead of "-1".
      Parameters:
      value - the number to be converted
      base - the numerical base to use (currently 10 or 16)
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatNumber

      public static String formatNumber(int value, int base)
      Produces a string form of an integer given the value and the numerical base to convert it to. There is an instance method that uses the internally stored base. This class method can be used by anyone anytime.
      Parameters:
      value - the number to be converted
      base - the numerical base to use (currently 10 or 16)
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatNumber

      public static String formatNumber(float value, int base)
      Produces a string form of a float given the value and the numerical base to convert it to. There is an instance method that uses the internally stored base. This class method can be used by anyone anytime.
      Parameters:
      value - the number to be converted
      base - the numerical base to use (currently 10 or 16)
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatNumber

      public static String formatNumber(double value, int base)
      Produces a string form of a double given the value and the numerical base to convert it to. There is an instance method that uses the internally stored base. This class method can be used by anyone anytime.
      Parameters:
      value - the number to be converted
      base - the numerical base to use (currently 10 or 16)
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatNumber

      public String formatNumber(int value)
      Produces a string form of a number given the value. There is also an class (static method) that uses a specified base.
      Parameters:
      value - the number to be converted
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatUnsignedInteger

      public String formatUnsignedInteger(int value)
      Produces a string form of an unsigned integer given the value. There is also an class (static method) that uses a specified base. If the current base is 16, this produces the same result as formatNumber().
      Parameters:
      value - the number to be converted
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatFloatNumber

      public static String formatFloatNumber(int value, int base)
      Produces a string form of a float given an integer containing the 32 bit pattern and the numerical base to use (10 or 16). If the base is 16, the string will be built from the 32 bits. If the base is 10, the int bits will be converted to float and the string constructed from that. Seems an odd distinction to make, except that contents of floating point registers are stored internally as int bits. If the int bits represent a NaN value (of which there are many!), converting them to float then calling formatNumber(float, int) above, causes the float value to become the canonical NaN value 0x7fc00000. It does not preserve the bit pattern! Then converting it to hex string yields the canonical NaN. Not an issue if display base is 10 since result string will be NaN no matter what the internal NaN value is.
      Parameters:
      value - the int bits to be converted to string of corresponding float.
      base - the numerical base to use (currently 10 or 16)
      Returns:
      a String equivalent of the value rendered appropriately.
    • formatDoubleNumber

      public static String formatDoubleNumber(long value, int base)
      Produces a string form of a double given a long containing the 64 bit pattern and the numerical base to use (10 or 16). If the base is 16, the string will be built from the 64 bits. If the base is 10, the long bits will be converted to double and the string constructed from that. Seems an odd distinction to make, except that contents of floating point registers are stored internally as int bits. If the int bits represent a NaN value (of which there are many!), converting them to double then calling formatNumber(double, int) above, causes the double value to become the canonical NaN value. It does not preserve the bit pattern! Then converting it to hex string yields the canonical NaN. Not an issue if display base is 10 since result string will be NaN no matter what the internal NaN value is.
      Parameters:
      value - the long bits to be converted to string of corresponding double.
      base - the numerical base to use (currently 10 or 16)
      Returns:
      a String equivalent of the value rendered appropriately.
    • setSettingsMenuItem

      public void setSettingsMenuItem(JCheckBoxMenuItem setter)
      Set the menu item from Settings menu that corresponds to this chooser. It is the responsibility of that item to register here, because this one is created first (before the menu item). They need to communicate with each other so that whenever one changes, so does the other. They cannot be the same object (one is JCheckBox, the other is JCheckBoxMenuItem).
    • getBase

      public static int getBase(boolean setting)
      Return the number base corresponding to the specified setting.
      Returns:
      HEXADECIMAL if setting is true, DECIMAL otherwise.