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.util.sendable.Sendable;
009import edu.wpi.first.wpilibj.smartdashboard.SendableBuilderImpl;
010
011/**
012 * A Shuffleboard widget that handles a {@link Sendable} object such as a motor controller or
013 * sensor.
014 */
015public final class ComplexWidget extends ShuffleboardWidget<ComplexWidget> {
016  private final Sendable m_sendable;
017  private SendableBuilderImpl m_builder;
018
019  ComplexWidget(ShuffleboardContainer parent, String title, Sendable sendable) {
020    super(parent, title);
021    m_sendable = sendable;
022  }
023
024  @Override
025  public void buildInto(NetworkTable parentTable, NetworkTable metaTable) {
026    buildMetadata(metaTable);
027    if (m_builder == null) {
028      m_builder = new SendableBuilderImpl();
029      m_builder.setTable(parentTable.getSubTable(getTitle()));
030      m_sendable.initSendable(m_builder);
031      m_builder.startListeners();
032    }
033    m_builder.update();
034  }
035
036  /**
037   * Enables user control of this widget in the Shuffleboard application. This method is
038   * package-private to prevent users from enabling control themselves. Has no effect if the
039   * sendable is not marked as an actuator with {@link SendableBuilder#setActuator}.
040   */
041  void enableIfActuator() {
042    if (m_builder.isActuator()) {
043      m_builder.startLiveWindowMode();
044    }
045  }
046
047  /**
048   * Disables user control of this widget in the Shuffleboard application. This method is
049   * package-private to prevent users from enabling control themselves. Has no effect if the
050   * sendable is not marked as an actuator with {@link SendableBuilder#setActuator}.
051   */
052  void disableIfActuator() {
053    if (m_builder.isActuator()) {
054      m_builder.stopLiveWindowMode();
055    }
056  }
057}