wonderly.jeaprs
Class ClientImpl

java.lang.Object
  |
  +--wonderly.jeaprs.ClientImpl
All Implemented Interfaces:
Client
Direct Known Subclasses:
APRSDigi, APRSMessagePanel, APRSNWSHandler, APRSObjectItemManager, APRSPacketLogger, APRSPosit, HamHud, THD7AControl

public abstract class ClientImpl
extends java.lang.Object
implements Client

This class is the base class that most JeAPRS clients should subclass. This class provides default behavior and implementations of needed methods that will make JeAPRS have a more uniform behavior.

Version:
1.0
Author:
Gregg Wonderly - The information contained in this document is Copyright 2001-2003, Gregg Wonderly, all rights reserved.

Field Summary
protected  java.util.Hashtable actions
          The actions defined for this client
protected  wonderly.jeaprs.aprs.data.APRS APRS
           
protected  boolean dirty
           
protected  wonderly.jeaprs.aprs.packet.APRSEventListener evLis
          The event listener object that we have created
protected  java.util.Vector ins
          The event generator that we are currently getting events from
protected  wonderly.jeaprs.ClientImpl me
          A reference to ourselves for inner classes to use.
protected  java.lang.String name
          The name of this client
protected  java.util.Vector outs
          The outout stream that we write APRS messages to
protected  boolean stopped
          Is this client stopped?
protected  java.io.OutputStream strmLis
           
 
Constructor Summary
ClientImpl()
           
 
Method Summary
 void addAPRSEventGenerator(wonderly.jeaprs.aprs.packet.APRSEventGenerator gen)
          Sets the input event generator that this client is to use This default implementation removes the listener object from any previously registered generator, attaches the listener object to the passed generator (if non-null).
 void addAPRSOutputStream(wonderly.jeaprs.TNC out)
          Used to set the currently used output stream
abstract  void buildActions()
          Put code in here to register all of the actions that you want to use.
protected  javax.swing.JMenuBar buildMenu()
          Override this default implementation (which returns null to indicate no menu bar is to be used) to create your clients menu bar.
abstract  javax.swing.JPanel buildPanel(javax.swing.JFrame parent)
          Builds the applications user interface.
abstract  wonderly.jeaprs.aprs.packet.APRSEventListener createEventListener()
          This method needs to be implemented by subclasses and should return an APRSEventListener implementation that is applicable for their client.
abstract  java.io.OutputStream createStreamListener()
          This method needs to be implemented by subclasses and should return an OutputStream implementation that is applicable for their client.
protected  javax.swing.Action getAction(java.lang.String name)
          This method is a convienence method that returns actions that have been stored in the actions Hashtable.
 wonderly.jeaprs.aprs.packet.APRSEventGenerator[] getAPRSEventGenerators()
          Used to get the current input event stream generator
 wonderly.jeaprs.TNC[] getAPRSOutputStreams()
          Used to get the output stream this client is using
 java.lang.String getName()
          Returns the set name of this client @see setName(String)
 wonderly.jeaprs.aprs.APRSUIFactory getUIFactory()
          This default method returns a reference to an inner class that implements the APRSUIFactory interface.
 boolean isDirty()
           
 boolean isInputUser()
          If this client needs an event generator, this should return true.
 boolean isOutputUser()
          If this client needs an output stream, this should return true.
 void removeAPRSEventGenerator(wonderly.jeaprs.aprs.packet.APRSEventGenerator gen)
          Remove the client as a listener to the passed APRSEventGenerator.
 void removeAPRSOutputStream(wonderly.jeaprs.TNC out)
          Used to set the currently used output stream
 void setDirty(boolean how)
          Set whether this client needs to have its properties saved
 void setName(java.lang.String name)
          Sets the name of this client
 void start()
          Starts this client 'running' or whatever it needs when the user opens the client via the menus.
 void stop()
          Stops the client when the user closes the window or deactivates it via the menus
 java.lang.String toString()
          Returns name of Client for display in lists
 void write(wonderly.jeaprs.ByteString from, wonderly.jeaprs.ByteString path, byte[] arr)
          Write the indicated packet to all TNCs configured for output for this client.
 void write(wonderly.jeaprs.ByteString from, wonderly.jeaprs.ByteString path, wonderly.jeaprs.ByteString str)
          Write the indicated packet to all TNCs configured for output for this client.
 void write(wonderly.jeaprs.ByteString from, wonderly.jeaprs.ByteString to, wonderly.jeaprs.ByteString path, byte[] arr)
          Write the indicated packet to all TNCs configured for output for this client.
 void write(wonderly.jeaprs.ByteString from, wonderly.jeaprs.ByteString to, wonderly.jeaprs.ByteString path, wonderly.jeaprs.ByteString str)
          Write the indicated packet to all TNCs configured for output for this client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

outs

protected transient java.util.Vector outs
The outout stream that we write APRS messages to


ins

protected transient java.util.Vector ins
The event generator that we are currently getting events from


evLis

protected transient wonderly.jeaprs.aprs.packet.APRSEventListener evLis
The event listener object that we have created


strmLis

protected transient java.io.OutputStream strmLis

me

protected transient wonderly.jeaprs.ClientImpl me
A reference to ourselves for inner classes to use.


actions

protected transient java.util.Hashtable actions
The actions defined for this client


stopped

protected boolean stopped
Is this client stopped?


name

protected java.lang.String name
The name of this client


APRS

protected transient wonderly.jeaprs.aprs.data.APRS APRS

dirty

protected boolean dirty
Constructor Detail

ClientImpl

public ClientImpl()
Method Detail

write

public void write(wonderly.jeaprs.ByteString from,
                  wonderly.jeaprs.ByteString path,
                  wonderly.jeaprs.ByteString str)
Write the indicated packet to all TNCs configured for output for this client.

Parameters:
from - the from call to use in the packet
path - the path to assign to the packet.
str - the packet payload.

write

public void write(wonderly.jeaprs.ByteString from,
                  wonderly.jeaprs.ByteString to,
                  wonderly.jeaprs.ByteString path,
                  wonderly.jeaprs.ByteString str)
Write the indicated packet to all TNCs configured for output for this client.

Parameters:
from - the from call to use in the packet
to - the to call to use in the packets header
path - the path to assign to the packet.
str - the packet payload.

write

public void write(wonderly.jeaprs.ByteString from,
                  wonderly.jeaprs.ByteString path,
                  byte[] arr)
Write the indicated packet to all TNCs configured for output for this client.

Parameters:
from - the from call to use in the packet
path - the path to assign to the packet.
arr - the packet payload.

write

public void write(wonderly.jeaprs.ByteString from,
                  wonderly.jeaprs.ByteString to,
                  wonderly.jeaprs.ByteString path,
                  byte[] arr)
Write the indicated packet to all TNCs configured for output for this client.

Parameters:
from - the from call to use in the packet
to - the to call to use in the packets header
path - the path to assign to the packet.
arr - the packet payload.

setName

public void setName(java.lang.String name)
Sets the name of this client

Specified by:
setName in interface Client

getName

public java.lang.String getName()
Returns the set name of this client @see setName(String)

Specified by:
getName in interface Client

start

public void start()
Starts this client 'running' or whatever it needs when the user opens the client via the menus.

Specified by:
start in interface Client

stop

public void stop()
Stops the client when the user closes the window or deactivates it via the menus

Specified by:
stop in interface Client

toString

public java.lang.String toString()
Description copied from interface: Client
Returns name of Client for display in lists

Specified by:
toString in interface Client
Overrides:
toString in class java.lang.Object
Returns:
the name of the client

isOutputUser

public boolean isOutputUser()
If this client needs an output stream, this should return true. This default implementation returns true

Specified by:
isOutputUser in interface Client

isInputUser

public boolean isInputUser()
If this client needs an event generator, this should return true. This default implementation returns true

Specified by:
isInputUser in interface Client

addAPRSOutputStream

public void addAPRSOutputStream(wonderly.jeaprs.TNC out)
Used to set the currently used output stream

Specified by:
addAPRSOutputStream in interface Client

removeAPRSOutputStream

public void removeAPRSOutputStream(wonderly.jeaprs.TNC out)
Used to set the currently used output stream

Specified by:
removeAPRSOutputStream in interface Client

getAPRSOutputStreams

public wonderly.jeaprs.TNC[] getAPRSOutputStreams()
Used to get the output stream this client is using

Specified by:
getAPRSOutputStreams in interface Client

addAPRSEventGenerator

public void addAPRSEventGenerator(wonderly.jeaprs.aprs.packet.APRSEventGenerator gen)
Sets the input event generator that this client is to use This default implementation removes the listener object from any previously registered generator, attaches the listener object to the passed generator (if non-null). The listener object is created by the implementation of the createEventListener method. Subclasses can either implement APRSEventListener, or they can create an object that does and return the appropriate reference in their implementation of createEventListener.

Specified by:
addAPRSEventGenerator in interface Client
See Also:
createEventListener()

removeAPRSEventGenerator

public void removeAPRSEventGenerator(wonderly.jeaprs.aprs.packet.APRSEventGenerator gen)
Remove the client as a listener to the passed APRSEventGenerator. This will stop the passed generator's packets from being handled by this client.

Specified by:
removeAPRSEventGenerator in interface Client
Parameters:
gen - the generator to stop listening too.

createEventListener

public abstract wonderly.jeaprs.aprs.packet.APRSEventListener createEventListener()
This method needs to be implemented by subclasses and should return an APRSEventListener implementation that is applicable for their client. Many times, a simple inner class is all that is needed as in.
  return new APRSEventAdapter() {
		public void message( APRSPacket pkt, APRSMessage msg ) {
			// Do something with message
		}
	};
	
This will create a new object that is an instance of the APRSEventAdapter class. That class implements the APRSEventListener interface with empty methods so that you can just override the methods that you want APRS packets for, and not have to deal with implementing all methods. Just return null here if you don't need an event listener.

See Also:
APRSEventAdapter, APRSEventListener

createStreamListener

public abstract java.io.OutputStream createStreamListener()
This method needs to be implemented by subclasses and should return an OutputStream implementation that is applicable for their client. This stream will be written to as data comes through a TNC implementatation, receiving copies of the text lines received by the TNC implementation. A simple mechanism is typically to use a thread and a PipedInputStream and PipedOutputStream pair to receive the data and process it. Just return null if you don't need raw stream events.
	public OutputStream createStreamListener() throws IOException {
		final PipedOutputStream pipe = new PipedOutputStream();
		final PipedInputStream is = new PipedInputStream(pipe);
		new Thread("TNC Stream listener") {
			public void run() {
				try {
					BufferedReader rd = new BufferedReader( new InputStreamReader(is) );
					String str;
					while( (str = rd.readLine()) != null ) {
						System.out.println( "Got TNC line: "+str );
					}
					System.out.println("End of TNC Stream, exiting");
				} finally {
					try {
						is.close();
						pipe.close();
					} catch( Exception ex ) {
						JeAPRS.getUI().reportException(ex);
					}
				}
			}
		}.start();
		return pipe;
	}
	


getAPRSEventGenerators

public wonderly.jeaprs.aprs.packet.APRSEventGenerator[] getAPRSEventGenerators()
Description copied from interface: Client
Used to get the current input event stream generator

Specified by:
getAPRSEventGenerators in interface Client
Returns:
the current APRSEventGenerator associated with this object

getUIFactory

public wonderly.jeaprs.aprs.APRSUIFactory getUIFactory()
This default method returns a reference to an inner class that implements the APRSUIFactory interface. It relies on subclasses implementing the buildPanel(JFrame) that returns their application UI. It also uses the buildMenu() methods return value to attach a JMenu to a JFrame or JInternalFrame if such is needed

Specified by:
getUIFactory in interface Client
See Also:
buildPanel(JFrame), buildMenu()

isDirty

public boolean isDirty()
Specified by:
isDirty in interface Client
Returns:
true if properties of client have changed and need to be saved

setDirty

public void setDirty(boolean how)
Description copied from interface: Client
Set whether this client needs to have its properties saved

Specified by:
setDirty in interface Client

buildPanel

public abstract javax.swing.JPanel buildPanel(javax.swing.JFrame parent)
Builds the applications user interface. The returned panel should contain all of the controls needed to provide the user with convienent access to the features of this client/module, except for a menu bar. The buildMenu() method can be used to create a menu bar if neededed.

See Also:
buildMenu()

buildMenu

protected javax.swing.JMenuBar buildMenu()
Override this default implementation (which returns null to indicate no menu bar is to be used) to create your clients menu bar.


getAction

protected javax.swing.Action getAction(java.lang.String name)
This method is a convienence method that returns actions that have been stored in the actions Hashtable. If an action is not defined, then an empty, disabled action is created and returned. This allows the UI elements to be easily populated, and those that are not implemented yet will be disabled so the user won't feel like they don't work.

See Also:
buildActions()

buildActions

public abstract void buildActions()
Put code in here to register all of the actions that you want to use.
                actions.put(  "MyAction", act = new AbstractAction( "MyAction" ) {
                        public void actionPerformed( ActionEvent ev ) {
                                //...do something here
                        }
                });
                act.putValue( Action.NAME, "MyAction" );
 

See Also:
getAction(String)