Skip to content

Commit

Permalink
topcat: subscribe to new experimental table.get.stil SAMP MType
Browse files Browse the repository at this point in the history
This allows external clients to get a URL for one of the tables currently
loaded into topcat.  This functionality was suggested by @taldcroft
in the discussion astropy/astropy#1920.

The MType (table.get.stil) resembles the existing table.load.stil.
Possibly there should be a table.get.votable etc, but that should
probably be decided with consultation from the SAMP community.
The machinery is present to add those - it would be a one-liner.
  • Loading branch information
mbtaylor authored and mmpcn committed Nov 27, 2014
1 parent a98c89d commit abae98f
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 40 deletions.
62 changes: 44 additions & 18 deletions topcat/src/docs/sun253.xml
Expand Up @@ -3331,8 +3331,9 @@ unless you are a tool developer you can probably ignore these.
</p>
<p>SAMP MTypes:
<code>table.load.votable</code> or
<code>table.load.fits</code>;
PLASTIC Message IDs:
<code>table.load.fits</code>
</p>
<p>PLASTIC Message IDs:
<code>ivo://votech.org/votable/load</code> or
<code>ivo://votech.org/votable/loadFromURL</code>
</p></dd>
Expand Down Expand Up @@ -3361,8 +3362,9 @@ unless you are a tool developer you can probably ignore these.
listening applications as an alternative to adding the new subset to
the table's subset list.
</p>
<p>SAMP MType: <code>table.select.rowList</code>;
PLASTIC Message ID: <code>ivo://votech.org/votable/showObjects</code>
<p>SAMP MType: <code>table.select.rowList</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/votable/showObjects</code>
</p></dd>

<dt>Transmit Row</dt>
Expand All @@ -3382,8 +3384,9 @@ unless you are a tool developer you can probably ignore these.
window itself - the checkbox is just a convenience for a commonly-used
option.
</p>
<p>SAMP MType: <code>table.highlight.row</code>;
PLASTIC Message ID: <code>ivo://votech.org/votable/highlightObject</code>
<p>SAMP MType: <code>table.highlight.row</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/votable/highlightObject</code>
</p></dd>

<dt>Transmit Coordinates</dt>
Expand All @@ -3398,8 +3401,9 @@ unless you are a tool developer you can probably ignore these.
The table columns which correspond to Right Ascension and Declination
must be selected for this to work.
</p>
<p>SAMP MType: <code>coord.pointAt.sky</code>;
PLASTIC Message ID: <code>ivo://votech.org/sky/pointAtCoords</code>
<p>SAMP MType: <code>coord.pointAt.sky</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/sky/pointAtCoords</code>
</p></dd>

<dt>Transmit Image</dt>
Expand All @@ -3418,8 +3422,9 @@ unless you are a tool developer you can probably ignore these.
option allows you to identify a column which contains an image URL
and to send it to a suitable image viewer or viewers.
</p>
<p>SAMP MType: <code>image.load.fits</code>;
PLASTIC Message ID: <code>ivo://votech.org/fits/image/loadFromURL</code>
<p>SAMP MType: <code>image.load.fits</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/fits/image/loadFromURL</code>
</p></dd>

<dt>Transmit Spectrum</dt>
Expand All @@ -3428,8 +3433,9 @@ unless you are a tool developer you can probably ignore these.
option allows you to identify a column which contains a spectrum URL
and to send it to a suitable spectrum viewer or viewers.
</p>
<p>SAMP MType: <code>spectrum.load.ssa-generic</code>;
PLASTIC Message ID: <code>ivo://votech.org/spectrum/loadFromURL</code>
<p>SAMP MType: <code>spectrum.load.ssa-generic</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/spectrum/loadFromURL</code>
</p></dd>

<dt>Transmit Resource List</dt>
Expand Down Expand Up @@ -3466,8 +3472,9 @@ unless you are a tool developer you can probably ignore these.
<code>table.load.votable</code>,
<code>table.load.fits</code>,
<code>table.load.cdf</code> or
<code>table.load.stil</code>;
PLASTIC Message ID:
<code>table.load.stil</code>
</p>
<p>PLASTIC Message ID:
<code>ivo://votech.org/votable/load</code> or
<code>ivo://votech.org/votable/loadFromURL</code>
</p>
Expand Down Expand Up @@ -3499,8 +3506,9 @@ unless you are a tool developer you can probably ignore these.
Different options for handling exactly how a received row selection is
treated may be made available in future versions.
</p>
<p>SAMP MType: <code>table.select.rowList</code>;
PLASTIC Message ID: <code>ivo://votech.org/votable/showObjects</code>
<p>SAMP MType: <code>table.select.rowList</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/votable/showObjects</code>
</p></dd>

<dt>Highlight Row</dt>
Expand All @@ -3512,8 +3520,9 @@ unless you are a tool developer you can probably ignore these.
as well as performing any additional actions you have configured
in the <ref id="ActivationQueryWindow">Activation Window</ref>.
</p>
<p>SAMP MType: <code>table.highlight.row</code>;
PLASTIC Message ID: <code>ivo://votech.org/votable/highlightObject</code>
<p>SAMP MType: <code>table.highlight.row</code>
</p>
<p>PLASTIC Message ID: <code>ivo://votech.org/votable/highlightObject</code>
</p></dd>

<dt>Load Resource Identifiers</dt>
Expand All @@ -3536,6 +3545,22 @@ unless you are a tool developer you can probably ignore these.
<code>voresource.loadlist.ssap</code>
</p></dd>

<dt>Pull Table</dt>
<dd><p>Allows clients to retrieve tables currently loaded into TOPCAT.
This MType has a mandatory parameter <code>format</code>
giving the (case-insensitive) table format name required for
the output table.
It also takes an optional integer-like parameter <code>id</code>,
giving the ID number (as shown in the Control Window table list)
of the table required. If <code>id</code> is missing or non-positive,
the current table is used.
The returned response has an output parameter <code>url</code>
giving the URL of the <ref id="apparent">apparent</ref> table in question.
This MType is experimental and may be modified or withdrawn in
the future.
</p>
<p>SAMP MType: <code>table.get.stil</code>.
</p></dd>
</dl>
</p>

Expand Down Expand Up @@ -18395,6 +18420,7 @@ introduced since the last version:
You can turn it off using the <label>Antialias</label> checkbox
in the <label>Axes</label>-<label>Grid</label> tabs
of the various plot windows.</li>
<li>New experimental SAMP message <code>table.get.stil</code>.</li>
</ul>
</p></dd>
</dl>
Expand Down
151 changes: 151 additions & 0 deletions topcat/src/main/uk/ac/starlink/topcat/interop/TablePullHandler.java
@@ -0,0 +1,151 @@
package uk.ac.starlink.topcat.interop;

import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.ListModel;
import org.astrogrid.samp.ErrInfo;
import org.astrogrid.samp.Message;
import org.astrogrid.samp.Response;
import org.astrogrid.samp.SampUtils;
import org.astrogrid.samp.client.AbstractMessageHandler;
import org.astrogrid.samp.client.HubConnection;
import org.astrogrid.samp.httpd.ServerResource;
import uk.ac.starlink.table.StarTable;
import uk.ac.starlink.table.StarTableOutput;
import uk.ac.starlink.table.StarTableWriter;
import uk.ac.starlink.topcat.ControlWindow;
import uk.ac.starlink.topcat.TopcatModel;

/**
* Message handler for table pull messages.
* These return a reponse giving the URL of a table held by topcat.
*
* @author Mark Taylor
* @since 28 Feb 2014
*/
public abstract class TablePullHandler extends AbstractMessageHandler {

/**
* Constructor.
*
* @param mtype SAMP Mtype
*/
protected TablePullHandler( String mtype ) {
super( mtype );
}

/**
* If it's a Response, use it as is, if not wrap it up as a success.
*/
@Override
protected Response createResponse( Map processOutput ) {
return processOutput instanceof Response
? (Response) processOutput
: super.createResponse( processOutput );
}

@Override
public Map processCall( HubConnection conn, String senderId, Message msg )
throws IOException {
StarTableWriter twriter = getTableWriter( msg );

/* Parameter "index" gives the ID number of the table in topcat. */
String indexObj = (String) msg.getParam( "index" );
int index = indexObj == null ? -1 : SampUtils.decodeInt( indexObj );
TopcatModel tcModel = getTableByID( index );

/* If there's no table, return an error response. */
if ( tcModel == null ) {
String errtxt = index <= 0 ? "No current table"
: "No table with ID " + index;
return Response.createErrorResponse( new ErrInfo( errtxt ) );
}

/* Otherwise turn it into an HTTP resource and send its URL as a
* response value. */
else {
StarTable table = tcModel.getApparentStarTable();
String fname = "t" + index;
ServerResource resource =
TableSendActionManager.createTableResource( table, twriter );
URL turl =
TopcatServer.getInstance().addResource( fname, resource );
Map result = new LinkedHashMap();
result.put( "url", turl.toString() );
return Response.createSuccessResponse( result );
}
}

/**
* Returns a table writer to use for the given message.
*
* @param msg message
* @return table serializer
*/
protected abstract StarTableWriter getTableWriter( Message msg )
throws IOException;

/**
* Returns a message handler for a given fixed table format.
*
* @param mtype SAMP Mtype
* @param twriter serializer defining table format
* @return new table pull handler
*/
public static TablePullHandler
createFormatTablePullHandler( String mtype,
final StarTableWriter twriter ) {
return new TablePullHandler( mtype ) {
protected StarTableWriter getTableWriter( Message msg ) {
return twriter;
}
};
}

/**
* Returns a message handler where the table format is determined by
* a "format" parameter in the incoming message.
*
* @param mtype SAMP Mtype
* @return new table pull handler
*/
public static TablePullHandler
createGenericTablePullHandler( String mtype ) {
final StarTableOutput sto = new StarTableOutput();
return new TablePullHandler( mtype ) {
protected StarTableWriter getTableWriter( Message msg )
throws IOException {
String fmtName = (String) msg.getRequiredParam( "format" );
return sto.getHandler( fmtName );
}
};
}

/**
* Returns the topcat table with a given ID, or the default one
* if it's non-positive.
*
* @param id topcat table ID or non-positive value
* @return topcat model or null if ID not used
*/
private static TopcatModel getTableByID( int id ) throws IOException {
ControlWindow cwin = ControlWindow.getInstance();
if ( id <= 0 ) {
return cwin.getCurrentModel();
}
else {
ListModel listModel = cwin.getTablesListModel();
int nt = listModel.getSize();
for ( int it = 0; it < nt; it++ ) {
TopcatModel tcModel =
(TopcatModel) listModel.getElementAt( it );
if ( tcModel.getID() == id ) {
return tcModel;
}
}
return null;
}
}
}
Expand Up @@ -97,6 +97,30 @@ public JMenu createSendMenu() {
return menu;
}

/**
* Obtains a (somewhat) persistent resource object via which
* a table can be made available to external processes.
*
* @param table table
* @param writer table serializer
* @return servable resource
*/
public static ServerResource
createTableResource( final StarTable table,
final StarTableWriter writer ) {
return new ServerResource() {
public long getContentLength() {
return -1L;
}
public String getContentType() {
return writer.getMimeType();
}
public void writeBody( OutputStream out ) throws IOException {
writer.writeStarTable( table, out );
}
};
}

/**
* Returns a Sender object which can send a table to a given client.
*
Expand Down Expand Up @@ -176,7 +200,8 @@ public Message createMessage( StarTable table, String ident,
throws IOException {
String fname = "t" + ident + extension_;
URL turl = TopcatServer.getInstance()
.addResource( fname, createResource( table ) );
.addResource( fname,
createTableResource( table, writer_ ) );
Message msg = new Message( getMtype() );
msg.addParam( "url", turl.toString() );
msg.addParam( "table-id", sampId );
Expand All @@ -186,27 +211,6 @@ public Message createMessage( StarTable table, String ident,
return msg;
}

/**
* Obtains a (somewhat) persistent resource object via which
* a table can be made available to external processes.
*
* @param table table
* @return servable resource
*/
private ServerResource createResource( final StarTable table ) {
return new ServerResource() {
public long getContentLength() {
return -1L;
}
public String getContentType() {
return writer_.getMimeType();
}
public void writeBody( OutputStream out ) throws IOException {
writer_.writeStarTable( table, out );
}
};
}

public String toString() {
return mtype_;
}
Expand Down
Expand Up @@ -420,6 +420,9 @@ public Map processCall( HubConnection conn, String senderId,
SsaMultiWindow.class ),
new ResourceListHandler( "voresource.loadlist.tap",
TapTableLoadDialog.class, null ),

/* Supply URL for a currently loaded table. */
TablePullHandler.createGenericTablePullHandler( "table.get.stil" ),
};
}

Expand Down

0 comments on commit abae98f

Please sign in to comment.