]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/internal/Activator.java
Merge "Allow product-specific customization of new network diagram creation"
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / internal / Activator.java
1 package org.simantics.district.network.ui.internal;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.stream.Collectors;
6
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;
29
30 public class Activator extends AbstractUIPlugin {
31
32         private static final class HighlightSelectionEventHandler implements EventHandler {
33                 @Override
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;
38                                 try {
39                                         // convert components to dh components
40                                         List<Resource> dnElements = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
41                                                 
42                                                 @Override
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 -> {
47                                                                 try {
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();
53                                                                         try {
54                                                                                 varURI = var.getURI(graph);
55                                                                         } catch (DatabaseException ex) {
56                                                                                 LOGGER.error("Unable to resole uri for {}", var, ex);
57                                                                         }
58                                                                         LOGGER.error("Could not get dn element for {}", varURI, e);
59                                                                         return null;
60                                                                 }
61                                                         }).collect(Collectors.toList());
62                                                 }
63                                         });
64                                         DistrictNetworkUIUtil.openDNDiagramWithSelection(Display.getDefault(), dnElements);
65                                 } catch (DatabaseException e) {
66                                         LOGGER.error("Could not convert variables to dn elements", propertyTableComponents, e);
67                                 }
68                         }
69                 }
70         }
71
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";
75
76         private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
77
78     public static final String PLUGIN_ID = "org.simantics.district.network.ui";
79
80     private static Activator instance;
81     private static BundleContext context;
82
83     private ServiceTracker<SubgraphProvider, SubgraphProvider> subgraphProviderTracker;
84     private ServiceTracker<RouteService, RouteService> routeServiceTracker;
85
86         private HighlightSelectionEventHandler eventHandler;
87
88     @Override
89     public void start(BundleContext context) throws Exception {
90         Activator.instance = this;
91         Activator.context = context;
92
93         subgraphProviderTracker = new ServiceTracker<>(context, SubgraphProvider.class.getName(), null);
94         subgraphProviderTracker.open();
95         routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
96         routeServiceTracker.open();
97         
98         initializeEventListener();
99     }
100
101     @Override
102     public void stop(BundleContext context) throws Exception {
103         subgraphProviderTracker.close();
104         routeServiceTracker.close();
105         deinitializeEventListener();
106         Activator.instance = null;
107         Activator.context = null;
108     }
109
110     public static Activator getInstance() {
111         return instance;
112     }
113
114     public static BundleContext getContext() {
115         return context;
116     }
117
118     public SubgraphProvider[] getSubgraphProviders() {
119         return subgraphProviderTracker.getServices(new SubgraphProvider[0]);
120     }
121
122     public RouteService getRouteService() {
123         return routeServiceTracker.getService();
124     }
125
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);
133                 } else {
134                         LOGGER.info("EventBroker is somehow null for {}", this);
135                 }
136         }
137
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);
144                 } else {
145                         LOGGER.info("EventBroker is somehow null for {}", this);
146                 }
147         }
148 }