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: ListModelSource.java,v 1.3 2002/09/05 15:41:46 ludovicc Exp $ 37 */ 38 package org.scopemvc.model.collection; 39 40 41 import java.util.Collection; 42 import java.util.Enumeration; 43 import java.util.Iterator; 44 import java.util.List; 45 import org.scopemvc.core.ModelChangeEventSource; 46 import org.scopemvc.core.PropertyManager; 47 import org.scopemvc.core.Selector; 48 49 /*** 50 * Used with {@link ListModelAdaptor} to wrap a Collection, List, Array, 51 * Enumeration or Iterator as the source of a list of model objects. Useful when 52 * initialising {@link org.scopemvc.view.swing.SComboBox} or {@link 53 * org.scopemvc.view.swing.SList} with a static list of data. 54 * 55 * @author Roytman, Alex 56 * @created 05 September 2002 57 * @version $Revision: 1.3 $ $Date: 2002/09/05 15:41:46 $ 58 */ 59 public class ListModelSource { 60 61 private Object listSource; 62 private Selector listSourceSelector; 63 private PropertyManager listModelManager; 64 65 66 /*** 67 * Constructor for the ListModelSource object 68 * 69 * @param listSourceModel TODO: Describe the Parameter 70 * @param listSourceSelector TODO: Describe the Parameter 71 */ 72 public ListModelSource(ModelChangeEventSource listSourceModel, Selector listSourceSelector) { 73 this.listSource = listSourceModel; 74 this.listSourceSelector = listSourceSelector; 75 this.listModelManager = PropertyManager.getInstance(listSourceModel); 76 } 77 78 79 /*** 80 * Constructor for the ListModelSource object 81 * 82 * @param listSource TODO: Describe the Parameter 83 */ 84 public ListModelSource(Collection listSource) { 85 this.listSource = listSource; 86 } 87 88 89 /*** 90 * Constructor for the ListModelSource object 91 * 92 * @param listSource TODO: Describe the Parameter 93 */ 94 public ListModelSource(List listSource) { 95 this.listSource = listSource; 96 } 97 98 99 /*** 100 * Constructor for the ListModelSource object 101 * 102 * @param listSource TODO: Describe the Parameter 103 */ 104 public ListModelSource(Iterator listSource) { 105 this.listSource = listSource; 106 } 107 108 109 /*** 110 * Constructor for the ListModelSource object 111 * 112 * @param listSource TODO: Describe the Parameter 113 */ 114 public ListModelSource(Enumeration listSource) { 115 this.listSource = listSource; 116 } 117 118 119 /*** 120 * Gets the model based 121 * 122 * @return The modelBased value 123 */ 124 public boolean isModelBased() { 125 return (listModelManager != null); 126 } 127 128 129 /*** 130 * Gets the list 131 * 132 * @return The list value 133 */ 134 public boolean isList() { 135 return (getListSource() instanceof List); 136 } 137 138 139 /*** 140 * Gets the list source model 141 * 142 * @return The listSourceModel value 143 */ 144 public ModelChangeEventSource getListSourceModel() { 145 return (ModelChangeEventSource) listSource; 146 } 147 148 149 /*** 150 * Gets the list source 151 * 152 * @return The listSource value 153 */ 154 public Object getListSource() { 155 if (isModelBased()) { 156 try { 157 return (listSource == null) ? null : listModelManager.get(listSource, listSourceSelector); 158 } catch (Exception ex) { 159 if (ex instanceof RuntimeException) { 160 throw (RuntimeException) ex; 161 } else { 162 throw new RuntimeException(ex.toString()); 163 } 164 } 165 } else { 166 return listSource; 167 } 168 } 169 170 171 /*** 172 * Gets the list source selector 173 * 174 * @return The listSourceSelector value 175 */ 176 public Selector getListSourceSelector() { 177 return listSourceSelector; 178 } 179 180 181 /*** 182 * Adds an element to the ToList attribute of the ListModelSource object 183 * 184 * @param list The element to be added to the ToList attribute 185 */ 186 public void addToList(List list) { 187 Object ls = getListSource(); 188 if (ls == null) { 189 return; 190 } 191 if (ls instanceof Collection) { 192 list.addAll((Collection) ls); 193 } else if (ls instanceof Iterator) { 194 for (Iterator iter = (Iterator) ls; iter.hasNext(); ) { 195 list.add(iter.next()); 196 } 197 } else if (ls instanceof Enumeration) { 198 for (Enumeration enum = (Enumeration) ls; enum.hasMoreElements(); ) { 199 list.add(enum.nextElement()); 200 } 201 } else if (ls.getClass().isArray()) { 202 Object src[] = (Object[]) ls; 203 for (int i = 0; i < src.length; i++) { 204 list.add(src[i]); 205 } 206 } else { 207 throw new IllegalArgumentException("Unsupported List Source: " + ls.getClass()); 208 } 209 } 210 }

This page was automatically generated by Maven