f9d7a8fcfdcf36bf1f4f0a85aebc1f85fb1c705a
[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 jScrollPane2.setViewportView(tableReport);
81
82 checkFilterByClient.setText("Filter by Client");
83
84 buttonApply.setMnemonic('a');
85 buttonApply.setText("Apply");
86 buttonApply.setToolTipText("");
87 buttonApply.addActionListener(new java.awt.event.ActionListener() {
88 public void actionPerformed(java.awt.event.ActionEvent evt) {
89 buttonApplyActionPerformed(evt);
90 }
91 });
92
93 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
94 getContentPane().setLayout(layout);
95 layout.setHorizontalGroup(
96 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
97 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
98 .addGroup(layout.createSequentialGroup()
99 .addComponent(labelReportTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
100 .addContainerGap())
101 .addGroup(layout.createSequentialGroup()
102 .addContainerGap()
103 .addComponent(checkFilterByClient, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
104 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
105 .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
106 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
107 .addComponent(buttonApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
108 );
109 layout.setVerticalGroup(
110 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
111 .addGroup(layout.createSequentialGroup()
112 .addComponent(labelReportTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
113 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
114 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
115 .addComponent(checkFilterByClient)
116 .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
117 .addComponent(buttonApply))
118 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
119 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
120 );
121
122 pack();
123 }// </editor-fold>//GEN-END:initComponents
124
125 private void menuExportCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportCSVActionPerformed
126 // TODO add your handling code here:
127 JFileChooser chooser = new JFileChooser ();
128 FileFilter filter = new FileNameExtensionFilter("Comma Separated Values (.csv)", "csv");
129 chooser.setFileFilter(filter);
130 int rt = chooser.showSaveDialog(this);
131 if (rt == JFileChooser.APPROVE_OPTION) {
132 String filePath = chooser.getSelectedFile().getAbsolutePath();
133 if (!filePath.endsWith(".csv"))
134 filePath = filePath.concat(".csv");
135 boolean ret = Utility.saveReportCSV (filePath, reportCols, reportData);
136 if (ret == false)
137 JOptionPane.showMessageDialog(this, ERROR_SAVING_REPORT);
138 else
139 JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
140 }
141 }//GEN-LAST:event_menuExportCSVActionPerformed
142
143 private void buttonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonApplyActionPerformed
144 // TODO add your handling code here:
145 if (! checkFilterByClient.isSelected()) {
146 reportData = Utility.getReportData(currentReport, -1);
147
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.getReportData(currentReport, 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(int reportNum, String title, String[] columns,
218 ArrayList<Object> data) {
219 currentReport = reportNum;
220 reportTitle = title;
221 reportCols = columns;
222 reportData = data;
223 DefaultTableModel mdl = new DefaultTableModel (columns, 0) {
224 @Override
225 public boolean isCellEditable (int row, int col) {
226 return false;
227 }
228 };
229 tableReport.setModel(mdl);
230 tableReport.setComponentPopupMenu(popMenu);
231
232 labelReportTitle.setText(title);
233 populateReport ();
234 }
235
236 private void populateReport () {
237 DefaultTableModel mdl = (DefaultTableModel)tableReport.getModel();
238 mdl.setRowCount(0);
239 for (int i = 0; i < reportData.size(); i += reportCols.length) {
240 Object[] row = new Object[reportCols.length];
241 for (int j = 0; j < reportCols.length; j ++)
242 row[j] = reportData.get(i+j);
243 mdl.addRow (row);
244
245 }
246 }
247
248 private void populateClients() {
249 ArrayList<Object> clients = Utility.getClientsNameAndId();
250 if (clients == null) return;
251 for (int i = 0; i < clients.size(); i += 2) {
252 comboClients.addItem (new DBItem ((int)clients.get(i),
253 (String)clients.get(i+1)));
254 }
255 }
256 }