001    package de.deepamehta.plugins.boxrenderer.dom;
002    
003    import de.deepamehta.plugins.topicmaps.ViewmodelCustomizer;
004    import de.deepamehta.plugins.topicmaps.model.ViewProperties;
005    import de.deepamehta.plugins.topicmaps.service.TopicmapsService;
006    
007    import de.deepamehta.core.Association;
008    import de.deepamehta.core.RelatedTopic;
009    import de.deepamehta.core.osgi.PluginActivator;
010    import de.deepamehta.core.service.Inject;
011    import de.deepamehta.core.service.PluginService;
012    
013    import java.util.logging.Logger;
014    
015    
016    
017    public class BoxRendererPlugin extends PluginActivator implements ViewmodelCustomizer {
018    
019        // ------------------------------------------------------------------------------------------------------- Constants
020    
021        private static final String PROP_COLOR    = "dm4.boxrenderer.color";
022        private static final String PROP_EXPANDED = "dm4.boxrenderer.expanded";
023    
024        // ---------------------------------------------------------------------------------------------- Instance Variables
025    
026        // Note: this instance variable is not used but we must declare it in order to initiate service tracking.
027        // The Topicmaps service is accessed only on-the-fly within the serviceArrived() and serviceGone() hooks.
028        @Inject
029        private TopicmapsService topicmapsService;
030    
031        private Logger logger = Logger.getLogger(getClass().getName());
032    
033        // -------------------------------------------------------------------------------------------------- Public Methods
034    
035        // *** Hook Implementations ***
036    
037        @Override
038        public void serviceArrived(PluginService service) {
039            ((TopicmapsService) service).registerViewmodelCustomizer(this);
040        }
041    
042        @Override
043        public void serviceGone(PluginService service) {
044            // Note 1: unregistering is crucial. Otherwise the Topicmaps plugin would hold a viewmodel customizer with
045            // a stale dms instance as soon as the Box Renderer is redeployed. A subsequent storeViewProperties() call
046            // (see below) would fail.
047            // Note 2: we must unregister via serviceGone() hook, that is immediately when the Topicmaps service is about
048            // to go away. Using the shutdown() hook instead would be too late as the Topicmaps service might already gone.
049            ((TopicmapsService) service).unregisterViewmodelCustomizer(this);
050        }
051    
052        // *** ViewmodelCustomizer Implementation ***
053    
054        @Override
055        public void enrichViewProperties(RelatedTopic topic, ViewProperties viewProps) {
056            boolean expanded = _enrichViewProperties(topic, viewProps);
057            if (expanded) {
058                topic.loadChildTopics("dm4.notes.text");
059            }
060        }
061    
062        // ------------------------------------------------------------------------------------------------- Private Methods
063    
064        private boolean _enrichViewProperties(RelatedTopic topic, ViewProperties viewProps) {
065            Association mapcontextAssoc = topic.getRelatingAssociation();
066            // 1) color
067            if (mapcontextAssoc.hasProperty(PROP_COLOR)) {
068                String color = (String) mapcontextAssoc.getProperty(PROP_COLOR);
069                viewProps.put(PROP_COLOR, color);
070            }
071            // 2) expanded
072            boolean expanded = false;
073            if (topic.getTypeUri().equals("dm4.notes.note")) {
074                if (mapcontextAssoc.hasProperty(PROP_EXPANDED)) {
075                    expanded = (Boolean) mapcontextAssoc.getProperty(PROP_EXPANDED);
076                    viewProps.put(PROP_EXPANDED, expanded);
077                }
078            }
079            return expanded;
080        }
081    }