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.DistrictNetworkUIUtil;
24 import org.simantics.district.network.ui.breakdown.SubgraphProvider;
25 import org.simantics.district.route.RouteService;
26 import org.simantics.modeling.ModelingResources;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class Activator extends AbstractUIPlugin {
32 private static final class HighlightSelectionEventHandler implements EventHandler {
34 public void handleEvent(Event event) {
35 Object data = event.getProperty("org.eclipse.e4.data");
36 if (data instanceof Variable[]) {
37 Variable[] propertyTableComponents = (Variable[]) data;
39 // convert components to dh components
40 List<Resource> dnElements = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
43 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
44 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
45 ModelingResources MOD = ModelingResources.getInstance(graph);
46 return Arrays.asList(propertyTableComponents).stream().map(var -> {
48 Resource res = var.getRepresents(graph);
49 Resource element = graph.getSingleObject(res, MOD.ComponentToElement);
50 return graph.getSingleObject(element, DN.MappedFromElement);
51 } catch (Exception e) {
52 String varURI = var.toString();
54 varURI = var.getURI(graph);
55 } catch (DatabaseException ex) {
56 LOGGER.error("Unable to resole uri for {}", var, ex);
58 LOGGER.error("Could not get dn element for {}", varURI, e);
61 }).collect(Collectors.toList());
64 DistrictNetworkUIUtil.openDNDiagramWithSelection(Display.getDefault(), dnElements);
65 } catch (DatabaseException e) {
66 LOGGER.error("Could not convert variables to dn elements", propertyTableComponents, e);
72 // This is hard coded for now.. the same value is used for multipropertytable to
73 // send the events we are here listening and interested in
74 private static final String PROPERTY_TABLE_HIGHLIGHT = "PROPERTY_TABLE_HIGHLIGHT";
76 private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
78 public static final String PLUGIN_ID = "org.simantics.district.network.ui";
80 private static Activator instance;
81 private static BundleContext context;
83 private ServiceTracker<SubgraphProvider, SubgraphProvider> subgraphProviderTracker;
84 private ServiceTracker<RouteService, RouteService> routeServiceTracker;
86 private HighlightSelectionEventHandler eventHandler;
89 public void start(BundleContext context) throws Exception {
90 Activator.instance = this;
91 Activator.context = context;
93 subgraphProviderTracker = new ServiceTracker<>(context, SubgraphProvider.class.getName(), null);
94 subgraphProviderTracker.open();
95 routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
96 routeServiceTracker.open();
98 initializeEventListener();
102 public void stop(BundleContext context) throws Exception {
103 subgraphProviderTracker.close();
104 routeServiceTracker.close();
105 deinitializeEventListener();
106 Activator.instance = null;
107 Activator.context = null;
110 public static Activator getInstance() {
114 public static BundleContext getContext() {
118 public SubgraphProvider[] getSubgraphProviders() {
119 return subgraphProviderTracker.getServices(new SubgraphProvider[0]);
122 public RouteService getRouteService() {
123 return routeServiceTracker.getService();
126 private void initializeEventListener() {
127 @SuppressWarnings("restriction")
128 IEclipseContext contxt = E4Workbench.getServiceContext();
129 IEventBroker broker = contxt.get(IEventBroker.class);
130 eventHandler = new HighlightSelectionEventHandler();
131 if (broker != null) {
132 broker.subscribe(PROPERTY_TABLE_HIGHLIGHT, eventHandler);
134 LOGGER.info("EventBroker is somehow null for {}", this);
138 private void deinitializeEventListener() {
139 @SuppressWarnings("restriction")
140 IEclipseContext contxt = E4Workbench.getServiceContext();
141 IEventBroker broker = contxt.get(IEventBroker.class);
142 if (broker != null) {
143 broker.unsubscribe(eventHandler);
145 LOGGER.info("EventBroker is somehow null for {}", this);