View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: ModelChangeEventSupport.java,v 1.10 2002/09/05 15:41:46 ludovicc Exp $ 37 */ 38 package org.scopemvc.model.basic; 39 40 41 import java.util.ArrayList; 42 import java.util.Iterator; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.scopemvc.core.ModelChangeEvent; 46 import org.scopemvc.core.ModelChangeEventSource; 47 import org.scopemvc.core.ModelChangeListener; 48 import org.scopemvc.core.Selector; 49 import org.scopemvc.util.Debug; 50 51 /*** 52 * <P> 53 * 54 * Delegate to help ModelChangeEvent listener registration and firing. Used by 55 * {@link BasicModel}. Events are fired synchronously in this implementation. 56 * </P> 57 * 58 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 59 * @created 05 August 2002 60 * @version $Revision: 1.10 $ $Date: 2002/09/05 15:41:46 $ 61 * @see BasicModel 62 */ 63 public class ModelChangeEventSupport { 64 65 private final static Log LOG = LogFactory.getLog(ModelChangeEventSupport.class); 66 67 private ModelChangeEventSource source; 68 private ArrayList listeners; 69 private int activationCount; 70 71 72 /*** 73 * Constructor for the ModelChangeEventSupport object 74 * 75 * @param inSource The source of ModelChangeEvents 76 */ 77 public ModelChangeEventSupport(ModelChangeEventSource inSource) { 78 if (inSource == null) { 79 throw new IllegalArgumentException("Make an MCESupport for a specified model, not null"); 80 } 81 source = inSource; 82 } 83 84 85 /*** 86 * Returns true if the model is deactivated 87 * 88 * @return true if the model is deactivated 89 */ 90 public boolean isModelDeactivated() { 91 if (LOG.isDebugEnabled()) { 92 LOG.debug("isModelDeactivated: " + source); 93 } 94 95 return (activationCount > 0); 96 } 97 98 99 /*** 100 * Disable ModelChangeEvent broadcasting for a Model. This can 'stack' 101 * multiple inactivation requests, allowing nested inactivations. 102 * 103 * @see BasicModel#makeActive 104 */ 105 public final void activateModel() { 106 if (LOG.isDebugEnabled()) { 107 LOG.debug("activateModel: " + source); 108 } 109 110 --activationCount; 111 112 if (activationCount < 0) { 113 LOG.warn("unmatched activation? activationCount < 0 for: " + source); 114 } 115 } 116 117 118 /*** 119 * Enable ModelChangeEvent broadcasting for a Model that was disabled. This 120 * can 'stack' multiple activation requests, allowing nested activations. 121 * 122 * @see BasicModel#makeActive 123 */ 124 public final void deactivateModel() { 125 if (LOG.isDebugEnabled()) { 126 LOG.debug("deactivateModel: " + source); 127 } 128 129 ++activationCount; 130 } 131 132 133 /*** 134 * Register a ModelChangeListener to receive ModelChangeEvent broadcasts 135 * from the passed Model. 136 * 137 * @param inListener the ModelChangeEvent listener to register with the 138 * Model to receive its ModelChangeEvent broadcasts. 139 * @see BasicModel#addModelChangeListener 140 */ 141 public synchronized void addModelChangeListener(ModelChangeListener inListener) { 142 if (LOG.isDebugEnabled()) { 143 LOG.debug("addModelChangeListener: " + source + " listener: " + inListener); 144 } 145 146 if (inListener == null) { 147 throw new IllegalArgumentException("Can't add null listener to: " + source); 148 } 149 150 if (listeners == null) { 151 listeners = new ArrayList(); 152 } 153 154 listeners.add(inListener); 155 } 156 157 158 /*** 159 * Remove a ModelChangeListener from the set of registered listeners to the 160 * passed Model. The passed listener must be registered as a listener to the 161 * Model. 162 * 163 * @param inListener the ModelChangeListener to deregister from the passed 164 * Model. 165 * @see BasicModel#removeModelChangeListener 166 */ 167 public synchronized void removeModelChangeListener(ModelChangeListener inListener) { 168 if (LOG.isDebugEnabled()) { 169 LOG.debug("removeModelChangeListener: " + source + " listener: " + inListener); 170 } 171 172 if (inListener == null) { 173 throw new IllegalArgumentException("Can't remove null listener for: " + source); 174 } 175 176 if (listeners != null) { 177 listeners.remove(inListener); 178 } 179 } 180 181 182 // ------------ ModelChangeEvent firing -------------------- 183 184 /*** 185 * Fire a ModelChangeEvent to all listeners 186 * 187 * @param inSelector The Selector for the property of the model affected by 188 * the change 189 * @param inType TODO: Describe the Parameter 190 */ 191 public synchronized void fireModelChange(int inType, Selector inSelector) { 192 if (LOG.isDebugEnabled()) { 193 LOG.debug("fireModelChange: " + inType + ", " + inSelector + ", " + source); 194 } 195 196 if (isModelDeactivated()) { 197 return; 198 } 199 200 if (listeners == null || listeners.size() < 1) { 201 return; 202 } 203 204 ModelChangeEvent event = new BasicModelChangeEvent(); 205 event.setType(inType); 206 event.setModel(source); 207 event.setSelector(inSelector); 208 209 for (Iterator i = listeners.iterator(); i.hasNext(); ) { 210 Object o = i.next(); 211 if (Debug.ON) { 212 Debug.assertTrue(o instanceof ModelChangeListener); 213 } 214 ModelChangeListener listener = (ModelChangeListener) o; 215 listener.modelChanged(event); 216 } 217 } 218 }

This page was automatically generated by Maven