Debian Codegraph @

src/java/org/astrogrid/samp/gui/SelectiveClientListModel.java

raw
1 package org.astrogrid.samp.gui;
2
3 import java.util.Arrays;
4 import javax.swing.AbstractListModel;
5 import javax.swing.ListModel;
6 import javax.swing.event.ListDataEvent;
7 import javax.swing.event.ListDataListener;
8 import org.astrogrid.samp.Client;
9
10 /**
11 * ListModel implementation which sits on top of an existing ListModel
12 * containing {@link org.astrogrid.samp.Client}s, but only includes a
13 * subset of its elements.
14 *
15 * <p>Concrete subclasses must
16 * <ol>
17 * <li>implement the {@link #isIncluded} method to determine which clients
18 * from the base list appear in this one</li>
19 * <li>call {@link #init} before the class is used
20 * (for instance in their constructor)</li>
21 * </ol>
22 *
23 * @author Mark Taylor
24 * @since 1 Sep 2008
25 */
26 public abstract class SelectiveClientListModel extends AbstractListModel {
27
28 private final ListModel baseModel_;
29 private final ListDataListener listDataListener_;
30 private int[] map_;
31
32 /**
33 * Constructor.
34 *
35 * @param clientListModel base ListModel containing
36 * {@link org.astrogrid.samp.Client} objects
37 */
38 public SelectiveClientListModel( ListModel clientListModel ) {
39 baseModel_ = clientListModel;
40
41 // Somewhat haphazard implementation. The ListDataListener interface
42 // is not constructed (or documented) so as to make it easy to
43 // fire the right events. Some efficiency measures are taken here,
44 // but it would be possible to do more.
45 listDataListener_ = new ListDataListener() {
46 public void contentsChanged( ListDataEvent evt ) {
47 int[] oldMap = map_;
48 map_ = calculateMap();
49 if ( Arrays.equals( oldMap, map_ ) &&
50 evt.getIndex0() == evt.getIndex1() &&
51 evt.getIndex0() >= 0 ) {
52 int index = evt.getIndex0();
53 for ( int i = 0; i < map_.length; i++ ) {
54 if ( map_[ i ] == index ) {
55 fireContentsChanged( this, index, index );
56 }
57 }
58 }
59 else {
60 fireContentsChanged( this, -1, -1 );
61 }
62 }
63 public void intervalAdded( ListDataEvent evt ) {
64 int[] oldMap = map_;
65 map_ = calculateMap();
66 if ( ! Arrays.equals( oldMap, map_ ) ) {
67 int leng = Math.min( map_.length, oldMap.length );
68 int index0 = -1;
69 for ( int i = 0; i < leng; i++ ) {
70 if ( oldMap[ i ] != map_[ i ] ) {
71 index0 = i;
72 break;
73 }
74 }
75 int index1 = -1;
76 for ( int i = 0; i < leng; i++ ) {
77 if ( oldMap[ oldMap.length - 1 - i ] !=
78 map_[ map_.length - 1 - i ] ) {
79 index1 = map_.length - 1 - i;
80 break;
81 }
82 }
83 if ( index0 >= 0 && index1 >= 0 ) {
84 fireIntervalAdded( this, index0, index1 );
85 }
86 else {
87 fireContentsChanged( this, -1, -1 );
88 }
89 }
90 }
91 public void intervalRemoved( ListDataEvent evt ) {
92 int[] oldMap = map_;
93 map_ = calculateMap();
94 if ( ! Arrays.equals( oldMap, map_ ) ) {
95 fireContentsChanged( this, -1, -1 );
96 }
97 }
98 };
99 }
100
101 /**
102 * Implement this method to determine which clients are included in
103 * this list.
104 *
105 * @param client client for consideration
106 * @return true iff client is to be included in this list
107 */
108 protected abstract boolean isIncluded( Client client );
109
110 /**
111 * Must be called by subclass prior to use.
112 */
113 protected void init() {
114 refresh();
115 baseModel_.addListDataListener( listDataListener_ );
116 }
117
118 /**
119 * Recalculates the inclusions. This should be called if the return
120 * value from {@link #isIncluded} might have changed for some of the
121 * elements.
122 */
123 protected void refresh() {
124 map_ = calculateMap();
125 }
126
127 public int getSize() {
128 return map_.length;
129 }
130
131 public Object getElementAt( int index ) {
132 return baseModel_.getElementAt( map_[ index ] );
133 }
134
135 /**
136 * Releases any resources associated with this transmitter.
137 */
138 public void dispose() {
139 baseModel_.removeListDataListener( listDataListener_ );
140 }
141
142 /**
143 * Recalculates the this list -> base list lookup table.
144 *
145 * @return array whose indices represent elements of this list, and
146 * values represent elements of the base list
147 */
148 private int[] calculateMap() {
149 int nc = baseModel_.getSize();
150 int[] map = new int[ nc ];
151 int ij = 0;
152 for ( int ic = 0; ic < nc; ic++ ) {
153 Client client = (Client) baseModel_.getElementAt( ic );
154 if ( isIncluded( client ) ) {
155 map[ ij++ ] = ic;
156 }
157 }
158 int[] map1 = new int[ ij ];
159 System.arraycopy( map, 0, map1, 0, ij );
160 return map1;
161 }
162 }