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: ListModelAdaptor.java,v 1.6 2002/09/12 10:51:03 ludovicc Exp $ 37 */ 38 package org.scopemvc.model.collection; 39 40 41 import java.util.ArrayList; 42 import java.util.Collections; 43 import java.util.Comparator; 44 import java.util.List; 45 import org.scopemvc.core.ModelChangeEvent; 46 import org.scopemvc.core.ModelChangeListener; 47 import org.scopemvc.core.ModelChangeTypes; 48 import org.scopemvc.core.Selector; 49 import org.scopemvc.model.basic.BasicModel; 50 51 /*** 52 * <P> 53 * 54 * Used to present a list of {@link ListModelSource}s as an active BasicModel. 55 * Useful when initialising {@link org.scopemvc.view.swing.SComboBox} or {@link 56 * org.scopemvc.view.swing.SList} with a static list of data. </P> <P> 57 * 58 * This will propagate ModelChangeEvents from sublists that are 59 * ModelChangeEventSources. Note that this adaptor registers as a listener with 60 * such sublists: it may be necessary to manually unregister with {@link 61 * #removeModelChangeListeners} if an adaptor is no longer needed. </P> <P> 62 * 63 * The adaptor can present its ListModelSources as a sorted list if a Comparator 64 * is passed to {@link #setComparator} or all list elements implement Comparable 65 * and {@link #setSorted} is called. </P> 66 * 67 * @author Roytman, Alex 68 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 69 * @created 05 September 2002 70 * @version $Revision: 1.6 $ $Date: 2002/09/12 10:51:03 $ 71 */ 72 public class ListModelAdaptor extends BasicModel 73 implements ModelChangeListener { 74 75 /*** 76 * TODO: describe of the Field 77 */ 78 public static final Selector LIST_SELECTOR = Selector.fromString("list"); 79 /*** 80 * TODO: describe of the Field 81 */ 82 public static final Selector LIST_SIZE_SELECTOR = Selector.fromString("size"); 83 84 private List list; 85 private ListModelSource listSources[]; 86 87 private boolean sorted; 88 private Comparator comparator; 89 90 91 /*** 92 * Constructor for the ListModelAdaptor object 93 * 94 * @param listSources TODO: Describe the Parameter 95 */ 96 public ListModelAdaptor(ListModelSource listSources[]) { 97 this.listSources = listSources; 98 for (int i = 0; i < listSources.length; i++) { 99 if (listSources[i].isModelBased()) { 100 listSources[i].getListSourceModel().addModelChangeListener(this); 101 } 102 } 103 } 104 105 106 /*** 107 * Constructor for the ListModelAdaptor object 108 * 109 * @param listSource TODO: Describe the Parameter 110 */ 111 public ListModelAdaptor(ListModelSource listSource) { 112 this(new ListModelSource[]{listSource}); 113 } 114 115 116 /*** 117 * Gets the list 118 * 119 * @return The list value 120 */ 121 public List getList() { 122 if (list == null) { 123 loadList(); 124 } 125 return list; 126 } 127 128 129 /*** 130 * Gets the size 131 * 132 * @return The size value 133 */ 134 public int getSize() { 135 if (list == null) { 136 loadList(); 137 } 138 return list.size(); 139 } 140 141 142 /*** 143 * Gets the element at 144 * 145 * @param inIndex TODO: Describe the Parameter 146 * @return The elementAt value 147 */ 148 public Object getElementAt(int inIndex) { 149 if (list == null) { 150 loadList(); 151 } 152 if (list == null) { 153 return null; 154 } else { 155 return list.get(inIndex); 156 } 157 } 158 159 160 /*** 161 * Gets the sorted 162 * 163 * @return The sorted value 164 */ 165 public boolean isSorted() { 166 return sorted; 167 } 168 169 170 /*** 171 * Sets the sorted 172 * 173 * @param inSorted The new sorted value 174 */ 175 public void setSorted(boolean inSorted) { 176 sorted = true; 177 } 178 179 180 /*** 181 * Sets the comparator 182 * 183 * @param inComparator The new comparator value 184 */ 185 public void setComparator(Comparator inComparator) { 186 if (inComparator == null) { 187 setSorted(false); 188 comparator = null; 189 list = null; 190 } else { 191 setSorted(true); 192 comparator = inComparator; 193 list = null; 194 } 195 fireModelChange(ModelChangeTypes.VALUE_CHANGED, LIST_SELECTOR); 196 } 197 198 199 /*** 200 * TODO: document the method 201 */ 202 public void removeModelChangeListeners() { 203 if (listSources != null) { 204 for (int i = 0; i < listSources.length; i++) { 205 if (listSources[i].isModelBased()) { 206 listSources[i].getListSourceModel().removeModelChangeListener(this); 207 } 208 } 209 } 210 } 211 212 213 /*** 214 * TODO: document the method 215 * 216 * @param inEvent TODO: Describe the Parameter 217 */ 218 public void modelChanged(ModelChangeEvent inEvent) { 219 if (list != null && isListChangeEvent(inEvent)) { 220 loadList(); 221 fireModelChange(ModelChangeTypes.VALUE_CHANGED, LIST_SELECTOR); 222 } 223 } 224 225 226 /*** 227 * TODO: document the method 228 */ 229 protected void loadList() { 230 if (listSources.length == 1 && listSources[0].isList() && !isSorted()) { 231 list = (List) listSources[0].getListSource(); 232 return; 233 } 234 235 if (list == null) { 236 list = new ArrayList(); 237 } else { 238 list.clear(); 239 } 240 241 for (int i = 0; i < listSources.length; i++) { 242 listSources[i].addToList(list); 243 } 244 245 if (isSorted() && comparator != null) { 246 Collections.sort(list, comparator); 247 } else if (isSorted()) { 248 Collections.sort(list); 249 } 250 } 251 252 253 private boolean isListChangeEvent(ModelChangeEvent inEvent) { 254 for (int i = 0; i < listSources.length; i++) { 255 if (listSources[i].isModelBased() && 256 inEvent.getModel().equals(listSources[i].getListSourceModel()) && 257 (inEvent.getSelector() == null || inEvent.getSelector().equals(listSources[i].getListSourceSelector()))) { 258 return true; 259 } 260 } 261 return false; 262 } 263 }

This page was automatically generated by Maven