Class SwitchItem

All Implemented Interfaces:
Animatable<SwitchItem>, CanDrop<SwitchItem>, Clickable<SwitchItem>, BooleanValueFormItem, SupportHelpIcon<SwitchItem>, Draggable<SwitchItem>, Focusable<SwitchItem>, HandleHotKey<SwitchItem>, HandleMouseEvents<SwitchItem>, Touchable<SwitchItem>

Switch item

The switch item allows the user to input a logic value. The item is displayed as a switch, that can be switched left to right. By default, this is a rectangular switch, that has ON and OFF written on it. This text can be customized, and the switch itself can be set to be rounded. If the user switches this, it will have a true value, if switches it off, it will be false. The component cannot have a null value, thus if the component doesn't get a value, it will have a false value.

The switch component is a form item, that can be placed on a Form component, and the form handles it as well. The component can be used on its own, but in that case, the handling of its events and values must be provided by the program code.

Supported events:

AnimationStart AnimationEnd, Focus FocusOut FocusIn LosesFocus Click, DoubleClick, MouseDown, MouseDown, MouseLeave, MouseMove, MouseOver, MouseOut, MouseUp, Wheel, TouchCancel, TouchEnd, TouchMove, TouchStart, HotKey Drop, DragStart, DragEnd

Since:
4.0
Author:
JBStrap
See Also:
  • Constructor Details

    • SwitchItem

      public SwitchItem(String name, String label)
      Creates a switch item, with the specified name and label.
      Parameters:
      name - The name of the item
      label - The label of the item. If null, the menu won't have a label.
      Throws:
      NullPointerException - If the name is null
    • SwitchItem

      public SwitchItem(String name, String label, String language)
      Creates a switch item, with the specified name and label, with the specified language code.
      Parameters:
      name - The name of the item
      label - The label of the item. If null, the menu won't have a label.
      language - The language code, that the form item will use. If not specified, or null, the framework will use the default language. The default language can be set with the JBStrapParamType.DEFAULT_LANGUAGE JBStrap parameter.
      Throws:
      NullPointerException - If the name is null
    • SwitchItem

      public SwitchItem(DataDescriptorColumn column)

      Createts a switch item, based on the specified DataDescriptor column.

      The method creates and sets the switch item, based on the DataDescriptor column, specified in the parameters. The following parameters from the DataDescriptor column are used:

      • DatadescriptorColumn.getTitle - The title set in the DataDescriptor column will be used by the input field as its label.
      • DataDescriptorColumn.isNullable() - If the field can a null value, the field will be marked as not mandatory. If it cannot have a null value, the field will be marked as mandatory.
      • MetaParamName.ON_TEXT - This specifies the text that is displayed when the switch is in the ON state.
      • MetaParamName.OFF_TEXT - This specifies the text that is displayed when the switch is in the OFF state.
      • MetaParamName.WIDTH - This specifies the switch item's width. The default is 54 px.
      • MetaParamName.PLACEHOLDER - This text will be used as a placeholder.
      • MetaParamName.SIZE - The value specified here defines the size of the input field. (Applicable values: SMALL, MEDIUM, LARGE)
      • MetaParamName.STATE - The value specified here defines the state of the input field. (Applicable values: DEFAULT, ERROR, WARNING, SUCCESS, INFO)
      • MetaParamName.DEFAULT_VALUE - The value specified here defines the field's default value. The value must have the type of field, in this case, it must be a text value.
      • MetaParamName.READ_ONLY - If true, the field will be read only. Otherwise, the field can have its contents modified by the user.
      • MetaParamName.ENABLED - If false, the field will be disabled (it will be created in an unusable state.) Otherwise, the field will be enabled when created.
      • MetaParamName.HELP - If the parameter is specified, an icon will be displayed next to the field. If the user moves over the icon, the text specified here will be displayed, as a help text.
      • MetaParamName.HELP_ICON - If the icon name is specified (from the Icon enum), then this icon will replace the default "?" icon.
      • MetaParamName.DESCRIPTION - The text specified here will appear below the field, in a smaller, italic font.
      • MetaParamName.TAB_INDEX - The value specified here determines the order of the input focus (when the user presses the Tab key). The order goes in ascending order. If not specified, the component will use the order value from the DataDescriptor. If that not specified either, the order in which it was placed in the DataDescriptor will be used.

      Parameters:
      column - The DataDescriptor column, that will be used as a basis for the form item.
      Throws:
      NullPointerException - If the column was specified as null
    • SwitchItem

      public SwitchItem(DataDescriptorColumn column, String language)

      Creates a switch item, based on the specified DataDescriptor column and using the specified language code.

      The method creates and sets the switch item, based on the DataDescriptor column, specified in the parameters. The following parameters from the DataDescriptor column are used:

      • DataDescriptorColumn.getTitle - The title set in the DataDescriptor column will be used by the input field as its label.
      • DataDescriptorColumn.isNullable() - If the field can a null value, the field will be marked as not mandatory. If it cannot have a null value, the field will be marked as mandatory.
      • MetaParamName.ON_TEXT - This specifies the text that is displayed when the switch is in the ON state.
      • MetaParamName.OFF_TEXT - This specifies the text that is displayed when the switch is in the OFF state.
      • MetaParamName.WIDTH - This specifies the switch item's width. The default is 54 px.
      • MetaParamName.PLACEHOLDER - This text will be used as a placeholder.
      • MetaParamName.SIZE - The value specified here defines the size of the input field. (Applicable values: SMALL, MEDIUM, LARGE)
      • MetaParamName.STATE - The value specified here defines the state of the input field. (Applicable values: DEFAULT, ERROR, WARNING, SUCCESS, INFO)
      • MetaParamName.DEFAULT_VALUE - The value specified here defines the field's default value. The value must have the type of field, in this case, it must be a text value.
      • MetaParamName.READ_ONLY - If true, the field will be read only. Otherwise, the field can have its contents modified by the user.
      • MetaParamName.ENABLED - If false, the field will be disabled (it will be created in an unusable state.) Otherwise, the field will be enabled when created.
      • MetaParamName.HELP - If the parameter is specified, an icon will be displayed next to the field. If the user moves over the icon, the text specified here will be displayed, as a help text.
      • MetaParamName.HELP_ICON - If the icon name is specified (from the Icon enum), then this icon will replace the default "?" icon.
      • MetaParamName.DESCRIPTION - The text specified here will appear below the field, in a smaller, italic font.
      • MetaParamName.TAB_INDEX - The value specified here determines the order of the input focus (when the user presses the Tab key). The order goes in ascending order. If not specified, the component will use the order value from the DataDescriptor. If that not specified either, the order in which it was placed in the DataDescriptor will be used.

      Parameters:
      column - The DataDescriptor column, that will be used as a basis for the form item.
      language - The language code, that the form item will use. If not specified, or null, the framework will use the default language. The default language can be set with the JBStrapParamType.DEFAULT_LANGUAGE JBStrap parameter.
      Throws:
      NullPointerException - If the column was specified as null
  • Method Details

    • addStyleClass

      public SwitchItem addStyleClass(String styleClass)
      Adds the specified CSS class to the component
      Overrides:
      addStyleClass in class Component<SwitchItem>
      Parameters:
      styleClass - The name of the CSS style class
      Returns:
      The component
    • setOnText

      public SwitchItem setOnText(String onText)
      Sets the text that is displayed when the switch is in the ON state. The default is "ON"
      Parameters:
      onText - The text that is displayed when the switch is in the ON state.
      Returns:
      The switch component
    • getOnText

      public String getOnText()
      Gets the text that is displayed when the switch is in the ON state.
      Returns:
      The text that is displayed when the switch is in the ON state or null if not set.
    • setOffText

      public SwitchItem setOffText(String offText)
      Sets the text that is displayed when the switch is in the OFF state. The default is "OFF"
      Parameters:
      offText - The text that is displayed when the switch is in the OFF state.
      Returns:
      The switch component
    • getOffText

      public String getOffText()
      Gets the text that is displayed when the switch is in the OFF state.
      Returns:
      The text that is displayed when the switch is in the OFF state or null if not set.
    • setText

      public SwitchItem setText(String onText, String offText)
      Sets the text that is displayed when the switch is in the ON and OFF state.
      Parameters:
      onText - The text that is displayed when the switch is in the ON state.
      offText - The text that is displayed when the switch is in the OFF state.
      Returns:
      The switch component
    • setWidth

      public SwitchItem setWidth(String width)
      Sets the width of the switch. The default is 54 pixels
      Parameters:
      width - The width, specified by using CSS3 units.
      Returns:
      The switch component
    • getWidth

      public String getWidth()
      Gets the width of the switch.
      Returns:
      The switch width, or 54px, if the width was not customized.
    • setRounded

      public SwitchItem setRounded(boolean rounded)
      Sets the switch to have rounded edges. By default, this is disabled, and the switch is rectangular.
      Parameters:
      rounded - If true, the switch will be rounded, otherwise it will be rectangular.
      Returns:
      The switch component
    • isRounded

      public boolean isRounded()
      Determines if the switch is rounded.
      Returns:
      If true, the switch will be rounded, otherwise it will be rectangular.
    • getStyleClasses

      public List<String> getStyleClasses()
      Gets a list of every style class that was assigned to the component.
      Overrides:
      getStyleClasses in class Component<SwitchItem>
      Returns:
      A list of the style classes. If none were assigned to the component, the list will be empty.
    • containsStyleClass

      public boolean containsStyleClass(String styleClass)
      Determines if the specified style class has been assigned to the component.
      Overrides:
      containsStyleClass in class Component<SwitchItem>
      Parameters:
      styleClass - The name of the style class
      Returns:
      true if it is assigned, otherwise false
    • addStyleClasses

      public SwitchItem addStyleClasses(String... styleClasses)
      Adds the specified style classes to the component
      Overrides:
      addStyleClasses in class Component<SwitchItem>
      Parameters:
      styleClasses - The name of the style classes
      Returns:
      The component
    • removeStyleClass

      public SwitchItem removeStyleClass(String styleClass)
      Removes the specified style class from the component
      Overrides:
      removeStyleClass in class Component<SwitchItem>
      Parameters:
      styleClass - The name of the style class
      Returns:
      The component
    • removeAllStyleClasses

      public SwitchItem removeAllStyleClasses()
      Removes every specified style class from the component
      Overrides:
      removeAllStyleClasses in class Component<SwitchItem>
      Returns:
      The component
    • isChecked

      public final boolean isChecked()
      Determines if the switch is in the "ON" state.
      Returns:
      If true, it is on, otherwise false
    • setState

      public SwitchItem setState(FormItemState formItemState)
      Sets the state of the switch. This setting cannot be used if the switch label is not displayed.
      Overrides:
      setState in class FormItem<Boolean,SwitchItem>
      Parameters:
      formItemState - The switch state. The applicable values can be found in the FormItemState enum.
      Returns:
      The switch component
    • drawEditor

      public void drawEditor(StringWriter writer)
      Description copied from class: FormItem

      Draws the editor component.

      You do not need to use this method during development, but all components extending the form item must implement it. Use the method to generate the input field’s HTML code. This code is then inserted into the corresponding DOM code when drawing the form item.

      Specified by:
      drawEditor in class FormItem<Boolean,SwitchItem>
      Parameters:
      writer - The HTML string writer class instance; write here the HTML code for the input field
    • setValue

      public SwitchItem setValue(Boolean value)
      Sets the switch value.
      Overrides:
      setValue in class FormItem<Boolean,SwitchItem>
      Parameters:
      value - The switch value. If true, the switch is on, otherwise it is off.
      Returns:
      The switch component
    • setOriginalValue

      public SwitchItem setOriginalValue(Boolean originalValue)
      Sets the original value of the switch.
      Overrides:
      setOriginalValue in class FormItem<Boolean,SwitchItem>
      Parameters:
      originalValue - The original value of the switch. If true, the original value of the switch will be true too. Otherwise false
      Returns:
      The switch component
    • setObjectValue

      public SwitchItem setObjectValue(Object value)
      Description copied from class: FormItem
      Sets the form item’s current value using the object in the parameter.
      Overrides:
      setObjectValue in class FormItem<Boolean,SwitchItem>
      Parameters:
      value - The switch actual value
      Returns:
      The switch component
    • setOriginalObjectValue

      public SwitchItem setOriginalObjectValue(Object value)
      Description copied from class: FormItem
      Sets the form item’s original value using the object in the parameter.
      Overrides:
      setOriginalObjectValue in class FormItem<Boolean,SwitchItem>
      Parameters:
      value - The value to be assigned for the actual and original value.

      Returns:
      The switch component
    • setDefaultValue

      public SwitchItem setDefaultValue(Object defaultValue)
      Sets the default value of the switch. The following rules are followed when setting the value.
      • If the value is null, then the false value will be set.
      • If the specified value is from Boolean class, then that value will be set.
      • In other cases, the framework will try to interpret the value as a logic value, and set the switch value accordingly. In this case, a true value is set if the specified value is one of the following: 1, 1L, "1", "Y", "YES", "TRUE", "T", "ON". This check is not case-sensitive.
      Specified by:
      setDefaultValue in class FormItem<Boolean,SwitchItem>
      Parameters:
      defaultValue - The default value of the switch.
      Returns:
      The switch component
    • setReverseShow

      public SwitchItem setReverseShow(boolean reverse)
    • setReadOnly

      public SwitchItem setReadOnly(boolean readOnly)
      Description copied from class: FormItem
      Sets the form item to read-only mode. By default, the form item is displayed as writable and the user can freely modify the form item value. If the component is set to read-only mode, the user is unable to modify the form item value.
      Overrides:
      setReadOnly in class FormItem<Boolean,SwitchItem>
      Parameters:
      readOnly - If true is set, the form item is switched to read-only mode and the user is unable to modify the value. If false is set, the form item is switched to writable mode and the user is able to modify form item value.
      Returns:
      Form item component