Coverage Report - org.jtheque.core.managers.view.impl.components.model.SimpleComboBoxModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleComboBoxModel
0 %
0/39
0 %
0/2
1.071
 
 1  
 package org.jtheque.core.managers.view.impl.components.model;
 2  
 
 3  
 import org.jtheque.utils.collections.CollectionUtils;
 4  
 
 5  
 import javax.swing.DefaultComboBoxModel;
 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 combo box model to store dynamic objects.
 29  
  *
 30  
  * @param <T> The type of objects.
 31  
  * 
 32  
  * @author Baptiste Wicht
 33  
  */
 34  
 public class SimpleComboBoxModel<T> extends DefaultComboBoxModel {
 35  
     private final List<T> objects;
 36  
 
 37  
     /**
 38  
      * Construct a new SimpleComboBoxModel.
 39  
      */
 40  
     public SimpleComboBoxModel(){
 41  0
         this(10);
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Construct a new SimpleComboBoxModel.
 46  
      *
 47  
      * @param capacity The initial capacity of the model.
 48  
      */
 49  
     public SimpleComboBoxModel(int capacity){
 50  0
         super();
 51  
 
 52  0
         objects = new ArrayList<T>(capacity);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Construct a new SimpleComboBoxModel.
 57  
      *
 58  
      * @param objects The objects to put in the model.
 59  
      */
 60  
     public SimpleComboBoxModel(Collection<T> objects){
 61  0
         super();
 62  
 
 63  0
         this.objects = CollectionUtils.copyOf(objects);
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Construct a new SimpleComboBoxModel.
 68  
      *
 69  
      * @param objects The objects to put in the model.
 70  
      */
 71  
     public SimpleComboBoxModel(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 int getSize(){
 84  0
         return objects.size();
 85  
     }
 86  
 
 87  
     @Override
 88  
     public final void addElement(Object obj){
 89  0
         objects.add((T) obj);
 90  0
         fireIntervalAdded(this, getSize(), getSize());
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Add the objects to the model.
 95  
      *
 96  
      * @param elements The objects to add.
 97  
      */
 98  
     public void addElements(Iterable<T> elements){
 99  0
         int index = objects.size();
 100  
 
 101  0
         for (T category : elements){
 102  0
             objects.add(category);
 103  
         }
 104  
 
 105  0
         fireIntervalAdded(this, index, getSize());
 106  0
     }
 107  
 
 108  
     @Override
 109  
     public final void removeElement(Object obj){
 110  0
         T category = (T) obj;
 111  
 
 112  0
         int index = objects.indexOf(category);
 113  
 
 114  0
         objects.remove(category);
 115  
 
 116  0
         fireIntervalRemoved(this, index, index);
 117  0
     }
 118  
 
 119  
     @Override
 120  
     public final void removeAllElements(){
 121  0
         objects.clear();
 122  0
         fireContentsChanged(this, 0, getSize());
 123  0
     }
 124  
 
 125  
     /**
 126  
      * Set the elements contained on the model.
 127  
      *
 128  
      * @param elements The elements to set on the model.
 129  
      */
 130  
     public void setElements(Collection<T> elements){
 131  0
         objects.clear();
 132  0
         objects.addAll(elements);
 133  0
         fireContentsChanged(this, 0, getSize());
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Set the elements contained on the model.
 138  
      *
 139  
      * @param elements The elements to set on the model.
 140  
      */
 141  
     public void setElements(T[] elements){
 142  0
         objects.clear();
 143  0
         objects.addAll(Arrays.asList(elements));
 144  0
         fireContentsChanged(this, 0, getSize());
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Return the objects.
 149  
      *
 150  
      * @return A List containing all the objects of the model.
 151  
      */
 152  
     public Iterable<T> getObjects(){
 153  0
         return objects;
 154  
     }
 155  
 
 156  
     @Override
 157  
     public final T getSelectedItem(){
 158  0
         return (T)super.getSelectedItem();
 159  
     }
 160  
 }