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