001    package de.deepamehta.plugins.topicmaps.model;
002    
003    import de.deepamehta.core.model.CompositeValueModel;
004    import de.deepamehta.core.model.TopicModel;
005    
006    import org.codehaus.jettison.json.JSONObject;
007    
008    
009    
010    /**
011     * A topic viewmodel as contained in a topicmap viewmodel.
012     * That is a generic topic model enriched by view properties ("x", "y", "visibility").
013     */
014    public class TopicViewmodel extends TopicModel {
015    
016        // --- Instance Variables ---
017    
018        private CompositeValueModel viewProps;
019    
020        // --- Constructors ---
021    
022        TopicViewmodel(TopicModel topic, CompositeValueModel viewProps) {
023            super(topic);
024            this.viewProps = viewProps;
025        }
026    
027        // --- Public Methods ---
028    
029        @Override
030        public JSONObject toJSON() {
031            try {
032                JSONObject o = super.toJSON();
033                o.put("view_props", viewProps.toJSON());
034                return o;
035            } catch (Exception e) {
036                throw new RuntimeException("Serialization failed (" + this + ")", e);
037            }
038        }
039    
040        // ---
041    
042        // ### not used
043        int getX() {
044            // Note: coordinates can be both: double (through JavaScript) and integer (programmatically placed).
045            // ### TODO: store coordinates always as integers
046            Object x = viewProps.getObject("x");
047            return x instanceof Double ? ((Double) x).intValue() : (Integer) x;
048        }
049    
050        // ### used by GridPositioning
051        int getY() {
052            // Note: coordinates can be both: double (through JavaScript) and integer (programmatically placed).
053            // ### TODO: store coordinates always as integers
054            Object y = viewProps.getObject("y");
055            return y instanceof Double ? ((Double) y).intValue() : (Integer) y;
056        }
057    }