1 package org.simantics.district.network.ui.internal;
3 import java.util.Arrays;
5 import java.util.stream.Collectors;
7 import org.eclipse.e4.core.contexts.IEclipseContext;
8 import org.eclipse.e4.core.services.events.IEventBroker;
9 import org.eclipse.e4.ui.internal.workbench.E4Workbench;
10 import org.eclipse.swt.widgets.Display;
11 import org.eclipse.ui.plugin.AbstractUIPlugin;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.service.event.Event;
14 import org.osgi.service.event.EventHandler;
15 import org.osgi.util.tracker.ServiceTracker;
16 import org.simantics.Simantics;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.UniqueRead;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.district.network.ontology.DistrictNetworkResource;
23 import org.simantics.district.network.ui.DistrictDiagramViewerListener;
24 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
25 import org.simantics.district.network.ui.breakdown.SubgraphProvider;
26 import org.simantics.district.route.RouteService;
27 import org.simantics.modeling.ModelingResources;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class Activator extends AbstractUIPlugin {
33 private static final class HighlightSelectionEventHandler implements EventHandler {
35 public void handleEvent(Event event) {
36 Object data = event.getProperty("org.eclipse.e4.data");
37 if (data instanceof Variable[]) {
38 Variable[] propertyTableComponents = (Variable[]) data;
40 // convert components to dh components
41 List<Resource> dnElements = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
44 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
45 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
46 ModelingResources MOD = ModelingResources.getInstance(graph);
47 return Arrays.asList(propertyTableComponents).stream().map(var -> {
49 Resource res = var.getRepresents(graph);
50 Resource element = graph.getSingleObject(res, MOD.ComponentToElement);
51 return graph.getSingleObject(element, DN.MappedFromElement);
52 } catch (Exception e) {
53 String varURI = var.toString();
55 varURI = var.getURI(graph);
56 } catch (DatabaseException ex) {
57 LOGGER.error("Unable to resole uri for {}", var, ex);
59 LOGGER.error("Could not get dn element for {}", varURI, e);
62 }).collect(Collectors.toList());
65 DistrictNetworkUIUtil.openDNDiagramWithSelection(Display.getDefault(), dnElements);
66 } catch (DatabaseException e) {
67 LOGGER.error("Could not convert variables to dn elements", propertyTableComponents, e);
73 // This is hard coded for now.. the same value is used for multipropertytable to
74 // send the events we are here listening and interested in
75 private static final String PROPERTY_TABLE_HIGHLIGHT = "PROPERTY_TABLE_HIGHLIGHT";
77 private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
79 public static final String PLUGIN_ID = "org.simantics.district.network.ui";
81 private static Activator instance;
82 private static BundleContext context;
84 private ServiceTracker<SubgraphProvider, SubgraphProvider> subgraphProviderTracker;
85 private ServiceTracker<RouteService, RouteService> routeServiceTracker;
86 private ServiceTracker<DistrictDiagramViewerListener, DistrictDiagramViewerListener> districtDiagramViewerListenerTracker;
88 private HighlightSelectionEventHandler eventHandler;
91 public void start(BundleContext context) throws Exception {
92 Activator.instance = this;
93 Activator.context = context;
95 subgraphProviderTracker = new ServiceTracker<>(context, SubgraphProvider.class.getName(), null);
96 subgraphProviderTracker.open();
97 routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
98 routeServiceTracker.open();
99 districtDiagramViewerListenerTracker = new ServiceTracker<>(context, DistrictDiagramViewerListener.class.getName(), null);
100 districtDiagramViewerListenerTracker.open();
102 initializeEventListener();
106 public void stop(BundleContext context) throws Exception {
107 subgraphProviderTracker.close();
108 routeServiceTracker.close();
109 deinitializeEventListener();
110 Activator.instance = null;
111 Activator.context = null;
114 public static Activator getInstance() {
118 public static BundleContext getContext() {
122 public SubgraphProvider[] getSubgraphProviders() {
123 return subgraphProviderTracker.getServices(new SubgraphProvider[0]);
126 public RouteService getRouteService() {
127 return routeServiceTracker.getService();
130 public DistrictDiagramViewerListener[] getDistrictDiagramViewerListeners() {
131 return districtDiagramViewerListenerTracker.getServices(new DistrictDiagramViewerListener[0]);
134 private void initializeEventListener() {
135 @SuppressWarnings("restriction")
136 IEclipseContext contxt = E4Workbench.getServiceContext();
137 IEventBroker broker = contxt.get(IEventBroker.class);
138 eventHandler = new HighlightSelectionEventHandler();
139 if (broker != null) {
140 broker.subscribe(PROPERTY_TABLE_HIGHLIGHT, eventHandler);
142 LOGGER.info("EventBroker is somehow null for {}", this);
146 private void deinitializeEventListener() {
147 @SuppressWarnings("restriction")
148 IEclipseContext contxt = E4Workbench.getServiceContext();
149 IEventBroker broker = contxt.get(IEventBroker.class);
150 if (broker != null) {
151 broker.unsubscribe(eventHandler);
153 LOGGER.info("EventBroker is somehow null for {}", this);