package de.javasoft.synthetica.democenter.examples.systemmonitor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import de.javasoft.swing.SystemMonitor;
import de.javasoft.synthetica.addons.systemmonitor.Collector;

/**
 * This application demonstrates SystemMonitor customization.
 */
@SuppressWarnings("serial")
public class CustomSystemMonitor extends JFrame
{
  public CustomSystemMonitor()
  {
    super("Custom SystemMonitor");
    setLayout(new BorderLayout());

//@highlight
    SystemMonitor monitor = new SystemMonitor();
    String id = "MyCustomCollectorID";
    //add collector with a buffersize for 1000 values and 100ms callback delay
    monitor.addCollector(id, 1000, 100, new MyCollector());
    monitor.addCaption(id, "Random value:", "%{value},.0f / %{maxValue},.0f / %{percentValue}.0f%%", false);
    monitor.setColor(id, new Color(0xFFFFD8));
    //a smaller refresh time means a higher refresh rate
    monitor.setRefreshTime(500);
    monitor.setGridSize(12);
    //bring in some green color...
    monitor.setBackground(new Color(0x002000));
    monitor.setGridColor(new Color(0x003300));
    monitor.getCaptionPanel().setBackground(new Color(0x001000));
    //customize caption for context menu
    monitor.setPopupMenuCaption(id, "Random Collector");

    monitor.setPopupMonitorGridSize(6);
    monitor.setPopupMonitorSize(new Dimension(150, 80));    
    //enable MonitorPopup ToolTip
    monitor.setPopupEnabled(true);
//@/highlight

    add(monitor);
    ((JComponent)getContentPane()).setBorder(new EmptyBorder(10,10,10,10));
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);        //center
    setVisible(true);
  }

  /**
   * Static main method for application startup. 
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel");
          new CustomSystemMonitor();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }
  
  
  /**
   * Custom collector class - responsible for retrieving and 
   * returning random values.
   */
  private static class MyCollector implements Collector
  {
//@highlight
    public double getValue()
    {
      return new Random().nextInt(100);
    }

    public double getMaxValue()
    {
      return 99;
    }    
//@/highlight
  }
  
}
