| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import org.astrogrid.samp.Client; |
| 4 |
import org.astrogrid.samp.Subscriptions; |
| 5 |
import org.astrogrid.samp.client.HubConnection; |
| 6 |
import org.astrogrid.samp.client.SampException; |
| 7 |
|
| 8 |
/** |
| 9 |
* Selective client list model which contains only those non-self clients |
| 10 |
* which are subscribed to one or more of a given list of MTypes. |
| 11 |
* |
| 12 |
* @author Mark Taylor |
| 13 |
* @since 1 Sep 2008 |
| 14 |
*/ |
| 15 |
public class SubscribedClientListModel extends SelectiveClientListModel { |
| 16 |
|
| 17 |
private final GuiHubConnector connector_; |
| 18 |
private String[] mtypes_; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor for multiple MTypes. |
| 22 |
* |
| 23 |
* @param connector hub connector |
| 24 |
* @param mtypes mtypes of interest (may have wildcards) |
| 25 |
*/ |
| 26 |
public SubscribedClientListModel( GuiHubConnector connector, |
| 27 |
String[] mtypes ) { |
| 28 |
super( connector.getClientListModel() ); |
| 29 |
connector_ = connector; |
| 30 |
mtypes_ = (String[]) mtypes.clone(); |
| 31 |
init(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor for single MType. |
| 36 |
* |
| 37 |
* @param connector hub connector |
| 38 |
* @param mtype mtype of interest (may have wildcards) |
| 39 |
*/ |
| 40 |
public SubscribedClientListModel( GuiHubConnector connector, |
| 41 |
String mtype ) { |
| 42 |
this( connector, new String[] { mtype } ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Sets the list of MTypes which defines the elements of this list. |
| 47 |
* Any client subscribed to one or more of these MTypes is included. |
| 48 |
* |
| 49 |
* @param mtypes new MType list |
| 50 |
*/ |
| 51 |
public void setMTypes( String[] mtypes ) { |
| 52 |
mtypes_ = (String[]) mtypes.clone(); |
| 53 |
refresh(); |
| 54 |
fireContentsChanged( this, -1, -1 ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the list of MTypes which defines the elements of this list. |
| 59 |
* |
| 60 |
* @return MType list |
| 61 |
*/ |
| 62 |
public String[] getMTypes() { |
| 63 |
return mtypes_; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns true if <code>client</code> is subscribed to one of this |
| 68 |
* model's MTypes. |
| 69 |
*/ |
| 70 |
protected boolean isIncluded( Client client ) { |
| 71 |
String selfId; |
| 72 |
try { |
| 73 |
HubConnection connection = connector_.getConnection(); |
| 74 |
if ( connection == null ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
else { |
| 78 |
selfId = connection.getRegInfo().getSelfId(); |
| 79 |
} |
| 80 |
} |
| 81 |
catch ( SampException e ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
if ( client.getId().equals( selfId ) ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
Subscriptions subs = client.getSubscriptions(); |
| 88 |
if ( subs != null ) { |
| 89 |
for ( int im = 0; im < mtypes_.length; im++ ) { |
| 90 |
if ( subs.isSubscribed( mtypes_[ im ] ) ) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |