64f5536c580d61bab9668f0297db2c876d90f1f0
[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.filechooser.FileFilter;
13 import javax.swing.filechooser.FileNameExtensionFilter;
14 import javax.swing.table.DefaultTableModel;
15 import javax.swing.table.TableModel;
16
17 /**
18 *
19 * @author hari
20 */
21 public class NoticesReportViewDialog extends javax.swing.JDialog {
22
23 private String reportTitle;
24 private String[] reportCols;
25 private ArrayList<Object> reportData;
26
27 /**
28 * Creates new form NoticesReportViewDialog
29 */
30 public NoticesReportViewDialog(java.awt.Frame parent, boolean modal) {
31 super(parent, modal);
32 initComponents();
33 populateClients ();
34 }
35
36 /**
37 * This method is called from within the constructor to initialize the form.
38 * WARNING: Do NOT modify this code. The content of this method is always
39 * regenerated by the Form Editor.
40 */
41 @SuppressWarnings("unchecked")
42 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
43 private void initComponents() {
44
45 popMenu = new javax.swing.JPopupMenu();
46 menuExportCSV = new javax.swing.JMenuItem();
47 labelReportTitle = new javax.swing.JLabel();
48 jScrollPane2 = new javax.swing.JScrollPane();
49 tableReport = new javax.swing.JTable();
50 checkFilterByClient = new javax.swing.JCheckBox();
51 comboClients = new javax.swing.JComboBox<>();
52 buttonApply = new javax.swing.JButton();
53
54 popMenu.setLabel("Popup");
55
56 menuExportCSV.setMnemonic('C');
57 menuExportCSV.setText("Export as CSV...");
58 menuExportCSV.addActionListener(new java.awt.event.ActionListener() {
59 public void actionPerformed(java.awt.event.ActionEvent evt) {
60 menuExportCSVActionPerformed(evt);
61 }
62 });
63 popMenu.add(menuExportCSV);
64
65 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
66 setTitle("View Report");
67 setLocationByPlatform(true);
68
69 labelReportTitle.setFont(new java.awt.Font("Noto Sans", 1, 15)); // NOI18N
70 labelReportTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
71 labelReportTitle.setText("REPORT TITLE");
72
73 tableReport.setModel(new javax.swing.table.DefaultTableModel(
74 new Object [][] {
75
76 },
77 new String [] {
78
79 }
80 ));
81 jScrollPane2.setViewportView(tableReport);
82
83 checkFilterByClient.setText("Filter by Client");
84
85 buttonApply.setMnemonic('a');
86 buttonApply.setText("Apply");
87 buttonApply.setToolTipText("");
88 buttonApply.addActionListener(new java.awt.event.ActionListener() {
89 public void actionPerformed(java.awt.event.ActionEvent evt) {
90 buttonApplyActionPerformed(evt);
91 }
92 });
93
94 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
95 getContentPane().setLayout(layout);
96 layout.setHorizontalGroup(
97 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
98 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
99 .addGroup(layout.createSequentialGroup()
100 .addComponent(labelReportTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
101 .addContainerGap())
102 .addGroup(layout.createSequentialGroup()
103 .addContainerGap()
104 .addComponent(checkFilterByClient, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
105 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
106 .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
107 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
108 .addComponent(buttonApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
109 );
110 layout.setVerticalGroup(
111 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
112 .addGroup(layout.createSequentialGroup()
113 .addComponent(labelReportTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
114 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
115 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
116 .addComponent(checkFilterByClient)
117 .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
118 .addComponent(buttonApply))
119 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
120 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
121 );
122
123 pack();
124 }// </editor-fold>//GEN-END:initComponents
125
126 private void menuExportCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportCSVActionPerformed
127 // TODO add your handling code here:
128 JFileChooser chooser = new JFileChooser ();
129 FileFilter filter = new FileNameExtensionFilter("Comma Separated Values (.csv)", "csv");
130 chooser.setFileFilter(filter);
131 int rt = chooser.showSaveDialog(this);
132 if (rt == JFileChooser.APPROVE_OPTION) {
133 String filePath = chooser.getSelectedFile().getAbsolutePath();
134 if (!filePath.endsWith(".csv"))
135 filePath = filePath.concat(".csv");
136 boolean ret = Utility.saveReportCSV (filePath, reportCols, reportData);
137 if (ret == false)
138 JOptionPane.showMessageDialog(this, ERROR_SAVING_REPORT);
139 else
140 JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
141 }
142 }//GEN-LAST:event_menuExportCSVActionPerformed
143
144 private void buttonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonApplyActionPerformed
145 // TODO add your handling code here:
146 if (! checkFilterByClient.isSelected()) {
147 reportData = Utility.getPendingPaymentBills(-1);
148 if (reportData != null)
149 populateReport();
150 } else {
151 DBItem db = (DBItem)comboClients.getSelectedItem();
152 if (db == null)
153 return;
154 int selid = db.getKey();
155 reportData = Utility.getPendingPaymentBills(selid);
156 if (reportData != null)
157 populateReport();
158 }
159
160 }//GEN-LAST:event_buttonApplyActionPerformed
161 private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
162 private static final String ERROR_SAVING_REPORT = "Error saving report";
163
164 /**
165 * @param args the command line arguments
166 */
167 public static void main(String args[]) {
168 /* Set the Nimbus look and feel */
169 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
170 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
171 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
172 */
173 try {
174 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
175 if ("Nimbus".equals(info.getName())) {
176 javax.swing.UIManager.setLookAndFeel(info.getClassName());
177 break;
178 }
179 }
180 } catch (ClassNotFoundException ex) {
181 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
182 } catch (InstantiationException ex) {
183 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
184 } catch (IllegalAccessException ex) {
185 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
186 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
187 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
188 }
189 //</editor-fold>
190
191 /* Create and display the dialog */
192 java.awt.EventQueue.invokeLater(new Runnable() {
193 public void run() {
194 NoticesReportViewDialog dialog = new NoticesReportViewDialog(new javax.swing.JFrame(), true);
195 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
196 @Override
197 public void windowClosing(java.awt.event.WindowEvent e) {
198 System.exit(0);
199 }
200 });
201 dialog.setVisible(true);
202 }
203 });
204 }
205
206 // Variables declaration - do not modify//GEN-BEGIN:variables
207 private javax.swing.JButton buttonApply;
208 private javax.swing.JCheckBox checkFilterByClient;
209 private javax.swing.JComboBox<DBItem> comboClients;
210 private javax.swing.JScrollPane jScrollPane2;
211 private javax.swing.JLabel labelReportTitle;
212 private javax.swing.JMenuItem menuExportCSV;
213 private javax.swing.JPopupMenu popMenu;
214 private javax.swing.JTable tableReport;
215 // End of variables declaration//GEN-END:variables
216
217 public void setupReportTable(String title, String[] columns,
218 ArrayList<Object> data) {
219 reportTitle = title;
220 reportCols = columns;
221 reportData = data;
222 DefaultTableModel mdl = new DefaultTableModel (columns, 0) {
223 @Override
224 public boolean isCellEditable (int row, int col) {
225 return false;
226 }
227 };
228 tableReport.setModel(mdl);
229 tableReport.setComponentPopupMenu(popMenu);
230
231 labelReportTitle.setText(title);
232 populateReport ();
233 }
234
235 private void populateReport () {
236 DefaultTableModel mdl = (DefaultTableModel)tableReport.getModel();
237 mdl.setRowCount(0);
238 for (int i = 0; i < reportData.size(); i += reportCols.length) {
239 Object[] row = new Object[reportCols.length];
240 for (int j = 0; j < reportCols.length; j ++)
241 row[j] = reportData.get(i+j);
242 mdl.addRow (row);
243
244 }
245 }
246
247 private void populateClients() {
248 ArrayList<Object> clients = Utility.getClientsNameAndId();
249 if (clients == null) return;
250 for (int i = 0; i < clients.size(); i += 2) {
251 comboClients.addItem (new DBItem ((int)clients.get(i),
252 (String)clients.get(i+1)));
253 }
254 }
255 }