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 ch.bfh.lca._15h.server.gui; import ch.bfh.lca._15h.library.Database.SQLLiteDatabase; import ch.bfh.lca._15h.library.GenericResultRow; import ch.bfh.lca._15h.server.exporter.DatabaseExportHandler; import ch.bfh.lca._15h.server.exporter.ExportHandler; import ch.bfh.lca._15h.server.importer.ImportHandler; import ch.bfh.lca._15h.server.statisticServices.StatisticHandler; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.ComponentOrientation; import java.util.List; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.filechooser.FileNameExtensionFilter; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; /** * * @author Stefan */ public class MainWindowServer extends javax.swing.JFrame { private String activeModule = ""; /** * Creates new form MainWindowServer */ public MainWindowServer() { 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() { jSplitPane1 = new javax.swing.JSplitPane(); jPanel2 = new javax.swing.JPanel(); txtJsonPath = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jpbImportProgress = new javax.swing.JProgressBar(); lblFixSentinellaJson = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); btnSetDatabasePath = new javax.swing.JButton(); txtDatabasePath = new javax.swing.JTextField(); lblFixSQLLite = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); cmbDimension = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); txtYear = new javax.swing.JTextField(); btnLoadTable = new javax.swing.JButton(); btnLoadGraph = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); btnExport = new javax.swing.JButton(); pnlMain = new javax.swing.JPanel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMaximumSize(new java.awt.Dimension(1136, 2147483647)); setMinimumSize(new java.awt.Dimension(1136, 0)); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Import")); txtJsonPath.setText("Dateipfad"); jButton1.setText("..."); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); lblFixSentinellaJson.setText("Sentinella json"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(txtJsonPath) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jpbImportProgress, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(lblFixSentinellaJson).addGap(0, 0, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblFixSentinellaJson) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtJsonPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jpbImportProgress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(245, 245, 245))); txtJsonPath.getAccessibleContext().setAccessibleName("txtFilePath"); jButton1.getAccessibleContext().setAccessibleName("btnImport"); lblFixSentinellaJson.getAccessibleContext().setAccessibleName("lblFixJson"); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("BAG Database")); btnSetDatabasePath.setText("..."); btnSetDatabasePath.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSetDatabasePathActionPerformed(evt); } }); txtDatabasePath.setText("Dateipfad"); lblFixSQLLite.setText("SQLLite Datenbank"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(txtDatabasePath) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSetDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(lblFixSQLLite).addGap(0, 0, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(lblFixSQLLite, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnSetDatabasePath)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Statistik")); cmbDimension.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Alter gruppiert", "Alter nicht gruppiert" })); jLabel3.setText("Jahr"); txtYear.setText("2014"); btnLoadTable.setText("Tabellen"); btnLoadTable.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnLoadTableActionPerformed(evt); } }); btnLoadGraph.setText("Grafik"); btnLoadGraph.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnLoadGraphActionPerformed(evt); } }); jLabel2.setText("Dimension"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cmbDimension, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtYear) .addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addGroup(jPanel3Layout.createSequentialGroup().addComponent(btnLoadTable) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnLoadGraph, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel2)).addGap(0, 135, Short.MAX_VALUE))) .addContainerGap())); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cmbDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnLoadTable).addComponent(btnLoadGraph)) .addContainerGap(41, Short.MAX_VALUE))); cmbDimension.getAccessibleContext().setAccessibleName(""); jLabel3.getAccessibleContext().setAccessibleName("lblFixYear"); jLabel3.getAccessibleContext().setAccessibleDescription(""); txtYear.getAccessibleContext().setAccessibleName("txtYear"); btnLoadTable.getAccessibleContext().setAccessibleName("btnTable"); btnLoadGraph.getAccessibleContext().setAccessibleName("btnGraph"); jLabel2.getAccessibleContext().setAccessibleName("lblFixDimension"); btnExport.setText("Export"); btnExport.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnExportActionPerformed(evt); } }); javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain); pnlMain.setLayout(pnlMainLayout); pnlMainLayout.setHorizontalGroup(pnlMainLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 711, Short.MAX_VALUE)); pnlMainLayout.setVerticalGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE)); jMenu1.setText("File"); 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() .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()))) .addGroup(layout.createSequentialGroup().addComponent(btnExport).addGap(0, 0, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnExport))); jPanel2.getAccessibleContext().setAccessibleName("pnlImport"); jPanel3.getAccessibleContext().setAccessibleName("pnlImport"); btnExport.getAccessibleContext().setAccessibleName("btnExport"); getAccessibleContext().setAccessibleName("test"); pack(); }// </editor-fold>//GEN-END:initComponents private void btnLoadGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadGraphActionPerformed this.activeModule = "graph"; int year = 0; try { year = Integer.parseInt(this.txtYear.getText()); } catch (Exception exp) { year = 2014; } String dbFilePath = this.txtDatabasePath.getText(); ExportHandler eh = new ExportHandler(new DatabaseExportHandler(new SQLLiteDatabase(dbFilePath))); JFreeChart chart = null; switch (this.cmbDimension.getSelectedItem().toString()) { case "Alter gruppiert": chart = eh.getChartByAgeGroup(year); break; case "Alter nicht gruppiert": chart = eh.getChartByAge(year); break; } ChartPanel pnl = new ChartPanel(chart); this.getChart(pnl); }//GEN-LAST:event_btnLoadGraphActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // JFileChooser-Objekt erstellen JFileChooser chooser = new JFileChooser(); // Dialog zum Speichern von Dateien anzeigen int resultValue = chooser.showDialog(null, "Bitte whle die json Datei aus, die Sie importieren mchten"); if (resultValue == JFileChooser.APPROVE_OPTION) { this.txtJsonPath.setText(chooser.getSelectedFile().getAbsolutePath()); ImportHandler ih = new ImportHandler(this.txtJsonPath.getText(), this.txtDatabasePath.getText()); ih.importData(); JOptionPane.showMessageDialog(new JFrame(), "Import finished", "Information", JOptionPane.INFORMATION_MESSAGE); } }//GEN-LAST:event_jButton1ActionPerformed private void getChart(ChartPanel pnl) { this.pnlMain.removeAll(); this.pnlMain.setLayout(new java.awt.BorderLayout()); this.pnlMain.add(pnl, BorderLayout.CENTER); this.pnlMain.validate(); this.pnlMain.repaint(); } private void drawTable(List<GenericResultRow> lines) { Object[][] rows = new Object[lines.size()][3]; Object[] header = { "Alter", "Mnner", "Frauen" }; int i = 0; for (GenericResultRow grr : lines) { rows[i][0] = grr.getValueAt(0); rows[i][1] = grr.getValueAt(1); rows[i][2] = grr.getValueAt(2); i++; } JTable table = new JTable(rows, header); this.pnlMain.removeAll(); this.pnlMain.setLayout(new java.awt.BorderLayout()); this.pnlMain.add(new JScrollPane(table), BorderLayout.WEST); this.pnlMain.validate(); this.pnlMain.repaint(); } private void btnSetDatabasePathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetDatabasePathActionPerformed // JFileChooser-Objekt erstellen JFileChooser chooser = new JFileChooser(); // Dialog zum Speichern von Dateien anzeigen int resultValue = chooser.showDialog(null, "Bitte whle die SQLLite Datenbank aus"); if (resultValue == JFileChooser.APPROVE_OPTION) { this.txtDatabasePath.setText(chooser.getSelectedFile().getAbsolutePath()); } }//GEN-LAST:event_btnSetDatabasePathActionPerformed private void btnLoadTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadTableActionPerformed this.activeModule = "table"; int year = 0; try { year = Integer.parseInt(this.txtYear.getText()); } catch (Exception e) { year = 2015; } ExportHandler eh = new ExportHandler( new DatabaseExportHandler(new SQLLiteDatabase(this.txtDatabasePath.getText()))); StatisticHandler sh = new StatisticHandler(); List<GenericResultRow> lines = null; JTable aTable = null; switch (this.cmbDimension.getSelectedItem().toString()) { case "Alter gruppiert": lines = sh.getResultRowByAgeGroups(eh.getDataSourceByYear(year)); break; case "Alter nicht gruppiert": lines = sh.getResultRowByAge(eh.getDataSourceByYear(year)); break; } this.drawTable(lines); }//GEN-LAST:event_btnLoadTableActionPerformed private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed String dbPath = this.txtDatabasePath.getText(); ExportHandler eh = new ExportHandler(new DatabaseExportHandler(new SQLLiteDatabase(dbPath))); List<GenericResultRow> lines = null; try { if (cmbDimension.getSelectedItem().toString().equals("Alter gruppiert")) { if (this.activeModule.equals("table")) { eh.ExportToExcelByYearAndAgeCategories(this.getYear(), this.getPath()); } else { eh.ExportToPopulationCharByAgeGroup(this.getYear(), this.getPath()); } } else { if (this.activeModule.equals("table")) { eh.ExportToExcelByYearAndAge(this.getYear(), this.getPath()); } else { eh.ExportToPopulationCharByAge(this.getYear(), this.getPath()); } } } catch (Exception exc) { } }//GEN-LAST:event_btnExportActionPerformed /** * @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(MainWindowServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainWindowServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainWindowServer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainWindowServer.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 MainWindowServer().setVisible(true); } }); } private int getYear() { int year = 0; try { year = Integer.parseInt(this.txtYear.getText()); } catch (Exception e) { year = 2014; } return year; } private String getPath() { String text = ""; FileNameExtensionFilter filter = null; String path = ""; if (this.activeModule.equals("table")) { filter = new FileNameExtensionFilter("EXCEL File", "xlsx"); text = "Wo mchten Sie das Excel ersellen?"; } else { filter = new FileNameExtensionFilter("JPEG File", "jpg"); text = "Wo mchten Sie das Bild ersellen?"; } // JFileChooser-Objekt erstellen JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(filter); // Dialog zum Speichern von Dateien anzeigen int resultValue = chooser.showDialog(null, text); if (resultValue != JFileChooser.ABORT) { path = chooser.getSelectedFile().getAbsolutePath(); } return path; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnExport; private javax.swing.JButton btnLoadGraph; private javax.swing.JButton btnLoadTable; private javax.swing.JButton btnSetDatabasePath; private javax.swing.JComboBox cmbDimension; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; 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.JSplitPane jSplitPane1; private javax.swing.JProgressBar jpbImportProgress; private javax.swing.JLabel lblFixSQLLite; private javax.swing.JLabel lblFixSentinellaJson; private javax.swing.JPanel pnlMain; private javax.swing.JTextField txtDatabasePath; private javax.swing.JTextField txtJsonPath; private javax.swing.JTextField txtYear; // End of variables declaration//GEN-END:variables }