Class FormattedText

java.lang.Object
org.eclipse.nebula.widgets.formattedtext.FormattedText

public class FormattedText
extends java.lang.Object
Formatted text viewer. Adds formatting capabilities to the Text widget of SWT. This control works on the same principle than the JFace viewers. The embedded text widget is accessible by the getControl() method, allowing to apply to it all necessary behaviors (layout, listeners...).

Formatting is delegated to formatter objects implementing the ITextFormatter interface. Each formatter class manages a base class of values (date, number...).
Formatters are associated by 2 different means :

  • By the setFormatter() method.
  • When setValue() is called and there is currently no formatter, a new one is automatically created based on the class of the value.

Styles:

CENTER, LEFT, RIGHT, READ_ONLY
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected static boolean beepSound
    Flag to activate or not beep sound on input errors
    protected int caretPos
    Save position of cursor when the focus is lost
    protected static int count  
    protected ITextFormatter formatter
    Formatter
    protected int id  
    protected org.eclipse.swt.layout.GridLayout layout
    Layout
    protected org.eclipse.swt.widgets.Listener modifyFilter
    Filter for modify events
    protected org.eclipse.swt.widgets.Text text
    Encapsulated Text widget
    static java.lang.String TEXT_DATA_KEY
    The key used to register the FormattedText in its Text widget data
  • Constructor Summary

    Constructors 
    Constructor Description
    FormattedText​(org.eclipse.swt.widgets.Composite parent)
    Creates a formatted text on a newly-created text control under the given parent.
    FormattedText​(org.eclipse.swt.widgets.Composite parent, int style)
    Creates a formatted text on a newly-created text control under the given parent.
    FormattedText​(org.eclipse.swt.widgets.Text t)
    Creates a formatted text on the given text control.
  • Method Summary

    Modifier and Type Method Description
    org.eclipse.swt.widgets.Text getControl()
    Returns the primary Text control associated with this viewer.
    ITextFormatter getFormatter()
    Returns the formatter associated to the Text widget.
    java.lang.Object getValue()
    Returns the current value of the widget.
    java.lang.Class<?> getValueType()
    Returns the type of value the ITextFormatter associated with this FormattedText handles, i.e. returns in getValue().
    static boolean isBeepSound()
    Returns true if beep sound must be produced on input errors, else false.
    boolean isEmpty()
    Returns true if the current value is empty, else false.
    An empty value depends of the formatter applied on the Text widget and is not just an empty String in the widget.
    boolean isValid()
    Returns true if the current value is valid, else false.
    static void setBeepSound​(boolean beepSound)
    Set the beep sound to ON or OFF for all the FormattedText fields.
    void setFormatter​(ITextFormatter formatter)
    Associates a formatter to the widget.
    Parameter can not be null.
    void setValue​(java.lang.Object value)
    Sets a new value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TEXT_DATA_KEY

      public static final java.lang.String TEXT_DATA_KEY
      The key used to register the FormattedText in its Text widget data
      See Also:
      Constant Field Values
    • text

      protected org.eclipse.swt.widgets.Text text
      Encapsulated Text widget
    • formatter

      protected ITextFormatter formatter
      Formatter
    • caretPos

      protected int caretPos
      Save position of cursor when the focus is lost
    • layout

      protected org.eclipse.swt.layout.GridLayout layout
      Layout
    • modifyFilter

      protected org.eclipse.swt.widgets.Listener modifyFilter
      Filter for modify events
    • beepSound

      protected static boolean beepSound
      Flag to activate or not beep sound on input errors
    • count

      protected static int count
    • id

      protected int id
  • Constructor Details

    • FormattedText

      public FormattedText​(org.eclipse.swt.widgets.Composite parent)
      Creates a formatted text on a newly-created text control under the given parent. The text control is created using the SWT style bits BORDER.
      Parameters:
      parent - the parent control
    • FormattedText

      public FormattedText​(org.eclipse.swt.widgets.Composite parent, int style)
      Creates a formatted text on a newly-created text control under the given parent. The text control is created using the given SWT style bits.
      Parameters:
      parent - the parent control
      style - the SWT style bits used to create the text
    • FormattedText

      public FormattedText​(org.eclipse.swt.widgets.Text t)
      Creates a formatted text on the given text control.
      Parameters:
      t - the text control
  • Method Details

    • getControl

      public org.eclipse.swt.widgets.Text getControl()
      Returns the primary Text control associated with this viewer.
      Returns:
      the SWT text control which displays this viewer's content
    • getFormatter

      public ITextFormatter getFormatter()
      Returns the formatter associated to the Text widget.
      Returns:
      Formatter, or null if no formatter is currently associated
    • getValue

      public java.lang.Object getValue()
      Returns the current value of the widget.

      The returned value is provided by the formatter and is of the type managed by the formatter. For example a DateFormatter will return a Date value.
      If no formatter is associated, the String contained in the Text widget is returned.

      Returns:
      Current value
    • getValueType

      public java.lang.Class<?> getValueType()
      Returns the type of value the ITextFormatter associated with this FormattedText handles, i.e. returns in getValue().
      Returns:
      The value type.
    • isBeepSound

      public static boolean isBeepSound()
      Returns true if beep sound must be produced on input errors, else false.
      Returns:
      true / false
    • isEmpty

      public boolean isEmpty()
      Returns true if the current value is empty, else false.
      An empty value depends of the formatter applied on the Text widget and is not just an empty String in the widget. Formatters can use special formatting characters to format the value. These characters are not always considered as part of the value. For example, in a DateFormatter the pattern uses "/" separator and always displays it in the input field.
      Returns:
      true if empty.
    • isValid

      public boolean isValid()
      Returns true if the current value is valid, else false.
      Returns:
      true if valid.
    • setBeepSound

      public static void setBeepSound​(boolean beepSound)
      Set the beep sound to ON or OFF for all the FormattedText fields. Beep sound are produced by formatters on every input error. Formatter implementation must check this flag on before to emit a beep sound.
      Parameters:
      beepSound - true to emit beep sound on errors, else false
    • setFormatter

      public void setFormatter​(ITextFormatter formatter)
      Associates a formatter to the widget.
      Parameter can not be null. In some situations, the FormattedText component must not do formatting (eg. when reusing the same object for editing of different types of values). In this case, use a StringFormatter. This formatter do no formatting.
      Parameters:
      formatter - formatter
    • setValue

      public void setValue​(java.lang.Object value)
      Sets a new value.

      If no formatter is currently associated to he widget, a new one is created by the factory based on the value's class.
      If the value is incompatible with the formatter, an IllegalArgumentException is returned.

      Parameters:
      value - new value