001    package de.deepamehta.plugins.topicmaps.model;
002    
003    import de.deepamehta.plugins.topicmaps.model.ViewProperties;
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     * <p>
013     * That is a generic topic model enriched by view properties. Standard view properties are "dm4.topicmaps.x",
014     * "dm4.topicmaps.y", and "dm4.topicmaps.visibility". Additional view properties can be added by plugins (by
015     * implementing a Viewmodel Customizer).
016     */
017    public class TopicViewmodel extends TopicModel {
018    
019        // --- Instance Variables ---
020    
021        private ViewProperties viewProps;
022    
023        // --- Constructors ---
024    
025        public TopicViewmodel(TopicModel topic, ViewProperties viewProps) {
026            super(topic);
027            this.viewProps = viewProps;
028        }
029    
030        // --- Public Methods ---
031    
032        public ViewProperties getViewProperties() {
033            return viewProps;
034        }
035    
036        // ---
037    
038        /**
039         * Convencience method to access the "dm4.topicmaps.x" standard view property.
040         */
041        public int getX() {
042            return viewProps.getInt("dm4.topicmaps.x");
043        }
044    
045        /**
046         * Convencience method to access the "dm4.topicmaps.y" standard view property.
047         */
048        public int getY() {
049            return viewProps.getInt("dm4.topicmaps.y");
050        }
051    
052        /**
053         * Convencience method to access the "dm4.topicmaps.visibility" standard view property.
054         */
055        public boolean getVisibility() {
056            return viewProps.getBoolean("dm4.topicmaps.visibility");
057        }
058    
059        // ---
060    
061        @Override
062        public JSONObject toJSON() {
063            try {
064                JSONObject o = super.toJSON();
065                o.put("view_props", viewProps);
066                return o;
067            } catch (Exception e) {
068                throw new RuntimeException("Serialization failed (" + this + ")", e);
069            }
070        }
071    }