Reporting Functionality - added HTML export
[habeas.git] / src / habeas / NoticesReportViewDialog.java
index ed0932d..4563f64 100644 (file)
@@ -5,12 +5,17 @@
  */
 package habeas;
 
+import java.awt.Component;
 import java.util.ArrayList;
 import javax.swing.JFileChooser;
 import javax.swing.JOptionPane;
 import javax.swing.JTable;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.filechooser.FileNameExtensionFilter;
 import javax.swing.table.DefaultTableModel;
-import javax.swing.table.TableModel;
+import javax.swing.table.TableCellRenderer;
+import javax.swing.table.TableColumn;
+import javax.swing.table.TableColumnModel;
 
 /**
  *
@@ -18,6 +23,7 @@ import javax.swing.table.TableModel;
  */
 public class NoticesReportViewDialog extends javax.swing.JDialog {
 
+    private int currentReport;
     private String reportTitle;
     private String[] reportCols;
     private ArrayList<Object> reportData;
@@ -28,6 +34,7 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     public NoticesReportViewDialog(java.awt.Frame parent, boolean modal) {
         super(parent, modal);
         initComponents();
+        populateClients ();
     }
 
     /**
@@ -41,9 +48,13 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
 
         popMenu = new javax.swing.JPopupMenu();
         menuExportCSV = new javax.swing.JMenuItem();
+        menuExportHTML = new javax.swing.JMenuItem();
         labelReportTitle = new javax.swing.JLabel();
         jScrollPane2 = new javax.swing.JScrollPane();
         tableReport = new javax.swing.JTable();
+        checkFilterByClient = new javax.swing.JCheckBox();
+        comboClients = new javax.swing.JComboBox<>();
+        buttonApply = new javax.swing.JButton();
 
         popMenu.setLabel("Popup");
 
@@ -56,8 +67,18 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
         });
         popMenu.add(menuExportCSV);
 
+        menuExportHTML.setMnemonic('H');
+        menuExportHTML.setText("Export as HTML...");
+        menuExportHTML.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                menuExportHTMLActionPerformed(evt);
+            }
+        });
+        popMenu.add(menuExportHTML);
+
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
         setTitle("View Report");
+        setLocationByPlatform(true);
 
         labelReportTitle.setFont(new java.awt.Font("Noto Sans", 1, 15)); // NOI18N
         labelReportTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
@@ -71,23 +92,49 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
 
             }
         ));
+        tableReport.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
+        tableReport.setCellSelectionEnabled(true);
+        tableReport.setComponentPopupMenu(popMenu);
         jScrollPane2.setViewportView(tableReport);
 
+        checkFilterByClient.setText("Filter by Client");
+
+        buttonApply.setMnemonic('a');
+        buttonApply.setText("Apply");
+        buttonApply.setToolTipText("");
+        buttonApply.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                buttonApplyActionPerformed(evt);
+            }
+        });
+
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
             .addGroup(layout.createSequentialGroup()
                 .addComponent(labelReportTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                 .addContainerGap())
-            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
+            .addGroup(layout.createSequentialGroup()
+                .addContainerGap()
+                .addComponent(checkFilterByClient, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(comboClients, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(buttonApply, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                 .addComponent(labelReportTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(checkFilterByClient)
+                    .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(buttonApply))
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
         );
 
         pack();
@@ -96,9 +143,13 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     private void menuExportCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportCSVActionPerformed
         // TODO add your handling code here:
         JFileChooser chooser = new JFileChooser ();
+        FileFilter filter = new FileNameExtensionFilter("Comma Separated Values (.csv)", "csv");
+        chooser.setFileFilter(filter);
         int rt = chooser.showSaveDialog(this);
         if (rt == JFileChooser.APPROVE_OPTION) {
             String filePath = chooser.getSelectedFile().getAbsolutePath();
+            if (!filePath.endsWith(".csv"))
+                filePath = filePath.concat(".csv");
             boolean ret = Utility.saveReportCSV (filePath, reportCols, reportData);
             if (ret == false) 
                 JOptionPane.showMessageDialog(this, ERROR_SAVING_REPORT);
@@ -106,6 +157,44 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
                 JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
         }
     }//GEN-LAST:event_menuExportCSVActionPerformed
+
+    private void buttonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonApplyActionPerformed
+        // TODO add your handling code here:
+        if (! checkFilterByClient.isSelected()) {
+            reportData = Utility.getReportData(currentReport, -1);
+                        
+           if (reportData != null)
+                populateReport();
+        } else {
+            DBItem db = (DBItem)comboClients.getSelectedItem();
+            if (db == null)
+                return;
+            int selid = db.getKey();
+            reportData = Utility.getReportData(currentReport, selid);
+            if (reportData != null)
+                populateReport();
+        }
+        
+    }//GEN-LAST:event_buttonApplyActionPerformed
+
+    private void menuExportHTMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportHTMLActionPerformed
+        // TODO add your handling code here:
+        JFileChooser chooser = new JFileChooser ();
+        FileNameExtensionFilter flt = new FileNameExtensionFilter("Hypertext Markup File (.html)", "html");
+        chooser.setFileFilter(flt);
+        int rt = chooser.showSaveDialog(this);
+        if (rt == JFileChooser.APPROVE_OPTION) {
+            String filepath = chooser.getSelectedFile().getAbsolutePath();
+            if (! (filepath.endsWith(".html") || filepath.endsWith(".htm")) ) 
+                filepath = filepath.concat(".html");
+            boolean ret= Utility.saveReportHTML (filepath, reportTitle, reportCols, reportData);
+            if (ret == false)
+                JOptionPane.showMessageDialog (this, ERROR_SAVING_REPORT);
+            else
+                JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
+            
+        }
+    }//GEN-LAST:event_menuExportHTMLActionPerformed
     private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
     private static final String ERROR_SAVING_REPORT = "Error saving report";
 
@@ -152,15 +241,20 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     }
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
+    private javax.swing.JButton buttonApply;
+    private javax.swing.JCheckBox checkFilterByClient;
+    private javax.swing.JComboBox<DBItem> comboClients;
     private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JLabel labelReportTitle;
     private javax.swing.JMenuItem menuExportCSV;
+    private javax.swing.JMenuItem menuExportHTML;
     private javax.swing.JPopupMenu popMenu;
     private javax.swing.JTable tableReport;
     // End of variables declaration//GEN-END:variables
 
-    public void setupReportTable(String title, String[] columns, 
+    public void setupReportTable(int reportNum, String title, String[] columns, 
             ArrayList<Object> data) {
+        currentReport = reportNum;
         reportTitle = title;
         reportCols = columns;
         reportData = data;
@@ -171,14 +265,48 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
             }
         };
         tableReport.setModel(mdl);
-        tableReport.setComponentPopupMenu(popMenu);
         
         labelReportTitle.setText(title);
-        for (int i = 0; i < data.size(); i += columns.length) {
-            Object[] row = new Object[columns.length];
-            for (int j = 0; j < columns.length; j ++)
-                row[j] = data.get(i+j);
+        populateReport ();
+    }
+    
+    private void populateReport () {
+        DefaultTableModel mdl = (DefaultTableModel)tableReport.getModel();
+        mdl.setRowCount(0);
+        for (int i = 0; i < reportData.size(); i += reportCols.length) {
+            Object[] row = new Object[reportCols.length];
+            for (int j = 0; j < reportCols.length; j ++)
+                row[j] = reportData.get(i+j);
             mdl.addRow (row);
         }
+        // resize the column widths to content size
+        for (int i = 0; i < tableReport.getColumnCount(); i ++) {
+
+            TableColumn clm = tableReport.getColumnModel().getColumn (i);
+            int preferredWidth = clm.getMinWidth()+10;
+            int maxWidth = 300;
+            int minWidth = 100;
+            for (int r = 0; r < tableReport.getRowCount(); r ++) {
+                TableCellRenderer rend = tableReport.getCellRenderer(r, i);
+                Component c = tableReport.prepareRenderer(rend, r, i);
+                int width = c.getPreferredSize().width + tableReport.getIntercellSpacing().width;
+                preferredWidth = Math.max (preferredWidth, width+10);
+                if (preferredWidth >= maxWidth)
+                {
+                    preferredWidth = maxWidth;
+                    break;
+                }
+            }
+            clm.setPreferredWidth(Math.max(minWidth, preferredWidth));
+        }        
+    }
+
+    private void populateClients() {
+        ArrayList<Object> clients = Utility.getClientsNameAndId();
+        if (clients == null) return;
+        for (int i = 0; i < clients.size(); i += 2) {
+            comboClients.addItem (new DBItem ((int)clients.get(i), 
+                    (String)clients.get(i+1)));
+        }
     }
 }