1   /**
2    * Distribution License:
3    * BibleDesktop is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU General Public License, version 2 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/gpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: InstallManagerComboBoxModel.java 2056 2010-12-12 04:34:41Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.book.install;
23  
24  import java.io.IOException;
25  import java.io.ObjectInputStream;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import javax.swing.AbstractListModel;
31  import javax.swing.ComboBoxModel;
32  
33  import org.crosswire.jsword.book.install.InstallManager;
34  import org.crosswire.jsword.book.install.InstallerEvent;
35  import org.crosswire.jsword.book.install.InstallerListener;
36  
37  /**
38   * A ListModel for a JList that uses the list of Installers given by the
39   * InstallManager.
40   * 
41   * @see gnu.gpl.License for license details.<br>
42   *      The copyright to this program is held by it's authors.
43   * @author Joe Walker [joe at eireneh dot com]
44   */
45  public class InstallManagerComboBoxModel extends AbstractListModel implements ComboBoxModel {
46      /**
47       * Simple ctor
48       */
49      public InstallManagerComboBoxModel(InstallManager imanager) {
50          this.imanager = imanager;
51  
52          update(null);
53          selection = getElementAt(0);
54  
55          imanager.addInstallerListener(new CustomInstallerListener());
56      }
57  
58      /* (non-Javadoc)
59       * @see javax.swing.ComboBoxModel#getSelectedItem()
60       */
61      public Object getSelectedItem() {
62          return selection;
63      }
64  
65      /* (non-Javadoc)
66       * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
67       */
68      public void setSelectedItem(Object selection) {
69          this.selection = selection;
70      }
71  
72      /* (non-Javadoc)
73       * @see javax.swing.ListModel#getSize()
74       */
75      public int getSize() {
76          return names.size();
77      }
78  
79      /* (non-Javadoc)
80       * @see javax.swing.ListModel#getElementAt(int)
81       */
82      public final Object getElementAt(int index) {
83          return names.get(index);
84      }
85  
86      /**
87       * Listens to the InstallManager for Installer changes
88       */
89      class CustomInstallerListener implements InstallerListener {
90          /* (non-Javadoc)
91           * @see org.crosswire.jsword.book.install.InstallerListener#installerAdded(org.crosswire.jsword.book.install.InstallerEvent)
92           */
93          public void installerAdded(InstallerEvent ev) {
94              update(ev);
95          }
96  
97          /* (non-Javadoc)
98           * @see org.crosswire.jsword.book.install.InstallerListener#installerRemoved(org.crosswire.jsword.book.install.InstallerEvent)
99           */
100         public void installerRemoved(InstallerEvent ev) {
101             update(ev);
102         }
103     }
104 
105     /**
106      * Simple way to avoid eclipse private/protected warning
107      */
108     protected final void update(InstallerEvent ev) {
109         int oldmax = names.size();
110 
111         names.clear();
112         names.addAll(imanager.getInstallers().keySet());
113         Collections.sort(names);
114 
115         if (ev != null) {
116             fireContentsChanged(ev.getSource(), 0, oldmax);
117         }
118     }
119 
120     /**
121      * Serialization support.
122      * 
123      * @param is
124      * @throws IOException
125      * @throws ClassNotFoundException
126      */
127     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
128         // Broken but we don't serialize views
129         imanager = null;
130         is.defaultReadObject();
131     }
132 
133     /**
134      * The currently selected object
135      */
136     private Object selection;
137 
138     /**
139      * A cache of the names in the Install Manager
140      */
141     private List<String> names = new ArrayList<String>();
142 
143     /**
144      * The install manager that we are representing
145      */
146     private transient InstallManager imanager;
147 
148     /**
149      * Serialization ID
150      */
151     private static final long serialVersionUID = 3256725082729756980L;
152 }
153