cgpanalyser.gui.MainWindow.java Source code

Java tutorial

Introduction

Here is the source code for cgpanalyser.gui.MainWindow.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cgpanalyser.gui;

import cgpanalyser.Evolution;
import cgpanalyser.file.EvolutionLoader;
import cgpanalyser.truthtable.GateFuctionsAll;
import cgpanalyser.GraphDataSampler;
import cgpanalyser.file.DefaultFF;
import cgpanalyser.file.FunctionLoader;
import cgpanalyser.gui.chart.ChartCreator;
import cgpanalyser.gui.draw.DrawVariables;
import cgpanalyser.gui.openfile.EvoFileFilter;
import cgpanalyser.gui.openfile.FncFileFilter;
import cgpanalyser.gui.openfile.OpenFileChooser;
import java.awt.Color;
import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author Vlastimil Kapusta, xkapus02@stud.fit.vutbr.cz
 */
public class MainWindow extends javax.swing.JFrame {

    private final EvolutionLoader evoLoader;
    private Evolution evo;
    private final GateFuctionsAll gateFuncsAll;
    private JFreeChart jFreeChart;
    private GraphDataSampler dataSampler;
    private final DrawVariables dVars;

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        evoLoader = new EvolutionLoader();
        jFreeChart = new ChartCreator().create(null);
        dVars = new DrawVariables();
        gateFuncsAll = new GateFuctionsAll();

        //new Thread(new FunctionLoader(new File("defaultFF.txt"), gateFuncsAll, this)).start(); //for debugging
        initComponents();
        //openAndShowEvo(new File("dekoder.evo")); //for debugging
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCompare = new javax.swing.JButton();
        btnNextGraph = new javax.swing.JButton();
        btnZoomInGraph = new javax.swing.JButton();
        btnPrevGraph = new javax.swing.JButton();
        btnZoomOutGraph = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        panelMain = new cgpanalyser.gui.MainPanel(dVars, gateFuncsAll, this);
        xYChartPanel = new cgpanalyser.gui.chart.XYChartPanel(jFreeChart, panelMain);
        checkBoxGreyUnused = new javax.swing.JCheckBox();
        checkBoxColorsFF = new javax.swing.JCheckBox();
        spinnerFitDeltaThreshold = new javax.swing.JSpinner(new SpinnerNumberModel(0, 0, 9999, 1));
        btnResample = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuOpenEvo = new javax.swing.JMenuItem();
        menuOpenFF = new javax.swing.JMenuItem();
        menuDefaultFF = new javax.swing.JMenuItem();
        menuGateUsage = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        menuAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CGP Analyser");

        btnCompare.setText("Compare");
        btnCompare.setToolTipText("Compare selected chromosomes (with left and right mouse button).");
        btnCompare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCompareActionPerformed(evt);
            }
        });

        btnNextGraph.setText(">");
        btnNextGraph.setToolTipText("Graph next.");
        btnNextGraph.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextGraphActionPerformed(evt);
            }
        });

        btnZoomInGraph.setText("Zoom In");
        btnZoomInGraph.setToolTipText("Graph zoom in selected area.");
        btnZoomInGraph.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomInGraphActionPerformed(evt);
            }
        });

        btnPrevGraph.setText("<");
        btnPrevGraph.setToolTipText("Graph previous.");
        btnPrevGraph.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevGraphActionPerformed(evt);
            }
        });

        btnZoomOutGraph.setText("Zoom Out");
        btnZoomOutGraph.setToolTipText("Graph zoom out.");
        btnZoomOutGraph.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomOutGraphActionPerformed(evt);
            }
        });

        jSplitPane1.setBorder(null);
        jSplitPane1.setDividerSize(6);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setResizeWeight(0.5);

        javax.swing.GroupLayout panelMainLayout = new javax.swing.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(panelMainLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        panelMainLayout.setVerticalGroup(panelMainLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 369, Short.MAX_VALUE));

        jSplitPane1.setRightComponent(panelMain);

        xYChartPanel.setPreferredSize(new java.awt.Dimension(680, 280));
        xYChartPanel.setLayout(null);
        jSplitPane1.setLeftComponent(xYChartPanel);

        checkBoxGreyUnused.setSelected(true);
        checkBoxGreyUnused.setText("Grey Unused");
        checkBoxGreyUnused.setToolTipText("Gery unused elements.");
        checkBoxGreyUnused.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxGreyUnusedActionPerformed(evt);
            }
        });

        checkBoxColorsFF.setSelected(true);
        checkBoxColorsFF.setText("Function colors");
        checkBoxColorsFF.setToolTipText("Use colors from function file.");
        checkBoxColorsFF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxColorsFFActionPerformed(evt);
            }
        });

        spinnerFitDeltaThreshold
                .setToolTipText("Fitness delta threshold for graph sampling - higher threshold, less samples.");
        spinnerFitDeltaThreshold.setPreferredSize(new java.awt.Dimension(29, 23));

        btnResample.setText("Resample");
        btnResample.setToolTipText(
                "Sample graph data again with new threshold. Too low threshold may cause performance or memory issues.");
        btnResample.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResampleActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        menuOpenEvo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O,
                java.awt.event.InputEvent.CTRL_MASK));
        menuOpenEvo.setText("Open Evolution");
        menuOpenEvo.setToolTipText("Open evolution record file.");
        menuOpenEvo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuOpenEvoActionPerformed(evt);
            }
        });
        jMenu1.add(menuOpenEvo);

        menuOpenFF.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I,
                java.awt.event.InputEvent.CTRL_MASK));
        menuOpenFF.setText("Open Function File");
        menuOpenFF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuOpenFFActionPerformed(evt);
            }
        });
        jMenu1.add(menuOpenFF);

        menuDefaultFF.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P,
                java.awt.event.InputEvent.CTRL_MASK));
        menuDefaultFF.setText("Create Default FF");
        menuDefaultFF.setToolTipText("Creates default function file in current working directory.");
        menuDefaultFF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDefaultFFActionPerformed(evt);
            }
        });
        jMenu1.add(menuDefaultFF);

        menuGateUsage.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U,
                java.awt.event.InputEvent.CTRL_MASK));
        menuGateUsage.setText("Gate Usage");
        menuGateUsage.setToolTipText("Swot stats of used function in whole evolution (best chromosomes only).");
        menuGateUsage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuGateUsageActionPerformed(evt);
            }
        });
        jMenu1.add(menuGateUsage);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        menuAbout.setText("About");
        menuAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAboutActionPerformed(evt);
            }
        });
        jMenu2.add(menuAbout);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(0, 0, 0).addComponent(btnCompare)
                                .addGap(12, 12, 12).addComponent(btnZoomOutGraph).addGap(6, 6, 6)
                                .addComponent(btnZoomInGraph).addGap(12, 12, 12).addComponent(btnPrevGraph)
                                .addGap(6, 6, 6).addComponent(btnNextGraph).addGap(12, 12, 12)
                                .addComponent(checkBoxGreyUnused).addGap(6, 6, 6).addComponent(checkBoxColorsFF)
                                .addGap(12, 12, 12)
                                .addComponent(spinnerFitDeltaThreshold, javax.swing.GroupLayout.PREFERRED_SIZE, 65,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6).addComponent(btnResample).addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1216, Short.MAX_VALUE))
                        .addContainerGap()));

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
                new java.awt.Component[] { btnZoomInGraph, btnZoomOutGraph });

        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnCompare).addComponent(btnNextGraph).addComponent(btnZoomInGraph)
                                .addComponent(btnPrevGraph).addComponent(btnZoomOutGraph)
                                .addComponent(checkBoxGreyUnused).addComponent(checkBoxColorsFF)
                                .addComponent(spinnerFitDeltaThreshold, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnResample))
                        .addGap(6, 6, 6)
                        .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
                        .addGap(12, 12, 12)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void openAndShowEvo(File file) {
        //load evolution and smaple data in worker thread, then create graph in EDT
        new Thread() {
            @Override
            public void run() {
                evo = evoLoader.loadEvo(file);
                System.out.println("Evolution loaded");

                //choose data to diplay in graph
                dataSampler = new GraphDataSampler(evo, spinnerFitDeltaThreshold);
                dataSampler.sampleAndAddData(0, evo.getListOfGenerations().size() - 1);

                java.awt.EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        panelMain.setEvo(evo);
                        panelMain.setDataSampler(dataSampler);

                        //draw graph and as reaction to graph crosshair event, draw chromosomes
                        jFreeChart = new ChartCreator().create(dataSampler);
                        xYChartPanel.setChart(jFreeChart);
                        xYChartPanel.setDataSampler(dataSampler);

                        setTitle("CGP Analyser | " + file.getAbsolutePath());
                    }
                });
            }
        }.start();
    }

    private void btnCompareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCompareActionPerformed
        if (evo == null)
            return;
        if (dataSampler == null)
            return;
        SelectedWindow selectedWindow = new SelectedWindow(dVars, gateFuncsAll, evo, dataSampler,
                panelMain.getSelectedChromosomesGens()[0], panelMain.getSelectedChromosomesGens()[1]);
    }//GEN-LAST:event_btnCompareActionPerformed

    private void btnNextGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextGraphActionPerformed
        if (dataSampler == null)
            return;

        xYChartPanel.setNextCrosshairValue(jFreeChart);
    }//GEN-LAST:event_btnNextGraphActionPerformed

    private void btnPrevGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevGraphActionPerformed
        if (dataSampler == null)
            return;

        xYChartPanel.setPrevCrosshairValue(jFreeChart);
    }//GEN-LAST:event_btnPrevGraphActionPerformed

    private void btnZoomInGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZoomInGraphActionPerformed
        if (dataSampler == null)
            return;

        if (xYChartPanel.isMarkerActive()) { //zoom in graph - sample evolution using new bounds
            System.out.println("markerStart: " + xYChartPanel.getMarkerStart() + ", markerEnd: "
                    + xYChartPanel.getMarkerEnd());

            //sample data in new worker thread and create chart in EDT
            new Thread() {
                @Override
                public void run() {
                    dataSampler.sampleAndAddData(xYChartPanel.getMarkerStart() - 1,
                            xYChartPanel.getMarkerEnd() - 1);

                    java.awt.EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jFreeChart = new ChartCreator().create(dataSampler);
                            xYChartPanel.setChart(jFreeChart);
                        }
                    });
                }
            }.start();

        }
    }//GEN-LAST:event_btnZoomInGraphActionPerformed

    private void btnZoomOutGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZoomOutGraphActionPerformed
        if (dataSampler == null)
            return;

        if (dataSampler.isZoomableOut()) { //zoom out if possible (show previous data sampling)
            dataSampler.zoomOut(true);
            jFreeChart = new ChartCreator().create(dataSampler);
            xYChartPanel.setChart(jFreeChart);
        }
    }//GEN-LAST:event_btnZoomOutGraphActionPerformed

    private void checkBoxGreyUnusedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxGreyUnusedActionPerformed
        if (checkBoxGreyUnused.isSelected())
            dVars.setUsedColorMain(Color.LIGHT_GRAY);
        else
            dVars.setUsedColorMain(null);
        repaint();
    }//GEN-LAST:event_checkBoxGreyUnusedActionPerformed

    private void checkBoxColorsFFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxColorsFFActionPerformed
        dVars.setDrawColorsFF(checkBoxColorsFF.isSelected());
        repaint();
    }//GEN-LAST:event_checkBoxColorsFFActionPerformed

    private void menuOpenEvoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuOpenEvoActionPerformed
        OpenFileChooser fileChooser = new OpenFileChooser(this, new EvoFileFilter());
        File evoFile = fileChooser.getFilePath();
        if (evoFile != null)
            openAndShowEvo(evoFile);
    }//GEN-LAST:event_menuOpenEvoActionPerformed

    private void menuOpenFFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuOpenFFActionPerformed
        OpenFileChooser fileChooser = new OpenFileChooser(this, new FncFileFilter());
        File functionFile = fileChooser.getFilePath();
        if (functionFile != null)
            new Thread(new FunctionLoader(functionFile, gateFuncsAll, this)).start();
    }//GEN-LAST:event_menuOpenFFActionPerformed

    private void menuDefaultFFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuDefaultFFActionPerformed
        DefaultFF.createDefaultFF();
    }//GEN-LAST:event_menuDefaultFFActionPerformed

    private void menuGateUsageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuGateUsageActionPerformed
        evo.getEvoInfo().computeAndShowEvolutionInfo(gateFuncsAll);
    }//GEN-LAST:event_menuGateUsageActionPerformed

    private void menuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAboutActionPerformed
        JOptionPane.showMessageDialog(null,
                "A Tool for Analysis of Digital Circuit Evolution Records\n" + "Master thesis 2014 \n" + "\n"
                        + "Brno University of Technology\n" + "Faculty of Information Technology\n" + "\n"
                        + "Author: Vlastimil Kapusta, xkapus02@stud.fit.vutbr.cz\n",
                "About", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_menuAboutActionPerformed

    private void btnResampleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResampleActionPerformed
        if (dataSampler == null)
            return;

        new Thread() {
            @Override
            public void run() {
                dataSampler.resample((int) spinnerFitDeltaThreshold.getValue());
                java.awt.EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        jFreeChart = new ChartCreator().create(dataSampler);
                        xYChartPanel.setChart(jFreeChart);
                    }
                });
            }
        }.start();

    }//GEN-LAST:event_btnResampleActionPerformed

    public void addZoomMainValue(int value) {
        panelMain.zoomChroms((double) (dVars.getZoom(true) * 10 + value));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
        * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
        */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCompare;
    private javax.swing.JButton btnNextGraph;
    private javax.swing.JButton btnPrevGraph;
    private javax.swing.JButton btnResample;
    private javax.swing.JButton btnZoomInGraph;
    private javax.swing.JButton btnZoomOutGraph;
    private javax.swing.JCheckBox checkBoxColorsFF;
    private javax.swing.JCheckBox checkBoxGreyUnused;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JMenuItem menuAbout;
    private javax.swing.JMenuItem menuDefaultFF;
    private javax.swing.JMenuItem menuGateUsage;
    private javax.swing.JMenuItem menuOpenEvo;
    private javax.swing.JMenuItem menuOpenFF;
    private cgpanalyser.gui.MainPanel panelMain;
    private javax.swing.JSpinner spinnerFitDeltaThreshold;
    private cgpanalyser.gui.chart.XYChartPanel xYChartPanel;
    // End of variables declaration//GEN-END:variables

}