package mil.navy.nps.logger; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import mil.navy.nps.dis.*; /** * Displays the properties of the file we opened, if any. */ public class PropertiesDialog extends JDialog { public static final int CANCEL = 0; public static final int OK = 1; protected int state = CANCEL; // what choice the user made protected JPanel attributeHolder; protected JPanel valueHolder; protected JPanel buttonPanel; // Hold JTextFields for attributes and values protected Vector attributeList = new Vector(); protected Vector valueList = new Vector(); /** * constructor */ public PropertiesDialog(JFrame pParentFrame, BehaviorStreamBufferInfo info) { super(pParentFrame, true); StringTokenizer pairTokenizer; // Gets attribute-value pairs from the overall group StringTokenizer valueTokenizer; // within one attribute-value pair, splits on "=" String values; int valueCount = 0; JButton ok, cancel; this.setSize(200, 200); // Get string-ified attribute-value data from info object, which was // saved at the start of the file. values = info.toString(); pairTokenizer = new StringTokenizer(values); valueCount = pairTokenizer.countTokens(); // Attributes on the left, values on the right, buttons below, and jokers straight ahead // (apologies to steve miller) attributeHolder = new JPanel(); valueHolder = new JPanel(); buttonPanel = new JPanel(); // OK button ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { WindowEvent we; PropertiesDialog.this.state = OK; we = new WindowEvent(PropertiesDialog.this, WindowEvent.WINDOW_CLOSING); PropertiesDialog.this.processWindowEvent(we); } }); // Cancel button cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { WindowEvent we; PropertiesDialog.this.state = CANCEL; we = new WindowEvent(PropertiesDialog.this, WindowEvent.WINDOW_CLOSING); PropertiesDialog.this.processWindowEvent(we); } }); // Add buttons to button panel buttonPanel.add(ok); buttonPanel.add(cancel); // Layout for attributes and values. we want as many text fields // as we have attributes. attributeHolder.setLayout(new GridLayout(valueCount + 1, 1)); valueHolder.setLayout(new GridLayout(valueCount + 1, 1)); // Parse out one attribute-value pair, and add the contents to our panel // We add one extra field so we can add new properties; this is sleazy, // and there needs to be a better way to do it. for(int idx = 0; idx < valueCount + 1; idx++) { JTextField attr, value; String pair; String attrString, valueString; attr = new JTextField(); value = new JTextField(); attr.setPreferredSize(new Dimension(40, 20)); value.setPreferredSize(new Dimension(40, 20)); attributeList.add(attr); valueList.add(value); attributeHolder.add(attr); valueHolder.add(value); // We want the last one to be empty, the other ones filled out if(idx < valueCount) { // The attribute-value pairs are split up by an "=" character. pair = pairTokenizer.nextToken(); valueTokenizer = new StringTokenizer(pair, "="); attr.setText(valueTokenizer.nextToken()); value.setText(valueTokenizer.nextToken()); } } // Smash everything together this.getContentPane().setLayout(new GridLayout(2, 1)); JPanel draw = new JPanel(); draw.setLayout(new GridLayout(1,2)); draw.add(attributeHolder); draw.add(valueHolder); this.getContentPane().add(draw); this.getContentPane().add(buttonPanel); } /** * Returns OK or CANCEL, the button the user hit to end * the dialog */ public int getResult() { return state; } /** * Converts the attribute-values to a string of format * "at=val at=val" */ public String toString() { String result = ""; for(int idx=0; idx < attributeList.size(); idx++) { JTextField at, val; String left, right, pair; at = (JTextField)attributeList.elementAt(idx); val = (JTextField)valueList.elementAt(idx); left = at.getText(); right = val.getText(); pair = left + "=" + right; result = result + " " + pair; } return result; } };