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.
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
;
19 public class NoticesReportViewDialog
extends javax
.swing
.JDialog
{
21 private int currentReport
;
22 private String reportTitle
;
23 private String
[] reportCols
;
24 private ArrayList
<Object
> reportData
;
27 * Creates new form NoticesReportViewDialog
29 public NoticesReportViewDialog(java
.awt
.Frame parent
, boolean modal
) {
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.
40 @SuppressWarnings("unchecked")
41 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
42 private void initComponents() {
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();
53 popMenu
.setLabel("Popup");
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
);
62 popMenu
.add(menuExportCSV
);
64 setDefaultCloseOperation(javax
.swing
.WindowConstants
.DISPOSE_ON_CLOSE
);
65 setTitle("View Report");
66 setLocationByPlatform(true);
68 labelReportTitle
.setFont(new java
.awt
.Font("Noto Sans", 1, 15)); // NOI18N
69 labelReportTitle
.setHorizontalAlignment(javax
.swing
.SwingConstants
.CENTER
);
70 labelReportTitle
.setText("REPORT TITLE");
72 tableReport
.setModel(new javax
.swing
.table
.DefaultTableModel(
80 jScrollPane2
.setViewportView(tableReport
);
82 checkFilterByClient
.setText("Filter by Client");
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
);
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
)
101 .addGroup(layout
.createSequentialGroup()
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
))
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
))
123 }// </editor-fold>//GEN-END:initComponents
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
);
137 JOptionPane
.showMessageDialog(this, ERROR_SAVING_REPORT
);
139 JOptionPane
.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY
);
141 }//GEN-LAST:event_menuExportCSVActionPerformed
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);
148 if (reportData
!= null)
151 DBItem db
= (DBItem
)comboClients
.getSelectedItem();
154 int selid
= db
.getKey();
155 reportData
= Utility
.getReportData(currentReport
, selid
);
156 if (reportData
!= null)
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";
165 * @param args the command line arguments
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
174 for (javax
.swing
.UIManager
.LookAndFeelInfo info
: javax
.swing
.UIManager
.getInstalledLookAndFeels()) {
175 if ("Nimbus".equals(info
.getName())) {
176 javax
.swing
.UIManager
.setLookAndFeel(info
.getClassName());
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
);
191 /* Create and display the dialog */
192 java
.awt
.EventQueue
.invokeLater(new Runnable() {
194 NoticesReportViewDialog dialog
= new NoticesReportViewDialog(new javax
.swing
.JFrame(), true);
195 dialog
.addWindowListener(new java
.awt
.event
.WindowAdapter() {
197 public void windowClosing(java
.awt
.event
.WindowEvent e
) {
201 dialog
.setVisible(true);
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
217 public void setupReportTable(int reportNum
, String title
, String
[] columns
,
218 ArrayList
<Object
> data
) {
219 currentReport
= reportNum
;
221 reportCols
= columns
;
223 DefaultTableModel mdl
= new DefaultTableModel (columns
, 0) {
225 public boolean isCellEditable (int row
, int col
) {
229 tableReport
.setModel(mdl
);
230 tableReport
.setComponentPopupMenu(popMenu
);
232 labelReportTitle
.setText(title
);
236 private void populateReport () {
237 DefaultTableModel mdl
= (DefaultTableModel
)tableReport
.getModel();
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
);
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)));