/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ims.GUIInterface;
import ims.dao.TAssetDao;
import ims.service.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import ims.util.*;
import ims.sqlClasses.*;
/**
*
* @author Deleepa
*/
public class GUIUpdate extends javax.swing.JFrame {
JTable dataTable;
/**
* Creates new form GUIUpdate
*/
public GUIUpdate() {
initComponents();
// outputTable.addMouseListener( new MouseAdapter() {
// public void clickOnOutputTable(MouseEvent e) {
// JTable source = (JTable) e.getSource();
//
// int selectedIndex = source.getSelectedRow();
// }
// });
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
searchInput = new javax.swing.JTextField();
searchByCombo = new javax.swing.JComboBox();
searchByLabel = new javax.swing.JLabel();
searchButton = new javax.swing.JButton();
showRecordsPane = new javax.swing.JScrollPane();
headerLabel = new javax.swing.JLabel();
updateButton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setPreferredSize(new java.awt.Dimension(500, 300));
searchByCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "User's Name" }));
searchByLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
searchByLabel.setText("Search By");
searchButton.setText("Search");
searchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonClicked(evt);
}
});
showRecordsPane.setPreferredSize(new java.awt.Dimension(478, 150));
headerLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
headerLabel.setText("Update a record");
updateButton.setText("Update");
updateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateButtonClicked(evt);
}
});
deleteButton.setText("Delete");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(updateButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteButton))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(searchByLabel)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(searchByCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addComponent(searchInput, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44)
.addComponent(searchButton))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(headerLabel))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(showRecordsPane, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(headerLabel)
.addGap(18, 18, 18)
.addComponent(searchByLabel)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(searchByCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchButton))
.addGap(18, 18, 18)
.addComponent(showRecordsPane, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(updateButton)
.addComponent(deleteButton))
.addContainerGap(21, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void submitButtonClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonClicked
System.out.println("Submit button clicked");
if(!searchInput.getText().equals("")) {
try {
ServiceSearch serviceCall = new ServiceSearch(searchInput.getText());
UpdateTableModel model = new UpdateTableModel(serviceCall.getAssetIds(),
serviceCall.getUsers(),
serviceCall.getVendorNames(),
serviceCall.getCpuBrand());
dataTable = new JTable(model);
showRecordsPane.getViewport().add(dataTable);
} catch (Exception e) {
String error = "Sorry. There was an error with your query. Error: " + e.getMessage();
JOptionPane.showMessageDialog(this, error, "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
String message = "You need to enter a search query";
JOptionPane.showMessageDialog(this, message, "Information", JOptionPane.INFORMATION_MESSAGE);
}
}//GEN-LAST:event_submitButtonClicked
private void updateButtonClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonClicked
if(dataTable != null) {
int selectedRow = dataTable.getSelectedRow();
TAssetDao assetDao = new TAssetDao();
ServicePopulateInsertForm populateServiceCall = new ServicePopulateInsertForm(assetDao.selectTAssetFromId((int) dataTable.getValueAt(selectedRow, 0)));
GUIInsertFromUpdate updateRecord = new GUIInsertFromUpdate(populateServiceCall.mapBeans());
updateRecord.setVisible(true);
}
}//GEN-LAST:event_updateButtonClicked
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
if(dataTable != null) {
int selectedRow = dataTable.getSelectedRow();
System.out.println(dataTable.getValueAt(selectedRow, 0));
ServiceDelete serviceCall = new ServiceDelete();
//TODO: test the joptionpane that is returned!
if(serviceCall.deleteRecord((int) dataTable.getValueAt(selectedRow, 0))) {
String message = "That record has been deleted sucessfull!";
JOptionPane.showMessageDialog(this, message, "Record deleted", JOptionPane.INFORMATION_MESSAGE);
} else {
String message = "There was an error deleting that record";
JOptionPane.showMessageDialog(this, message, "Record not deleted", JOptionPane.INFORMATION_MESSAGE);
}
}
}//GEN-LAST:event_deleteButtonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUIUpdate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUIUpdate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUIUpdate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUIUpdate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUIUpdate().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton deleteButton;
private javax.swing.JLabel headerLabel;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton searchButton;
private javax.swing.JComboBox searchByCombo;
private javax.swing.JLabel searchByLabel;
private javax.swing.JTextField searchInput;
private javax.swing.JScrollPane showRecordsPane;
private javax.swing.JButton updateButton;
// End of variables declaration//GEN-END:variables
}