Coverage Report - org.jtheque.primary.view.impl.models.PrincipalDataModel
 
Classes in this File Line Coverage Branch Coverage Complexity
PrincipalDataModel
0 %
0/29
0 %
0/8
1.364
 
 1  
 package org.jtheque.primary.view.impl.models;
 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.jdesktop.swingx.event.WeakEventListenerList;
 20  
 import org.jtheque.primary.od.able.Data;
 21  
 import org.jtheque.primary.view.impl.listeners.CurrentObjectListener;
 22  
 import org.jtheque.primary.view.impl.listeners.DisplayListListener;
 23  
 import org.jtheque.primary.view.impl.listeners.ObjectChangedEvent;
 24  
 import org.jtheque.primary.view.impl.models.able.IPrincipalDataModel;
 25  
 
 26  
 import java.util.Collection;
 27  
 
 28  
 /**
 29  
  * A model for the principal data.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  
 public abstract class PrincipalDataModel<T extends Data> implements IPrincipalDataModel<T> {
 34  
         private final WeakEventListenerList listenerList;
 35  
 
 36  
         private Collection<T> displayList;
 37  
 
 38  
         /**
 39  
          * Construct a new PrincipalDataModel.
 40  
          */
 41  
         protected PrincipalDataModel(){
 42  0
                 super();
 43  
 
 44  0
                 listenerList = new WeakEventListenerList();
 45  0
         }
 46  
 
 47  
         /**
 48  
          * Return the datas managed by this model.
 49  
          *
 50  
          * @return The datas managed by this model.
 51  
          */
 52  
         public abstract Collection<T> getDatas();
 53  
 
 54  
         /**
 55  
          * Return the event listener list.
 56  
          *
 57  
          * @return The event listener list.
 58  
          */
 59  
         protected final WeakEventListenerList getEventListenerList(){
 60  0
                 return listenerList;
 61  
         }
 62  
 
 63  
         @Override
 64  
         public final void addCurrentObjectListener(CurrentObjectListener listener){
 65  0
                 listenerList.add(CurrentObjectListener.class, listener);
 66  0
         }
 67  
 
 68  
         @Override
 69  
         public final void addDisplayListListener(DisplayListListener listener){
 70  0
                 listenerList.add(DisplayListListener.class, listener);
 71  0
         }
 72  
 
 73  
         /**
 74  
          * Fire an event to say that the current object has changed.
 75  
          *
 76  
          * @param event The event to fire
 77  
          */
 78  
         protected final void fireCurrentObjectChanged(ObjectChangedEvent event){
 79  0
                 CurrentObjectListener[] listeners = listenerList.getListeners(CurrentObjectListener.class);
 80  
 
 81  0
                 for (CurrentObjectListener listener : listeners){
 82  0
                         listener.objectChanged(event);
 83  
                 }
 84  0
         }
 85  
 
 86  
         /**
 87  
          * Avert the listeners that the display list have changed.
 88  
          */
 89  
         protected final void fireDisplayListChanged(){
 90  0
                 DisplayListListener[] listeners = listenerList.getListeners(DisplayListListener.class);
 91  
 
 92  0
                 for (DisplayListListener listener : listeners){
 93  0
                         listener.displayListChanged();
 94  
                 }
 95  0
         }
 96  
 
 97  
         @Override
 98  
         public final Collection<T> getDisplayList(){
 99  0
                 if (displayList == null){
 100  0
                         displayList = getDatas();
 101  
                 }
 102  
 
 103  0
                 return displayList;
 104  
         }
 105  
 
 106  
         @Override
 107  
         public final void updateDisplayList(Collection<T> datas){
 108  0
                 getDisplayList().clear();
 109  
 
 110  0
                 if (datas != null){
 111  0
                         getDisplayList().addAll(datas);
 112  
                 } else {
 113  0
                         getDisplayList().addAll(getDatas());
 114  
                 }
 115  
 
 116  0
                 fireDisplayListChanged();
 117  0
         }
 118  
 
 119  
         @Override
 120  
         public final void dataChanged(){
 121  0
                 updateDisplayList();
 122  0
         }
 123  
 
 124  
         /**
 125  
          * Update the display list.
 126  
          */
 127  
         public final void updateDisplayList(){
 128  0
                 updateDisplayList(null);
 129  0
         }
 130  
 }