/** This is a (first) attempt at a bean. The idea is to create a reusable component that encapsulates some modestly complex functionality, so it can be reused in different situations. This handles the creation of some entry boxes, buttons, and multiple selection lists that allow for setting of various aspects of multicast and unicast sockets. This is strictly a UI component; no actual sockets are harmed during the filming of this event. Once the user enters the data we can access the data they entered via some accessor methods. This is intended for _reading_ from a socket; we do not have UI to handle what host a unicast datagram should be sent to, what the TTL of a multicast packet should be, etc. This means that when reading we specify a multicast address and a port; when reading from unicast we specify only a port. There is a companion file that goes with this, SocketInfo.mf. This is the "manifest" for the jar file; it designates this java class as a bean. It just looks like this: Manifest-Version: 1.0 Name: SocketInfo.class Java-Bean: True The class and the manifest file are loaded into a jar file as follows: jar -cfm SocketInfo.jar SocketInfo.mf SocketInfo.class The jar file that is created can be loaded into the "beanbox" distributed by javasoft, and manipulated there. this class does NOT implement the serializable interface, but it can still be serialized. All Java objects have a default serialization scheme, and it turns out that this scheme is good enough for us in this case. If it had not been enough, we would have had to implement the Serializable or Externizable interfaces. So objects of this class can be saved after being instantiated in the beanbox, and then later restored with their individual states intact. */ package mil.navy.nps.awt; import java.awt.*; import java.awt.event.*; import java.net.*; public class SocketReadUI extends java.awt.Panel implements java.awt.event.ItemListener { // constants that describe the type of socket, multicast or unicast private static final int MULTICAST = 0; private static final int UNICAST = 1; private static final boolean DEBUGGING = true; // Default values for various fields private static final String DEFAULT_MCAST_ADDRESS = "224.2.181.145"; private static final int DEFAULT_PORT = 62040; private static final int DEFAULT_SOCKET_STYLE = MULTICAST; private static final int TIME_TO_LIVE = 15; // Three panels; one holds checkbox selection for multicast vs. unicast; // one holds text fields for entring things such as network addresses, // destination ports, and the like; and one holds both of the above // boxes. private static Panel socketStyleRegion; // Holds checkbox for multicast/unicast choice private static Panel socketTextRegion; // Holds text data; multicast address, port no. private static Panel socketInfoRegion; // Holds both of above panels // 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 mcastAddressField, // Text field that holds mcast addr portField; // holds port number // These are a couple values kept around that make switching between // multicast and unicast a bit easier.... private static String lastMulticastAddress = DEFAULT_MCAST_ADDRESS; private static String lastUnicastAddress; private static int lastPort = DEFAULT_PORT; // Some hackery to make beans work a bit better private static boolean multicastSocket; private static String address; private static int port; private static boolean isEnabled = true; /** default constructor */ public SocketReadUI() { // Layouts for each of the panels GridLayout socketStyleLayout = new GridLayout(2,2); // 2 X 2 layout GridLayout socketTextLayout = new GridLayout(2,2); GridLayout socketInfoLayout = new GridLayout(2,1); // One row, two columns // Create panels, set them up with layout mangers socketStyleRegion = new Panel(); socketTextRegion = new Panel(); socketInfoRegion = new Panel(); socketStyleRegion.setLayout(socketStyleLayout); socketTextRegion.setLayout(socketTextLayout); socketInfoRegion.setLayout(socketInfoLayout); // Set up stuff for multicast/unicast checkbox socketTypeRadio = new CheckboxGroup(); 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); // set up text info fields relating to sockets--multicast & port address // text field & label to hold multicast address mcastAddressField = new TextField(DEFAULT_MCAST_ADDRESS, 15); socketTextRegion.add(new Label("Internet Address:")); socketTextRegion.add(mcastAddressField); // Text field & label to hold port number portField = new TextField(Integer.toString(62040), 5); socketTextRegion.add(new Label("Port:")); socketTextRegion.add(portField); // Compose the socket info region by adding two above regions... socketInfoRegion.add(socketStyleRegion); socketInfoRegion.add(socketTextRegion); add(socketInfoRegion); /* System.out.println ("SocketReadUI: entering try/catch.."); try { InetAddress localHost; System.out.println ("SocketReadUI: call InetAddress.getLocalHost();"); localHost = InetAddress.getLocalHost(); System.out.println ("SocketReadUI: done InetAddress.getLocalHost();"); lastUnicastAddress = localHost.getHostAddress(); System.out.println ("SocketReadUI: lastUnicastAddress = " + lastUnicastAddress); } catch(UnknownHostException unkHostException) { System.out.println ("SocketReadUI: something is seriously wrong; can't find IP number of local host"); System.out.println (unkHostException); } catch(Exception catchAllException) { System.out.println ("SocketReadUI exception: " + catchAllException); catchAllException.printStackTrace(); } System.out.println ("SocketReadUI: leaving try/catch.."); */ lastUnicastAddress="nolocalhost"; validateTree(); } public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(300,100); } /** * @deprecated provided for backward compatibility with old layout managers. public Dimension preferredSize() { return getPreferredSize(); } */ /** Used for debugging purposes. If the debug flag is on, print out to the console. */ private void debug(String pDebugString) { if(DEBUGGING) System.out.println(pDebugString); } /** Handles checkbox hits; this is the implementation of the java.awt.event.ItemListener interface. When registered listeners are hit, they send the event to this method. */ public void itemStateChanged(ItemEvent ie) { Object source = ie.getSource(); // Radio button hit; if they hit unicast, turn off the multicast address field, // and vice versa. if(source == unicastCheckbox) { mcastAddressField.setEnabled(false); //lastMulticastAddress = mcastAddressField.getText(); //mcastAddressField.setText(lastUnicastAddress); } if(source == multicastCheckbox) { //lastUnicastAddress = mcastAddressField.getText(); mcastAddressField.setEnabled(true); mcastAddressField.setText(lastMulticastAddress); } return; } // accessor methods for beans. /** returns true (yes, it is a multicast socket) or false (it's a unicast socket.) */ public boolean getMulticastSocket() { Checkbox selectedCheckbox; selectedCheckbox = socketTypeRadio.getSelectedCheckbox(); if(selectedCheckbox == multicastCheckbox) return true; return false; } /** Set the radio value. 0 is multicast; anything else sets it to unicast. */ public void setMulticastSocket(boolean pIsMulticast) { if(pIsMulticast == true) { socketTypeRadio.setSelectedCheckbox(multicastCheckbox); mcastAddressField.setEnabled(true); } else { mcastAddressField.setEnabled(false); socketTypeRadio.setSelectedCheckbox(unicastCheckbox); } } public String getAddress() { return mcastAddressField.getText(); } public void setAddress(String pAddress) { mcastAddressField.setText(pAddress); } public int getPort() { return Integer.valueOf(portField.getText()).intValue(); } public void setPort(int pPort) { String portValue = Integer.toString(pPort); } /** Turns off all the UI elements, so no one can entere anything. */ public void setIsEnabled(boolean pIsEnabled) { isEnabled = pIsEnabled; if(pIsEnabled) { if(socketTypeRadio.getSelectedCheckbox() == multicastCheckbox) mcastAddressField.setEnabled(true); else mcastAddressField.setEnabled(false); portField.setEnabled(true); multicastCheckbox.setEnabled(true); unicastCheckbox.setEnabled(true); } else { mcastAddressField.setEnabled(false); portField.setEnabled(false); multicastCheckbox.setEnabled(false); unicastCheckbox.setEnabled(false); } } public boolean getIsEnabled() { return isEnabled; } /** for testing. Just run from the command line. */ public static void main(String args[]) { SocketReadUI SocketReadUIObject = new SocketReadUI(); Frame appFrame = new Frame("Woo hoo"); //Panel aPanel = new Panel(); //BridgeTester bridgeTester = new BridgeTester(); //aPanel.add(socketInfoObject); //add(aPanel); appFrame.add(SocketReadUIObject); appFrame.setSize(400, 200); appFrame.setVisible(true); //add(new TextField("131.120.63.1", 15)); //add(new PanelSub()); //add(bridgeTester); return; } } // end of class