Search in sources :

Example 1 with Message

use of org.astrogrid.samp.Message in project gaiasky by langurmonkey.

the class SAMPClient method initialize.

public void initialize(final Skin skin) {
    // Disable logging
    java.util.logging.Logger.getLogger("org.astrogrid.samp").setLevel(Level.OFF);
    // Init map
    idToNode = new TwoWayHashmap<>();
    idToUrl = new HashMap<>();
    ClientProfile cp = DefaultClientProfile.getProfile();
    conn = new GaiaSkyHubConnector(cp);
    // Configure it with metadata about this application
    Metadata meta = new Metadata();
    meta.setName(Settings.APPLICATION_NAME);
    meta.setDescriptionText("3D Universe application focused on ESA's Gaia satellite");
    meta.setDocumentationUrl(Settings.DOCUMENTATION);
    meta.setIconUrl(Settings.ICON_URL.replaceAll("[^\\x00-\\x7F]", "?"));
    meta.put("author.name", Settings.AUTHOR_NAME_PLAIN);
    meta.put("author.email", Settings.AUTHOR_EMAIL);
    meta.put("author.affiliation", Settings.AUTHOR_AFFILIATION_PLAIN);
    meta.put("home.page", Settings.WEBPAGE);
    meta.put("gaiasky.version", Settings.settings.version.version);
    conn.declareMetadata(meta);
    // Load table
    conn.addMessageHandler(new AbstractMessageHandler("table.load.votable") {

        public Map<Object, Object> processCall(HubConnection c, String senderId, Message msg) {
            // do stuff
            String name = (String) msg.getParam("name");
            String id = (String) msg.getParam("table-id");
            String url = (String) msg.getParam("url");
            boolean loaded = loadVOTable(url, id, name, skin);
            if (!loaded) {
                logger.info(I18n.txt("samp.error.votable", name));
            }
            return null;
        }
    });
    // Select one row
    conn.addMessageHandler(new AbstractMessageHandler("table.highlight.row") {

        public Map<Object, Object> processCall(HubConnection c, String senderId, Message msg) {
            // do stuff
            long row = Parser.parseLong((String) msg.getParam("row"));
            String id = (String) msg.getParam("table-id");
            // First, fetch table if not here
            boolean loaded = idToNode.containsKey(id);
            // If table here, select
            if (loaded) {
                logger.info("Select row " + row + " of " + id);
                if (idToNode.containsKey(id)) {
                    if (idToNode.getForward(id) instanceof ParticleGroup) {
                        // Stars or particles
                        ParticleGroup pg = (ParticleGroup) idToNode.getForward(id);
                        pg.setFocusIndex((int) row);
                        preventProgrammaticEvents = true;
                        EventManager.publish(Event.CAMERA_MODE_CMD, this, CameraMode.FOCUS_MODE);
                        EventManager.publish(Event.FOCUS_CHANGE_CMD, this, pg);
                        preventProgrammaticEvents = false;
                    } else if (idToNode.getForward(id) != null) {
                        // Star cluster
                        FadeNode fn = idToNode.getForward(id);
                        if (fn.children != null && fn.children.size > (int) row) {
                            SceneGraphNode sgn = fn.children.get((int) row);
                            preventProgrammaticEvents = true;
                            EventManager.publish(Event.CAMERA_MODE_CMD, this, CameraMode.FOCUS_MODE);
                            EventManager.publish(Event.FOCUS_CHANGE_CMD, this, sgn);
                            preventProgrammaticEvents = false;
                        } else {
                            logger.info("Star cluster to select not found: " + row);
                        }
                    }
                }
            }
            return null;
        }
    });
    // Select multiple rows
    conn.addMessageHandler(new AbstractMessageHandler("table.select.rowList") {

        public Map<Object, Object> processCall(HubConnection c, String senderId, Message msg) {
            // do stuff
            ArrayList<String> rows = (ArrayList<String>) msg.getParam("row-list");
            String id = (String) msg.getParam("table-id");
            // First, fetch table if not here
            boolean loaded = idToNode.containsKey(id);
            // If table here, select
            if (loaded && rows != null && !rows.isEmpty()) {
                logger.info("Select " + rows.size() + " rows of " + id + ". Gaia Sky does not support multiple selection, so only the first entry is selected.");
                // We use the first one, as multiple selections are not supported in Gaia Sky
                int row = Integer.parseInt(rows.get(0));
                if (idToNode.containsKey(id)) {
                    FadeNode fn = idToNode.getForward(id);
                    if (fn instanceof ParticleGroup) {
                        ParticleGroup pg = (ParticleGroup) fn;
                        pg.setFocusIndex(row);
                        preventProgrammaticEvents = true;
                        EventManager.publish(Event.CAMERA_MODE_CMD, this, CameraManager.CameraMode.FOCUS_MODE);
                        EventManager.publish(Event.FOCUS_CHANGE_CMD, this, pg);
                        preventProgrammaticEvents = false;
                    }
                }
            }
            return null;
        }
    });
    // Point in sky
    conn.addMessageHandler(new AbstractMessageHandler("coord.pointAt.sky") {

        public Map<Object, Object> processCall(HubConnection c, String senderId, Message msg) {
            // do stuff
            double ra = Parser.parseDouble((String) msg.getParam("ra"));
            double dec = Parser.parseDouble((String) msg.getParam("dec"));
            logger.info("Point to coordinate (ra,dec): (" + ra + ", " + dec + ")");
            EventManager.publish(Event.CAMERA_MODE_CMD, this, CameraMode.FREE_MODE);
            EventManager.publish(Event.FREE_MODE_COORD_CMD, this, ra, dec);
            return null;
        }
    });
    // This step required even if no custom message handlers added.
    conn.declareSubscriptions(conn.computeSubscriptions());
    // Keep a look out for hubs if initial one shuts down
    conn.setAutoconnect(10);
}
Also used : Message(org.astrogrid.samp.Message) ParticleGroup(gaiasky.scenegraph.ParticleGroup) SceneGraphNode(gaiasky.scenegraph.SceneGraphNode) Metadata(org.astrogrid.samp.Metadata) ArrayList(java.util.ArrayList) FadeNode(gaiasky.scenegraph.FadeNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Message

use of org.astrogrid.samp.Message in project gaiasky by langurmonkey.

the class SAMPClient method notify.

@Override
public void notify(final Event event, Object source, final Object... data) {
    switch(event) {
        case FOCUS_CHANGED:
            if (!preventProgrammaticEvents) {
                if (conn != null && conn.isConnected()) {
                    if (data[0] instanceof ParticleGroup) {
                        ParticleGroup pg = (ParticleGroup) data[0];
                        if (idToNode.containsValue(pg)) {
                            String id = idToNode.getBackward(pg);
                            String url = idToUrl.get(id);
                            int row = pg.getCandidateIndex();
                            Message msg = new Message("table.highlight.row");
                            msg.addParam("row", Integer.toString(row));
                            msg.addParam("table-id", id);
                            msg.addParam("url", url);
                            try {
                                conn.getConnection().notifyAll(msg);
                            } catch (SampException e) {
                                logger.error(e);
                            }
                        }
                    } else if (data[0] instanceof StarCluster) {
                        StarCluster sc = (StarCluster) data[0];
                        if (sc.parent instanceof FadeNode) {
                            // Comes from a catalog
                            FadeNode parent = (FadeNode) sc.parent;
                            if (idToNode.containsValue(parent)) {
                                String id = idToNode.getBackward(parent);
                                String url = idToUrl.get(id);
                                int row = parent.children.indexOf(sc, true);
                                Message msg = new Message("table.highlight.row");
                                msg.addParam("row", Integer.toString(row));
                                msg.addParam("table-id", id);
                                msg.addParam("url", url);
                                try {
                                    conn.getConnection().notifyAll(msg);
                                } catch (SampException e) {
                                    logger.error(e);
                                }
                            }
                        }
                    }
                }
            }
            break;
        case CATALOG_REMOVE:
            String dsName = (String) data[0];
            if (idToNode.containsKey(dsName)) {
                idToNode.removeKey(dsName);
            }
            idToUrl.remove(dsName);
            break;
        case DISPOSE:
            if (conn != null) {
                conn.setActive(false);
            }
            break;
    }
}
Also used : Message(org.astrogrid.samp.Message) ParticleGroup(gaiasky.scenegraph.ParticleGroup) FadeNode(gaiasky.scenegraph.FadeNode) StarCluster(gaiasky.scenegraph.StarCluster)

Aggregations

FadeNode (gaiasky.scenegraph.FadeNode)2 ParticleGroup (gaiasky.scenegraph.ParticleGroup)2 Message (org.astrogrid.samp.Message)2 SceneGraphNode (gaiasky.scenegraph.SceneGraphNode)1 StarCluster (gaiasky.scenegraph.StarCluster)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Metadata (org.astrogrid.samp.Metadata)1