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 2:19:39 AM, Mar 12, 2010
 
 package com.dmurph.mvc;
 
This stores all the listener information, dispatches events to the corresponding listeners. To dispatch events use MVCEvent.dispatch(). Also, look at splitOff().

Author(s):
Daniel Murphy
 
 public class MVC extends Thread{
 	
 	private static final ThreadGroup mvcThreadGroup = new ThreadGroup("MVC Thread Group");
 	private static final ArrayList<MVCmvcThreads = new ArrayList<MVC>();
 	private volatile static MVC mainThread = new MVC();
 	private volatile static IGlobalEventMonitor monitor = new WarningMonitor();
 	
 	private final Queue<MVCEventeventQueue = new LinkedList<MVCEvent>();
 	private volatile boolean running = false;
 	
 	private volatile static int threadCounter = 0;
 	
 	private MVC() {
 		super("MVC Thread #"+(++));
 		.add(this);
 	}

Adds a listener for the given event key. If the listener is already listening to that key, then nothing is done.

Parameters:
argKey
argListener
 
 	public synchronized static void addEventListenerString argKeyIEventListener argListener) {
 
 		if (..containsKey(argKey)) {
 			// return if we're already listening
 			ifisEventListenerargKeyargListener)){
 				return;
 			}
 			..get(argKey).addFirst(argListener);
 		}
 		else {
 			final LinkedList<IEventListenerstack = new LinkedList<IEventListener>();
 			stack.addFirst(argListener);
 			..put(argKeystack);
 		}
 	}

Checks to see if the listener is listening to the given key.

Parameters:
argKey
argListener
Returns:
 
 	public synchronized static boolean isEventListenerString argKeyIEventListener argListener) {
 			return false;
 		}
 		
 		return stack.containsargListener);
 	}

gets the listeners for the given event key.

Parameters:
argKey
Returns:
	public synchronized static LinkedList<IEventListenergetListenersString argKey) {
			return ..get(argKey);
		}
		else {
			..put(argKeystack);
			return stack;
		}
	}

removes a listener from the given key.

Parameters:
argKey
argListener
Returns:
true if the listener was removed, and false if it wasn't there to begin with
	public static synchronized boolean removeEventListenerString argKeyIEventListener argListener) {
			return stack.remove(argListener);
		}
		return false;
	}

Adds an event to the dispatch queue for the MVC thread. Used by MVCEvent.dispatch().

Parameters:
argEvent
	protected synchronized static void dispatchEventMVCEvent argEvent) {
		if (..containsKey(argEvent.key)) {
				}
			}
		}else{
			if( != null){
			}
		}
	}

Split off the current MVC thread, all queued events and future event dispatches are handled by a new MVC thread, while this one runs to completion. If the thread calling this is not the current core MVC thread, then an exception is thrown

Throws:
IllegalThreadException if the thread calling this is not an MVC thread
IncorrectThreadException if the MVC thread calling this is not the main thread, e.g. it has already split off.
	public synchronized static void splitOff() throws IllegalThreadExceptionIncorrectThreadException{
		if( Thread.currentThread() instanceof MVC){
			MVC thread = (MVC) Thread.currentThread();
			if(thread == ){
				MVC old = ;
				 = new MVC();
				for(MVCEvent event : old.eventQueue){
				}
				old.eventQueue.clear();
				for(String key : old.listeners.keySet()){
					..put(keyold.listeners.get(key));
				}
				old.listeners.clear();
				old.running = false;
			}else{
			}
		}else{
		}
	}

Sets the global event monitor, which is called before and after each event is dispatched.

Parameters:
argMonitor
See also:
IGlobalEventMonitor
	public synchronized static void setGlobalEventMonitor(IGlobalEventMonitor argMonitor){
		 = argMonitor;
	}

Gets the global event monitor. Default is WarningMonitor.

Returns:
See also:
IGlobalEventMonitor
	public synchronized static IGlobalEventMonitor getGlobalEventMonitor(){
		return ;
	}

Stops the dispatch thread, dispatching any remaining events before cleanly returning. Thread automatically gets started when new events are dispatched
	public synchronized static void stopDispatchThread(){
	}

Manually starts the dispatch thread.
	public synchronized static void startDispatchThread(){
			return;
		}
	}
	public void stopGracefully(){
		 = false;
	}
	public void run(){
		 = true;
		while(){
				try {
					Thread.sleep(100);
catch ( InterruptedException e) {}
			}else {
				MVCEvent event = .poll();
			}
		}
	}
	private synchronized void internalDispatchEvent(MVCEvent argEvent){
		LinkedList<IEventListenerstack = .get(argEvent.key);
		if( != null){
		}
		while(it.hasNext() && argEvent.isPropagating()){
			it.next().eventReceivedargEvent);
		}
		if( != null){
		}
	}
New to GrepCode? Check out our FAQ X