/*
* GuiTestBox.java
*
* Created on 10-ene-2010, 20:30:53
*/
package gui;
//import javax.swing.JFrame;
import communicator.Communicator;
import communicator.Node;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import messages.TestMessage;
/**
*
* @author oscar
*/
public class GuiTestBox extends javax.swing.JDialog {
Vector<TestMessage> testMessages = new Vector<TestMessage>();
/** Creates new form GuiTestBox */
public GuiTestBox(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
getRootPane().setDefaultButton(addButton);
}
/** 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() {
jScrollPane1 = new javax.swing.JScrollPane();
testsTable = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
submitButton = new javax.swing.JButton();
addButton = new javax.swing.JButton();
closeButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
testsTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Query", "Node Id.", "Timestamp"
}
));
testsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
testsTable.setName("testsTable"); // NOI18N
jScrollPane1.setViewportView(testsTable);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gui.Gui.class).getContext().getResourceMap(GuiTestBox.class);
testsTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("testsTable.columnModel.title0")); // NOI18N
testsTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("testsTable.columnModel.title1")); // NOI18N
testsTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("testsTable.columnModel.title2")); // NOI18N
jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
submitButton.setMnemonic('S');
submitButton.setText(resourceMap.getString("submitButton.text")); // NOI18N
submitButton.setName("submitButton"); // NOI18N
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(evt);
}
});
addButton.setMnemonic('A');
addButton.setText(resourceMap.getString("addButton.text")); // NOI18N
addButton.setName("addButton"); // NOI18N
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt, testsTable);
}
});
closeButton.setMnemonic('C');
closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
closeButton.setName("closeButton"); // NOI18N
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(submitButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 620, Short.MAX_VALUE)
.addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 808, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 464, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(addButton)
.addComponent(closeButton)
.addComponent(submitButton))
.addContainerGap(27, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
dispose();
}//GEN-LAST:event_closeButtonActionPerformed
private void addButtonActionPerformed(java.awt.event.ActionEvent evt, JTable testsTable) {//GEN-FIRST:event_addButtonActionPerformed
JFrame mainFrame = Gui.getApplication().getMainFrame();
GuiAddBox addBox = new GuiAddBox(mainFrame, true, this);
addBox.setVisible(true);
}//GEN-LAST:event_addButtonActionPerformed
public void addTest(TestMessage newmessage) {
String query = newmessage.getQuery();
String nodeId = newmessage.getNodeId();
long time = newmessage.getOrder();
Object [] row = new Object[3];
row[0] = query;
row[1] = nodeId;
row[2] = time;
testMessages.add(newmessage);
((DefaultTableModel) testsTable.getModel() ).addRow(row);
}
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
//send all test messages
Collections.sort(testMessages);
try {
Communicator comm = Communicator.getCommunicator();
for(TestMessage msg : testMessages) {
Node node = comm.getNodeForId(msg.getNodeId());
if(node!=null)
node.sendMessage(msg);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_submitButtonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GuiTestBox dialog = new GuiTestBox(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addButton;
private javax.swing.JButton closeButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton submitButton;
private javax.swing.JTable testsTable;
// End of variables declaration//GEN-END:variables
}