Debian Codegraph @

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

raw
1 package org.astrogrid.samp.gui;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
7 import java.util.List;
8 import javax.swing.ListModel;
9 import javax.swing.SwingUtilities;
10 import javax.swing.event.ListDataEvent;
11 import javax.swing.event.ListDataListener;
12 import org.astrogrid.samp.hub.BasicClientSet;
13 import org.astrogrid.samp.hub.HubClient;
14 import org.astrogrid.samp.hub.MessageRestriction;
15 import org.astrogrid.samp.hub.ProfileToken;
16
17 /**
18 * ClientSet implementation used by GuiHubService.
19 * It also implements {@link javax.swing.ListModel}.
20 *
21 * @author Mark Taylor
22 * @since 20 Nov 2008
23 */
24 class GuiClientSet extends BasicClientSet implements ListModel {
25
26 private final List clientList_;
27 private final List listenerList_;
28 private final static HubClient MORIBUND_CLIENT =
29 new HubClient( "<no-id>",
30 new ProfileToken() {
31 public String getProfileName() {
32 return "<no-profile>";
33 }
34 public MessageRestriction getMessageRestriction() {
35 return null;
36 }
37 } );
38
39 /**
40 * Constructor.
41 *
42 * @param clientIdComparator comparator for client IDs
43 */
44 public GuiClientSet( Comparator clientIdComparator ) {
45 super( clientIdComparator );
46 clientList_ = Collections.synchronizedList( new ArrayList() );
47 listenerList_ = new ArrayList();
48 }
49
50 public synchronized void add( HubClient client ) {
51 super.add( client );
52 int index = clientList_.size();
53 clientList_.add( client );
54 scheduleListDataEvent( ListDataEvent.INTERVAL_ADDED, index, index );
55 }
56
57 public synchronized void remove( HubClient client ) {
58 super.remove( client );
59 clientList_.remove( client );
60 int index = clientList_.size();
61 scheduleListDataEvent( ListDataEvent.INTERVAL_REMOVED, index, index );
62 }
63
64 public synchronized HubClient[] getClients() {
65 return (HubClient[]) clientList_.toArray( new HubClient[ 0 ] );
66 }
67
68 public Object getElementAt( int index ) {
69 try {
70 return clientList_.get( index );
71 }
72
73 // May be called from other than the event dispatch thread.
74 catch ( IndexOutOfBoundsException e ) {
75 return MORIBUND_CLIENT;
76 }
77 }
78
79 public int getSize() {
80 return clientList_.size();
81 }
82
83 public void addListDataListener( ListDataListener l ) {
84 listenerList_.add( l );
85 }
86
87 public void removeListDataListener( ListDataListener l ) {
88 listenerList_.remove( l );
89 }
90
91 /**
92 * Schedules notification of list data listeners that the attributes
93 * of a client have changed.
94 * May be called from any thread.
95 *
96 * @param client client which has changed
97 */
98 public synchronized void scheduleClientChanged( HubClient client ) {
99 for ( int ix = 0; ix < clientList_.size(); ix++ ) {
100 if ( clientList_.get( ix ).equals( client ) ) {
101 scheduleListDataEvent( ListDataEvent.CONTENTS_CHANGED,
102 ix, ix );
103 return;
104 }
105 }
106 scheduleListDataEvent( ListDataEvent.CONTENTS_CHANGED,
107 0, clientList_.size() );
108 }
109
110 /**
111 * Schedules notification of list data listeners about an event.
112 * May be called from any thread.
113 *
114 * @param type ListDataEvent event type
115 * @param int index0 ListDataEvent start index
116 * @param int index1 ListDataEvent end index
117 */
118 private void scheduleListDataEvent( int type, int index0, int index1 ) {
119 if ( ! listenerList_.isEmpty() ) {
120 final ListDataEvent evt =
121 new ListDataEvent( this, type, index0, index1 );
122 SwingUtilities.invokeLater( new Runnable() {
123 public void run() {
124 fireListDataEvent( evt );
125 }
126 } );
127 }
128 }
129
130 /**
131 * Passes a ListDataEvent to all listeners.
132 * Must be called from AWT event dispatch thread.
133 *
134 * @param evt event to forward
135 */
136 public void fireListDataEvent( ListDataEvent evt ) {
137 assert SwingUtilities.isEventDispatchThread();
138 int type = evt.getType();
139 for ( Iterator it = listenerList_.iterator(); it.hasNext(); ) {
140 ListDataListener listener = (ListDataListener) it.next();
141 if ( type == ListDataEvent.INTERVAL_ADDED ) {
142 listener.intervalAdded( evt );
143 }
144 else if ( type == ListDataEvent.INTERVAL_REMOVED ) {
145 listener.intervalRemoved( evt );
146 }
147 else if ( type == ListDataEvent.CONTENTS_CHANGED ) {
148 listener.contentsChanged( evt );
149 }
150 else {
151 assert false;
152 }
153 }
154 }
155 }