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
;
18 public class GenerateRaisedBillsDialog
extends javax
.swing
.JDialog
{
21 * Creates new form GenerateRaisedBillsDialog
23 public GenerateRaisedBillsDialog(java
.awt
.Frame parent
, boolean modal
) {
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.
34 @SuppressWarnings("unchecked")
35 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
36 private void initComponents() {
38 jLabel1
= new javax
.swing
.JLabel();
39 comboClients
= new javax
.swing
.JComboBox
<>();
40 buttonGenerate
= new javax
.swing
.JButton();
41 buttonClose
= new javax
.swing
.JButton();
43 setDefaultCloseOperation(javax
.swing
.WindowConstants
.DISPOSE_ON_CLOSE
);
44 setTitle("Generate Raised Bills");
45 setLocationByPlatform(true);
47 jLabel1
.setText("For client");
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
);
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
);
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()
71 .addComponent(jLabel1
, javax
.swing
.GroupLayout
.PREFERRED_SIZE
, 113, javax
.swing
.GroupLayout
.PREFERRED_SIZE
)
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
)))
81 layout
.setVerticalGroup(
82 layout
.createParallelGroup(javax
.swing
.GroupLayout
.Alignment
.LEADING
)
83 .addGroup(layout
.createSequentialGroup()
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
))
96 }// </editor-fold>//GEN-END:initComponents
98 private void buttonCloseActionPerformed(java
.awt
.event
.ActionEvent evt
) {//GEN-FIRST:event_buttonCloseActionPerformed
99 // TODO add your handling code here:
101 }//GEN-LAST:event_buttonCloseActionPerformed
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
);
111 } else if (client
.isEmpty()) {
112 JOptionPane
.showMessageDialog(this, ERROR_CLIENT_DETAILS
);
115 ArrayList
<Object
> bills
= Utility
.getRaisedBills(selitem
.getKey());
117 JOptionPane
.showMessageDialog(this, ERROR_RAISED_BILLS
);
119 } else if (bills
.isEmpty()) {
120 JOptionPane
.showMessageDialog(this, ERROR_RAISED_BILLS
);
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");
133 boolean rt
= Utility
.generateRaisedNoticesBill(filename
, (String
)client
.get(0),
134 (String
)client
.get(1), (String
)client
.get(2), bills
);
136 JOptionPane
.showMessageDialog(this, ERROR_WRITING_FILE
);
138 JOptionPane
.showMessageDialog(this, SUCCESSFULLY_GENERATED_BILL
);
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";
148 * @param args the command line arguments
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
157 for (javax
.swing
.UIManager
.LookAndFeelInfo info
: javax
.swing
.UIManager
.getInstalledLookAndFeels()) {
158 if ("Nimbus".equals(info
.getName())) {
159 javax
.swing
.UIManager
.setLookAndFeel(info
.getClassName());
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
);
174 /* Create and display the dialog */
175 java
.awt
.EventQueue
.invokeLater(new Runnable() {
177 GenerateRaisedBillsDialog dialog
= new GenerateRaisedBillsDialog(new javax
.swing
.JFrame(), true);
178 dialog
.addWindowListener(new java
.awt
.event
.WindowAdapter() {
180 public void windowClosing(java
.awt
.event
.WindowEvent e
) {
184 dialog
.setVisible(true);
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
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)));