Generate raised bills functionality completed
[habeas.git] / src / habeas / GenerateRaisedBillsDialog.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
14 /**
15 *
16 * @author hari
17 */
18 public class GenerateRaisedBillsDialog extends javax.swing.JDialog {
19
20 /**
21 * Creates new form GenerateRaisedBillsDialog
22 */
23 public GenerateRaisedBillsDialog(java.awt.Frame parent, boolean modal) {
24 super(parent, modal);
25 initComponents();
26 populateFields ();
27 }
28
29 /**
30 * This method is called from within the constructor to initialize the form.
31 * WARNING: Do NOT modify this code. The content of this method is always
32 * regenerated by the Form Editor.
33 */
34 @SuppressWarnings("unchecked")
35 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
36 private void initComponents() {
37
38 jLabel1 = new javax.swing.JLabel();
39 comboClients = new javax.swing.JComboBox<>();
40 buttonGenerate = new javax.swing.JButton();
41 buttonClose = new javax.swing.JButton();
42
43 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
44 setTitle("Generate Raised Bills");
45 setLocationByPlatform(true);
46
47 jLabel1.setText("For client");
48
49 buttonGenerate.setMnemonic('g');
50 buttonGenerate.setText("Generate");
51 buttonGenerate.addActionListener(new java.awt.event.ActionListener() {
52 public void actionPerformed(java.awt.event.ActionEvent evt) {
53 buttonGenerateActionPerformed(evt);
54 }
55 });
56
57 buttonClose.setMnemonic('C');
58 buttonClose.setText("Close");
59 buttonClose.addActionListener(new java.awt.event.ActionListener() {
60 public void actionPerformed(java.awt.event.ActionEvent evt) {
61 buttonCloseActionPerformed(evt);
62 }
63 });
64
65 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
66 getContentPane().setLayout(layout);
67 layout.setHorizontalGroup(
68 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
69 .addGroup(layout.createSequentialGroup()
70 .addContainerGap()
71 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
72 .addGap(18, 18, 18)
73 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
74 .addComponent(comboClients, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
75 .addGroup(layout.createSequentialGroup()
76 .addComponent(buttonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
77 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
78 .addComponent(buttonClose, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
79 .addContainerGap())
80 );
81 layout.setVerticalGroup(
82 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
83 .addGroup(layout.createSequentialGroup()
84 .addGap(13, 13, 13)
85 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
86 .addComponent(jLabel1)
87 .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
88 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
89 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
90 .addComponent(buttonGenerate)
91 .addComponent(buttonClose))
92 .addContainerGap())
93 );
94
95 pack();
96 }// </editor-fold>//GEN-END:initComponents
97
98 private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
99 // TODO add your handling code here:
100 dispose ();
101 }//GEN-LAST:event_buttonCloseActionPerformed
102
103 private void buttonGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGenerateActionPerformed
104 // TODO add your handling code here:
105 DBItem selitem = (DBItem)comboClients.getSelectedItem();
106 if (selitem == null) return;
107 ArrayList<Object> client = Utility.getClientDetails(selitem.getKey());
108 if (client == null) {
109 JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
110 return;
111 } else if (client.isEmpty()) {
112 JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
113 return;
114 }
115 ArrayList<Object> bills = Utility.getRaisedBills(selitem.getKey());
116 if (bills == null) {
117 JOptionPane.showMessageDialog(this, ERROR_RAISED_BILLS);
118 return;
119 } else if (bills.isEmpty()) {
120 JOptionPane.showMessageDialog(this, ERROR_RAISED_BILLS);
121 return;
122 }
123
124 JFileChooser chooser = new JFileChooser ();
125 FileFilter filter = new FileNameExtensionFilter("Flat OpenDocument text", "fodt");
126 chooser.setFileFilter(filter);
127 int r = chooser.showSaveDialog(this);
128 if (r == JFileChooser.APPROVE_OPTION) {
129 String filename = chooser.getSelectedFile().getAbsolutePath();
130 if (!filename.endsWith(".fodt"))
131 filename = filename.concat(".fodt");
132
133 boolean rt = Utility.generateRaisedNoticesBill(filename, (String)client.get(0),
134 (String)client.get(1), (String)client.get(2), bills);
135 if (rt == false)
136 JOptionPane.showMessageDialog(this, ERROR_WRITING_FILE);
137 else
138 JOptionPane.showMessageDialog(this, SUCCESSFULLY_GENERATED_BILL);
139 }
140 }//GEN-LAST:event_buttonGenerateActionPerformed
141 private static final String ERROR_WRITING_FILE = "Error writing file";
142 private static final String SUCCESSFULLY_GENERATED_BILL = "Successfully generated bill";
143 private static final String ERROR_RAISED_BILLS = "Error getting raised "
144 + "bills or no raised bills found for this client";
145 private static final String ERROR_CLIENT_DETAILS = "Error getting client details";
146
147 /**
148 * @param args the command line arguments
149 */
150 public static void main(String args[]) {
151 /* Set the Nimbus look and feel */
152 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
153 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
154 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
155 */
156 try {
157 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
158 if ("Nimbus".equals(info.getName())) {
159 javax.swing.UIManager.setLookAndFeel(info.getClassName());
160 break;
161 }
162 }
163 } catch (ClassNotFoundException ex) {
164 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
165 } catch (InstantiationException ex) {
166 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
167 } catch (IllegalAccessException ex) {
168 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
169 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
170 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
171 }
172 //</editor-fold>
173
174 /* Create and display the dialog */
175 java.awt.EventQueue.invokeLater(new Runnable() {
176 public void run() {
177 GenerateRaisedBillsDialog dialog = new GenerateRaisedBillsDialog(new javax.swing.JFrame(), true);
178 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
179 @Override
180 public void windowClosing(java.awt.event.WindowEvent e) {
181 System.exit(0);
182 }
183 });
184 dialog.setVisible(true);
185 }
186 });
187 }
188
189 // Variables declaration - do not modify//GEN-BEGIN:variables
190 private javax.swing.JButton buttonClose;
191 private javax.swing.JButton buttonGenerate;
192 private javax.swing.JComboBox<DBItem> comboClients;
193 private javax.swing.JLabel jLabel1;
194 // End of variables declaration//GEN-END:variables
195
196 private void populateFields() {
197 comboClients.removeAllItems();;
198 ArrayList<Object> clients = Utility.getClientsNameAndId();
199 if (clients == null) return;
200 for (int i = 0; i < clients.size(); i +=2) {
201 comboClients.addItem(new DBItem ((int)clients.get(i), (String)clients.get(i+1)));
202 }
203
204 }
205 }