|
| 1 | +//! A Singleton IController implementation. |
| 2 | +//! |
| 3 | +//! In RuEx, the Controller class follows the |
| 4 | +//! 'Command and Controller' strategy, and assumes these |
| 5 | +//! responsibilities: |
| 6 | +//! |
| 7 | +//! * Remembering which ICommands |
| 8 | +//! are intended to handle which INotifications. |
| 9 | +//! * Registering itself as an IObserver with |
| 10 | +//! the View for each INotification |
| 11 | +//! that it has an ICommand mapping for. |
| 12 | +//! * Creating a new instance of the proper ICommand |
| 13 | +//! to handle a given INotification when notified by the View. |
| 14 | +//! * Calling the ICommand's execute |
| 15 | +//! method, passing in the INotification. |
| 16 | +//! |
| 17 | +//! Your application must register ICommands with the |
| 18 | +//! Controller. |
| 19 | +//! |
| 20 | +//! The simplest way is to subclass Facade, |
| 21 | +//! and use its initializeController method to add your |
| 22 | +//! registrations. |
| 23 | +//! |
| 24 | +
|
| 25 | +#![allow(unused_variables)] |
| 26 | +#![allow(dead_code)] |
| 27 | + |
| 28 | +// use crate::core::*; |
| 29 | +use crate::interfaces::*; |
| 30 | +// use crate::patterns::observer::*; |
| 31 | + |
| 32 | +// // Singleton instance |
| 33 | +// protected static var instance : IController; |
| 34 | + |
| 35 | +// // Message Constants |
| 36 | +// protected const SINGLETON_MSG : String = "Controller Singleton already constructed!"; |
| 37 | + |
| 38 | +pub struct Controller<B, C, V> { |
| 39 | + /// Local reference to View |
| 40 | + view: Box<dyn IView<B, C, V>>, |
| 41 | + |
| 42 | + /// Mapping of Notification names to Command Class references |
| 43 | + command_map: Vec<String>, |
| 44 | +} |
| 45 | + |
| 46 | +impl<B, C, V> Controller<B, C, V> { |
| 47 | + /// Constructor. |
| 48 | + /// |
| 49 | + /// |
| 50 | + /// This IController implementation is a Singleton, |
| 51 | + /// so you should not call the constructor |
| 52 | + /// directly, but instead call the static Singleton |
| 53 | + /// Factory method Controller.getInstance() |
| 54 | + /// |
| 55 | + /// @throws Error Error if Singleton instance has already been constructed |
| 56 | + pub fn new() { |
| 57 | + // if (instance != null) throw Error(SINGLETON_MSG); |
| 58 | + // instance = this; |
| 59 | + // commandMap = new Array(); |
| 60 | + // initializeController(); |
| 61 | + } |
| 62 | + |
| 63 | + /// Initialize the Singleton Controller instance. |
| 64 | + /// |
| 65 | + /// Called automatically by the constructor. |
| 66 | + /// |
| 67 | + /// Note that if you are using a subclass of View |
| 68 | + /// in your application, you should also subclass Controller |
| 69 | + /// and override the initializeController method in the |
| 70 | + /// following way: |
| 71 | + /// |
| 72 | + /// //TODO: Example here |
| 73 | + /// |
| 74 | + fn initialize_controller() { |
| 75 | + // view = View.getInstance(); |
| 76 | + } |
| 77 | + |
| 78 | + /// Controller Singleton Factory method. |
| 79 | + /// |
| 80 | + /// Returns the Singleton instance of Controller |
| 81 | + /// pub static fn |
| 82 | + pub fn get_instance() -> Box<dyn IController<B>> { |
| 83 | + // if ( instance == null ) instance = new Controller( ); |
| 84 | + // return instance; |
| 85 | + unimplemented!() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<B, C, V> IController<B> for Controller<B, C, V> { |
| 90 | + /// If an ICommand has previously been registered |
| 91 | + /// to handle a the given INotification, then it is executed. |
| 92 | + /// |
| 93 | + /// * `note` - an INotification |
| 94 | + fn execute_command(&self, note: Box<dyn INotification<B>>) { |
| 95 | + // var commandFactory : Box<dyn FnOnce()> = commandMap[ note.getName() ]; |
| 96 | + // if ( commandFactory == null ) return; |
| 97 | + |
| 98 | + // var commandInstance : ICommand = new commandFactory(); |
| 99 | + // commandInstance.execute( note ); |
| 100 | + } |
| 101 | + |
| 102 | + /// Register a particular ICommand class as the handler |
| 103 | + /// for a particular INotification. |
| 104 | + /// |
| 105 | + /// If an ICommand has already been registered to |
| 106 | + /// handle INotifications with this name, it is no longer |
| 107 | + /// used, the new ICommand is used instead. |
| 108 | + /// |
| 109 | + /// The Observer for the new ICommand is only created if this the |
| 110 | + /// first time an ICommand has been regisered for this Notification name. |
| 111 | + /// |
| 112 | + /// * `notification_name` - the name of the INotification |
| 113 | + /// * `command_factory` - the Class of the ICommand |
| 114 | + fn register_command(&self, notification_name: String, command_factory: Box<dyn FnOnce()>) { |
| 115 | + // if ( commandMap[ notificationName ] == null ) { |
| 116 | + // view.registerObserver( notificationName, new Observer( executeCommand, this ) ); |
| 117 | + // } |
| 118 | + // commandMap[ notificationName ] = commandFactory; |
| 119 | + } |
| 120 | + |
| 121 | + /// Check if a Command is registered for a given Notification |
| 122 | + /// |
| 123 | + /// * `notification_name` - |
| 124 | + /// Returns whether a Command is currently registered for the given notificationName. |
| 125 | + fn has_command(&self, notification_name: String) -> bool { |
| 126 | + // return commandMap[ notificationName ] != null; |
| 127 | + unimplemented!() |
| 128 | + } |
| 129 | + |
| 130 | + /// Remove a previously registered ICommand to INotification mapping. |
| 131 | + /// |
| 132 | + /// * `notification_name` - the name of the INotification to remove the ICommand mapping for |
| 133 | + fn remove_command(&self, notification_name: String) { |
| 134 | + // // if the Command is registered... |
| 135 | + // if ( hasCommand( notificationName ) ) |
| 136 | + // { |
| 137 | + // // remove the observer |
| 138 | + // view.removeObserver( notificationName, this ); |
| 139 | + |
| 140 | + // // remove the command |
| 141 | + // commandMap[ notificationName ] = null; |
| 142 | + // } |
| 143 | + } |
| 144 | +} |
0 commit comments