ihm.mainActivity.java Source code

Java tutorial

Introduction

Here is the source code for ihm.mainActivity.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 ihm;

import CSVtoObject.CSVtoObject;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import kevin.LayerStack;
import models.DataList;
import models.Point;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author kenygia
 */
public class mainActivity extends javax.swing.JFrame implements Observer {

    JFreeChart chart;
    String[] columnNames = { "Date", "x", "value" };
    Object[][] data;
    DataList dataList;
    JFileChooser fileChooser;
    CSVtoObject csv;
    LayerStack layer;

    /**
     * Creates new form testBuild
     */
    public mainActivity() {
        dataList = new DataList();
        dataList.addObserver(this);
        chart = ChartFactory.createXYLineChart("Graphique", "x", "Value", createDataset(), PlotOrientation.VERTICAL,
                true, true, false);
        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Files", "csv");
        fileChooser.setFileFilter(filter);
        initComponents();
    }

    /**
     * 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 XYDataset createDataset() {
        final XYSeries sheet = new XYSeries("Serie");
        if (dataList == null) {
            //dataList = new DataList();
            //dataList.addObserver(this);
            dataList.add(new Point(new Date(1970, 1, 1), 5, 7.2));
        }
        int i = 0;
        data = new Object[dataList.size()][3];
        while (i != dataList.size()) {
            Point currentPoint = dataList.getPointAt(i);
            sheet.add(currentPoint.getX(), currentPoint.getValue());
            data[i][0] = currentPoint.getDate();
            data[i][1] = currentPoint.getX();
            data[i][2] = currentPoint.getValue();
            i++;
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(sheet);
        return dataset;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabStack = new javax.swing.JTabbedPane();
        tabPane1 = new ChartPanel(chart);
        tabPane2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableUI = new javax.swing.JTable();
        kevinLayerStack = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        closeMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Bibine Chart");

        javax.swing.GroupLayout tabPane1Layout = new javax.swing.GroupLayout(tabPane1);
        tabPane1.setLayout(tabPane1Layout);
        tabPane1Layout.setHorizontalGroup(tabPane1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 645, Short.MAX_VALUE));
        tabPane1Layout.setVerticalGroup(tabPane1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 445, Short.MAX_VALUE));

        tabStack.addTab("Graphique", tabPane1);

        tableUI.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
        jScrollPane1.setViewportView(tableUI);

        javax.swing.GroupLayout tabPane2Layout = new javax.swing.GroupLayout(tabPane2);
        tabPane2.setLayout(tabPane2Layout);
        tabPane2Layout.setHorizontalGroup(tabPane2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(tabPane2Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
                        .addContainerGap()));
        tabPane2Layout
                .setVerticalGroup(
                        tabPane2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabPane2Layout
                                        .createSequentialGroup().addContainerGap().addComponent(jScrollPane1,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                                        .addContainerGap()));

        tabStack.addTab("Tableau", tabPane2);

        javax.swing.GroupLayout kevinLayerStackLayout = new javax.swing.GroupLayout(kevinLayerStack);
        kevinLayerStack.setLayout(kevinLayerStackLayout);
        kevinLayerStackLayout.setHorizontalGroup(kevinLayerStackLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 136, Short.MAX_VALUE));
        kevinLayerStackLayout.setVerticalGroup(kevinLayerStackLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

        fileMenu.setText("File");

        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, 0));
        openMenuItem.setText("Open");
        openMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                openMenuItemMousePressed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,
                java.awt.event.InputEvent.SHIFT_MASK));
        saveMenuItem.setText("Save");
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,
                java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        saveAsMenuItem.setText("Save as...");
        fileMenu.add(saveAsMenuItem);

        closeMenuItem.setText("Close");
        fileMenu.add(closeMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText("Edit");
        menuBar.add(editMenu);

        setJMenuBar(menuBar);

        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().addComponent(tabStack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(kevinLayerStack, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(tabStack)
                                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(
                                                kevinLayerStack, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addContainerGap()));

        tabStack.getAccessibleContext().setAccessibleName("TabUI");

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

    private void openMenuItemMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openMenuItemMousePressed
        // TODO add your handling code here:
        int open = fileChooser.showOpenDialog(this);
        if (open == JFileChooser.APPROVE_OPTION) {
            System.out.print(fileChooser.getSelectedFile().getAbsolutePath());
            try {
                csv = new CSVtoObject(fileChooser.getSelectedFile().getAbsolutePath(), dataList);
            } catch (IOException | ParseException ex) {
                Logger.getLogger(mainActivity.class.getName()).log(Level.SEVERE, null, ex);
            }
            //System.out.print(dataList);
        } else {
            System.out.print("FAILURE");
        }
    }//GEN-LAST:event_openMenuItemMousePressed

    /**
     * @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 ex) {
            java.util.logging.Logger.getLogger(mainActivity.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(mainActivity.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(mainActivity.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(mainActivity.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>
        //</editor-fold>

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem closeMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel kevinLayerStack;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JPanel tabPane1;
    private javax.swing.JPanel tabPane2;
    private javax.swing.JTabbedPane tabStack;
    private javax.swing.JTable tableUI;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable arg0, Object arg1) {
        chart.getXYPlot().setDataset(createDataset());
        tableUI.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
        tabPane1.repaint();
        tableUI.repaint();
    }
}