Newer
Older
nonet / src / ch / epfl / lca / sc250 / gui / CnFrameUDP.java
@Andreas Jaggi Andreas Jaggi on 4 May 2006 3 KB Changed directory structure.
package ch.epfl.lca.sc250.gui;

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


/**
 * This Class is used to display UDP Broadcast Messages sent from the Server to the Client. <br>
 * The Client should create a CnFrameUDP Instance and call the method "appendBroadcastMessage"
 * each time an UDP Broadcast is received.<br>
 * <pre>Example:
 * 
 * CnFrameUDP cnFrameUDP = new CnFrameUDP("Networking Course");
 * while(true) {
	try {
	 Thread.sleep(1000);
	} catch (InterruptedException e) {
	 e.printStackTrace();
	}
	 cnFrameUDP.appendBroadcastMessage("192.168.1.5", "****X***X****X");
 }
 * </pre>
 * @author Christophe Trefois
 *
 */
public class CnFrameUDP extends CnFrame {
	/**
	 * Generated serialID by Eclipse
	 */
	private static final long serialVersionUID = 2774784580042117224L;
	
	/**
	 * Vector containing the table rows data
	 */
	private Vector tableRows;
	
	/**
	 * Vector containing the column names. Filled with addColumns.
	 */
	private Vector columns;
	
	/**
	 * Array containing the column names
	 */
	private static final String[] columnNames = {"Received At", "User ID", "IP Address", "Letters"};

	/**
	 * tableModel used by the Table
	 */
	private DefaultTableModel tableModel;

	/**
	 * The table used to display the broadcast messages
	 */
	private JTable broadcastTable;
	
	/**
	 * Constructs a CnFrameUDP Instance with title windowTitle
	 * @param windowTitle The Title if the window
	 */
	public CnFrameUDP(String windowTitle) {
		super(windowTitle);
		
		// Create new JPanel
		JPanel centerPanel = new JPanel();
		
		// Fill the columns Vector
		addColumns(columnNames);

		tableModel = new DefaultTableModel();
		tableModel.setDataVector(tableRows, columns);

		// create new JPanel
		broadcastTable = new JTable(tableModel);
		broadcastTable.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
		broadcastTable.getColumnModel().getColumn(1).setCellRenderer(new CellRenderer());
		broadcastTable.getColumnModel().getColumn(2).setCellRenderer(new CellRenderer());
		broadcastTable.getColumnModel().getColumn(3).setCellRenderer(new CellRenderer());
		
		// Add ScrollBar to the broadcastTable
		JScrollPane scrollPane = new JScrollPane(broadcastTable);
		centerPanel.add(scrollPane);
		
		// put the centerPanel to the Center 
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		// Pack
		pack();
		
		// Make this frame visible
		setVisible(true);
	}
	
	/**
	 * Fill the column name vector
	 * @param columnNames the array of Strings to fill the vector with
	 */
	private void addColumns(String[] columnNames) {
		// Init tableRows and columns Vectors
		tableRows = new Vector();
		columns = new Vector();
		
		for (int i = 0; i < columnNames.length; i++)
			columns.addElement((String) columnNames[i]);
	}
	
	/**
	 * Adds a row to the JTable
	 * @param broadcastIP The IP from the broadcast message
	 * @param letters The Letters
	 */
	private void addRow(String broadcastIP, String userID, String letters) {
		Vector tempVector = new Vector();
		tempVector.addElement(super.getCurrentTime());
		tempVector.addElement(userID);
		tempVector.addElement(broadcastIP);
		tempVector.addElement(letters);
		tableRows.addElement(tempVector);
		broadcastTable.addNotify();
	}
	
	/**
	 * Modifies the GUI by appending the broadcast Message
	 * @param broadcastIP the IP extracted from the broadcast UDP Message
	 * @param broadcastLetters the letters from the UDP Broadcast
	 */
	public void appendBroadcastMessage(String broadcastIP, String userID, String broadcastLetters) {
		addRow(broadcastIP, userID, broadcastLetters);
	}
	
	/**
	 * Clears the Table
	 *
	 */
	public void clearTable() {
		tableRows.clear();
		broadcastTable.addNotify();
	}
}