Coverage Report - org.jtheque.core.managers.view.impl.components.model.SimpleListModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleListModel
0 %
0/45
0 %
0/2
1.062
 
 1  
 package org.jtheque.core.managers.view.impl.components.model;
 2  
 
 3  
 import org.jtheque.utils.collections.CollectionUtils;
 4  
 
 5  
 import javax.swing.DefaultListModel;
 6  
 import java.util.ArrayList;
 7  
 import java.util.Arrays;
 8  
 import java.util.Collection;
 9  
 import java.util.List;
 10  
 
 11  
 /*
 12  
  * This file is part of JTheque.
 13  
  *
 14  
  * JTheque is free software: you can redistribute it and/or modify
 15  
  * it under the terms of the GNU General Public License as published by
 16  
  * the Free Software Foundation, either version 3 of the License.
 17  
  *
 18  
  * JTheque is distributed in the hope that it will be useful,
 19  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  
  * GNU General Public License for more details.
 22  
  *
 23  
  * You should have received a copy of the GNU General Public License
 24  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 25  
  */
 26  
 
 27  
 /**
 28  
  * A simple list model to store dynamic objects.
 29  
  *
 30  
  * @param <T> The type of objects.
 31  
  *
 32  
  * @author Baptiste Wicht
 33  
  */
 34  
 public class SimpleListModel<T> extends DefaultListModel {
 35  
     private final List<T> objects;
 36  
 
 37  
     /**
 38  
      * Construct a new SimpleListModel.
 39  
      */
 40  
     public SimpleListModel(){
 41  0
         this(10);
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Construct a new SimpleListModel.
 46  
      *
 47  
      * @param capacity The initial capacity of the model.
 48  
      */
 49  
     public SimpleListModel(int capacity){
 50  0
         super();
 51  
 
 52  0
         objects = new ArrayList<T>(capacity);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Construct a new SimpleListModel. 
 57  
      *
 58  
      * @param objects The objects to put in the model.
 59  
      */
 60  
     public SimpleListModel(Collection<T> objects){
 61  0
         super();
 62  
 
 63  0
         this.objects = CollectionUtils.copyOf(objects);
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Construct a new SimpleListModel.
 68  
      *
 69  
      * @param objects The objects to put in the model.
 70  
      */
 71  
     public SimpleListModel(T[] objects){
 72  0
         super();
 73  
 
 74  0
         this.objects = Arrays.asList(objects);
 75  0
     }
 76  
 
 77  
     @Override
 78  
     public final Object getElementAt(int index){
 79  0
         return objects.get(index);
 80  
     }
 81  
 
 82  
     @Override
 83  
     public final Object get(int index){
 84  0
         return objects.get(index);
 85  
     }
 86  
 
 87  
     @Override
 88  
     public final int getSize(){
 89  0
         return objects.size();
 90  
     }
 91  
 
 92  
     @Override
 93  
     public final Object remove(int index){
 94  0
         T category = objects.remove(index);
 95  0
         fireIntervalRemoved(this, index, index);
 96  0
         return category;
 97  
     }
 98  
 
 99  
     @Override
 100  
     public final void addElement(Object obj){
 101  0
         objects.add((T) obj);
 102  0
         fireIntervalAdded(this, getSize(), getSize());
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Add the objects to the model.
 107  
      *
 108  
      * @param elements The objects to add.
 109  
      */
 110  
     public void addElements(Iterable<T> elements){
 111  0
         int index = objects.size();
 112  
 
 113  0
         for (T category : elements){
 114  0
             objects.add(category);
 115  
         }
 116  
 
 117  0
         fireIntervalAdded(this, index, getSize());
 118  0
     }
 119  
 
 120  
     @Override
 121  
     public final void clear(){
 122  0
         objects.clear();
 123  0
         fireContentsChanged(this, 0, getSize());
 124  0
     }
 125  
 
 126  
     @Override
 127  
     public final boolean removeElement(Object obj){
 128  0
         T category = (T) obj;
 129  
 
 130  0
         int index = objects.indexOf(category);
 131  0
         boolean remove = objects.remove(category);
 132  0
         fireIntervalRemoved(this, index, index);
 133  0
         return remove;
 134  
     }
 135  
 
 136  
     @Override
 137  
     public final void removeAllElements(){
 138  0
         objects.clear();
 139  0
         fireContentsChanged(this, 0, getSize());
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Set the elements contained on the model.
 144  
      *
 145  
      * @param elements The elements to set on the model.
 146  
      */
 147  
     public void setElements(Collection<T> elements){
 148  0
         objects.clear();
 149  0
         objects.addAll(elements);
 150  0
         fireContentsChanged(this, 0, getSize());
 151  0
     }
 152  
 
 153  
     /**
 154  
      * Set the elements contained on the model.
 155  
      *
 156  
      * @param elements The elements to set on the model.
 157  
      */
 158  
     public void setElements(T[] elements){
 159  0
         objects.clear();
 160  0
         objects.addAll(Arrays.asList(elements));
 161  0
         fireContentsChanged(this, 0, getSize());
 162  0
     }
 163  
 
 164  
     /**
 165  
      * Return the objects.
 166  
      *
 167  
      * @return A List containing all the objects of the model.
 168  
      */
 169  
     public Collection<T> getObjects(){
 170  0
         return objects;
 171  
     }
 172  
 }