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.wpilibj2.command.button;
006
007import edu.wpi.first.networktables.NetworkTable;
008import edu.wpi.first.networktables.NetworkTableEntry;
009import edu.wpi.first.networktables.NetworkTableInstance;
010
011/** A {@link Button} that uses a {@link NetworkTable} boolean field. */
012public class NetworkButton extends Button {
013  private final NetworkTableEntry m_entry;
014
015  /**
016   * Creates a NetworkButton that commands can be bound to.
017   *
018   * @param entry The entry that is the value.
019   */
020  public NetworkButton(NetworkTableEntry entry) {
021    m_entry = entry;
022  }
023
024  /**
025   * Creates a NetworkButton that commands can be bound to.
026   *
027   * @param table The table where the networktable value is located.
028   * @param field The field that is the value.
029   */
030  public NetworkButton(NetworkTable table, String field) {
031    this(table.getEntry(field));
032  }
033
034  /**
035   * Creates a NetworkButton that commands can be bound to.
036   *
037   * @param table The table where the networktable value is located.
038   * @param field The field that is the value.
039   */
040  public NetworkButton(String table, String field) {
041    this(NetworkTableInstance.getDefault().getTable(table), field);
042  }
043
044  @Override
045  public boolean get() {
046    return m_entry.getInstance().isConnected() && m_entry.getBoolean(false);
047  }
048}