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 InterfaceModule; import ExceptionModule.InvalidLoggerException; import java.awt.BorderLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Observable; import java.util.Observer; import java.util.Set; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import org.jfree.chart.ChartPanel; /** * * @author Pedro */ public class JRobert extends javax.swing.JFrame implements Observer { /** * Creates new form JRobert */ public JRobert() { 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); jPanel2 = new javax.swing.JPanel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jLabel1 = new javax.swing.JLabel(); generateValueReport = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jRadioButton3 = new javax.swing.JRadioButton(); jRadioButton4 = new javax.swing.JRadioButton(); jTextField1 = new javax.swing.JTextField(); generateGeneralReport = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox<>(); generateGraph = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jComboBox2 = new javax.swing.JComboBox<>(); jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); exportToFile = new javax.swing.JButton(); cleanTextArea = new javax.swing.JButton(); jPanel3 = new javax.swing.JPanel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); importLogger = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); buttonGroup1.add(jRadioButton1); jRadioButton1.setSelected(true); jRadioButton1.setText("Ms"); jRadioButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton1ActionPerformed(evt); } }); buttonGroup1.add(jRadioButton2); jRadioButton2.setText("Ano"); jLabel1.setText("Relatrio de Valor arrecadado agrupado por"); generateValueReport.setText("Gerar"); generateValueReport.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateValueReportActionPerformed(evt); } }); jLabel2.setText("Relatrio Geral de Parqumetros"); buttonGroup2.add(jRadioButton3); jRadioButton3.setSelected(true); jRadioButton3.setText("Dia"); jRadioButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton3ActionPerformed(evt); } }); buttonGroup2.add(jRadioButton4); jRadioButton4.setText("Ms"); generateGeneralReport.setText("Gerar"); generateGeneralReport.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateGeneralReportActionPerformed(evt); } }); jLabel3.setText("Grfico de Arrecadao Anual"); jComboBox1.setModel(createParkingMeterList()); generateGraph.setText("Gerar"); generateGraph.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generateGraphActionPerformed(evt); } }); jLabel4.setText("Parqumetro"); jLabel5.setText("Parqumetro"); jComboBox2.setModel(createParkingMeterList()); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel1).addComponent(generateValueReport).addComponent(jLabel2) .addComponent(generateGeneralReport).addComponent(jLabel3).addComponent(generateGraph) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jRadioButton1) .addGap(18, 18, 18).addComponent(jRadioButton2)) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup().addComponent(jLabel4) .addGap(18, 18, 18).addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup().addComponent(jLabel5) .addGap(18, 18, 18).addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(29, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(38, 38, 38).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton1).addComponent(jRadioButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(generateValueReport).addGap(41, 41, 41).addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton3).addComponent(jRadioButton4) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(generateGeneralReport).addGap(41, 41, 41).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(generateGraph) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jTextArea1.setEditable(false); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); exportToFile.setText("Exportar para arquivo"); exportToFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exportToFileActionPerformed(evt); } }); cleanTextArea.setText("Novo relatrio"); cleanTextArea.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cleanTextAreaActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(exportToFile) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(cleanTextArea))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(exportToFile).addComponent(cleanTextArea)) .addContainerGap())); jTabbedPane1.addTab("Relatrios", jPanel1); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 573, Short.MAX_VALUE)); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 445, Short.MAX_VALUE)); jTabbedPane1.addTab("Grfico", jPanel3); jMenu1.setText("File"); importLogger.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK)); importLogger.setText("Import Logger"); importLogger.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { importLoggerActionPerformed(evt); } }); jMenu1.add(importLogger); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); 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().addComponent(jTabbedPane1) .addGap(18, 18, 18) .addComponent(jPanel2, 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().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTabbedPane1).addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void generateValueReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateValueReportActionPerformed Controller c = Controller.getInstance(); int filterType; if (buttonGroup1.isSelected(jRadioButton1.getModel())) filterType = 0; else filterType = 1; String id = jComboBox2.getSelectedItem().toString(); jTextArea1.setText(c.generateValueReport(id, filterType)); }//GEN-LAST:event_generateValueReportActionPerformed private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jRadioButton1ActionPerformed private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jRadioButton3ActionPerformed private void generateGeneralReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateGeneralReportActionPerformed Controller c = Controller.getInstance(); int filterType; if (buttonGroup2.isSelected(jRadioButton3.getModel())) filterType = 1; else filterType = 0; int date = Integer.parseInt(jTextField1.getText()); jTextArea1.setText(c.generateGeneralReport(date, filterType)); }//GEN-LAST:event_generateGeneralReportActionPerformed private void generateGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateGraphActionPerformed Object o = jComboBox1.getSelectedItem(); ChartPanel chartPanel = null; if (o != null) { String id = o.toString(); chartPanel = new ChartPanel(Controller.getInstance().generateGraph(id)); } else JOptionPane.showMessageDialog(this, "Nenhum parqumetro selecionado \n importe um Logger para realizar operao"); //jPanel3.add(chartPanel, BorderLayout.NORTH); jTabbedPane1.setComponentAt(1, chartPanel); jTabbedPane1.setSelectedIndex(1); }//GEN-LAST:event_generateGraphActionPerformed private void exportToFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportToFileActionPerformed JFileChooser chooser = new JFileChooser(System.getProperty("user.dir") + "/saves/"); chooser.setDialogTitle("Salve o relatrio"); File fileToSave; int selection = chooser.showSaveDialog(this); if (selection == JFileChooser.APPROVE_OPTION) fileToSave = chooser.getSelectedFile(); else return; try { FileWriter fr = new FileWriter(fileToSave); BufferedWriter bw = new BufferedWriter(fr); String[] text = jTextArea1.getText().split("\n"); for (String s : text) { bw.write(s); bw.newLine(); } bw.close(); fr.close(); } catch (IOException e) { } }//GEN-LAST:event_exportToFileActionPerformed private void importLoggerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importLoggerActionPerformed try { Controller c = Controller.getInstance(); File f = loadFile(); if (f != null) c.importLogger(f); update(c, null); } catch (InvalidLoggerException | IOException ex) { JOptionPane.showMessageDialog(this, ex.getMessage()); } }//GEN-LAST:event_importLoggerActionPerformed private void cleanTextAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanTextAreaActionPerformed jTextArea1.setText(""); }//GEN-LAST:event_cleanTextAreaActionPerformed private File loadFile() { JFileChooser chooser = new JFileChooser(System.getProperty("user.dir") + "/saves/"); FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT file", "txt"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } else return null; } private ComboBoxModel createParkingMeterList() { Controller c = Controller.getInstance(); DefaultComboBoxModel model = new DefaultComboBoxModel(); Set<String> set = (Set<String>) c.getParkingMeterList(); for (String id : set) model.addElement(id); return model; } /** * @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(JRobert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(JRobert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(JRobert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(JRobert.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 JRobert().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JButton cleanTextArea; private javax.swing.JButton exportToFile; private javax.swing.JButton generateGeneralReport; private javax.swing.JButton generateGraph; private javax.swing.JButton generateValueReport; private javax.swing.JMenuItem importLogger; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JComboBox<String> jComboBox2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JRadioButton jRadioButton4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables private javax.swing.JButton closeGraph; @Override public void update(Observable o, Object arg) { jComboBox1.setModel(createParkingMeterList()); jComboBox2.setModel(createParkingMeterList()); } }