All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lessvoid.nifty.controls.listbox.ListBoxSelectionModeSingle Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package de.lessvoid.nifty.controls.listbox;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

/**
 * A single selection mode for a Nifty ListBox. You can only select a single item.
 * Selecting a new one will remove any previous selected items.
 *
 * @param 
 * @author void
 */
class ListBoxSelectionModeSingle implements ListBoxSelectionMode {
  @Nullable
  private T selection;
  private boolean requiresSelection = false;

  @Override
  public void clear() {
    selection = null;
  }

  @Nonnull
  @Override
  public List getSelection() {
    if (selection == null) {
      return Collections.emptyList();
    }
    return Collections.singletonList(selection);
  }

  @Override
  public void remove(@Nonnull final T item) {
    if (requiresSelection) {
      return;
    }
    removeForced(item);
  }

  @Override
  public void removeForced(@Nonnull final T item) {
    if (item.equals(selection)) {
      selection = null;
    }
  }

  @Override
  public void add(@Nonnull final T item) {
    selection = item;
  }

  @Override
  public void enableRequiresSelection(final boolean enable) {
    requiresSelection = enable;
  }

  @Override
  public boolean requiresAutoSelection() {
    return requiresSelection && selection == null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy