View Javadoc

1   package com.insanityengine.ghia.events;
2   
3   import java.awt.event.*;
4   import javax.swing.event.*;
5   
6   /***
7    *
8    *  ActionEventGenerator, is a simplistic event generator pal for you.
9    *  ActionListeners clients will need to implement:
10   *
11   *    	public void actionPerformed( ActionEvent e );
12   * 
13   *  From java.awt.event.ActionListener interface.  To use this class
14   *  subclass it (and blow your one inheritance... :P) or cut-and-paste
15   *  this:
16   *
17   *
18   *		ActionEventGenerator actionAl = new ActionEventGenerator( this );
19   *		
20   *		public void addActionListener( ActionListener l ) {
21   *			actionAl.addActionListener( l );
22   *		}
23   *		
24   *		public void removeActionListener( ActionListener l ) {
25   *			actionAl.removeActionListener( l );
26   *		}
27   *	
28   *		public void fire( String s ) { actionAl.fire( s ); }
29   *
30   *
31   *  to generate an event do something like this:
32   *
33   *		actionAl.fire( "some message" );
34   *
35   *	any listeners will get an ActionEvent with your class as the 
36   *	return for ActionEvent.getSource() and your message from
37   *  ActionEvent.getActionCommand(), so make shore yo sh!t is tite!
38   *
39   *	Special thanks to:  JFileChooser.java, which I ripped off long ago...
40   *
41   */
42  public class ActionEventGenerator {
43  
44  	/***
45  	 * public ActionEventGenerator() {
46  	 * @param x
47  	 * @return x
48  	 */
49  	public ActionEventGenerator() {
50  		setOwner( this );
51  	}
52  
53  	/***
54  	 * public ActionEventGenerator( Object owner ) {
55  	 * @param x
56  	 * @return x
57  	 */
58  	public ActionEventGenerator( Object owner ) {
59  		setOwner( owner );
60  	}
61  
62      /***
63       * public void addActionListener( ActionListener l ) {
64       * @param x
65       * @return x
66       */
67      public void addActionListener( ActionListener l ) {
68  		listenerList.add( ActionListener.class, l );
69      }
70  
71      /***
72       * public void removeActionListener( ActionListener l ) {
73       * @param x
74       * @return x
75       */
76      public void removeActionListener( ActionListener l ) {
77          listenerList.remove( ActionListener.class, l );
78      }
79  
80      /***
81       * protected void fireActionPerformed( String command ) {
82       * @param x
83       * @return x
84       */
85      public void fire( String command ) {
86  		fireActionPerformed( command, owner );
87  	}
88  
89      /***
90       * protected void fireActionPerformed( String command ) {
91       * @param x
92       * @return x
93       */
94      public void fireActionPerformed( String command ) {
95  		fireActionPerformed( command, owner );
96  	}
97  
98  	/***
99  	 * Object getOwner() { return owner; }
100 	 * @param x
101 	 * @return x
102 	 */
103 	Object getOwner() { return owner; }
104 
105 	/***
106 	 * void setOwner( Object newValue ) { owner = newValue; }
107 	 * @param x
108 	 * @return x
109 	 */
110 	void setOwner( Object newValue ) { owner = newValue; }
111 
112 	/***
113 	 * void disown() {
114 	 * @param x
115 	 * @return x
116 	 */
117 	void disown() {
118 		setOwner( this );
119 	}
120 
121 	///////////////////////////////////////////////////////////////////
122 
123 	private EventListenerList listenerList = new EventListenerList();
124 	private Object owner = this;
125 	
126     /***
127      * private void fireActionPerformed( String command, Object src ) {
128      * @param x
129      * @return x
130      */
131     private void fireActionPerformed( String command, Object src ) {
132 
133         // Guaranteed to return a non-null array
134         Object[] listeners = listenerList.getListenerList();
135         ActionEvent e = null;
136 
137         // Process the listeners last to first, notifying
138         // those that are interested in this event
139         for ( int i = listeners.length - 2 ; i >= 0; i -= 2 ) {
140             if ( listeners[ i ] == ActionListener.class ) {
141                 // Lazily create the event:
142                 if ( null == e ) {
143                     e = new ActionEvent( 
144 						src, 
145 						ActionEvent.ACTION_PERFORMED, command 
146 					);
147                 }
148                 ( ( ActionListener ) listeners[ i + 1 ] ).actionPerformed( e );
149             }
150         }
151     }
152 
153 };
154 
155 /***
156  * 
157  * $Log: ActionEventGenerator.java,v $
158  * Revision 1.3  2005/03/19 17:50:02  brian
159  * repackaging
160  *
161  * Revision 1.2  2004/09/23 00:46:38  brian
162  * fix package name
163  *
164  * Revision 1.1  2004/09/22 23:36:18  brian
165  * to generate action events...
166  *
167  * 
168  */