| 1 | /** |
| 2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.deduced.viewer.web.shared; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | |
| 20 | /** |
| 21 | * |
| 22 | * List Model |
| 23 | * |
| 24 | * @author Steve McDuff |
| 25 | * |
| 26 | * @param <c> the type of model stored in the list |
| 27 | */ |
| 28 | public class ListModel<c extends Model> extends Model |
| 29 | { |
| 30 | /** |
| 31 | * serialVersionUID |
| 32 | */ |
| 33 | private static final long serialVersionUID = -2286842539977177638L; |
| 34 | |
| 35 | /** |
| 36 | * the list of model |
| 37 | */ |
| 38 | private ArrayList<c> list = new ArrayList<c>(); |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * ListModel constructor |
| 43 | */ |
| 44 | public ListModel() |
| 45 | { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * (non-JSDoc) |
| 51 | * |
| 52 | * @see org.deduced.viewer.web.shared.Model#propertyChanged(org.deduced.viewer.web.shared.ChangeEvent) |
| 53 | */ |
| 54 | @SuppressWarnings("unchecked") |
| 55 | @Override |
| 56 | public void propertyChanged( |
| 57 | ChangeEvent event) |
| 58 | { |
| 59 | if (isEventAdd(event)) |
| 60 | { |
| 61 | c modelToAdd = null; |
| 62 | |
| 63 | modelToAdd = (c) event.getSerializableValue(); |
| 64 | |
| 65 | add(modelToAdd); |
| 66 | } |
| 67 | else if (isEventRemove(event)) |
| 68 | { |
| 69 | handleRemoveEvent(event); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | super.propertyChanged(event); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * handle Remove Event |
| 79 | * |
| 80 | * @param event the remove event |
| 81 | */ |
| 82 | @SuppressWarnings("unchecked") |
| 83 | public void handleRemoveEvent( |
| 84 | ChangeEvent event) |
| 85 | { |
| 86 | remove((c) event.getSerializableValue()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * remove a model from the list |
| 91 | * |
| 92 | * @param modelToRemove model to remove from the list |
| 93 | */ |
| 94 | public void remove( |
| 95 | c modelToRemove) |
| 96 | { |
| 97 | getList().remove(modelToRemove); |
| 98 | notifyParentThatModelChanged(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * add a model to the list |
| 103 | * |
| 104 | * @param newModel the model to add |
| 105 | */ |
| 106 | public void add( |
| 107 | c newModel) |
| 108 | { |
| 109 | getList().add(newModel); |
| 110 | notifyParentThatModelChanged(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * (non-JSDoc) |
| 115 | * |
| 116 | * @see org.deduced.viewer.web.shared.Model#deleteChildModels() |
| 117 | */ |
| 118 | @Override |
| 119 | protected void deleteChildModels() |
| 120 | { |
| 121 | getList().clear(); |
| 122 | super.deleteChildModels(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return the list |
| 127 | */ |
| 128 | public ArrayList<c> getList() |
| 129 | { |
| 130 | return list; |
| 131 | } |
| 132 | |
| 133 | } |