package mil.navy.nps.logger; import mil.navy.nps.dis.*; import mil.navy.nps.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * An editor for PDU headers. Every PDU has the same few fields to * start with, so it doesn't make sense to duplicate that code in * every PDU type editor. This panel should be at the top of the * editor, and the PDU-specific editor panel beneath it.

* * @author DMcG */ public class HeaderEditor extends PduEditPanel { protected ProtocolDataUnit editedPdu; // PDU we're editing // Editing field Painfully obvious restatement of the obvious // ------------- --------------------------------------------- JTextField protocolVersion; // Protocol version field JTextField exerciseID; // exercise ID field JTextField pduType; // Type of pdu JTextField protocolFamily; // protocol family JTextField timestamp; // timestamp JTextField pduLength; // PDU length (in bytes) JTextField timeReceived; // when we got packet (NOT AN OFFICIAL DIS FIELD) /** * Constructor; lay out GUI fields. */ public HeaderEditor() { super(); JPanel left, right, draw; JScrollPane scrollPane; this.add(new JLabel("PDU Header")); left = new JPanel(); left.setLayout(new GridLayout(7,1)); right = new JPanel(); right.setLayout(new GridLayout(7,1)); protocolVersion = new JTextField(); right.add(protocolVersion); exerciseID = new JTextField(); right.add(exerciseID); pduType = new JTextField(); right.add(pduType); protocolFamily = new JTextField(); right.add(protocolFamily); timestamp = new JTextField(); right.add(timestamp); pduLength = new JTextField(); right.add(pduLength); timeReceived = new JTextField(); right.add(timeReceived); left.add(new JLabel("Protocol Version")); left.add(new JLabel("Exercise ID")); left.add(new JLabel("PDU Type")); left.add(new JLabel("Protocol Family")); left.add(new JLabel("Timestamp")); left.add(new JLabel("PDU Length")); left.add(new JLabel("Time Received")); draw = new JPanel(); draw.setLayout(new GridLayout(1,2)); draw.add(left); draw.add(right); scrollPane = new JScrollPane(draw); this.add(scrollPane); } /** * When we start editing, we need to pass in a PDU object * for the panel to edit. That's what this does. */ public void setPdu(ProtocolDataUnit pPdu) { editedPdu = pPdu; // Fill out the text fields &c with the values in the PDU. Horrific lines of // nested code that basically mean "put a string representation of the number // into the text field" protocolVersion.setText(new Integer(editedPdu.getProtocolVersion().intValue()).toString()); exerciseID.setText(new Integer(editedPdu.getExerciseID().intValue()).toString()); pduType.setText(new Integer( editedPdu.getPduType().intValue() ).toString()); protocolFamily.setText(new Integer(editedPdu.getProtocolFamily().intValue()).toString()); timestamp.setText(new Long(editedPdu.getTimestamp().longValue()).toString()); pduLength.setText(new Integer(editedPdu.length()).toString()); } /** * when the user says we're finished editing, the values in * the fields need to be updated to the pdu object. Then we * return the PDU object. */ public ProtocolDataUnit updatePdu() { UnsignedByte ver; UnsignedByte id; UnsignedByte type; UnsignedByte family; UnsignedInt stamp; // Read the values typed in to the field and update the PDU. try { ver = new UnsignedByte(Integer.parseInt(protocolVersion.getText())); id = new UnsignedByte(Integer.parseInt(exerciseID.getText())); type = new UnsignedByte(Integer.parseInt(pduType.getText())); family = new UnsignedByte(Integer.parseInt(protocolFamily.getText())); stamp = new UnsignedInt(Integer.parseInt(timestamp.getText())); editedPdu.setProtocolVersion(ver); editedPdu.setExerciseID(id); editedPdu.setPduType(type); editedPdu.setProtocolFamily(family); editedPdu.setTimestamp(stamp); } catch(NumberFormatException nfe) { System.out.println(nfe); } return editedPdu; } }