| 1 |
package org.astrogrid.samp.util; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Container; |
| 6 |
import java.awt.Dimension; |
| 7 |
import java.awt.GridBagConstraints; |
| 8 |
import java.awt.GridBagLayout; |
| 9 |
import java.awt.Insets; |
| 10 |
import java.awt.event.ActionEvent; |
| 11 |
import java.awt.event.ActionListener; |
| 12 |
import java.awt.event.MouseAdapter; |
| 13 |
import java.awt.event.MouseEvent; |
| 14 |
import java.io.File; |
| 15 |
import java.io.IOException; |
| 16 |
import java.net.MalformedURLException; |
| 17 |
import java.net.URL; |
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.Arrays; |
| 20 |
import java.util.Collection; |
| 21 |
import java.util.HashSet; |
| 22 |
import java.util.Iterator; |
| 23 |
import java.util.LinkedHashMap; |
| 24 |
import java.util.List; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.logging.Level; |
| 27 |
import java.util.logging.Logger; |
| 28 |
import javax.swing.AbstractAction; |
| 29 |
import javax.swing.Action; |
| 30 |
import javax.swing.BorderFactory; |
| 31 |
import javax.swing.Box; |
| 32 |
import javax.swing.ComboBoxModel; |
| 33 |
import javax.swing.DefaultComboBoxModel; |
| 34 |
import javax.swing.Icon; |
| 35 |
import javax.swing.ImageIcon; |
| 36 |
import javax.swing.JButton; |
| 37 |
import javax.swing.JComboBox; |
| 38 |
import javax.swing.JComponent; |
| 39 |
import javax.swing.JDialog; |
| 40 |
import javax.swing.JLabel; |
| 41 |
import javax.swing.JPanel; |
| 42 |
import javax.swing.JTextField; |
| 43 |
import javax.swing.Timer; |
| 44 |
import javax.swing.UIManager; |
| 45 |
import javax.swing.event.ChangeEvent; |
| 46 |
import javax.swing.event.ChangeListener; |
| 47 |
import javax.swing.event.ListDataEvent; |
| 48 |
import javax.swing.event.ListDataListener; |
| 49 |
import org.astrogrid.samp.Client; |
| 50 |
import org.astrogrid.samp.ErrInfo; |
| 51 |
import org.astrogrid.samp.Message; |
| 52 |
import org.astrogrid.samp.Metadata; |
| 53 |
import org.astrogrid.samp.Response; |
| 54 |
import org.astrogrid.samp.SampUtils; |
| 55 |
import org.astrogrid.samp.client.ClientProfile; |
| 56 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 57 |
import org.astrogrid.samp.client.HubConnection; |
| 58 |
import org.astrogrid.samp.client.ResultHandler; |
| 59 |
import org.astrogrid.samp.client.SampException; |
| 60 |
import org.astrogrid.samp.gui.AbstractCallActionManager; |
| 61 |
import org.astrogrid.samp.gui.SendActionManager; |
| 62 |
import org.astrogrid.samp.gui.UniformCallActionManager; |
| 63 |
import org.astrogrid.samp.gui.GuiHubConnector; |
| 64 |
import org.astrogrid.samp.httpd.UtilServer; |
| 65 |
|
| 66 |
/** |
| 67 |
* Dialog window for sending a fixed load-type message to a selected |
| 68 |
* client or clients. |
| 69 |
* This is intended for use as a small free-standing SAMP client that |
| 70 |
* for instance can be used as a browser helper application. |
| 71 |
* |
| 72 |
* <p>A main method is supplied for command-line use. |
| 73 |
* |
| 74 |
* @author Mark Taylor |
| 75 |
* @since 21 Aug 2019 |
| 76 |
*/ |
| 77 |
public class SampLoad extends JDialog { |
| 78 |
|
| 79 |
private final GuiHubConnector connector_; |
| 80 |
private final URL url_; |
| 81 |
private final String mtype_; |
| 82 |
private final JComboBox targetSelector_; |
| 83 |
private final JLabel statusField_; |
| 84 |
private final Action sendAct_; |
| 85 |
private Map responseMap_; |
| 86 |
|
| 87 |
private static final URL ICON_URL = |
| 88 |
SampLoad.class.getResource( "/org/astrogrid/samp/images/sampload.png" ); |
| 89 |
private static final Logger logger_ = |
| 90 |
Logger.getLogger( SampLoad.class.getName() ); |
| 91 |
|
| 92 |
/** |
| 93 |
* Constructor. |
| 94 |
* |
| 95 |
* @param connector connector |
| 96 |
* @param rtype resource type |
| 97 |
* @param URL resource URL |
| 98 |
* @param sendType type of item being sent (for user info) |
| 99 |
* @param location location of item being sent (for user info) |
| 100 |
*/ |
| 101 |
public SampLoad( GuiHubConnector connector, final ResourceType rtype, |
| 102 |
final URL url, String location ) { |
| 103 |
super( (JDialog) null, "SAMP Loader", true ); |
| 104 |
connector_ = connector; |
| 105 |
url_ = url; |
| 106 |
mtype_ = rtype == null ? null : rtype.getMType(); |
| 107 |
|
| 108 |
// Prepare to send the message to selected target client(s). |
| 109 |
String tname = rtype == null ? null : rtype.getName(); |
| 110 |
AbstractCallActionManager callManager = |
| 111 |
new UniformCallActionManager( this, connector, mtype_, tname ) { |
| 112 |
protected Map createMessage() { |
| 113 |
return rtype.createMessage( url ); |
| 114 |
} |
| 115 |
protected ResultHandler createResultHandler( HubConnection conn, |
| 116 |
Message msg, |
| 117 |
Client[] recipients ) { |
| 118 |
final Collection recips = |
| 119 |
new HashSet( Arrays.asList( recipients ) ); |
| 120 |
return new ResultHandler() { |
| 121 |
|
| 122 |
// Record which responses we have got. |
| 123 |
public void result( Client responder, Response response ) { |
| 124 |
responseMap_.put( responder, response ); |
| 125 |
if ( response.isOK() ) { |
| 126 |
recips.remove( responder ); |
| 127 |
} |
| 128 |
updateStatus(); |
| 129 |
} |
| 130 |
|
| 131 |
// If all successful, just dismiss the window after |
| 132 |
// a short delay. If there were any problems, |
| 133 |
// leave it posted so that any error messages are visible. |
| 134 |
public void done() { |
| 135 |
boolean success = recips.size() == 0; |
| 136 |
if ( success ) { |
| 137 |
Timer timer = |
| 138 |
new Timer( 1000, new ActionListener() { |
| 139 |
public void actionPerformed( ActionEvent evt ) { |
| 140 |
closeDialog(); |
| 141 |
} |
| 142 |
} ); |
| 143 |
timer.setRepeats( false ); |
| 144 |
timer.start(); |
| 145 |
} |
| 146 |
} |
| 147 |
}; |
| 148 |
} |
| 149 |
}; |
| 150 |
|
| 151 |
// Provide a target client selection widget and associated send button. |
| 152 |
final ComboBoxModel targetModel; |
| 153 |
if ( rtype == null ) { |
| 154 |
targetModel = |
| 155 |
new DefaultComboBoxModel( new String[] { SendActionManager |
| 156 |
.BROADCAST_TARGET } ); |
| 157 |
sendAct_ = new AbstractAction( "Send to selected target" ) { |
| 158 |
public void actionPerformed( ActionEvent evt ) { |
| 159 |
} |
| 160 |
}; |
| 161 |
} |
| 162 |
else { |
| 163 |
targetModel = callManager.createTargetSelector(); |
| 164 |
sendAct_ = callManager.createTargetAction( targetModel ); |
| 165 |
} |
| 166 |
targetSelector_ = new JComboBox( targetModel ) { |
| 167 |
public Dimension getMaximumSize() { |
| 168 |
return super.getPreferredSize(); |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
// Provide a button that dismisses the window. |
| 173 |
Action cancelAct = new AbstractAction( "Cancel" ) { |
| 174 |
public void actionPerformed( ActionEvent evt ) { |
| 175 |
closeDialog(); |
| 176 |
} |
| 177 |
}; |
| 178 |
setDefaultCloseOperation( DISPOSE_ON_CLOSE ); |
| 179 |
|
| 180 |
// Update GUI on relevant SAMP events. |
| 181 |
connector_.addConnectionListener( new ChangeListener() { |
| 182 |
public void stateChanged( ChangeEvent evt ) { |
| 183 |
updateStatus(); |
| 184 |
} |
| 185 |
} ); |
| 186 |
targetModel.addListDataListener( new ListDataListener() { |
| 187 |
public void contentsChanged( ListDataEvent evt ) { |
| 188 |
updateSelector(); |
| 189 |
} |
| 190 |
public void intervalAdded( ListDataEvent evt ) { |
| 191 |
updateSelector(); |
| 192 |
} |
| 193 |
public void intervalRemoved( ListDataEvent evt ) { |
| 194 |
updateSelector(); |
| 195 |
} |
| 196 |
} ); |
| 197 |
|
| 198 |
// We don't have hub autodetect switched on, since in most cases |
| 199 |
// this will not be a long-lived application, but if the user |
| 200 |
// interacts with the window, take it as an opportunity |
| 201 |
// to see if a hub has started. |
| 202 |
getContentPane().addMouseListener( new MouseAdapter() { |
| 203 |
public void mouseEntered( MouseEvent evt ) { |
| 204 |
checkConnection(); |
| 205 |
} |
| 206 |
public void mouseExited( MouseEvent evt ) { |
| 207 |
checkConnection(); |
| 208 |
} |
| 209 |
public void mousePressed( MouseEvent evt ) { |
| 210 |
checkConnection(); |
| 211 |
} |
| 212 |
private void checkConnection() { |
| 213 |
try { |
| 214 |
connector_.getConnection(); |
| 215 |
} |
| 216 |
catch ( SampException e ) { |
| 217 |
} |
| 218 |
} |
| 219 |
} ); |
| 220 |
|
| 221 |
// Note when a send has started by resetting the response map to null. |
| 222 |
JButton sendButton = new JButton( sendAct_ ); |
| 223 |
sendButton.addActionListener( new ActionListener() { |
| 224 |
public void actionPerformed( ActionEvent evt ) { |
| 225 |
responseMap_ = new LinkedHashMap(); |
| 226 |
updateStatus(); |
| 227 |
} |
| 228 |
} ); |
| 229 |
|
| 230 |
// Place components. |
| 231 |
statusField_ = new JLabel(); |
| 232 |
|
| 233 |
JComponent displayPanel = new JPanel( new GridBagLayout() ); |
| 234 |
GridBagConstraints gbc = new GridBagConstraints(); |
| 235 |
addLine( displayPanel, gbc, "Location", createField( location ), true ); |
| 236 |
addLine( displayPanel, gbc, "MType", createField( mtype_ ), true ); |
| 237 |
addLine( displayPanel, gbc, "Target", targetSelector_, false ); |
| 238 |
addLine( displayPanel, gbc, "Status", statusField_, true ); |
| 239 |
|
| 240 |
JComponent buttLine = Box.createHorizontalBox(); |
| 241 |
buttLine.add( Box.createHorizontalGlue() ); |
| 242 |
buttLine.add( sendButton ); |
| 243 |
buttLine.add( Box.createHorizontalStrut( 10 ) ); |
| 244 |
buttLine.add( new JButton( cancelAct ) ); |
| 245 |
buttLine.add( Box.createHorizontalGlue() ); |
| 246 |
buttLine.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); |
| 247 |
|
| 248 |
Box qbox = Box.createVerticalBox(); |
| 249 |
Icon qicon = ICON_URL != null |
| 250 |
? new ImageIcon( ICON_URL ) |
| 251 |
: UIManager.getIcon( "OptionPane.questionIcon" ); |
| 252 |
qbox.add( new JLabel( qicon ) ); |
| 253 |
qbox.add( Box.createVerticalGlue() ); |
| 254 |
qbox.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); |
| 255 |
|
| 256 |
Component hstrut = Box.createHorizontalStrut( 450 ); |
| 257 |
|
| 258 |
JComponent contentPane = (JComponent) getContentPane(); |
| 259 |
contentPane.setLayout( new BorderLayout() ); |
| 260 |
contentPane.add( displayPanel, BorderLayout.CENTER ); |
| 261 |
contentPane.add( buttLine, BorderLayout.SOUTH ); |
| 262 |
contentPane.add( qbox, BorderLayout.WEST ); |
| 263 |
contentPane.add( hstrut, BorderLayout.NORTH ); |
| 264 |
contentPane.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); |
| 265 |
|
| 266 |
// Initialise state. |
| 267 |
updateSelector(); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Updates the GUI according to current state. |
| 272 |
*/ |
| 273 |
private void updateStatus() { |
| 274 |
final String status; |
| 275 |
final boolean canSend; |
| 276 |
if ( url_ == null ) { |
| 277 |
status = "No such file"; |
| 278 |
canSend = false; |
| 279 |
} |
| 280 |
else if ( mtype_ == null ) { |
| 281 |
status = "Unknown resource type (not " |
| 282 |
+ commaJoin( ResourceType.getKnownResourceTypes() ) + ")"; |
| 283 |
canSend = false; |
| 284 |
} |
| 285 |
else if ( ! connector_.isConnected() ) { |
| 286 |
status = "No hub"; |
| 287 |
canSend = false; |
| 288 |
} |
| 289 |
else if ( targetSelector_.getItemCount() <= 1 ) { |
| 290 |
status = "No clients for " + mtype_; |
| 291 |
canSend = false; |
| 292 |
} |
| 293 |
else if ( responseMap_ == null ) { |
| 294 |
status = "Ready"; |
| 295 |
canSend = true; |
| 296 |
} |
| 297 |
else if ( responseMap_.size() == 0 ) { |
| 298 |
status = "sending ..."; |
| 299 |
canSend = false; |
| 300 |
} |
| 301 |
else { |
| 302 |
StringBuffer sbuf = new StringBuffer(); |
| 303 |
for ( Iterator it = responseMap_.entrySet().iterator(); |
| 304 |
it.hasNext(); ) { |
| 305 |
Map.Entry entry = (Map.Entry) it.next(); |
| 306 |
if ( sbuf.length() > 0 ) { |
| 307 |
sbuf.append( "; " ); |
| 308 |
} |
| 309 |
Client client = (Client) entry.getKey(); |
| 310 |
Response response = (Response) entry.getValue(); |
| 311 |
sbuf.append( client + ": " ); |
| 312 |
if ( response.isOK() ) { |
| 313 |
sbuf.append( "OK" ); |
| 314 |
} |
| 315 |
else { |
| 316 |
ErrInfo err = response.getErrInfo(); |
| 317 |
if ( err == null ) { |
| 318 |
sbuf.append( "ERROR" ); |
| 319 |
} |
| 320 |
else { |
| 321 |
sbuf.append( response.getStatus() ) |
| 322 |
.append( " " ) |
| 323 |
.append( err.getErrortxt() ); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
status = sbuf.toString(); |
| 328 |
canSend = false; |
| 329 |
} |
| 330 |
statusField_.setText( status ); |
| 331 |
sendAct_.setEnabled( canSend ); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Updates the target client selector according to the current SAMP |
| 336 |
* client list. |
| 337 |
*/ |
| 338 |
private void updateSelector() { |
| 339 |
|
| 340 |
// Note, the first item in the list corresponds to Broadcast |
| 341 |
// (all clients), and subsequent items are single clients. |
| 342 |
if ( targetSelector_.getItemCount() == 2 ) { |
| 343 |
targetSelector_.setSelectedIndex( 1 ); |
| 344 |
} |
| 345 |
else if ( targetSelector_.getSelectedIndex() < 0 ) { |
| 346 |
targetSelector_.setSelectedIndex( 0 ); |
| 347 |
} |
| 348 |
updateStatus(); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Closes and disposes this dialogue. |
| 353 |
*/ |
| 354 |
private void closeDialog() { |
| 355 |
setVisible( false ); |
| 356 |
dispose(); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Utility method to append a labelled component to a JComponent |
| 361 |
* using GridBagLayout. |
| 362 |
* |
| 363 |
* @param panel container |
| 364 |
* @param gbc constraints object |
| 365 |
* @param labelTxt text of label |
| 366 |
* @param comp component to add |
| 367 |
* @param hfill true to fill horizontal line |
| 368 |
*/ |
| 369 |
private static void addLine( JComponent panel, GridBagConstraints gbc, |
| 370 |
String labelTxt, JComponent comp, |
| 371 |
boolean hfill ) { |
| 372 |
GridBagLayout layer = (GridBagLayout) panel.getLayout(); |
| 373 |
gbc.gridy++; |
| 374 |
gbc.gridx = 0; |
| 375 |
gbc.anchor = GridBagConstraints.WEST; |
| 376 |
gbc.weightx = 0.0; |
| 377 |
gbc.insets = new Insets( 2, 5, 5, 2 ); |
| 378 |
JLabel label = new JLabel( labelTxt + ": " ); |
| 379 |
layer.setConstraints( label, gbc ); |
| 380 |
panel.add( label ); |
| 381 |
GridBagConstraints gbc1 = (GridBagConstraints) gbc.clone(); |
| 382 |
gbc1.gridx = 1; |
| 383 |
gbc1.anchor = GridBagConstraints.WEST; |
| 384 |
gbc1.weightx = 1.0; |
| 385 |
gbc1.fill = hfill ? GridBagConstraints.HORIZONTAL |
| 386 |
: GridBagConstraints.NONE; |
| 387 |
gbc1.gridwidth = GridBagConstraints.REMAINDER; |
| 388 |
layer.setConstraints( comp, gbc1 ); |
| 389 |
panel.add( comp ); |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Utility method to create an uneditable field with given text. |
| 394 |
* |
| 395 |
* @param txt text content |
| 396 |
* @return field component |
| 397 |
*/ |
| 398 |
private static JTextField createField( String txt ) { |
| 399 |
JTextField field = new JTextField(); |
| 400 |
field.setEditable( false ); |
| 401 |
field.setText( txt ); |
| 402 |
field.setCaretPosition( 0 ); |
| 403 |
return field; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Returns a comma-separated string joining the toString values of |
| 408 |
* the elements of a supplied array. |
| 409 |
* |
| 410 |
* @param items array of items |
| 411 |
* @return "i1, i2, ..., iN" |
| 412 |
*/ |
| 413 |
private static String commaJoin( Object[] items ) { |
| 414 |
StringBuffer sbuf = new StringBuffer(); |
| 415 |
for ( int i = 0; i < items.length; i++ ) { |
| 416 |
if ( sbuf.length() > 0 ) { |
| 417 |
sbuf.append( ", " ); |
| 418 |
} |
| 419 |
sbuf.append( String.valueOf( items[ i ] ) ); |
| 420 |
} |
| 421 |
return sbuf.toString(); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Tries to turn a string into a URL. |
| 426 |
* |
| 427 |
* @param location URL or filename |
| 428 |
* @return URL, or null |
| 429 |
*/ |
| 430 |
private static final URL getUrl( String location ) { |
| 431 |
File f = new File( location ); |
| 432 |
if ( f.exists() ) { |
| 433 |
return SampUtils.fileToUrl( f ); |
| 434 |
} |
| 435 |
try { |
| 436 |
return new URL( location ); |
| 437 |
} |
| 438 |
catch ( MalformedURLException e ) { |
| 439 |
} |
| 440 |
return null; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Main invocation method. |
| 445 |
* Use -help for help. |
| 446 |
* |
| 447 |
* @param args arg vector |
| 448 |
*/ |
| 449 |
public static int runMain( String[] args ) throws IOException { |
| 450 |
ResourceType[] resourceTypes = ResourceType.getKnownResourceTypes(); |
| 451 |
|
| 452 |
// Assemble usage string. |
| 453 |
StringBuffer rtbuf = new StringBuffer(); |
| 454 |
for ( int ir = 0; ir < resourceTypes.length; ir++ ) { |
| 455 |
if ( rtbuf.length() > 0 ) { |
| 456 |
rtbuf.append( "|" ); |
| 457 |
} |
| 458 |
rtbuf.append( resourceTypes[ ir ].getName().toLowerCase() ); |
| 459 |
} |
| 460 |
String usage = new StringBuffer() |
| 461 |
.append( "\n Usage:" ) |
| 462 |
.append( "\n " + SampLoad.class.getName() ) |
| 463 |
.append( "\n " ) |
| 464 |
.append( " [-help]" ) |
| 465 |
.append( " [-/+verbose]" ) |
| 466 |
.append( "\n " ) |
| 467 |
.append( " [-rtype " + rtbuf + "]" ) |
| 468 |
.append( " <file-or-url>" ) |
| 469 |
.append( "\n" ) |
| 470 |
.toString(); |
| 471 |
|
| 472 |
// Set up variables which can be set or changed by the argument list. |
| 473 |
ResourceType rtype = null; |
| 474 |
String location = null; |
| 475 |
int verbAdjust = 0; |
| 476 |
|
| 477 |
// Parse the argument list. |
| 478 |
List argList = new ArrayList( Arrays.asList( args ) ); |
| 479 |
for ( Iterator it = argList.iterator(); it.hasNext(); ) { |
| 480 |
String arg = (String) it.next(); |
| 481 |
if ( arg.equals( "-rtype" ) && rtype == null && it.hasNext() ) { |
| 482 |
it.remove(); |
| 483 |
String rtName = (String) it.next(); |
| 484 |
it.remove(); |
| 485 |
for ( int ir = 0; ir < resourceTypes.length; ir++ ) { |
| 486 |
ResourceType rt = resourceTypes[ ir ]; |
| 487 |
if ( rtName.equalsIgnoreCase( rt.getName() ) ) { |
| 488 |
rtype = rt; |
| 489 |
} |
| 490 |
} |
| 491 |
if ( rtype == null ) { |
| 492 |
System.err.println( usage ); |
| 493 |
return 1; |
| 494 |
} |
| 495 |
} |
| 496 |
else if ( arg.startsWith( "-verb" ) ) { |
| 497 |
it.remove(); |
| 498 |
verbAdjust--; |
| 499 |
} |
| 500 |
else if ( arg.startsWith( "+verb" ) ) { |
| 501 |
it.remove(); |
| 502 |
verbAdjust++; |
| 503 |
} |
| 504 |
else if ( arg.equals( "-help" ) ) { |
| 505 |
it.remove(); |
| 506 |
System.out.println( usage ); |
| 507 |
return 0; |
| 508 |
} |
| 509 |
else if ( arg.startsWith( "-" ) || arg.startsWith( "+" ) ) { |
| 510 |
System.err.println( usage ); |
| 511 |
return 1; |
| 512 |
} |
| 513 |
else if ( location == null ) { |
| 514 |
it.remove(); |
| 515 |
location = arg; |
| 516 |
} |
| 517 |
else { |
| 518 |
System.err.println( usage ); |
| 519 |
return 1; |
| 520 |
} |
| 521 |
} |
| 522 |
if ( location == null ) { |
| 523 |
System.err.println( usage ); |
| 524 |
return 1; |
| 525 |
} |
| 526 |
|
| 527 |
// Set logging levels in accordance with flags. |
| 528 |
int logLevel = Level.WARNING.intValue() + 100 * verbAdjust; |
| 529 |
Logger.getLogger( "org.astrogrid.samp" ) |
| 530 |
.setLevel( Level.parse( Integer.toString( logLevel ) ) ); |
| 531 |
|
| 532 |
// Interpret location. |
| 533 |
final URL url; |
| 534 |
if ( location == null ) { |
| 535 |
System.err.println( usage ); |
| 536 |
return 1; |
| 537 |
} |
| 538 |
else { |
| 539 |
url = getUrl( location ); |
| 540 |
} |
| 541 |
if ( url == null ) { |
| 542 |
logger_.warning( "Bad location: " + location ); |
| 543 |
} |
| 544 |
|
| 545 |
// Make sure we have a resource type. This may have been supplied |
| 546 |
// on the command line, but if not, try to identify it by retrieving |
| 547 |
// the metadata or content of the resource itself. |
| 548 |
if ( url != null ) { |
| 549 |
if ( rtype == null ) { |
| 550 |
rtype = ResourceType.readHeadResourceType( url ); |
| 551 |
} |
| 552 |
if ( rtype == null ) { |
| 553 |
rtype = ResourceType.readContentResourceType( url ); |
| 554 |
} |
| 555 |
if ( rtype == null ) { |
| 556 |
logger_.warning( "Unknown resource type: " + url ); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
// Prepare client metadata. |
| 561 |
Metadata meta = new Metadata(); |
| 562 |
meta.setName( "SampLoad" ); |
| 563 |
try { |
| 564 |
meta.setIconUrl( UtilServer.getInstance() |
| 565 |
.exportResource( ICON_URL ).toString() ); |
| 566 |
} |
| 567 |
catch ( Exception e ) { |
| 568 |
logger_.log( Level.WARNING, "Can't export application icon", e ); |
| 569 |
} |
| 570 |
meta.setDescriptionText( new StringBuffer() |
| 571 |
.append( "Browser helper application to forward downloaded files" ) |
| 572 |
.append( " (" + rtype + ") " ) |
| 573 |
.append( "to SAMP Hub" ) |
| 574 |
.toString() ); |
| 575 |
meta.put( "author.name", "Mark Taylor" ); |
| 576 |
|
| 577 |
// Prepare hub registration. |
| 578 |
ClientProfile profile = DefaultClientProfile.getProfile(); |
| 579 |
GuiHubConnector connector = new GuiHubConnector( profile ); |
| 580 |
connector.declareMetadata( meta ); |
| 581 |
connector.declareSubscriptions( connector.computeSubscriptions() ); |
| 582 |
connector.setActive( rtype != null ); |
| 583 |
|
| 584 |
// Send message under GUI control. |
| 585 |
SampLoad dialog = new SampLoad( connector, rtype, url, location ); |
| 586 |
dialog.pack(); |
| 587 |
dialog.setLocationRelativeTo( null ); |
| 588 |
dialog.setVisible( true ); |
| 589 |
|
| 590 |
connector.setActive( false ); |
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Main method. |
| 596 |
*/ |
| 597 |
public static void main( String[] args ) throws IOException { |
| 598 |
int status = runMain( args ); |
| 599 |
System.exit( status ); |
| 600 |
} |
| 601 |
} |