Class ListSetting<T>

java.lang.Object
me.hackware.api.setting.Setting<Set<T>>
me.hackware.api.setting.impl.ListSetting<T>
Type Parameters:
T - the type of items in the list

public class ListSetting<T> extends Setting<Set<T>>
A generic setting that stores a set of selected items from a list.

In normal mode the options are fixed and items are toggled on/off. In editable mode the user can add, edit and remove entries at runtime (the options list itself becomes the data).

Usage examples:


 // Fixed-option mode
 ListSetting<String> fruits = new ListSetting<String>("Fruits")
     .options(List.of("apple", "banana", "cherry"))
     .nameMapper(s -> s)
     .idMapper(s -> s)
     .limit(3);

 // Editable free-form mode
 ListSetting<String> patterns = new ListSetting<String>("Patterns")
     .editable(s -> s);
 
  • Constructor Details

    • ListSetting

      public ListSetting(String name)
    • ListSetting

      public ListSetting(String name, String description)
  • Method Details

    • options

      public ListSetting<T> options(List<T> options)
      Set the available options for this list.
    • nameMapper

      public ListSetting<T> nameMapper(Function<T,String> nameMapper)
      Set the function that converts an item to its user-facing display name.
    • idMapper

      public ListSetting<T> idMapper(Function<T,String> idMapper)
      Set the function that converts an item to a unique string id (for serialization).
    • defaultValue

      public ListSetting<T> defaultValue(Set<T> defaultValue)
      Sets the default (and initial) selection for this list. Does NOT trigger a save.
      Overrides:
      defaultValue in class Setting<Set<T>>
    • visible

      public ListSetting<T> visible(boolean visible)
      Overrides:
      visible in class Setting<Set<T>>
    • visible

      public ListSetting<T> visible(BooleanSupplier visible)
      Overrides:
      visible in class Setting<Set<T>>
    • description

      public ListSetting<T> description(String description)
      Overrides:
      description in class Setting<Set<T>>
    • limit

      public ListSetting<T> limit(int max)
      Maximum number of items that may be selected. Defaults to the full list size.
    • editable

      public ListSetting<T> editable(Function<String,T> factory)
      Enables editable mode, allowing the user to add, edit and remove entries.
      Parameters:
      factory - converts a serialized string id back into a T instance (for ListSetting<String> this is simply s -> s)
    • onListChanged

      public ListSetting<T> onListChanged(Runnable callback)
      Called whenever the list content changes (add/remove/edit) in editable mode.
    • onSelectionChanged

      public ListSetting<T> onSelectionChanged(Runnable callback)
      Called whenever the selection state changes (toggle, setValue, setFromIds).
    • getOptions

      public List<T> getOptions()
    • getNameMapper

      public Function<T,String> getNameMapper()
    • getIdMapper

      public Function<T,String> getIdMapper()
    • getLimit

      public int getLimit()
    • isEditable

      public boolean isEditable()
    • displayName

      public String displayName(T item)
    • itemId

      public String itemId(T item)
    • fromId

      public T fromId(String id)
      Resolve an item from its serialized id, or null if unknown.
    • getEntries

      public List<T> getEntries()
      Returns an unmodifiable view of the entries (same as options in editable mode).
    • entryCount

      public int entryCount()
      Returns the number of entries.
    • getEntry

      public T getEntry(int index)
      Returns the entry at the given index, or null if out of range.
    • addEntry

      public void addEntry(T entry)
      Adds a new entry (editable mode).
    • removeEntry

      public void removeEntry(int index)
      Removes the entry at the given index (editable mode).
    • setEntry

      public void setEntry(int index, T newValue)
      Replaces the entry at the given index (editable mode).
    • isSelected

      public boolean isSelected(T item)
    • isSelectedById

      public boolean isSelectedById(String id)
    • toggle

      public void toggle(T item)
      Toggle the selection state of an item and save.
    • selectedCount

      public int selectedCount()
    • firstSelectedName

      public String firstSelectedName()
    • getSelected

      public Set<T> getSelected()
    • getSelectedIds

      public List<String> getSelectedIds()
      Get the selected items as a list of serialized string ids.
    • setValue

      public void setValue(Set<T> value)
      Overrides:
      setValue in class Setting<Set<T>>
    • setFromIds

      public void setFromIds(List<String> ids)
      Restore selected items from a list of serialized string ids.