1818
1919/**
2020 * A Multiton <code>IController</code> implementation.
21- *
21+ *
2222 * <P>
2323 * In PureMVC, the <code>Controller</code> class follows the
24- * 'Command and Controller' strategy, and assumes these
24+ * 'Command and Controller' strategy, and assumes these
2525 * responsibilities:
2626 * <UL>
27- * <LI> Remembering which <code>ICommand</code>s
27+ * <LI> Remembering which <code>ICommand</code>
2828 * are intended to handle which <code>INotifications</code>.</LI>
2929 * <LI> Registering itself as an <code>IObserver</code> with
30- * the <code>View</code> for each <code>INotification</code>
30+ * the <code>View</code> for each <code>INotification</code>
3131 * that it has an <code>ICommand</code> mapping for.</LI>
3232 * <LI> Creating a new instance of the proper <code>ICommand</code>
3333 * to handle a given <code>INotification</code> when notified by the <code>View</code>.</LI>
3434 * <LI> Calling the <code>ICommand</code>'s <code>execute</code>
35- * method, passing in the <code>INotification</code>.</LI>
35+ * method, passing in the <code>INotification</code>.</LI>
3636 * </UL>
37- *
37+ *
3838 * <P>
39- * Your application must register <code>ICommands</code> with the
39+ * Your application must register <code>ICommands</code> with the
4040 * Controller.
4141 * <P>
42- * The simplest way is to subclass </code>Facade</code>,
43- * and use its <code>initializeController</code> method to add your
44- * registrations.
45- *
42+ * The simplest way is to subclass </code>Facade</code>,
43+ * and use its <code>initializeController</code> method to add your
44+ * registrations.
45+ *
4646 * @see org.puremvc.java.multicore.core.View View
4747 * @see org.puremvc.java.multicore.patterns.observer.Observer Observer
4848 * @see org.puremvc.java.multicore.patterns.observer.Notification Notification
4949 * @see org.puremvc.java.multicore.patterns.command.SimpleCommand SimpleCommand
5050 * @see org.puremvc.java.multicore.patterns.command.MacroCommand MacroCommand
5151 */
52- public class Controller implements IController
53- {
52+ public class Controller implements IController {
5453
5554 /**
5655 * Mapping of Notification names to Command Class references
5756 */
58- protected Map <String ,ICommand > commandMap ;
57+ protected Map <String , ICommand > commandMap ;
5958
6059 /**
6160 * Local reference to View
6261 */
6362 protected View view ;
64-
63+
6564 /**
6665 * The Multiton Key for this Core
6766 */
6867 protected String multitonKey ;
69-
68+
7069 protected static Map <String , Controller > instanceMap = new HashMap <String , Controller >();
7170
7271 /**
73- * Constructor.
74- *
72+ * Constructor.
73+ *
7574 * <P>
76- * This <code>IController</code> implementation is a Multiton,
77- * so you should not call the constructor
78- * directly, but instead call the static Factory method,
79- * passing the unique key for this instance
75+ * This <code>IController</code> implementation is a Multiton,
76+ * so you should not call the constructor
77+ * directly, but instead call the static Factory method,
78+ * passing the unique key for this instance
8079 * <code>Controller.getInstance( multitonKey )</code>
81- *
80+ *
8281 * @throws Error Error if instance for this Multiton key has already been constructed
83- *
82+ *
8483 */
85- protected Controller (String key )
86- {
84+ protected Controller (String key ) {
8785 multitonKey = key ;
8886 instanceMap .put (multitonKey , this );
89- this .commandMap = new HashMap <String ,ICommand >();
87+ this .commandMap = new HashMap <String , ICommand >();
9088 initializeController ();
9189 }
9290
9391 /**
9492 * Initialize the Multiton <code>Controller</code> instance.
95- *
96- * <P>Called automatically by the constructor.</P>
97- *
93+ *
94+ * <P>Called automatically by the constructor.</P>
95+ *
9896 * <P>Note that if you are using a subclass of <code>View</code>
9997 * in your application, you should <i>also</i> subclass <code>Controller</code>
10098 * and override the <code>initializeController</code> method in the
10199 * following way:</P>
102- *
100+ *
103101 * <listing>
104102 * // ensure that the Controller is talking to my IView implementation
105103 * override public function initializeController( ) : void
106104 * {
107105 * view = MyView.getInstance();
108106 * }
109107 * </listing>
110- *
108+ *
111109 * @return void
112110 */
113- protected void initializeController ( )
114- {
115- this .view = View .getInstance ( multitonKey );
111+ protected void initializeController () {
112+ this .view = View .getInstance (multitonKey );
116113 }
117114
118115 /**
119116 * <code>Controller</code> Multiton Factory method.
120- *
117+ *
121118 * @return the Multiton instance of <code>Controller</code>
122119 */
123- public synchronized static Controller getInstance (String key )
124- {
125- if (instanceMap .get (key ) == null ){
120+ public synchronized static Controller getInstance (String key ) {
121+ if (instanceMap .get (key ) == null ) {
126122 new Controller (key );
127123 }
128124 return instanceMap .get (key );
@@ -131,88 +127,80 @@ public synchronized static Controller getInstance(String key )
131127 /**
132128 * If an <code>ICommand</code> has previously been registered to handle a
133129 * the given <code>INotification</code>, then it is executed.
134- *
130+ *
135131 * @param note
136132 * an <code>INotification</code>
137133 */
138- public void executeCommand ( INotification note )
139- {
134+ public void executeCommand (INotification note ) {
140135 //No reflexion in GWT
141136 //ICommand commandInstance = (ICommand) commandClassRef.newInstance();
142- ICommand commandInstance = (ICommand ) this .commandMap .get ( note .getName () );
137+ ICommand commandInstance = (ICommand ) this .commandMap .get (note .getName ());
143138 if (commandInstance !=null ){
144- commandInstance .initializeNotifier ( multitonKey );
145- commandInstance .execute ( note );
139+ commandInstance .initializeNotifier (multitonKey );
140+ commandInstance .execute (note );
146141 }
147142 }
148143
149144 /**
150145 * Register a particular <code>ICommand</code> class as the handler for a
151146 * particular <code>INotification</code>.
152- *
147+ *
153148 * <P>
154149 * If an <code>ICommand</code> has already been registered to handle
155150 * <code>INotification</code>s with this name, it is no longer used, the
156151 * new <code>ICommand</code> is used instead.
157152 * </P>
158- *
159- * The Observer for the new ICommand is only created if this the
153+ *
154+ * The Observer for the new ICommand is only created if this the
160155 * first time an ICommand has been regisered for this Notification name.
161- *
156+ *
162157 * @param notificationName
163158 * the name of the <code>INotification</code>
164159 * @param command
165160 * an instance of <code>ICommand</code>
166161 */
167- public void registerCommand ( String notificationName , ICommand command )
168- {
169- if (null != this .commandMap .put ( notificationName , command )) return ;
170- this .view .registerObserver ( notificationName , new Observer ( new IFunction ()
171- {
172- public void onNotification ( INotification notification )
173- {
174- executeCommand ( notification );
162+ public void registerCommand (String notificationName , ICommand command ) {
163+ if (null != this .commandMap .put (notificationName , command )) return ;
164+ this .view .registerObserver (notificationName , new Observer (new IFunction () {
165+ public void onNotification (INotification notification ) {
166+ executeCommand (notification );
175167 }
176168 }, this ) );
177169 }
178170
179171 /**
180172 * Remove a previously registered <code>ICommand</code> to
181173 * <code>INotification</code> mapping.
182- *
174+ *
183175 * @param notificationName
184176 * the name of the <code>INotification</code> to remove the
185177 * <code>ICommand</code> mapping for
186178 */
187- public void removeCommand ( String notificationName )
188- {
179+ public void removeCommand (String notificationName ) {
189180 // if the Command is registered...
190- if ( hasCommand ( notificationName ) )
191- {
181+ if (hasCommand (notificationName )) {
192182 // remove the observer
193- view .removeObserver ( notificationName , this );
194- this .commandMap .remove ( notificationName );
183+ view .removeObserver (notificationName , this );
184+ this .commandMap .remove (notificationName );
195185 }
196186 }
197-
187+
198188 /**
199189 * Remove an IController instance
200- *
190+ *
201191 * @param multitonKey of IController instance to remove
202192 */
203- public synchronized static void removeController (String key )
204- {
193+ public synchronized static void removeController (String key ) {
205194 instanceMap .remove (key );
206195 }
207-
196+
208197 /**
209- * Check if a Command is registered for a given Notification
210- *
198+ * Check if a Command is registered for a given Notification
199+ *
211200 * @param notificationName
212201 * @return whether a Command is currently registered for the given <code>notificationName</code>.
213202 */
214- public boolean hasCommand (String notificationName )
215- {
203+ public boolean hasCommand (String notificationName ) {
216204 return commandMap .containsKey (notificationName );
217205 }
218206}
0 commit comments