| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CollectionModel |
|
| 1.6666666666666667;1.667 |
| 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.dto.Idable; | |
| 25 | import org.kuali.student.common.ui.client.mvc.ModelChangeEvent.Action; | |
| 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 | 0 | public class CollectionModel<T> implements Model { |
| 38 | 0 | private Map<String, T> data = new HashMap<String, T>(); |
| 39 | 0 | private HandlerManager handlers = new HandlerManager(this); |
| 40 | ||
| 41 | 0 | private T value = null; |
| 42 | /** | |
| 43 | * Adds an object to the model, and fires a ModelChangeEvent | |
| 44 | * @param object | |
| 45 | */ | |
| 46 | public void add(T object) { | |
| 47 | 0 | if (object instanceof Idable){ |
| 48 | 0 | data.put(((Idable)object).getId(), object); |
| 49 | 0 | handlers.fireEvent(new CollectionModelChangeEvent<T>(Action.ADD, this, object)); |
| 50 | } | |
| 51 | 0 | } |
| 52 | ||
| 53 | /** | |
| 54 | * Removes the specified model object from the model | |
| 55 | * | |
| 56 | * @param object | |
| 57 | * @return the object that was removed, or null if not found | |
| 58 | * @deprecated should use new Data structures instead, accessed via getValue, setValue | |
| 59 | */ | |
| 60 | public T remove(T object) { | |
| 61 | 0 | if (object instanceof Idable){ |
| 62 | 0 | T result = data.remove(((Idable)object).getId()); |
| 63 | 0 | if (result != null) { |
| 64 | 0 | handlers.fireEvent(new CollectionModelChangeEvent<T>(Action.REMOVE, this, result)); |
| 65 | } | |
| 66 | 0 | return result; |
| 67 | } else { | |
| 68 | 0 | return null; |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Adds a ModelChangeHandler that will be invoked for ModelChangeEvents | |
| 74 | * | |
| 75 | * @param handler | |
| 76 | * the handler to add | |
| 77 | * @return HandlerRegistration that can be used to unregister the handler later | |
| 78 | */ | |
| 79 | @Override | |
| 80 | public HandlerRegistration addModelChangeHandler(ModelChangeHandler handler) { | |
| 81 | 0 | return handlers.addHandler(ModelChangeEvent.TYPE, handler); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Returns an unsorted, umodifiable collection of the values contained within the model. | |
| 86 | * | |
| 87 | * @return an unsorted, umodifiable collection of the values contained within the model. | |
| 88 | */ | |
| 89 | public Collection<T> getValues() { | |
| 90 | 0 | return Collections.unmodifiableList(new ArrayList<T>(data.values())); |
| 91 | } | |
| 92 | ||
| 93 | ||
| 94 | /** | |
| 95 | * Returns the Model's value | |
| 96 | * Going forward, the Model class should primarily be used with a single DataModel instance for use with the configurable UI framework. | |
| 97 | * @return the Model's value | |
| 98 | */ | |
| 99 | public T getValue() { | |
| 100 | 0 | return value; |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Sets the Model's value | |
| 105 | * Going forward, the Model class should primarily be used with a single DataModel instance for use with the configurable UI framework. | |
| 106 | * @param value the new value | |
| 107 | */ | |
| 108 | public void setValue(T value) { | |
| 109 | 0 | this.value = value; |
| 110 | 0 | handlers.fireEvent(new CollectionModelChangeEvent<T>(Action.RELOAD, this, value)); |
| 111 | 0 | } |
| 112 | ||
| 113 | ||
| 114 | ||
| 115 | } |