Debian Codegraph @

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

raw
1 package org.astrogrid.samp.gui;
2
3 import java.awt.Component;
4 import java.awt.event.ActionEvent;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.Map;
10 import javax.swing.AbstractAction;
11 import javax.swing.Action;
12 import javax.swing.ListModel;
13 import org.astrogrid.samp.Client;
14 import org.astrogrid.samp.Message;
15 import org.astrogrid.samp.client.HubConnection;
16 import org.astrogrid.samp.client.HubConnector;
17 import org.astrogrid.samp.client.ResultHandler;
18
19 /**
20 * SendActionManager which uses the Asynchronous Call/Response delivery
21 * pattern, but allows a "broadcast" to send different message objects
22 * to different recipients.
23 * Multiple targetted sends rather than an actual SAMP broadcast may be
24 * used to achieve this.
25 * Concrete subclasses need only implement the
26 * {@link #createMessage(org.astrogrid.samp.Client)} method.
27 * They may also wish to to customise the returned Send and Broadcast Action
28 * objects (for instance give them useful names and descriptions).
29 *
30 * @author Mark Taylor
31 * @since 3 Dec 2008
32 */
33 public abstract class IndividualCallActionManager
34 extends AbstractCallActionManager {
35 private final Component parent_;
36
37 /**
38 * Constructor.
39 *
40 * @param parent parent component
41 * @param connector hub connector
42 * @param clientListModel list model containing only those clients
43 * which are suitable recipients;
44 * all elements must be {@link Client}s
45 */
46 public IndividualCallActionManager( Component parent,
47 GuiHubConnector connector,
48 ListModel clientListModel ) {
49 super( parent, connector, clientListModel );
50 parent_ = parent;
51 }
52
53 protected abstract Map createMessage( Client client ) throws Exception;
54
55 public Action createBroadcastAction() {
56 return new BroadcastAction();
57 }
58
59 /**
60 * Action which performs "broadcasts". They may actually be multiple
61 * targetted sends.
62 */
63 private class BroadcastAction extends AbstractAction {
64
65 final HubConnector connector_ = getConnector();
66 final ListModel clientList_ = getClientListModel();
67
68 /**
69 * Constructor.
70 */
71 BroadcastAction() {
72 putValue( SMALL_ICON, getBroadcastIcon() );
73 }
74
75 public void actionPerformed( ActionEvent evt ) {
76
77 // Identify groups of recipients which can receive the same
78 // Message object as each other.
79 int nc = clientList_.getSize();
80 Map msgMap = new HashMap();
81 try {
82 for ( int ic = 0; ic < nc; ic++ ) {
83 Client client = (Client) clientList_.getElementAt( ic );
84 Map message = createMessage( client );
85 if ( message != null ) {
86 Message msg = Message.asMessage( message );
87 msg.check();
88 if ( ! msgMap.containsKey( msg ) ) {
89 msgMap.put( msg, new HashSet() );
90 }
91 Collection clientSet = (Collection) msgMap.get( msg );
92 clientSet.add( client );
93 }
94 }
95 }
96 catch ( Exception e ) {
97 ErrorDialog.showError( parent_, "Send Error",
98 "Error constructing message "
99 + e.getMessage(), e );
100 return;
101 }
102
103 // Send the message to each group at a time.
104 try {
105 HubConnection connection = connector_.getConnection();
106 if ( connection != null ) {
107 for ( Iterator it = msgMap.entrySet().iterator();
108 it.hasNext(); ) {
109 Map.Entry entry = (Map.Entry) it.next();
110 Message msg = (Message) entry.getKey();
111 Client[] recipients =
112 (Client[]) ((Collection) entry.getValue())
113 .toArray( new Client[ 0 ] );
114 String tag = createTag();
115 ResultHandler handler =
116 createResultHandler( connection, msg, recipients );
117 registerHandler( tag, recipients, handler );
118 for ( int ir = 0; ir < recipients.length; ir++ ) {
119 connection.call( recipients[ ir ].getId(),
120 tag, msg );
121 }
122 }
123 }
124 }
125 catch ( Exception e ) {
126 ErrorDialog.showError( parent_, "Send Error",
127 "Error sending message "
128 + e.getMessage(), e );
129 }
130 }
131 }
132 }