001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj.shuffleboard;
006
007import edu.wpi.first.networktables.NetworkTable;
008import edu.wpi.first.networktables.NetworkTableEntry;
009import java.util.function.BiConsumer;
010import java.util.function.Supplier;
011
012/**
013 * A Shuffleboard widget whose value is provided by user code.
014 *
015 * @param <T> the type of values in the widget
016 */
017public final class SuppliedValueWidget<T> extends ShuffleboardWidget<SuppliedValueWidget<T>> {
018  private final Supplier<T> m_supplier;
019  private final BiConsumer<NetworkTableEntry, T> m_setter;
020
021  /**
022   * Package-private constructor for use by the Shuffleboard API.
023   *
024   * @param parent the parent container for the widget
025   * @param title the title of the widget
026   * @param supplier the supplier for values to place in the NetworkTable entry
027   * @param setter the function for placing values in the NetworkTable entry
028   */
029  SuppliedValueWidget(
030      ShuffleboardContainer parent,
031      String title,
032      Supplier<T> supplier,
033      BiConsumer<NetworkTableEntry, T> setter) {
034    super(parent, title);
035    this.m_supplier = supplier;
036    this.m_setter = setter;
037  }
038
039  @Override
040  public void buildInto(NetworkTable parentTable, NetworkTable metaTable) {
041    buildMetadata(metaTable);
042    metaTable.getEntry("Controllable").setBoolean(false);
043
044    NetworkTableEntry entry = parentTable.getEntry(getTitle());
045    m_setter.accept(entry, m_supplier.get());
046  }
047}