View Javadoc

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      private Object item = null;
35      private HistoryListModel model = null;
36      
37      public HistoryComboBoxModel(HistoryListModel model) {
38          this.model = model;
39  
40          model.addListDataListener(this);
41      }
42  
43      public void intervalAdded(ListDataEvent event) {
44          fireIntervalAdded(event.getSource(), event.getIndex0(), event.getIndex1());
45      }
46  
47      public void intervalRemoved(ListDataEvent event) {
48          fireIntervalRemoved(event.getSource(), event.getIndex0(), event.getIndex1());
49      }
50  
51      public void contentsChanged(ListDataEvent event) {
52          fireContentsChanged(event.getSource(), event.getIndex0(), event.getIndex1());
53      }
54  
55      public void setSelectedItem(Object item) {
56          if(this.item != null && !this.item.equals(item) || this.item == null && item != null) {
57              this.item = item;
58              fireContentsChanged(this, -1, -1);
59          }
60      }
61  
62      public Object getSelectedItem() {
63          return item;
64      }
65  
66      public int getSize() {
67          return model.getSize();
68      }
69  
70      public Object getElementAt(int index) {
71          return model.getElementAt(index);
72      }
73      
74      public void removeListener() {
75          model.removeListDataListener(this);
76      }
77      
78      public void add(String value) {
79          HistoryList history = model.getHistory();
80  
81          if (value != null && value.length() > 0) {
82              List<String> list = history.getValue();
83              
84              for (String item : list) {
85                  if (item.equals(value)) {
86                      list.remove(item);
87                      model.removeElement(value);
88                      break;
89                  }
90              }
91              
92              list.add(0, value);
93              model.add(0, value);
94  
95              if (history.getSize() < list.size()) {
96                  // remove the last item!
97                  String last = list.get(list.size()-1);
98      
99                  model.removeElement(last);
100                 list.remove(last);
101             }
102         }
103     }
104 }