| RangeListModel.java |
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: RangeListModel.java 2056 2010-12-12 04:34:41Z dmsmith $
21 */
22 package org.crosswire.bibledesktop.passage;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.swing.AbstractListModel;
29
30 import org.crosswire.jsword.passage.Key;
31 import org.crosswire.jsword.passage.Passage;
32 import org.crosswire.jsword.passage.RestrictionType;
33
34 /**
35 * The RangeListModel class gives access to a Passage as a list of ranges via a
36 * ListModel.
37 *
38 * @see gnu.gpl.License for license details.<br>
39 * The copyright to this program is held by it's authors.
40 * @author DM Smith [dmsmith555 at yahoo dot com]
41 */
42 public class RangeListModel extends AbstractListModel {
43 /**
44 * Create a RangeListModel specifying whether to list the ranges bounded by
45 * Chapter or not at all.
46 *
47 * @param theRestriction
48 * Do we chop at chapter boundaries
49 */
50 public RangeListModel(RestrictionType theRestriction) {
51 restrict = theRestriction;
52 ranges = new ArrayList<Key>();
53 }
54
55 /**
56 * Change the restrictions we are using. Must be one of:
57 * <code>RestrictType.NONE</code>, or <code>RestrictType.CHAPTER</code>
58 *
59 * @param restrict
60 * The new restrictions
61 */
62 public void setRestriction(RestrictionType restrict) {
63 this.restrict = restrict;
64
65 refresh();
66 }
67
68 /**
69 * Return the current Range Restriction
70 */
71 public RestrictionType getRestriction() {
72 return restrict;
73 }
74
75 /**
76 * Returns the length of the list.
77 *
78 * @return The number of verses/ranges in the list
79 */
80 public int getSize() {
81 return ranges.size();
82 }
83
84 /**
85 * Returns the value at the specified index.
86 *
87 * @param index
88 * The index (based at 0) of the element to fetch
89 * @return The required verse/range
90 */
91 public Object getElementAt(int index) {
92 return ranges.get(index);
93 }
94
95 /**
96 * Accessor for the current passage
97 */
98 public final void setPassage(Passage newRef) {
99 fireIntervalRemoved(this, 0, getSize());
100
101 ref = newRef;
102
103 if (ref != null) {
104 ref.optimizeReads();
105 }
106
107 refresh();
108
109 fireIntervalAdded(this, 0, getSize());
110 }
111
112 /**
113 * Accessor for the current passage
114 */
115 public Passage getPassage() {
116 return ref;
117 }
118
119 private void refresh() {
120 ranges.clear();
121 if (ref != null) {
122 Iterator<Key> iter = ref.rangeIterator(restrict);
123 while (iter.hasNext()) {
124 ranges.add(iter.next());
125 }
126 }
127 }
128
129 /**
130 * The Passage that we are modeling
131 */
132 private Passage ref;
133
134 /**
135 * The list of ranges in the passage.
136 */
137 private List<Key> ranges;
138
139 /**
140 * If we are modeling in groups, do we break at chapter/book boundaries
141 */
142 private RestrictionType restrict;
143
144 /**
145 * Serialization ID
146 */
147 private static final long serialVersionUID = 3761692273179964725L;
148 }
149