Java beans. Beans are Java classes written in according to a certain design pattern. By doing so, a visual programming environment such as NetBeans Developer can perform a so called introspection of the class and find out what attributes can be manipulated and which events the class can send. These design patterns stipulate that you must provide the following:
- Get and set methods for each attribute you wish to allow the user to manipulate. (See getIsSender, setIsSender)
- Add and removeListener methods. A listener is an object which can receive events. (See addComponentListener, removeComponentListener).
package ControlBeans;If not just set it to whatever you wish to name your package. Just don't forget to import the Control beans package... like this:
import ControlBeans.*;
private int myID;Used to store the ID provided when the communicator registers with your component. More on that later.
private AbstractCommunicator myCommunicator;Used to keep a reference to the communicator when it registers with your component. More on that later.
private ComponentListener myListener;Used to store a reference to the object to which you will send a registerComponentEvent.
Used to store whether or not your component should send values to the communicator or not.
Used to store whether or not your component should receive values from the communicator or not.
private int tag; or private int[] tags;Used to store this components tag(s).
- Set default values. If nothing else, it is good programming style to make sure you know what all values are initiated to. Also, you might save the users of your component some irritation if its values are not something they have to change every time they create a new instance of the component.
- Initiate event handling. Your component should handle the events triggered by someone using it. Most commonly this is done by implementing ActionListener.
public boolean getIsReceiver();
public void setIsReceiver(boolean value);
public boolean getIsSender();
public void setIsSender(boolean value);These methods are used for getting and setting the isSender and isReceiver attributes. The methods are needed in order for your component to pass as a Java bean.
public void addComponentListener(ComponentListener l);
public void removeComponentListener(ComponentListener l);These methods must be implemented so that your component can keep track of which object(s) should receive events from it. Normally, you don't need to keep track of more than one listener. At present, none of the components within the Control Beans package can keep track of multiple listeners.
addComponentListener should when invoked send a registerComponentEvent to the listener. This event will eventually end up within the communicator which will then invoke registerCommunicator. (See myListener).
public void registerCommunicator(AbstractCommunicator c, int ID);
When the communicator receives a registerComponentEvent it will invoke this method at the source of the event (which is your component in this case (just in case you hadn't figured)). c will then be a reference to the communicator itself and ID will be a unique ID number for your component. Both of these parameters should be stored. (See myCommunicator, myID).
public int[] getTags();
This method must be implemented to return an array containing all tags that your components subscribe to, even if your component only has one tag. The communicator invokes this method upon registering.
public void putValue(int value, int aTag, double time, int ID);
public void putValue(double value, int aTag, double time, int ID);
public void putValue(String value, int aTag, double time, int ID);When the communicator wishes to pass a value to your component it will invoke one of these methods. This is what these methods should do:
Methods for value types that are not supposed to be received by your component can be left with empty bodies. You may wish to provide conversion between the different types. For instance: even if your component is only supposed to receive integer values you should also implement the method which receives double values and cast value from double to integer before processing it.
- First check to see if isReceiver is set to true. If not, the method should exit since this indicates that the user does not wish for your component to receive values from the communicator, at least not for the moment.
- Check to see that aTag corresponds to tag. This is not necessary but can be used for increased error checking.
- Check to see that ID does not match myID. If it does, the value originates from your component and should be ignored.
- If all of the above conditions have been met, your component should then process value. Most commonly, a component will call its inherited setValue method and pass value as a parameter (this provided that your component does have this method of course).
public void requestValues();
When invoked, your component should send its value(s) to the communicator. This is used to provide the application your GUI is supposed to be controlling with initial parameter values. Don't forget to check whether or not isSender is set to true. If your component is not of such a kind that it contains a certain value (ordinary buttons for instance don't), this method can be left with an empty body. Please note if you are constructing your own communicator that AbstractCommunicator does not automatically invoke this method. See the next chapter for more information.
protected abstract void sendValue(int value, int aTag, double time);These methods are called by the communicator itself when it has received a value from one of the components that have registered with it (i.e. when someone has invoked putValue on the communicator). sendValue is where you are supposed to supply the means for your communicator to pass this value on to the application that your GUI is supposed to be controlling. How you do this is completely up to you. After you have implemented the three sendValue methods (and the methods and possible inner classes needed to support them) there is only one more thing to do...
protected abstract void sendValue(double value, int aTag, double time);
protected abstract void sendValue(String value, int aTag, double time);
putValue(value, tag, time, myID);where value is the value received from the application, tag is the tag that came with the value and time is the time stamp. myID is the communicators own ID number and must be sent along to prevent the value from "bouncing back" to any of the sendValue methods.
putValue(value, tag, CURRENT_TIME, myID);By doing so, the communicator will insert the time stamp itself.
And that - as they say - is it.