
import javax.swing.JFrame;
import byucc.jhdl.apps.Broker.Broker;
import byucc.jhdl.base.HWSystem;
import byucc.jhdl.util.cli.CLInterpreter;
import byucc.jhdl.util.cli.CLICommand;
import byucc.jhdl.util.cli.CLIException;

/** This is the controller for the Monitor application.  It
 * establishes the relationships in the model-view-controller
 * architecture.  Inter-component interactions occur through this
 * class.
 * @author Anthony L. Slade */
public class MonitorBroker extends Broker {

  /** The name of the restart wire */
  public static final String WIRE_RESTART = "restart";
  /** The CLICommand for clearing the hit counts */
  public static final String COMMAND_CLEAR_COUNTERS = "clear";
  /** The CLICommand for setting the restart wire value */
  public static final String COMMAND_SET_RESTART = "put "
    +WIRE_RESTART+" 1";
  /** The CLICommand for cycling the system clock */
  public static final String COMMAND_CYCLE = "cycle 1";
  /** The CLICommand for unsetting the restart wire value */
  public static final String COMMAND_UNSET_RESTART = "put "
    +WIRE_RESTART+" 0";
  public static final String TYPE_MONITOR = "Network Monitor";

  /** Creates a MonitorBroker.  This will also create a
   * MonitorViewerPanel and set up the CLInterpreter with an extra
   * command to clear the hit counts. */
  public MonitorBroker( HWSystem hwsystem,
			int[] toCount,
			int[] toBlock) {
    super( hwsystem, new CLInterpreter() );

    byucc.jhdl.apps.Stimulator.Stimulator.registerCLICommands(getInterp());
    MonitorViewerPanel panel =
      new MonitorViewerPanel( hwsystem,
			      getInterp(),
			      toCount,
			      toBlock );
    panel.setActionButton( new ViewIntermediary( hwsystem,
						 getInterp(),
						 panel,
						 toCount,
						 toBlock ) );
    // put the viewer panel in a JFrame and display it.
    JFrame graphFrame = new JFrame("NetworkMonitor Application");
    graphFrame.getContentPane().add(panel);
    graphFrame.setLocation(250,250);
    graphFrame.pack(); graphFrame.show();
  }
  /** Creates a new CLICommand that clears the hit counts */
  protected void registerCLICommands( HWSystem hws,
				      CLInterpreter interp ) {
    super.registerCLICommands(hws,interp);
    interp.registerCommand( COMMAND_CLEAR_COUNTERS,
			    new CLICommand() {
				public Object execute( CLInterpreter cli,
						       String[] args )
				throws CLIException {
				  clearCounts();
				  return null;
				}
				public String getHelpText( String cmdName ) {
				  return "clears the counts of address hits";
				}
				public String getHelpType( String cmdName ) {
				  return TYPE_MONITOR;
				}
				public String getUsageText( String cmdName ) {
				  return "";
				}
			      });
  }
  /** Method that takes care of the details necessary to clear the hit
   * counts without just reseting the whole system.  The process is
   * to assert the restart wire, cycle the clock once, then de-assert
   * the restart wire. */
  protected void clearCounts() {
    interp.parseCommandNoHistory(COMMAND_SET_RESTART);
    interp.parseCommandNoHistory(COMMAND_CYCLE);
    interp.parseCommandNoHistory(COMMAND_UNSET_RESTART);
  }
} // end class MonitorBroker

