Coverage Report - org.xmlhammer.gui.history.HistoryComboBoxModel
 
Classes in this File Line Coverage Branch Coverage Complexity
HistoryComboBoxModel
35% 
17% 
0
 
 1  
 /*
 2  
  * $Id: HistoryComboBoxModel.java,v 1.4 2007/09/26 12:29:29 edankert Exp $
 3  
  *
 4  
  * The contents of this file are subject to the Mozilla Public License 
 5  
  * Version 1.1 (the "License"); you may not use this file except in 
 6  
  * compliance with the License. You may obtain a copy of the License at 
 7  
  * http://www.mozilla.org/MPL/ 
 8  
  *
 9  
  * Software distributed under the License is distributed on an "AS IS" basis, 
 10  
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 11  
  * for the specific language governing rights and limitations under the License.
 12  
  *
 13  
  * The Original Code is XML Hammer code. (org.xmlhammer.*)
 14  
  *
 15  
  * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
 16  
  * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
 17  
  * All Rights Reserved.
 18  
  *
 19  
  * Contributor(s): Edwin Dankert <edankert@gmail.com>
 20  
  */
 21  
 package org.xmlhammer.gui.history;
 22  
 
 23  
 import java.util.List;
 24  
 
 25  
 import javax.swing.AbstractListModel;
 26  
 import javax.swing.ComboBoxModel;
 27  
 import javax.swing.event.ListDataEvent;
 28  
 import javax.swing.event.ListDataListener;
 29  
 
 30  
 import org.xmlhammer.model.preferences.HistoryList;
 31  
 
 32  
 public class HistoryComboBoxModel extends AbstractListModel implements ComboBoxModel, ListDataListener {
 33  
     private static final long serialVersionUID = -6751506903190064987L;
 34  7876
     private Object item = null;
 35  7876
     private HistoryListModel model = null;
 36  
     
 37  7876
     public HistoryComboBoxModel(HistoryListModel model) {
 38  7876
         this.model = model;
 39  
 
 40  7876
         model.addListDataListener(this);
 41  7876
     }
 42  
 
 43  
     public void intervalAdded(ListDataEvent event) {
 44  0
         fireIntervalAdded(event.getSource(), event.getIndex0(), event.getIndex1());
 45  0
     }
 46  
 
 47  
     public void intervalRemoved(ListDataEvent event) {
 48  0
         fireIntervalRemoved(event.getSource(), event.getIndex0(), event.getIndex1());
 49  0
     }
 50  
 
 51  
     public void contentsChanged(ListDataEvent event) {
 52  0
         fireContentsChanged(event.getSource(), event.getIndex0(), event.getIndex1());
 53  0
     }
 54  
 
 55  
     public void setSelectedItem(Object item) {
 56  8546
         if(this.item != null && !this.item.equals(item) || this.item == null && item != null) {
 57  1782
             this.item = item;
 58  1782
             fireContentsChanged(this, -1, -1);
 59  
         }
 60  8546
     }
 61  
 
 62  
     public Object getSelectedItem() {
 63  49912
         return item;
 64  
     }
 65  
 
 66  
     public int getSize() {
 67  15070
         return model.getSize();
 68  
     }
 69  
 
 70  
     public Object getElementAt(int index) {
 71  0
         return model.getElementAt(index);
 72  
     }
 73  
     
 74  
     public void removeListener() {
 75  462
         model.removeListDataListener(this);
 76  462
     }
 77  
     
 78  
     public void add(String value) {
 79  0
         HistoryList history = model.getHistory();
 80  
 
 81  0
         if (value != null && value.length() > 0) {
 82  0
             List<String> list = history.getValue();
 83  
             
 84  0
             for (String item : list) {
 85  0
                 if (item.equals(value)) {
 86  0
                     list.remove(item);
 87  0
                     model.removeElement(value);
 88  0
                     break;
 89  
                 }
 90  0
             }
 91  
             
 92  0
             list.add(0, value);
 93  0
             model.add(0, value);
 94  0
 
 95  0
             if (history.getSize() < list.size()) {
 96  0
                 // remove the last item!
 97  0
                 String last = list.get(list.size()-1);
 98  
     
 99  0
                 model.removeElement(last);
 100  0
                 list.remove(last);
 101  0
             }
 102  
         }
 103  0
     }
 104  
 }