Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Daniel Murphy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
Created at 3:21:37 AM, Mar 17, 2010
 
 package com.dmurph.mvc.control;
 
 
Abstract controller. Use registerCommand(java.lang.String,com.dmurph.mvc.control.ICommand) and registerCommand(java.lang.String,java.lang.String) to listen for events from the view objects. The registerCommand(java.lang.String,java.lang.String) method is easier to use, as it will call a method for you when an event with the given key is dispatched.

Author(s):
Daniel Murphy
 
 public abstract class FrontController{
 	
 	// for getting the method
 	private final Class<?> paramTypes[] = { MVCEvent.class };
 	private final HashMap<StringHashSet<ICommand>> keyToCommands;
 	
 	public FrontController(){
 	}
Registers the listener to the given key. com.dmurph.mvc.MVCEvent's are dispatched globally, so careful with the actual values of your keys and make sure they are unique.

Parameters:
argKey
argCommand
 
 	protected synchronized void registerCommand(String argKeyICommand argCommand){
 		if(argCommand == null){
 			throw new NullPointerException(I18n.getText("frontController.commandNull"));
 		}
 		if(argKey == null){
 			throw new NullPointerException(I18n.getText("frontController.keyNull"));
 		}
 		
 			.get(argKey).add(argCommand);
 		}else{
 			HashSet<ICommandcommands = new HashSet<ICommand>();
 			commands.add(argCommand);
 			MVC.addEventListener(argKeylistener);
 			.put(argKeycommands);
 		}
 	}

Registers the given method with the event key. The method must have only one argument of type com.dmurph.mvc.MVCEvent. It will be called with the event that was dispatched with this key. More than one method can be assigned to a key, and they will be called in the order of registration. Duplicate registrations will be ignored.

Parameters:
argKey
argCommandMethod
Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException
 
 	protected synchronized void registerCommand(String argKeyString argCommandMethodthrows SecurityExceptionNoSuchMethodException{
 		if(argKey == null){
 			throw new NullPointerException(I18n.getText("frontController.keyNull"));
 		}
 		if(argCommandMethod == null){
 			throw new NullPointerException(I18n.getText("frontController.commandNull"));
 		}
 		
 		Method m = getClass().getMethodargCommandMethod);
 		
 		MethodCommand mCommand = new MethodCommand(thism);
 		registerCommand(argKeymCommand);
	}
	private static class FrontControllerEventListener implements IEventListener{
		private final FrontController controller;
			 = argController;
		}
		public void eventReceivedMVCEvent argEvent) {
			HashSet<ICommandcommands = ..get(argEvent.key);
			for(ICommand command : commands){
				command.execute(argEvent);
			}
		}
	}
	private static class MethodCommand implements ICommand {
		private final FrontController controller;
		private final Method method;
		public MethodCommand(FrontController argControllerMethod argMethod){
			 = argController;
			 = argMethod;
		}

		public void execute(MVCEvent argEvent) {
			try {
				.invokeargEvent);
catch ( IllegalArgumentException e) {
				..println(I18n.getText("frontController.invokingMethod".toString()));
catch ( IllegalAccessException e) {
				..println(I18n.getText("frontController.invokingMethod".toString()));
catch ( InvocationTargetException e) {
				..println(I18n.getText("frontController.invokingMethod".toString()));
			}
		}

		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + (( == null) ? 0 : .hashCode());
			return result;
		}

		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			MethodCommand other = (MethodCommandobj;
			if ( == null) {
				if (other.controller != null)
					return false;
			}
			else if (!.equals(other.controller))
				return false;
			if ( == null) {
				if (other.method != null)
					return false;
			}
			else if (!.equals(other.method))
				return false;
			return true;
		}
	}
New to GrepCode? Check out our FAQ X