Debian Codegraph @

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

raw
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.logging.Logger;
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.Metadata;
16 import org.astrogrid.samp.client.HubConnection;
17 import org.astrogrid.samp.client.HubConnector;
18
19 /**
20 * SendActionManager subclass which works with messages of a single MType,
21 * using the Notify delivery pattern.
22 *
23 * @author Mark Taylor
24 * @since 5 Sep 2008
25 */
26 public abstract class NotifyActionManager extends SendActionManager {
27
28 private final Component parent_;
29 private final GuiHubConnector connector_;
30 private final String sendType_;
31 private static final Logger logger_ =
32 Logger.getLogger( NotifyActionManager.class.getName() );
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 NotifyActionManager( Component parent, GuiHubConnector connector,
44 String mtype, String sendType ) {
45 super( connector, new SubscribedClientListModel( connector, mtype ) );
46 parent_ = parent;
47 connector_ = connector;
48 sendType_ = sendType;
49 updateState();
50 }
51
52 /**
53 * Generates the message which is sent to one or all clients
54 * by this object's actions.
55 *
56 * @return {@link org.astrogrid.samp.Message}-like Map representing
57 * message to transmit
58 */
59 protected abstract Map createMessage() throws Exception;
60
61 /**
62 * Called when a message has been sent by this object.
63 * The default action is to notify via the logging system.
64 * Subclasses may override this method.
65 *
66 * @param connection connection object
67 * @param msg the message which was sent
68 * @param recipients the recipients to whom an attempt was made to send
69 * the message
70 */
71 protected void messageSent( HubConnection connection, Message msg,
72 Client[] recipients ) {
73 for ( int i = 0; i < recipients.length; i++ ) {
74 logger_.info( "Message " + msg.getMType() + " sent to "
75 + recipients[ i ] );
76 }
77 }
78
79 protected Action createBroadcastAction() {
80 Action act = new AbstractAction() {
81 public void actionPerformed( ActionEvent evt ) {
82 List recipientIdList = null;
83 Message msg = null;
84 HubConnection connection = null;
85 try {
86 msg = Message.asMessage( createMessage() );
87 msg.check();
88 connection = connector_.getConnection();
89 if ( connection != null ) {
90 recipientIdList = connection.notifyAll( msg );
91 }
92 }
93 catch ( Exception e ) {
94 ErrorDialog.showError( parent_, "Send Error",
95 "Send failure " + e.getMessage(),
96 e );
97 }
98 if ( recipientIdList != null ) {
99 assert connection != null;
100 assert msg != null;
101 List recipientList = new ArrayList();
102 Map clientMap = connector_.getClientMap();
103 for ( Iterator it = recipientIdList.iterator();
104 it.hasNext(); ) {
105 String id = (String) it.next();
106 Client recipient = (Client) clientMap.get( id );
107 if ( recipient != null ) {
108 recipientList.add( recipient );
109 }
110 }
111 messageSent( connection, msg,
112 (Client[])
113 recipientList.toArray( new Client[ 0 ] ) );
114 }
115 }
116 };
117 act.putValue( Action.NAME, "Broadcast " + sendType_ );
118 act.putValue( Action.SHORT_DESCRIPTION,
119 "Transmit " + sendType_ + " to all applications"
120 + " listening using the SAMP protocol" );
121 act.putValue( Action.SMALL_ICON, getBroadcastIcon() );
122 return act;
123 }
124
125 /**
126 * Returns a new menu for targetted sends with a title suitable for
127 * this object.
128 *
129 * @return new send menu
130 */
131 public JMenu createSendMenu() {
132 JMenu menu = super.createSendMenu( "Send " + sendType_ + " to..." );
133 menu.setIcon( getSendIcon() );
134 return menu;
135 }
136
137 protected Action getSendAction( Client client ) {
138 return new SendAction( client );
139 }
140
141 /**
142 * Action which performs a send.
143 */
144 private class SendAction extends AbstractAction {
145 private final Client client_;
146 private final String cName_;
147
148 /**
149 * Constructor.
150 *
151 * @param client target client
152 */
153 SendAction( Client client ) {
154 client_ = client;
155 cName_ = client.toString();
156 putValue( NAME, cName_ );
157 putValue( SHORT_DESCRIPTION,
158 "Transmit " + sendType_ + " to " + cName_
159 + " using SAMP protocol" );
160 }
161
162 public void actionPerformed( ActionEvent evt ) {
163 boolean sent = false;
164 Message msg = null;
165 HubConnection connection = null;
166 try {
167 msg = Message.asMessage( createMessage() );
168 msg.check();
169 connection = connector_.getConnection();
170 if ( connection != null ) {
171 connection.notify( client_.getId(), msg );
172 sent = true;
173 }
174 }
175 catch ( Exception e ) {
176 ErrorDialog.showError( parent_, "Send Error",
177 "Send failure " + e.getMessage(), e );
178 }
179 if ( sent ) {
180 assert connection != null;
181 assert msg != null;
182 messageSent( connection, msg, new Client[] { client_ } );
183 }
184 }
185
186 public boolean equals( Object o ) {
187 if ( o instanceof SendAction ) {
188 SendAction other = (SendAction) o;
189 return this.client_.equals( other.client_ )
190 && this.cName_.equals( other.cName_ );
191 }
192 else {
193 return false;
194 }
195 }
196
197 public int hashCode() {
198 return client_.hashCode() * 23 + cName_.hashCode();
199 }
200 }
201 }