/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import ilog.views.dashboard.IlvDashboardDiagram;
import ilog.views.sdm.event.SDMPropertyChangeEvent;
import ilog.views.sdm.event.SDMPropertyChangeListener;
import ilog.views.sdm.model.IlvDefaultSDMNode;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



/**
 * The monitoring view displays a frame containing two dashboards in order to monitor
 * traffic statistics and tunnel states and events.
 */
public class MonitoringView extends JFrame{
    
    // the name of the tunnel that is monitored in this view
    private String currtentTunnelName = null;
    
    // the dashboards used to display monitoring data about traffic and tunnel states and events
    private IlvDashboardDiagram trafficDashboard;
    private IlvDashboardDiagram monitoringDashboard;


    /**
     * Creates the views with specified name and dashboards
     */
    public MonitoringView(String tunnelName,IlvDashboardDiagram monitoringDashboard,IlvDashboardDiagram trafficDashboard) {
        super();        
        currtentTunnelName = tunnelName;
        this.trafficDashboard = trafficDashboard;
        this.monitoringDashboard = monitoringDashboard;       
        initDashboards();
        createGUI();
    }

    
    /**
     * Creates the GUI of the view
     */
    private void createGUI() {
        // put the dashboards into a panel
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(monitoringDashboard,BorderLayout.CENTER);
        panel.add(trafficDashboard,BorderLayout.PAGE_END);
        
        // initialise the frame of the view
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel,BorderLayout.CENTER);
        setTitle(currtentTunnelName);    
        setResizable(true);
        pack();
        setVisible(true);
    }

    /**
     * Initialise dashboards: size, scrollable.. and prepare for simulation 
     */
    private void initDashboards() {
        // GUI init
        monitoringDashboard.setPreferredSize(new Dimension(822,520));
        monitoringDashboard.setScrollable(true);        
        trafficDashboard.setScrollable(false);
        trafficDashboard.setPreferredSize( new Dimension(822, 242));
        
        // add a property listener on the monitoring dashboard in order to
        // detect clicks on symbols. This illustrates how to manage user interaction
        // on symbols
        SDMPropertyChangeListener lstnr=  new SDMPropertyChangeListener(){
            public void propertyChanged(SDMPropertyChangeEvent arg0) {
                // get data about the event
                IlvDefaultSDMNode object = (IlvDefaultSDMNode)arg0.getObject();
                String property = arg0.getPropertyName();
                Object oldValue = arg0.getOldValue();
                Object newValue = arg0.getNewValue();

                String ID = (String)object.getID();
                
                // if user clicks a button representing a fan do something
                if(newValue.equals("BUTTON_CLICKED")){
                    if(ID.startsWith("Fan")){
                        JOptionPane.showMessageDialog(monitoringDashboard, ID+"\nThis equipment is fully functionnal");
                    }
                    else if(ID.startsWith("SafetySystem")){
                        JOptionPane.showMessageDialog(monitoringDashboard, "This is a safety system");
                    }
                }
            }
        };
        
        //add the listener
        monitoringDashboard.getEngine().getModel().addSDMPropertyChangeListener(lstnr);      

        
        // create a simulator and the supervisors for each dashboards. Supervisors will
        // request values to the simulator
        Simulator simulator = new Simulator();
        Supervisor supervisor1 = new Supervisor("Monitoring",monitoringDashboard,1000,simulator);
        Supervisor supervisor2 = new Supervisor("Traffic",trafficDashboard,500,simulator);
    }


}