| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Component; |
| 4 |
import java.awt.event.ActionEvent; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Iterator; |
| 7 |
import java.util.List; |
| 8 |
import java.util.Map; |
| 9 |
import java.util.Set; |
| 10 |
import javax.swing.AbstractAction; |
| 11 |
import javax.swing.Action; |
| 12 |
import javax.swing.JMenu; |
| 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 subclass which works with messages of a single MType, |
| 21 |
* using the Aysnchronous Call/Response delivery pattern. |
| 22 |
* Concrete subclasses need only implement {@link #createMessage()}. |
| 23 |
* |
| 24 |
* @author Mark Taylor |
| 25 |
* @since 11 Nov 2008 |
| 26 |
*/ |
| 27 |
public abstract class UniformCallActionManager |
| 28 |
extends AbstractCallActionManager { |
| 29 |
|
| 30 |
private final Component parent_; |
| 31 |
private final String mtype_; |
| 32 |
private final String sendType_; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
* |
| 37 |
* @param parent parent component |
| 38 |
* @param connector hub connector |
| 39 |
* @param mtype MType for messages transmitted by this object's actions |
| 40 |
* @param sendType short string identifying the kind of thing being |
| 41 |
* sent (used for action descriptions etc) |
| 42 |
*/ |
| 43 |
public UniformCallActionManager( Component parent, |
| 44 |
GuiHubConnector connector, |
| 45 |
String mtype, String sendType ) { |
| 46 |
super( parent, connector, |
| 47 |
new SubscribedClientListModel( connector, mtype ) ); |
| 48 |
parent_ = parent; |
| 49 |
mtype_ = mtype; |
| 50 |
sendType_ = sendType; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Generates the message which is sent to one or all clients |
| 55 |
* by this object's actions. |
| 56 |
* |
| 57 |
* @return {@link org.astrogrid.samp.Message}-like Map representing |
| 58 |
* message to transmit |
| 59 |
*/ |
| 60 |
protected abstract Map createMessage() throws Exception; |
| 61 |
|
| 62 |
/** |
| 63 |
* Implemented simply by calling {@link #createMessage()}. |
| 64 |
*/ |
| 65 |
protected Map createMessage( Client client ) throws Exception { |
| 66 |
return createMessage(); |
| 67 |
} |
| 68 |
|
| 69 |
protected Action createBroadcastAction() { |
| 70 |
return new BroadcastAction(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns a new targetted send menu with a title suitable for this object. |
| 75 |
* |
| 76 |
* @return new send menu |
| 77 |
*/ |
| 78 |
public JMenu createSendMenu() { |
| 79 |
JMenu menu = super.createSendMenu( "Send " + sendType_ + " to..." ); |
| 80 |
menu.setIcon( getSendIcon() ); |
| 81 |
return menu; |
| 82 |
} |
| 83 |
|
| 84 |
public Action getSendAction( Client client ) { |
| 85 |
Action action = super.getSendAction( client ); |
| 86 |
action.putValue( Action.SHORT_DESCRIPTION, |
| 87 |
"Transmit to " + client + " using SAMP " + mtype_ ); |
| 88 |
return action; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Action for sending broadcast messages. |
| 93 |
*/ |
| 94 |
private class BroadcastAction extends AbstractAction { |
| 95 |
|
| 96 |
/** |
| 97 |
* Constructor. |
| 98 |
*/ |
| 99 |
BroadcastAction() { |
| 100 |
putValue( NAME, "Broadcast " + sendType_ ); |
| 101 |
putValue( SHORT_DESCRIPTION, |
| 102 |
"Transmit " + sendType_ + " to all applications" |
| 103 |
+ " listening using the SAMP protocol" ); |
| 104 |
putValue( SMALL_ICON, getBroadcastIcon() ); |
| 105 |
} |
| 106 |
|
| 107 |
public void actionPerformed( ActionEvent evt ) { |
| 108 |
HubConnector connector = getConnector(); |
| 109 |
Set recipientIdSet = null; |
| 110 |
Message msg = null; |
| 111 |
HubConnection connection = null; |
| 112 |
String tag = null; |
| 113 |
|
| 114 |
// Attempt to send the message. |
| 115 |
try { |
| 116 |
msg = Message.asMessage( createMessage() ); |
| 117 |
msg.check(); |
| 118 |
connection = connector.getConnection(); |
| 119 |
if ( connection != null ) { |
| 120 |
tag = createTag(); |
| 121 |
recipientIdSet = connection.callAll( tag, msg ).keySet(); |
| 122 |
} |
| 123 |
} |
| 124 |
catch ( Exception e ) { |
| 125 |
ErrorDialog.showError( parent_, "Send Error", |
| 126 |
"Send failure " + e.getMessage(), e ); |
| 127 |
} |
| 128 |
|
| 129 |
// If it was sent, arrange for the results to be passed to |
| 130 |
// a suitable result handler. |
| 131 |
if ( recipientIdSet != null ) { |
| 132 |
assert connection != null; |
| 133 |
assert msg != null; |
| 134 |
assert tag != null; |
| 135 |
List recipientList = new ArrayList(); |
| 136 |
Map clientMap = connector.getClientMap(); |
| 137 |
for ( Iterator it = recipientIdSet.iterator(); it.hasNext(); ) { |
| 138 |
String id = (String) it.next(); |
| 139 |
Client recipient = (Client) clientMap.get( id ); |
| 140 |
if ( recipient != null ) { |
| 141 |
recipientList.add( recipient ); |
| 142 |
} |
| 143 |
} |
| 144 |
Client[] recipients = |
| 145 |
(Client[]) recipientList.toArray( new Client[ 0 ] ); |
| 146 |
ResultHandler handler = |
| 147 |
createResultHandler( connection, msg, recipients ); |
| 148 |
if ( recipients.length == 0 ) { |
| 149 |
if ( handler != null ) { |
| 150 |
handler.done(); |
| 151 |
} |
| 152 |
handler = null; |
| 153 |
} |
| 154 |
registerHandler( tag, recipients, handler ); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |