/** A slightly more ambitious bean. This implements the UI for a fairly complex piece of code, a panel that accepts a variety of inputs and settings for writing to a socket. If writing to a multicast socket, the UI should collect the multicast destination address, the TTL, and the port. If writing to a unicast destination address, the UI should collect the destination unicast IP address, and the destination port, the port to use on this end when creating the sending socket. This bean does NOT, at this writing, support serialization. */ package mil.navy.nps.awt; import java.awt.*; import java.awt.event.*; import java.util.*; public class SocketWriteUI extends Panel implements ItemListener { // Default values public static final String DEFAULT_MCAST = "224.2.181.145"; public static final String DEFAULT_PORT = "62040"; public static final String DEFUALT_SITE_APP_ID = "localhost"; public static final String DEFAULT_UNICAST_DEST_PORT = "8006"; public static final String DEFAULT_UNICAST_DEST_ADDR = "localhost"; public static final boolean DEBUG = false; // debugging output // In the SocketStyleRegion panel private static CheckboxGroup socketTypeRadio; // Radio for how to send info--unicast or multicast private static Checkbox multicastCheckbox; // checkbox selection for multicast private static Checkbox unicastCheckbox; // Checkbox selection for unicast // In socketTextRegion private static TextField mcastAddress; // Text field that holds the multicast address private static TextField port; // Text field that holds the port/socket number private static TextField unicastDestAddress; // Holds destination unicast address private static TextField unicastDestPort; // port it goes to on the remote machine private static Choice ttl; // time to live pop-up selector // These are dummy fields used to facilitate getting beaned. A bean // must have ivars and getX and setX methods for those ivars. private static boolean beanIsMulticast; // multicast or unicast private static String beanMcastAddress; // multicast address private static int beanPort; // port to use ON THIS SIDE private static String beanUnicastDestAddress; // unicast desitnation address private static int beanUnicastDestPort; // unicast dest port ON FAR SIDE private static int beanTTL; // time to live for multicast public void debug(String pDebugString) { if(DEBUG) System.out.println(pDebugString); } public SocketWriteUI() { // panels to break up the layout of the app into logical areas Panel socketStyleRegion = new Panel(); // checkboxes Panel socketTextFieldsRegion = new Panel(); // text input fields // Layouts for the panels above. GridLayout socketRegionLayout = new GridLayout(1,2); // layout we use for us GridLayout socketRadioLayout = new GridLayout(2,2); GridLayout socketTextFieldsLayout = new GridLayout(5,2); // Add a radio button, unicast or multicast socketTypeRadio = new CheckboxGroup(); socketStyleRegion.setLayout(socketRadioLayout); multicastCheckbox = new Checkbox("Multicast", socketTypeRadio, true); socketStyleRegion.add(multicastCheckbox); unicastCheckbox = new Checkbox("Unicast", socketTypeRadio, false); socketStyleRegion.add(unicastCheckbox); // Call us when the user hits the radio button so we can enable & disable fields unicastCheckbox.addItemListener(this); multicastCheckbox.addItemListener(this); // Add a few text fields for socket address and port, one on top of the other socketTextFieldsRegion.setLayout(socketTextFieldsLayout); mcastAddress = new TextField(DEFAULT_MCAST, 15); socketTextFieldsRegion.add(new Label("Multicast Address:")); socketTextFieldsRegion.add(mcastAddress); // Time to live for mcast socketTextFieldsRegion.add(new Label("Multicast TTL:")); ttl = new Choice(); ttl.add("Site 15"); ttl.add("Region 63"); ttl.add("World 127"); socketTextFieldsRegion.add(ttl); // socket send port port = new TextField(DEFAULT_PORT, 5); socketTextFieldsRegion.add(new Label("Port:")); socketTextFieldsRegion.add(port); socketTextFieldsRegion.add(new Label("Unicast Destination Port:")); unicastDestPort = new TextField(DEFAULT_UNICAST_DEST_PORT, 5); socketTextFieldsRegion.add(unicastDestPort); unicastDestPort.setEnabled(false); unicastDestAddress = new TextField(DEFAULT_UNICAST_DEST_ADDR, 20); socketTextFieldsRegion.add(new Label("Unicast Destination Address:")); socketTextFieldsRegion.add(unicastDestAddress); unicastDestAddress.setEnabled(false); // add the two regions, checkbox and fields, left and right //this.setLayout(socketRegionLayout); this.add(socketStyleRegion, BorderLayout.EAST); this.add(socketTextFieldsRegion, BorderLayout.WEST); return; } public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(450,125); } /** itemStateChanged is the implementation of the interface ItemListener, which is used by checkboxes. The checkboxes add us as a listener, and we get their events. This is based on the Java 1.1 event model. */ public void itemStateChanged(ItemEvent e) // interface for ItemListener { Object source = e.getSource(); if(source == unicastCheckbox) { this.setMulticastFieldsOn(false); } if(source == multicastCheckbox) { this.setMulticastFieldsOn(true); } } // end itemStateChanged /** We have to set the status of the fields in a couple places to reflect either multicast or unicast. This does that. */ public void setMulticastFieldsOn(boolean pStatus) { beanIsMulticast = pStatus; if(pStatus == false) // turn on unicast-specific fields { unicastDestAddress.setEnabled(true); unicastDestPort.setEnabled(true); mcastAddress.setEnabled(false); ttl.setEnabled(false); } else // turnon multicast-specific fields { unicastDestAddress.setEnabled(false); unicastDestPort.setEnabled(false); mcastAddress.setEnabled(true); ttl.setEnabled(true); } } // end setMulticastFieldsOn // Bean get and set methods public void setBeanIsMulticast(boolean pIsMulticast) { beanIsMulticast = pIsMulticast; // Change status of text fields accordingly if(pIsMulticast) this.setMulticastFieldsOn(true); else this.setMulticastFieldsOn(false); } // end of setBeanIsMulticast public boolean getBeanIsMulticast() { Checkbox selectedCheckbox; selectedCheckbox = socketTypeRadio.getSelectedCheckbox(); if(selectedCheckbox == multicastCheckbox) { beanIsMulticast = true; return true; } beanIsMulticast = false; return false; } public void setBeanMcastAddress(String pMcastAddress) { beanMcastAddress = pMcastAddress; mcastAddress.setText(pMcastAddress); return; } public String getBeanMcastAddress() { beanMcastAddress = mcastAddress.getText(); return beanMcastAddress; } public void setBeanPort(int pBeanPort) { beanPort = pBeanPort; port.setText(Integer.toString(beanPort)); return; } public int getBeanPort() { beanPort = Integer.parseInt(port.getText()); return beanPort; } public void setBeanUnicastDestAddress(String pBeanUnicastDestAddress) { beanUnicastDestAddress = pBeanUnicastDestAddress; unicastDestAddress.setText(beanUnicastDestAddress); } public String getBeanUnicastDestAddress() { beanUnicastDestAddress = unicastDestAddress.getText(); return beanUnicastDestAddress; } public void setBeanUnicastDestPort(int pBeanUnicastDestPort) { beanUnicastDestPort = pBeanUnicastDestPort; unicastDestPort.setText(Integer.toString(beanUnicastDestPort)); } public int getBeanUnicastDestPort() { beanUnicastDestPort = Integer.parseInt(unicastDestPort.getText()); return beanUnicastDestPort; } public void setBeanTTL(int pBeanTTL) { beanTTL = pBeanTTL; // try a little bit of error correction. Can't have a TTL bigger than // 127 or smaller than 0. // The magic values: site = 15, region = 63, world = 127 if(beanTTL > 127) beanTTL = 127; if(beanTTL < 0) beanTTL = 0; if(beanTTL <=15) ttl.select("Site"); else if (beanTTL <=63) ttl.select("Region"); else ttl.select("World"); } public int getBeanTTL() { String selectedTTL = ttl.getSelectedItem(); // using compareToIgnoreCase() would be more robust here, but that's // a JDK 1.2 method, and besides the capitializtion of the Site and // Region strings is set in programmatic code. if(selectedTTL.compareTo("Site") == 0) beanTTL = 15; else if(selectedTTL.compareTo("Region") == 0) beanTTL = 63; else beanTTL = 127; return beanTTL; } /** The main method is just used for debugging purposes--you can launch the object from the command line and have it display without the use of a browser. */ public static void main(String args[]) { SocketWriteUI socketInfoObject = new SocketWriteUI(); Frame appFrame = new Frame("Woo hoo"); appFrame.add(socketInfoObject); appFrame.setSize(500, 200); appFrame.setVisible(true); return; } // end of main } // end of class