Merge origin/master
[habeas.git] / src / habeas / ClientMasterDialog.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.JOptionPane;
10 import javax.swing.event.ListSelectionEvent;
11 import javax.swing.event.ListSelectionListener;
12 import javax.swing.table.DefaultTableModel;
13
14 /**
15 *
16 * @author hari
17 */
18 public class ClientMasterDialog extends javax.swing.JDialog {
19
20 /**
21 * Creates new form ClientMasterDialog
22 * @param parent
23 * @param modal
24 */
25 public ClientMasterDialog(java.awt.Frame parent, boolean modal) {
26 super(parent, modal);
27 initComponents();
28 populateTable ();
29 tableClients.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
30 @Override
31 public void valueChanged(ListSelectionEvent lse) {
32 int r = getSelectedClient ();
33 if (r == -1) {
34 buttonAdd.setEnabled(true);
35 buttonUpdate.setEnabled(false);
36 buttonDelete.setEnabled(false);
37 return;
38 }
39
40 ArrayList<Object> clients = Utility.getClientDetails (r);
41 for (int q = 0; q < clients.size(); q += 5) {
42 textClientName.setText (clients.get(q).toString());
43 textClientAddress.setText(clients.get(q+1).toString());
44 textContactPerson.setText(clients.get(q+2).toString());
45 textMailID.setText(clients.get(q+3).toString());
46 textPhoneNumber.setText(clients.get(q+4).toString());
47 }
48 buttonAdd.setEnabled(false);
49 buttonUpdate.setEnabled(true);
50 buttonDelete.setEnabled(true);
51 }
52 });
53 }
54
55 private int getSelectedClient () {
56 int i = tableClients.getSelectedRow();
57 if (i == -1)
58 return -1;
59
60 DefaultTableModel mdl = (DefaultTableModel)tableClients.getModel();
61 int r = (int)mdl.getValueAt(i, 0);
62 return r;
63 }
64
65 private void populateTable () {
66 DefaultTableModel mdl = (DefaultTableModel)tableClients.getModel();
67 mdl.setRowCount(0);
68 ArrayList<Object> clientsdata = Utility.getClientsNameAndId();
69 if (clientsdata == null) return;
70 for (int i = 0 ; i < clientsdata.size(); i +=2) {
71 mdl.addRow(new Object[] {
72 clientsdata.get(i), clientsdata.get(i+1)});
73 }
74 }
75
76 /**
77 * This method is called from within the constructor to initialize the form.
78 * WARNING: Do NOT modify this code. The content of this method is always
79 * regenerated by the Form Editor.
80 */
81 @SuppressWarnings("unchecked")
82 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
83 private void initComponents() {
84
85 jScrollPane1 = new javax.swing.JScrollPane();
86 tableClients = new javax.swing.JTable();
87 buttonAdd = new javax.swing.JButton();
88 buttonDelete = new javax.swing.JButton();
89 buttonUpdate = new javax.swing.JButton();
90 jLabel1 = new javax.swing.JLabel();
91 textClientName = new javax.swing.JTextField();
92 jLabel2 = new javax.swing.JLabel();
93 jScrollPane2 = new javax.swing.JScrollPane();
94 textClientAddress = new javax.swing.JTextArea();
95 jLabel3 = new javax.swing.JLabel();
96 textContactPerson = new javax.swing.JTextField();
97 jLabel4 = new javax.swing.JLabel();
98 textPhoneNumber = new javax.swing.JTextField();
99 jLabel5 = new javax.swing.JLabel();
100 textMailID = new javax.swing.JTextField();
101 buttonClose = new javax.swing.JButton();
102 buttonClearSelection = new javax.swing.JButton();
103
104 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
105 setTitle("Clients Master");
106 setLocationByPlatform(true);
107
108 tableClients.setModel(new javax.swing.table.DefaultTableModel(
109 new Object [][] {
110
111 },
112 new String [] {
113 "id", "Client Name"
114 }
115 ) {
116 Class[] types = new Class [] {
117 java.lang.Integer.class, java.lang.String.class
118 };
119
120 public Class getColumnClass(int columnIndex) {
121 return types [columnIndex];
122 }
123 @Override
124 public boolean isCellEditable (int row, int col) {
125 return false;
126 }
127 });
128 tableClients.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
129 tableClients.setShowGrid(true);
130 jScrollPane1.setViewportView(tableClients);
131 tableClients.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
132
133 buttonAdd.setMnemonic('A');
134 buttonAdd.setText("Add");
135 buttonAdd.addActionListener(new java.awt.event.ActionListener() {
136 public void actionPerformed(java.awt.event.ActionEvent evt) {
137 buttonAddActionPerformed(evt);
138 }
139 });
140
141 buttonDelete.setMnemonic('D');
142 buttonDelete.setText("Delete");
143 buttonDelete.setEnabled(false);
144 buttonDelete.addActionListener(new java.awt.event.ActionListener() {
145 public void actionPerformed(java.awt.event.ActionEvent evt) {
146 buttonDeleteActionPerformed(evt);
147 }
148 });
149
150 buttonUpdate.setMnemonic('U');
151 buttonUpdate.setText("Update");
152 buttonUpdate.setEnabled(false);
153 buttonUpdate.addActionListener(new java.awt.event.ActionListener() {
154 public void actionPerformed(java.awt.event.ActionEvent evt) {
155 buttonUpdateActionPerformed(evt);
156 }
157 });
158
159 jLabel1.setText("Client Name");
160
161 jLabel2.setText("Client Address");
162
163 textClientAddress.setColumns(20);
164 textClientAddress.setRows(5);
165 jScrollPane2.setViewportView(textClientAddress);
166
167 jLabel3.setText("Contact Person");
168
169 jLabel4.setText("Phone Number");
170
171 jLabel5.setText("Mail ID");
172
173 buttonClose.setMnemonic('C');
174 buttonClose.setText("Close");
175 buttonClose.addActionListener(new java.awt.event.ActionListener() {
176 public void actionPerformed(java.awt.event.ActionEvent evt) {
177 buttonCloseActionPerformed(evt);
178 }
179 });
180
181 buttonClearSelection.setMnemonic('S');
182 buttonClearSelection.setText("Clear Selection");
183 buttonClearSelection.addActionListener(new java.awt.event.ActionListener() {
184 public void actionPerformed(java.awt.event.ActionEvent evt) {
185 buttonClearSelectionActionPerformed(evt);
186 }
187 });
188
189 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
190 getContentPane().setLayout(layout);
191 layout.setHorizontalGroup(
192 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
193 .addGroup(layout.createSequentialGroup()
194 .addContainerGap()
195 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
196 .addComponent(jScrollPane1)
197 .addGroup(layout.createSequentialGroup()
198 .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
199 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
200 .addComponent(buttonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
201 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
202 .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
203 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
204 .addComponent(buttonClearSelection)
205 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
206 .addComponent(buttonClose, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
207 .addGroup(layout.createSequentialGroup()
208 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
209 .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
210 .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
211 .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
212 .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
213 .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
214 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
215 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
216 .addComponent(textClientName)
217 .addComponent(jScrollPane2)
218 .addComponent(textContactPerson)
219 .addComponent(textPhoneNumber)
220 .addComponent(textMailID))))
221 .addContainerGap())
222 );
223 layout.setVerticalGroup(
224 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
225 .addGroup(layout.createSequentialGroup()
226 .addContainerGap()
227 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
228 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
229 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
230 .addComponent(buttonAdd)
231 .addComponent(buttonUpdate)
232 .addComponent(buttonDelete)
233 .addComponent(buttonClose)
234 .addComponent(buttonClearSelection))
235 .addGap(18, 18, 18)
236 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
237 .addComponent(jLabel1)
238 .addComponent(textClientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
239 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
240 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
241 .addComponent(jLabel2)
242 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
243 .addGap(10, 10, 10)
244 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
245 .addComponent(textContactPerson, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
246 .addComponent(jLabel3))
247 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
248 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
249 .addComponent(jLabel4)
250 .addComponent(textPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
251 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
252 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
253 .addComponent(jLabel5)
254 .addComponent(textMailID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
255 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
256 );
257
258 pack();
259 }// </editor-fold>//GEN-END:initComponents
260
261 private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
262 // TODO add your handling code here:
263 dispose ();
264 }//GEN-LAST:event_buttonCloseActionPerformed
265
266 private void buttonClearSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearSelectionActionPerformed
267 clearFields();
268 }//GEN-LAST:event_buttonClearSelectionActionPerformed
269
270 private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
271 // TODO add your handling code here:
272 boolean rt = Utility.addClient (textClientName.getText(), textClientAddress.getText(),
273 textContactPerson.getText(), textMailID.getText(),
274 textPhoneNumber.getText());
275
276 if (rt == true) {
277 JOptionPane.showMessageDialog(this, SUCCESSFULLY_ADDED);
278 clearFields();
279 populateTable();
280 }
281 else
282 JOptionPane.showMessageDialog (this, ERROR_IN_ADDING);
283 }//GEN-LAST:event_buttonAddActionPerformed
284 private static final String SUCCESSFULLY_ADDED = "Successfully added";
285 private static final String ERROR_IN_ADDING = "Error in adding";
286
287 private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed
288 // TODO add your handling code here:
289 int r = getSelectedClient();
290 if (r == -1) return;
291 int conf = JOptionPane.showConfirmDialog(this, CONFIRM_DELETE_CLIENT);
292 if (conf == JOptionPane.YES_OPTION) {
293 boolean rt = Utility.deleteClient (r);
294 if (rt == true) {
295 JOptionPane.showMessageDialog(this, SUCCESSFULLY_DELETED);
296 clearFields();
297 populateTable();
298 }
299 else
300 JOptionPane.showMessageDialog(this, ERROR_IN_DELETING);
301 }
302 }//GEN-LAST:event_buttonDeleteActionPerformed
303 private static final String ERROR_IN_DELETING = "Error in deleting";
304
305 private void buttonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUpdateActionPerformed
306 // TODO add your handling code here:
307 int r = getSelectedClient();
308 if (r == -1 ) return;
309 boolean rt = Utility.updateClient (r, textClientName.getText(),
310 textClientAddress.getText(), textContactPerson.getText(),
311 textMailID.getText(), textPhoneNumber.getText());
312 if (rt == true) {
313 JOptionPane.showMessageDialog(this, SUCCESSFULLY_UPDATED);
314 clearFields();
315 populateTable();
316 } else {
317 JOptionPane.showMessageDialog(this, ERROR_IN_UPDATING);
318 }
319
320 }//GEN-LAST:event_buttonUpdateActionPerformed
321 private static final String ERROR_IN_UPDATING = "Error in updating";
322 private static final String SUCCESSFULLY_UPDATED = "Successfully updated";
323 private static final String SUCCESSFULLY_DELETED = "Successfully deleted";
324 private static final String CONFIRM_DELETE_CLIENT = "Are you sure? This will"
325 + " also delete all other data associated with this client";
326
327 private void clearFields() {
328 // TODO add your handling code here:
329 tableClients.clearSelection();
330 textClientAddress.setText ("");
331 textClientName.setText ("");
332 textContactPerson.setText("");
333 textMailID.setText("");
334 textPhoneNumber.setText("");
335 }
336
337 /**
338 * @param args the command line arguments
339 */
340 public static void main(String args[]) {
341 /* Set the Nimbus look and feel */
342 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
343 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
344 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
345 */
346 try {
347 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
348 if ("Nimbus".equals(info.getName())) {
349 javax.swing.UIManager.setLookAndFeel(info.getClassName());
350 break;
351 }
352 }
353 } catch (ClassNotFoundException ex) {
354 java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
355 } catch (InstantiationException ex) {
356 java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
357 } catch (IllegalAccessException ex) {
358 java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
359 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
360 java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
361 }
362 //</editor-fold>
363
364 /* Create and display the dialog */
365 java.awt.EventQueue.invokeLater(new Runnable() {
366 public void run() {
367 ClientMasterDialog dialog = new ClientMasterDialog(new javax.swing.JFrame(), true);
368 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
369 @Override
370 public void windowClosing(java.awt.event.WindowEvent e) {
371 System.exit(0);
372 }
373 });
374 dialog.setVisible(true);
375 }
376 });
377 }
378
379 // Variables declaration - do not modify//GEN-BEGIN:variables
380 private javax.swing.JButton buttonAdd;
381 private javax.swing.JButton buttonClearSelection;
382 private javax.swing.JButton buttonClose;
383 private javax.swing.JButton buttonDelete;
384 private javax.swing.JButton buttonUpdate;
385 private javax.swing.JLabel jLabel1;
386 private javax.swing.JLabel jLabel2;
387 private javax.swing.JLabel jLabel3;
388 private javax.swing.JLabel jLabel4;
389 private javax.swing.JLabel jLabel5;
390 private javax.swing.JScrollPane jScrollPane1;
391 private javax.swing.JScrollPane jScrollPane2;
392 private javax.swing.JTable tableClients;
393 private javax.swing.JTextArea textClientAddress;
394 private javax.swing.JTextField textClientName;
395 private javax.swing.JTextField textContactPerson;
396 private javax.swing.JTextField textMailID;
397 private javax.swing.JTextField textPhoneNumber;
398 // End of variables declaration//GEN-END:variables
399 }