Newer
Older
websocket-client / src / main / java / websocket / tester / WebSocketTesterFrame.java
Mark on 31 Mar 2016 9 KB Initial commit.
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package websocket.tester;

import java.net.URI;
import java.net.URISyntaxException;

/**
 *
 * @author mark
 */
public class WebSocketTesterFrame extends javax.swing.JFrame {

	private Client client;

	/**
	 * Creates new form WebSocketTesterFrame
	 */
	public WebSocketTesterFrame() {
		initComponents();
	}

	/**
	 * 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() {

      jLabel1 = new javax.swing.JLabel();
      txtUri = new javax.swing.JTextField();
      btnConnect = new javax.swing.JButton();
      txtMessage = new javax.swing.JTextField();
      btnSend = new javax.swing.JButton();
      jScrollPane1 = new javax.swing.JScrollPane();
      txtResponses = new javax.swing.JTextArea();
      btnDisconnect = new javax.swing.JButton();
      btnClear = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jLabel1.setText("Server URI:");
      jLabel1.setName("jLabel1"); // NOI18N

      txtUri.setText("ws://");
      txtUri.setName("txtUri"); // NOI18N

      btnConnect.setText("Connect");
      btnConnect.setName("btnConnect"); // NOI18N
      btnConnect.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnConnectActionPerformed(evt);
         }
      });

      txtMessage.setText("message");
      txtMessage.setName("txtMessage"); // NOI18N

      btnSend.setText("Send");
      btnSend.setName("btnSend"); // NOI18N
      btnSend.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSendActionPerformed(evt);
         }
      });

      jScrollPane1.setName("jScrollPane1"); // NOI18N

      txtResponses.setColumns(20);
      txtResponses.setRows(5);
      txtResponses.setName("txtResponses"); // NOI18N
      jScrollPane1.setViewportView(txtResponses);

      btnDisconnect.setText("Disconnect");
      btnDisconnect.setName("btnDisconnect"); // NOI18N
      btnDisconnect.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDisconnectActionPerformed(evt);
         }
      });

      btnClear.setText("Clear");
      btnClear.setName("btnClear"); // NOI18N
      btnClear.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnClearActionPerformed(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()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jScrollPane1)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(txtMessage)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jLabel1)
                  .addGap(18, 18, 18)
                  .addComponent(txtUri, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(btnDisconnect, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
      );

      layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnClear, btnConnect, btnDisconnect, btnSend});

      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(txtUri, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel1)
               .addComponent(btnConnect)
               .addComponent(btnDisconnect))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnSend)
               .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(btnClear))
            .addContainerGap())
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
		try {
			this.client = new Client(new URI(txtUri.getText()), txtResponses);
			this.client.connect();
			txtResponses.append("** Connecting ... **\n");
		} catch (URISyntaxException ex) {
			txtResponses.append("** Connection error: " + ex.getMessage() + "**\n");
		}

   }//GEN-LAST:event_btnConnectActionPerformed

   private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
		if (client != null && client.getConnection().isOpen()) {
			client.send(txtMessage.getText());
		} else {
			txtResponses.append("** Connection is not established **\n");
		}
   }//GEN-LAST:event_btnSendActionPerformed

   private void btnDisconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisconnectActionPerformed
		if (client != null && client.getConnection().isOpen()) {
			client.close();
			txtResponses.append("** Disconnecting **\n");
		} else {
			txtResponses.append("** Connection is not established **\n");
		}
   }//GEN-LAST:event_btnDisconnectActionPerformed

   private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
      txtResponses.setText("");
   }//GEN-LAST:event_btnClearActionPerformed

	/**
	 * @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(WebSocketTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(WebSocketTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(WebSocketTesterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(WebSocketTesterFrame.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 WebSocketTesterFrame().setVisible(true);
			}
		});
	}

   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnClear;
   private javax.swing.JButton btnConnect;
   private javax.swing.JButton btnDisconnect;
   private javax.swing.JButton btnSend;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTextField txtMessage;
   private javax.swing.JTextArea txtResponses;
   private javax.swing.JTextField txtUri;
   // End of variables declaration//GEN-END:variables
}