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