View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.mvc;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.kuali.student.common.ui.client.mvc.ModelChangeEvent.Action;
25  import org.kuali.student.r1.common.dto.Idable;
26  
27  import com.google.gwt.event.shared.HandlerManager;
28  import com.google.gwt.event.shared.HandlerRegistration;
29  
30  /**
31   * Model object used as a container for the model state. Sources ModelChangeEvents used for tracking the model state.
32   * 
33   * @author Kuali Student Team
34   * @param <T>
35   *            the type of model object to be contained within the model
36   */
37  @Deprecated
38  public class CollectionModel<T> implements Model {
39      private Map<String, T> data = new HashMap<String, T>();
40      private HandlerManager handlers = new HandlerManager(this);
41      
42      private T value = null;
43      /**
44       * Adds an object to the model, and fires a ModelChangeEvent
45       * @param object
46       */
47      public void add(T object) {
48          if (object instanceof Idable){
49              data.put(((Idable)object).getId(), object);
50              handlers.fireEvent(new CollectionModelChangeEvent<T>(Action.ADD, this, object));
51          }
52      }
53  
54  	/**
55  	 * Removes the specified model object from the model
56  	 * 
57  	 * @param object
58  	 * @return the object that was removed, or null if not found
59  	 * @deprecated should use new Data structures instead, accessed via getValue, setValue  
60  	 */
61      public T remove(T object) {
62  	    if (object instanceof Idable){
63  	        T result = data.remove(((Idable)object).getId());
64  	        if (result != null) {
65  	            handlers.fireEvent(new CollectionModelChangeEvent<T>(Action.REMOVE, this, result));
66  	        }
67  	        return result;
68  	    } else {
69  	        return null;
70  	    }
71      }
72      
73      /**
74       * Adds a ModelChangeHandler that will be invoked for ModelChangeEvents
75       * 
76       * @param handler
77       *            the handler to add
78       * @return HandlerRegistration that can be used to unregister the handler later
79       */
80      @Override
81      public HandlerRegistration addModelChangeHandler(ModelChangeHandler handler) {
82          return handlers.addHandler(ModelChangeEvent.TYPE, handler);
83      }
84  
85      /**
86       * Returns an unsorted, umodifiable collection of the values contained within the model.
87       * 
88       * @return an unsorted, umodifiable collection of the values contained within the model.
89       */
90      public Collection<T> getValues() {
91          return Collections.unmodifiableList(new ArrayList<T>(data.values()));
92      }
93  
94  	
95      /**
96       * Returns the Model's value
97       * Going forward, the Model class should primarily be used with a single DataModel instance for use with the configurable UI framework.
98       * @return the Model's value
99       */
100     public T getValue() {
101 		return value;
102 	}
103 
104     /**
105      * Sets the Model's value
106      * Going forward, the Model class should primarily be used with a single DataModel instance for use with the configurable UI framework.
107      * @param value the new value
108 	 */
109 	public void setValue(T value) {
110 		this.value = value;
111 		handlers.fireEvent(new CollectionModelChangeEvent<T>(Action.RELOAD, this, value));
112 	}
113     
114     
115     
116 }