Java tutorial
/* * 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 rework; import CSVtoObject.CSVtoObject; import ihm.mainActivity; 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.TransformationList; 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; import tools.analysis.LinearRegression; /** * * @author hourdinf */ public class MainFrame extends javax.swing.JFrame implements Observer { DataList datalist = new DataList(); TransformationList transfList = new TransformationList(datalist); JFreeChart chart; String[] columnNames = { "Date", "x", "value" }; Object[][] data; JFileChooser fileChooser; CSVtoObject csv; /** * Creates new form MainFrame */ public MainFrame() { chart = ChartFactory.createXYLineChart("Graphique", "x", "Value", createDataset(), PlotOrientation.VERTICAL, true, true, false); initComponents(); fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Files", "csv"); fileChooser.setFileFilter(filter); layerStack1.initComponents(transfList); layerStack1.updateLayerStack(); datalist.addObserver(this); transfList.addObserver(layerStack1); datalist.add(new Point(new Date(), 1, 5.0)); datalist.add(new Point(new Date(), 2, 15.0)); datalist.add(new Point(new Date(), 3, 25.0)); datalist.add(new Point(new Date(), 4, 20.0)); datalist.add(new Point(new Date(), 5, 25.0)); datalist.add(new Point(new Date(), 6, 35.0)); datalist.add(new Point(new Date(), 7, 5.0)); datalist.add(new Point(new Date(), 8, 15.0)); datalist.add(new Point(new Date(), 9, 25.0)); datalist.add(new Point(new Date(), 10, 20.0)); datalist.add(new Point(new Date(), 11, 25.0)); datalist.add(new Point(new Date(), 12, 35.0)); datalist.add(new Point(new Date(), 13, 5.0)); datalist.add(new Point(new Date(), 14, 15.0)); datalist.add(new Point(new Date(), 15, 25.0)); datalist.add(new Point(new Date(), 16, 20.0)); datalist.add(new Point(new Date(), 17, 25.0)); datalist.add(new Point(new Date(), 18, 35.0)); tabStack.revalidate(); } 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; } /** * 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() { jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); tabStack = new javax.swing.JTabbedPane(); tabChart = new ChartPanel(chart); jScrollPane1 = new javax.swing.JScrollPane(); tableUI = new javax.swing.JTable(); layerStack1 = new kevin.LayerStack(datalist); jMenuBar2 = new javax.swing.JMenuBar(); jMenu3 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenu4 = new javax.swing.JMenu(); jMenu1.setText("File"); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout tabChartLayout = new javax.swing.GroupLayout(tabChart); tabChart.setLayout(tabChartLayout); tabChartLayout.setHorizontalGroup(tabChartLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 717, Short.MAX_VALUE)); tabChartLayout.setVerticalGroup(tabChartLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 634, Short.MAX_VALUE)); tabStack.addTab("tab1", tabChart); tableUI.setModel( new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(tableUI); tabStack.addTab("tab2", jScrollPane1); layerStack1.setLayout(new java.awt.GridLayout()); layerStack1.setLayout(new java.awt.GridLayout(10, 1)); jMenu3.setText("File"); jMenuItem1.setText("Ouvrir"); jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { jMenuItem1MousePressed(evt); } }); jMenu3.add(jMenuItem1); jMenuBar2.add(jMenu3); jMenu4.setText("Edit"); jMenuBar2.add(jMenu4); setJMenuBar(jMenuBar2); 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, javax.swing.GroupLayout.PREFERRED_SIZE, 719, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(layerStack1, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tabStack) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent( layerStack1, javax.swing.GroupLayout.PREFERRED_SIZE, 635, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jMenuItem1MousePressed {//GEN-HEADEREND:event_jMenuItem1MousePressed // 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_jMenuItem1MousePressed /** * @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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenu jMenu4; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuBar jMenuBar2; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JScrollPane jScrollPane1; private kevin.LayerStack layerStack1; private javax.swing.JPanel tabChart; 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)); tabChart.repaint(); tableUI.repaint(); } }