2636cbc51f4938a2ad0c2655a2663b1f74ac6a6f
[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.filechooser.FileFilter;
12 import javax.swing.filechooser.FileNameExtensionFilter;
13 import javax.swing.table.DefaultTableModel;
14
15 /**
16 *
17 * @author hari
18 */
19 public class NoticesReportViewDialog extends javax.swing.JDialog {
20
21 private int currentReport;
22 private String reportTitle;
23 private String[] reportCols;
24 private ArrayList<Object> reportData;
25
26 /**
27 * Creates new form NoticesReportViewDialog
28 */
29 public NoticesReportViewDialog(java.awt.Frame parent, boolean modal) {
30 super(parent, modal);
31 initComponents();
32 populateClients ();
33 }
34
35 /**
36 * This method is called from within the constructor to initialize the form.
37 * WARNING: Do NOT modify this code. The content of this method is always
38 * regenerated by the Form Editor.
39 */
40 @SuppressWarnings("unchecked")
41 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
42 private void initComponents() {
43
44 popMenu = new javax.swing.JPopupMenu();
45 menuExportCSV = new javax.swing.JMenuItem();
46 labelReportTitle = new javax.swing.JLabel();
47 jScrollPane2 = new javax.swing.JScrollPane();
48 tableReport = new javax.swing.JTable();
49 checkFilterByClient = new javax.swing.JCheckBox();
50 comboClients = new javax.swing.JComboBox<>();
51 buttonApply = new javax.swing.JButton();
52
53 popMenu.setLabel("Popup");
54
55 menuExportCSV.setMnemonic('C');
56 menuExportCSV.setText("Export as CSV...");
57 menuExportCSV.addActionListener(new java.awt.event.ActionListener() {
58 public void actionPerformed(java.awt.event.ActionEvent evt) {
59 menuExportCSVActionPerformed(evt);
60 }
61 });
62 popMenu.add(menuExportCSV);
63
64 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
65 setTitle("View Report");
66 setLocationByPlatform(true);
67
68 labelReportTitle.setFont(new java.awt.Font("Noto Sans", 1, 15)); // NOI18N
69 labelReportTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
70 labelReportTitle.setText("REPORT TITLE");
71
72 tableReport.setModel(new javax.swing.table.DefaultTableModel(
73 new Object [][] {
74
75 },
76 new String [] {
77
78 }
79 ));
80 tableReport.setCellSelectionEnabled(true);
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.getReportData(currentReport, -1);
148
149 if (reportData != null)
150 populateReport();
151 } else {
152 DBItem db = (DBItem)comboClients.getSelectedItem();
153 if (db == null)
154 return;
155 int selid = db.getKey();
156 reportData = Utility.getReportData(currentReport, selid);
157 if (reportData != null)
158 populateReport();
159 }
160
161 }//GEN-LAST:event_buttonApplyActionPerformed
162 private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
163 private static final String ERROR_SAVING_REPORT = "Error saving report";
164
165 /**
166 * @param args the command line arguments
167 */
168 public static void main(String args[]) {
169 /* Set the Nimbus look and feel */
170 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
171 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
172 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
173 */
174 try {
175 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
176 if ("Nimbus".equals(info.getName())) {
177 javax.swing.UIManager.setLookAndFeel(info.getClassName());
178 break;
179 }
180 }
181 } catch (ClassNotFoundException ex) {
182 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
183 } catch (InstantiationException ex) {
184 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
185 } catch (IllegalAccessException ex) {
186 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
187 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
188 java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
189 }
190 //</editor-fold>
191
192 /* Create and display the dialog */
193 java.awt.EventQueue.invokeLater(new Runnable() {
194 public void run() {
195 NoticesReportViewDialog dialog = new NoticesReportViewDialog(new javax.swing.JFrame(), true);
196 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
197 @Override
198 public void windowClosing(java.awt.event.WindowEvent e) {
199 System.exit(0);
200 }
201 });
202 dialog.setVisible(true);
203 }
204 });
205 }
206
207 // Variables declaration - do not modify//GEN-BEGIN:variables
208 private javax.swing.JButton buttonApply;
209 private javax.swing.JCheckBox checkFilterByClient;
210 private javax.swing.JComboBox<DBItem> comboClients;
211 private javax.swing.JScrollPane jScrollPane2;
212 private javax.swing.JLabel labelReportTitle;
213 private javax.swing.JMenuItem menuExportCSV;
214 private javax.swing.JPopupMenu popMenu;
215 private javax.swing.JTable tableReport;
216 // End of variables declaration//GEN-END:variables
217
218 public void setupReportTable(int reportNum, String title, String[] columns,
219 ArrayList<Object> data) {
220 currentReport = reportNum;
221 reportTitle = title;
222 reportCols = columns;
223 reportData = data;
224 DefaultTableModel mdl = new DefaultTableModel (columns, 0) {
225 @Override
226 public boolean isCellEditable (int row, int col) {
227 return false;
228 }
229 };
230 tableReport.setModel(mdl);
231 tableReport.setComponentPopupMenu(popMenu);
232
233 labelReportTitle.setText(title);
234 populateReport ();
235 }
236
237 private void populateReport () {
238 DefaultTableModel mdl = (DefaultTableModel)tableReport.getModel();
239 mdl.setRowCount(0);
240 for (int i = 0; i < reportData.size(); i += reportCols.length) {
241 Object[] row = new Object[reportCols.length];
242 for (int j = 0; j < reportCols.length; j ++)
243 row[j] = reportData.get(i+j);
244 mdl.addRow (row);
245
246 }
247 }
248
249 private void populateClients() {
250 ArrayList<Object> clients = Utility.getClientsNameAndId();
251 if (clients == null) return;
252 for (int i = 0; i < clients.size(); i += 2) {
253 comboClients.addItem (new DBItem ((int)clients.get(i),
254 (String)clients.get(i+1)));
255 }
256 }
257 }