Added Reporting Functionality
[habeas.git] / src / habeas / NoticesReportViewDialog.java
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package habeas;
7
8 import java.util.ArrayList;
9 import javax.swing.JFileChooser;
10 import javax.swing.JOptionPane;
11 import javax.swing.JTable;
12 import javax.swing.table.DefaultTableModel;
13 import javax.swing.table.TableModel;
14
15 /**
16 *
17 * @author hari
18 */
19 public class NoticesReportViewDialog extends javax.swing.JDialog {
20
21 private String reportTitle;
22 private String[] reportCols;
23 private ArrayList<Object> reportData;
24
25 /**
26 * Creates new form NoticesReportViewDialog
27 */
28 public NoticesReportViewDialog(java.awt.Frame parent, boolean modal) {
29 super(parent, modal);
30 initComponents();
31 }
32
33 /**
34 * This method is called from within the constructor to initialize the form.
35 * WARNING: Do NOT modify this code. The content of this method is always
36 * regenerated by the Form Editor.
37 */
38 @SuppressWarnings("unchecked")
39 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
40 private void initComponents() {
41
42 popMenu = new javax.swing.JPopupMenu();
43 menuExportCSV = new javax.swing.JMenuItem();
44 labelReportTitle = new javax.swing.JLabel();
45 jScrollPane2 = new javax.swing.JScrollPane();
46 tableReport = new javax.swing.JTable();
47
48 popMenu.setLabel("Popup");
49
50 menuExportCSV.setMnemonic('C');
51 menuExportCSV.setText("Export as CSV...");
52 menuExportCSV.addActionListener(new java.awt.event.ActionListener() {
53 public void actionPerformed(java.awt.event.ActionEvent evt) {
54 menuExportCSVActionPerformed(evt);
55 }
56 });
57 popMenu.add(menuExportCSV);
58
59 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
60 setTitle("View Report");
61
62 labelReportTitle.setFont(new java.awt.Font("Noto Sans", 1, 15)); // NOI18N
63 labelReportTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
64 labelReportTitle.setText("REPORT TITLE");
65
66 tableReport.setModel(new javax.swing.table.DefaultTableModel(
67 new Object [][] {
68
69 },
70 new String [] {
71
72 }
73 ));
74 jScrollPane2.setViewportView(tableReport);
75
76 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
77 getContentPane().setLayout(layout);
78 layout.setHorizontalGroup(
79 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
80 .addGroup(layout.createSequentialGroup()
81 .addComponent(labelReportTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
82 .addContainerGap())
83 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
84 );
85 layout.setVerticalGroup(
86 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
87 .addGroup(layout.createSequentialGroup()
88 .addComponent(labelReportTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
89 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
90 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
91 );
92
93 pack();
94 }// </editor-fold>//GEN-END:initComponents
95
96 private void menuExportCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportCSVActionPerformed
97 // TODO add your handling code here:
98 JFileChooser chooser = new JFileChooser ();
99 int rt = chooser.showSaveDialog(this);
100 if (rt == JFileChooser.APPROVE_OPTION) {
101 String filePath = chooser.getSelectedFile().getAbsolutePath();
102 boolean ret = Utility.saveReportCSV (filePath, reportCols, reportData);
103 if (ret == false)
104 JOptionPane.showMessageDialog(this, ERROR_SAVING_REPORT);
105 else
106 JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
107 }
108 }//GEN-LAST:event_menuExportCSVActionPerformed
109 private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
110 private static final String ERROR_SAVING_REPORT = "Error saving report";
111
112 /**
113 * @param args the command line arguments
114 */
115 public static void main(String args[]) {
116 /* Set the Nimbus look and feel */
117 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
118 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
119 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
120 */
121 try {
122 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
123 if ("Nimbus".equals(info.getName())) {
124 javax.swing.UIManager.setLookAndFeel(info.getClassName());
125 break;
126 }
127 }
128 } catch (ClassNotFoundException ex) {
129 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
130 } catch (InstantiationException ex) {
131 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
132 } catch (IllegalAccessException ex) {
133 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
134 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
135 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
136 }
137 //</editor-fold>
138
139 /* Create and display the dialog */
140 java.awt.EventQueue.invokeLater(new Runnable() {
141 public void run() {
142 NoticesReportViewDialog dialog = new NoticesReportViewDialog(new javax.swing.JFrame(), true);
143 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
144 @Override
145 public void windowClosing(java.awt.event.WindowEvent e) {
146 System.exit(0);
147 }
148 });
149 dialog.setVisible(true);
150 }
151 });
152 }
153
154 // Variables declaration - do not modify//GEN-BEGIN:variables
155 private javax.swing.JScrollPane jScrollPane2;
156 private javax.swing.JLabel labelReportTitle;
157 private javax.swing.JMenuItem menuExportCSV;
158 private javax.swing.JPopupMenu popMenu;
159 private javax.swing.JTable tableReport;
160 // End of variables declaration//GEN-END:variables
161
162 public void setupReportTable(String title, String[] columns,
163 ArrayList<Object> data) {
164 reportTitle = title;
165 reportCols = columns;
166 reportData = data;
167 DefaultTableModel mdl = new DefaultTableModel (columns, 0) {
168 @Override
169 public boolean isCellEditable (int row, int col) {
170 return false;
171 }
172 };
173 tableReport.setModel(mdl);
174 tableReport.setComponentPopupMenu(popMenu);
175
176 labelReportTitle.setText(title);
177 for (int i = 0; i < data.size(); i += columns.length) {
178 Object[] row = new Object[columns.length];
179 for (int j = 0; j < columns.length; j ++)
180 row[j] = data.get(i+j);
181 mdl.addRow (row);
182 }
183 }
184 }