import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import javax.swing.*;


import java.util.EventObject;
import net.agmodel.metBean.*;
import net.agmodel.utilBean.*;
import net.agmodel.weatherData.MetElement;
import net.agmodel.weatherData.MetDuration;
import net.agmodel.resBean.*;

public class MetBeanDemo extends JFrame implements StationDataListener{

  static DateFormat df=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);

  //Create MetBeans
  MetBrokerBean metBrokerBean1=new MetBrokerBean();
  MetSourceBean metSourceBean1=new MetSourceBean();
  MetRegionBean metRegionBean1=new MetRegionBean();
  MetStationBean metStationBean1 = new MetStationBean();
  StationDatesBean stationDatesBean1=new StationDatesBean();
  ElementAndResolutionBean eAndR=new ElementAndResolutionBean();
  TimeZoneBean tzBean1=new TimeZoneBean();
  IntervalBean intervalBean1=new IntervalBean();
  StationRequestBean dataRequestBean1=new StationRequestBean(JOptionPane.getFrameForComponent(this));
  RequestBeanButton requestButton=new RequestBeanButton();
  IndeterminateProgressBean progressBar=new IndeterminateProgressBean();
  AllowSummarizingBean summarizeBean=new AllowSummarizingBean();

  ResourceBean resBean=new ResourceBean();

  //Create other interface components
  JPanel criteriaPanel = new JPanel();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea outputTextArea = new JTextArea();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();

  //Construct the application
  public MetBeanDemo() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try  {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception  {

    this.setSize(new Dimension(584, 464));
    this.setTitle("Met Bean Tester");

    //Link together the MetBeans using event listener approach

    //Station selection links
    metBrokerBean1.addServiceBeanListener(metSourceBean1);
    metSourceBean1.addDataSourceListener(metRegionBean1);
    metRegionBean1.addRegionListener(metStationBean1);
    metStationBean1.addStationListener(dataRequestBean1);


    //Time interval links
    metSourceBean1.addDefaultTimeZoneListener(tzBean1);
    tzBean1.addTimeZoneListener(intervalBean1);
    metSourceBean1.addPeriodExtremesListener(intervalBean1);
    intervalBean1.addIntervalListener(dataRequestBean1);

    //Links to handle weather data results and progress reporting
    dataRequestBean1.addDataListener(this);
    dataRequestBean1.addIndeterminateListener(progressBar);
    requestButton.setRequestBean(dataRequestBean1);

    summarizeBean.addAllowSummarizingListener(dataRequestBean1);

    //Links to display station metadata and allow element and resolution selection
    metStationBean1.addStationListener(eAndR);
    eAndR.addElementsListener(dataRequestBean1);
    eAndR.addResolutionListener(dataRequestBean1);

    metStationBean1.addStationListener(stationDatesBean1);

    //Register beans with internationalization system
    metSourceBean1.setResourceServerBean(resBean);
    metRegionBean1.setResourceServerBean(resBean);
    metStationBean1.setResourceServerBean(resBean);
    intervalBean1.setResourceServerBean(resBean);
    tzBean1.setResourceServerBean(resBean);
    requestButton.setResourceServerBean(resBean);
    summarizeBean.setResourceServerBean(resBean);

    //Set up the broker bean
    metBrokerBean1.setClientInfo("MetBroker Bean test application");

    //Tell the broker bean where MetBroker is running
    metBrokerBean1.setRMIHost("mb1.dc.affrc.go.jp");

    //and where the wrapper servlet is running
    metBrokerBean1.setHTTPHost("www.agmodel.net");
    metBrokerBean1.setHTTPPort(80);

    //Tell the broker bean where ResourceServer is running
    resBean.setRMIHost("mb1.dc.affrc.go.jp");

    //and where the wrapper servlet is running
    resBean.setHTTPHost("www.agmodel.net");
    resBean.setHTTPPort(80);

    //Make an initial selection of weather elements and data resolution
    eAndR.setSelectedElements(new MetElement[] {MetElement.RAIN,MetElement.AIRTEMPERATURE});
    eAndR.setSelectedResolution(MetDuration.DAILY);
    eAndR.setMultipleSelectionAllowed(true);

    //Make an initial time selection
    intervalBean1.setStartDay(1);
    intervalBean1.setStartMonth(0);
    intervalBean1.setStartYear(2000);

    intervalBean1.setEndDay(31);
    intervalBean1.setEndMonth(0);
    intervalBean1.setEndYear(2000);

    //Default to allow MetBroker to summarize (eg hourly data to daily)
    summarizeBean.setSummarizingAllowed(true);

    //Set up buttons to demonstrate how to select a particular station programmatically
    jButton1.setText("Tsuchiura");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        metSourceBean1.setSourceAndStation("amedas","40341");
      }
    });

    jButton2.setText("Tifton");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        metSourceBean1.setSourceAndStation("gaemn","GATI");
      }
    });

    //Add the beans into the user interface
    criteriaPanel.setLayout(gridBagLayout1);
    criteriaPanel.add(metSourceBean1,     new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(metRegionBean1,    new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(metStationBean1,    new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(intervalBean1,   new GridBagConstraints(0, 3, 1, 2, 0.0, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(tzBean1,   new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(eAndR,    new GridBagConstraints(0, 7, 2, 5, 0.5, 1.0
            ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    jScrollPane1.getViewport().add(outputTextArea);
    jScrollPane1.setAutoscrolls(true);
    criteriaPanel.add(jScrollPane1, new GridBagConstraints(2, 7, 1, 5, 1.0, 1.0
            ,GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 60, 40));
    criteriaPanel.add(summarizeBean, new GridBagConstraints(0, 13, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(jButton1, new GridBagConstraints(0, 12, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(jButton2, new GridBagConstraints(1, 12, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(requestButton, new GridBagConstraints(2, 12, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(progressBar,   new GridBagConstraints(1, 13, 12, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    criteriaPanel.add(stationDatesBean1,    new GridBagConstraints(1, 2, 2, 3, 0.0, 0.0
            ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

    this.getContentPane().add(criteriaPanel);

    //Tell the bean to start connecting up to MetBroker
    metBrokerBean1.setLive(true);
    resBean.setLive(true);
  }

  //Overridden so we can exit on System Close
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      metBrokerBean1.setLive(false);
      System.exit(0);
    }
  }

  public void requestSent(EventObject evt){
    outputTextArea.setText("");
  }

  //What to do when data arrives from the StationRequestBean
  public void newStationData(StationDataEvent evt){
    outputTextArea.setText(evt.getStationData().dumpDuration(df,"\t","\n","date/time","no data"));
  }

  public static void main(String[] args) {
    JFrame frame = new  MetBeanDemo();
    frame.pack();
    frame.setVisible(true);
  }

}

