]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/internal/Activator.java
Dynamic visualisations interval and disable support
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / internal / Activator.java
index 1e7a4d512fae8ba7216410f91c6adec6578416f9..e5e20af1b97f8b50a6e623211951164a560c29ec 100644 (file)
-package org.simantics.district.network.ui.internal;\r
-\r
-import org.osgi.framework.BundleActivator;\r
-import org.osgi.framework.BundleContext;\r
-\r
-public class Activator implements BundleActivator {\r
-\r
-    @Override\r
-    public void start(BundleContext context) throws Exception {\r
-\r
-    }\r
-\r
-    @Override\r
-    public void stop(BundleContext context) throws Exception {\r
-\r
-    }\r
-\r
-}\r
+package org.simantics.district.network.ui.internal;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.services.events.IEventBroker;
+import org.eclipse.e4.ui.internal.workbench.E4Workbench;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+import org.osgi.util.tracker.ServiceTracker;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.district.network.ui.DistrictDiagramViewerListener;
+import org.simantics.district.network.ui.DistrictNetworkUIUtil;
+import org.simantics.district.network.ui.breakdown.SubgraphProvider;
+import org.simantics.district.route.RouteService;
+import org.simantics.modeling.ModelingResources;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Activator extends AbstractUIPlugin {
+
+       private static final class HighlightSelectionEventHandler implements EventHandler {
+               @Override
+               public void handleEvent(Event event) {
+                       Object data = event.getProperty("org.eclipse.e4.data");
+                       if (data instanceof Variable[]) {
+                               Variable[] propertyTableComponents = (Variable[]) data;
+                               try {
+                                       // convert components to dh components
+                                       List<Resource> dnElements = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
+                                               
+                                               @Override
+                                               public List<Resource> perform(ReadGraph graph) throws DatabaseException {
+                                                       DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                                                       ModelingResources MOD = ModelingResources.getInstance(graph);
+                                                       return Arrays.asList(propertyTableComponents).stream().map(var -> {
+                                                               try {
+                                                                       Resource res = var.getRepresents(graph);
+                                                                       Resource element = graph.getSingleObject(res, MOD.ComponentToElement);
+                                                                       return graph.getSingleObject(element, DN.MappedFromElement);
+                                                               } catch (Exception e) {
+                                                                       String varURI = var.toString();
+                                                                       try {
+                                                                               varURI = var.getURI(graph);
+                                                                       } catch (DatabaseException ex) {
+                                                                               LOGGER.error("Unable to resole uri for {}", var, ex);
+                                                                       }
+                                                                       LOGGER.error("Could not get dn element for {}", varURI, e);
+                                                                       return null;
+                                                               }
+                                                       }).collect(Collectors.toList());
+                                               }
+                                       });
+                                       DistrictNetworkUIUtil.openDNDiagramWithSelection(Display.getDefault(), dnElements);
+                               } catch (DatabaseException e) {
+                                       LOGGER.error("Could not convert variables to dn elements", propertyTableComponents, e);
+                               }
+                       }
+               }
+       }
+
+       // This is hard coded for now.. the same value is used for multipropertytable to
+       // send the events we are here listening and interested in
+       private static final String PROPERTY_TABLE_HIGHLIGHT = "PROPERTY_TABLE_HIGHLIGHT";
+
+       private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
+
+    public static final String PLUGIN_ID = "org.simantics.district.network.ui";
+
+    private static Activator instance;
+    private static BundleContext context;
+
+    private ServiceTracker<SubgraphProvider, SubgraphProvider> subgraphProviderTracker;
+    private ServiceTracker<RouteService, RouteService> routeServiceTracker;
+    private ServiceTracker<DistrictDiagramViewerListener, DistrictDiagramViewerListener> districtDiagramViewerListenerTracker;
+
+       private HighlightSelectionEventHandler eventHandler;
+
+    @Override
+    public void start(BundleContext context) throws Exception {
+        Activator.instance = this;
+        Activator.context = context;
+
+        subgraphProviderTracker = new ServiceTracker<>(context, SubgraphProvider.class.getName(), null);
+        subgraphProviderTracker.open();
+        routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
+        routeServiceTracker.open();
+        districtDiagramViewerListenerTracker = new ServiceTracker<>(context, DistrictDiagramViewerListener.class.getName(), null);
+        districtDiagramViewerListenerTracker.open();
+        
+        initializeEventListener();
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        subgraphProviderTracker.close();
+        routeServiceTracker.close();
+        deinitializeEventListener();
+        Activator.instance = null;
+        Activator.context = null;
+    }
+
+    public static Activator getInstance() {
+        return instance;
+    }
+
+    public static BundleContext getContext() {
+        return context;
+    }
+
+    public SubgraphProvider[] getSubgraphProviders() {
+        return subgraphProviderTracker.getServices(new SubgraphProvider[0]);
+    }
+
+    public RouteService getRouteService() {
+        return routeServiceTracker.getService();
+    }
+    
+    public DistrictDiagramViewerListener[] getDistrictDiagramViewerListeners() {
+        return districtDiagramViewerListenerTracker.getServices(new DistrictDiagramViewerListener[0]);
+    }
+
+       private void initializeEventListener() {
+               @SuppressWarnings("restriction")
+               IEclipseContext contxt = E4Workbench.getServiceContext();
+               IEventBroker broker = contxt.get(IEventBroker.class);
+               eventHandler = new HighlightSelectionEventHandler();
+               if (broker != null) {
+                       broker.subscribe(PROPERTY_TABLE_HIGHLIGHT, eventHandler);
+               } else {
+                       LOGGER.info("EventBroker is somehow null for {}", this);
+               }
+       }
+
+       private void deinitializeEventListener() {
+               @SuppressWarnings("restriction")
+               IEclipseContext contxt = E4Workbench.getServiceContext();
+               IEventBroker broker = contxt.get(IEventBroker.class);
+               if (broker != null) {
+                       broker.unsubscribe(eventHandler);
+               } else {
+                       LOGGER.info("EventBroker is somehow null for {}", this);
+               }
+       }
+}