Coverage Report - org.jtheque.films.view.impl.models.list.DataCachedContainerListModel
 
Classes in this File Line Coverage Branch Coverage Complexity
DataCachedContainerListModel
0 %
0/29
0 %
0/2
1.2
 
 1  
 package org.jtheque.films.view.impl.models.list;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.persistence.able.DataContainer;
 20  
 import org.jtheque.core.managers.persistence.able.DataListener;
 21  
 import org.jtheque.core.managers.persistence.able.Entity;
 22  
 
 23  
 import javax.swing.DefaultListModel;
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * A list model for actors.
 29  
  *
 30  
  * @author Baptiste Wicht
 31  
  */
 32  
 public final class DataCachedContainerListModel<T extends Entity> extends DefaultListModel implements DataListener {
 33  
     private static final long serialVersionUID = 627034111249354845L;
 34  
 
 35  
     private List<T> datas;
 36  
 
 37  
     private final DataContainer<T> dao;
 38  
 
 39  
     /**
 40  
      * Construct a new ActorsListModel.
 41  
      *
 42  
      * @param dao The dao to manage the objects from.
 43  
      */
 44  
     public DataCachedContainerListModel(DataContainer<T> dao) {
 45  0
         super();
 46  
 
 47  0
         this.dao = dao;
 48  
 
 49  0
         dao.addDataListener(this);
 50  0
         reload();
 51  0
     }
 52  
 
 53  
     @Override
 54  
     public Object getElementAt(int index) {
 55  0
         return datas.get(index);
 56  
     }
 57  
 
 58  
     @Override
 59  
     public Object get(int index) {
 60  0
         return datas.get(index);
 61  
     }
 62  
 
 63  
     @Override
 64  
     public int getSize() {
 65  0
         return datas.size();
 66  
     }
 67  
 
 68  
     @Override
 69  
     public Object remove(int i) {
 70  0
         T data = datas.remove(i);
 71  0
         fireIntervalRemoved(this, i, i);
 72  0
         return data;
 73  
     }
 74  
 
 75  
     @Override
 76  
     public void addElement(Object obj) {
 77  0
         datas.add((T) obj);
 78  0
         fireIntervalAdded(this, getSize(), getSize());
 79  0
     }
 80  
 
 81  
     @Override
 82  
     public void removeAllElements() {
 83  0
         datas.clear();
 84  0
         fireContentsChanged(this, 0, getSize());
 85  0
     }
 86  
 
 87  
     @Override
 88  
     public boolean removeElement(Object obj) {
 89  0
         T t = (T) obj;
 90  
 
 91  0
         int index = datas.indexOf(t);
 92  0
         if (index > -1) {
 93  0
             boolean remove = datas.remove(t);
 94  0
             fireIntervalRemoved(this, index, index);
 95  0
             return remove;
 96  
         }
 97  
 
 98  0
         return false;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public void dataChanged() {
 103  0
         reload();
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Reload the model.
 108  
      */
 109  
     public void reload() {
 110  0
         datas = new ArrayList<T>(dao.getDatas());
 111  0
         fireContentsChanged(this, 0, getSize());
 112  0
     }
 113  
 }