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