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,java.lang.Class) 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 HashSet<Stringkeys = new HashSet<String>();
 	private final HashMap<StringHashSet<Class<? extends ICommand>>> keyToCommands;
 	private final HashMap<StringHashSet<Method>> keyToMethods;
 	
 	public FrontController(){
 		 = new HashMap<StringHashSet<Class<? extends ICommand>>>();
 	}
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 argKeyClass<? extends ICommandargCommand){
 		if(argCommand == null){
 			throw new NullPointerException(I18n.getText("frontController.commandNull"));
 		}
 		if(argKey == null){
 			throw new NullPointerException(I18n.getText("frontController.keyNull"));
 		}
 		try {
 			argCommand.newInstance();
 		} catch (Exception e) {
 			throw new RuntimeException(I18n.getText("frontController.makingCommand"), e);
 		}
 		
 			.get(argKey).add(argCommand);
 		}else{
 			HashSet<Class<? extends ICommand>> commands = new HashSet<Class<? extends ICommand>>();
 			commands.add(argCommand);
 			.put(argKeycommands);
 			if(!.contains(argKey)){
 				MVC.addEventListener(argKeylistener);
 				.add(argKey);
 			}
 		}
 	}

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 argCommandMethod){
		if(argKey == null){
			throw new NullPointerException(I18n.getText("frontController.keyNull"));
		}
		if(argCommandMethod == null){
			throw new NullPointerException(I18n.getText("frontController.commandNull"));
		}
			set = .get(argKey);
		}else{
			set = new HashSet<Method>();
			.put(argKeyset);
		}
		try{
			set.add(getClass().getMethodargCommandMethod));
		}catch(Exception e){
			throw new RuntimeException(I18n.getText("frontController.findingMethod",argCommandMethod), e);
		}
		if(!.contains(argKey)){
			MVC.addEventListener(argKeylistener);
			.add(argKey);
		}
	}
	private static class FrontControllerEventListener implements IEventListener{
			 = argController;
		}
		public void eventReceivedMVCEvent argEvent) {
			HashSet<Class<? extends ICommand>> commands = ..get(argEvent.key);
			if(commands != null){
				for(Class<? extends ICommandcommandClass : commands){
					ICommand command;
					try {
						command = commandClass.newInstance();
catch (Exception e){
						// shouldn't happen
						continue;
					}
					command.execute(argEvent);
				}
			}
			HashSet<Methodmethods = ..get(argEvent.key);
			if(methods != null){
				for(Method m : methods){
					try {
							m.invoke(argEvent);
catch ( IllegalArgumentException e) {
						..println(I18n.getText("frontController.invokingMethod"m.toString()));
catch ( IllegalAccessException e) {
						..println(I18n.getText("frontController.invokingMethod"m.toString()));
catch ( InvocationTargetException e) {
						..println(I18n.getText("frontController.invokingMethod"m.toString()));
					}
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X