/*
 * MonitorWindow.java
 *
 * Created on April 29, 2005, 2:08 PM
 */

package DDMT.core;

import java.util.List;
import java.util.regex.*;


import DDMT.core.MainPanel.ControlClass;
import DDMT.core.misc.ListFilter;
import DDMT.core.misc.EventType.EventCellRenderer;

/**
 *
 * @author  jk3
 */
public class MonitorWindow extends javax.swing.JFrame {
    
    //Define some patterns to use for node filtering
    private Pattern integerPattern = Pattern.compile("\\d+");
    private Matcher integerMatcher;
    
    //The control class to get global data from
    ControlClass controlClass;
    
    //The filtered list of events
    ListFilter filteredEvents;
    
    /** Creates new form MonitorWindow */
    public MonitorWindow( ControlClass cc, List m) {
        initComponents();
        controlClass = cc;
        
        filteredEvents = new ListFilter(m);
        jList1.setCellRenderer(new EventCellRenderer() );
        jList1.setModel(filteredEvents);
        filteredEvents.filter();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel11 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(480, 300));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setPreferredSize(new java.awt.Dimension(110, 150));
        jSeparator3.setPreferredSize(new java.awt.Dimension(100, 5));
        jPanel3.add(jSeparator3);

        jLabel1.setText("Filter by Node:");
        jPanel3.add(jLabel1);

        jTextField1.setMinimumSize(new java.awt.Dimension(50, 20));
        jTextField1.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanel3.add(jTextField1);

        jSeparator1.setPreferredSize(new java.awt.Dimension(100, 5));
        jPanel3.add(jSeparator1);

        jLabel2.setText("Filter by Time:");
        jPanel3.add(jLabel2);

        jPanel3.add(jPanel10);

        jLabel3.setText("From ");
        jPanel3.add(jLabel3);

        jTextField2.setPreferredSize(new java.awt.Dimension(30, 20));
        jPanel3.add(jTextField2);

        jPanel11.setPreferredSize(new java.awt.Dimension(30, 10));
        jPanel3.add(jPanel11);

        jLabel4.setText("to ");
        jPanel3.add(jLabel4);

        jTextField3.setPreferredSize(new java.awt.Dimension(30, 20));
        jPanel3.add(jTextField3);

        jSeparator2.setPreferredSize(new java.awt.Dimension(100, 5));
        jPanel3.add(jSeparator2);

        jButton1.setText("Filter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel3.add(jButton1);

        jPanel2.add(jPanel3);

        jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel4.add(jPanel5, java.awt.BorderLayout.NORTH);

        jPanel4.add(jPanel6, java.awt.BorderLayout.EAST);

        jPanel4.add(jPanel7, java.awt.BorderLayout.SOUTH);

        jPanel4.add(jPanel8, java.awt.BorderLayout.WEST);

        jPanel9.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setViewportView(jList1);

        jPanel9.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel4.add(jPanel9, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    /**
     *Method for 'Filter' pushed
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Get the nodes to filter by
        integerMatcher = integerPattern.matcher( jTextField1.getText() );
        filteredEvents.clearNodes();
        while( integerMatcher.find() )
        {
            filteredEvents.addNode( Integer.parseInt( integerMatcher.group() ) );
        }
        
        //Get the start time and stop time
        double start, end;
        try{
            start = Double.parseDouble( jTextField2.getText() );
        }catch( NumberFormatException ex)
        {
            start = Double.MIN_VALUE;
        }
        try{
            end = Double.parseDouble( jTextField3.getText() );
        }catch( NumberFormatException ex)
        {
            end = Double.MAX_VALUE;
        }
        
        //Set the filers
        filteredEvents.setTimeFilter(start, end);
        filteredEvents.filter();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        //System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        new MonitorWindow().show();
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
    
}
