Source code: com/eireneh/bible/book/swing/DriversListModel.java
1
2 package com.eireneh.bible.book.swing;
3
4 import java.io.*;
5 import java.awt.*;
6
7 import javax.swing.*;
8 import javax.swing.event.*;
9 import javax.swing.border.*;
10
11 import com.eireneh.util.*;
12 import com.eireneh.bible.book.*;
13
14 /**
15 * DriversListModel.
16 *
17 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
18 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
19 * Distribution Licence:<br />
20 * Project B is free software; you can redistribute it
21 * and/or modify it under the terms of the GNU General Public License,
22 * version 2 as published by the Free Software Foundation.<br />
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.<br />
27 * The License is available on the internet
28 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
29 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
31 * The copyright to this program is held by it's authors.
32 * </font></td></tr></table>
33 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
34 * @see docs.Licence
35 * @author Joe Walker
36 * @version D0.I0.T0
37 */
38 public class DriversListModel extends AbstractListModel
39 {
40 /**
41 * Basic constructor
42 */
43 public DriversListModel()
44 {
45 drivers = BibleDriverManager.getDrivers();
46 }
47
48 /**
49 * Returns the length of the list.
50 */
51 public int getSize()
52 {
53 return drivers.length;
54 }
55
56 /**
57 * Returns the value at the specified index.
58 */
59 public Object getElementAt(int index)
60 {
61 if (index >= drivers.length) return null;
62 return drivers[index].getDriverName() + " (" + drivers[index].getClass().getName() + ")";
63 }
64
65 /**
66 * Given an item, work out the name of the Bible that it represents
67 * @param The item from the list
68 * @return A Bible name
69 */
70 public String getDriverName(Object test)
71 {
72 String item = test.toString();
73 int end = item.indexOf(" (");
74 return item.substring(0, end);
75 }
76
77 /**
78 * Given an item, work out the name of the Driver that it represents
79 * @param The item from the list
80 * @return A Driver
81 */
82 public BibleDriver getDriver(Object test)
83 {
84 return drivers[getIndexOf(test)];
85 }
86
87 /**
88 * Returns the index-position of the specified object in the list.
89 * @param test the object to find
90 * @return an int representing the index position, where 0 is the first position
91 */
92 public int getIndexOf(Object test)
93 {
94 for (int i=0; i<drivers.length; i++)
95 {
96 if (test.equals(getElementAt(i)))
97 return i;
98 }
99
100 return -1;
101 }
102
103 /** The array of drivers */
104 protected BibleDriver[] drivers;
105
106 /** The small version icon */
107 private final static ImageIcon small_icon = new ImageIcon("/com/eireneh/resources/task_small.gif");
108
109 /** border if we do not have focus */
110 protected static Border no_focus;
111
112 /**
113 * Create a BibleListCellRenderer
114 */
115 public static ListCellRenderer getListCellRenderer()
116 {
117 return new BibleListCellRenderer();
118 }
119
120 /**
121 * A custom list view that paints icons alongside the words. This is a
122 * simple modification of DeafultListCellRenderer
123 */
124 public static class BibleListCellRenderer extends JLabel implements ListCellRenderer
125 {
126 /**
127 * Constructs a default renderer object for an item in a list.
128 */
129 public BibleListCellRenderer()
130 {
131 if (no_focus == null)
132 no_focus = BorderFactory.createEmptyBorder(1, 1, 1, 1);
133
134 setOpaque(true);
135 setBorder(no_focus);
136 }
137
138 /**
139 * This is the only method defined by ListCellRenderer. We just
140 * reconfigure the Jlabel each time we're called.
141 * @param list The JLists that we are part of
142 * @param value Value to display
143 * @param index Cell index
144 * @param selected Is the cell selected
145 * @param focus Does the list and the cell have the focus
146 */
147 public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focus)
148 {
149 if (selected)
150 {
151 setBackground(list.getSelectionBackground());
152 setForeground(list.getSelectionForeground());
153 }
154 else
155 {
156 setBackground(list.getBackground());
157 setForeground(list.getForeground());
158 }
159
160 setText((value == null) ? "" : value.toString());
161 setIcon(small_icon);
162
163 setEnabled(list.isEnabled());
164 setFont(list.getFont());
165 setBorder(focus ? UIManager.getBorder("List.focusCellHighlightBorder") : no_focus);
166
167 return this;
168 }
169 }
170 }