]> gerrit.simantics Code Review - simantics/district.git/blob - 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
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.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;
30
31 public class Activator extends AbstractUIPlugin {
32
33         private static final class HighlightSelectionEventHandler implements EventHandler {
34                 @Override
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;
39                                 try {
40                                         // convert components to dh components
41                                         List<Resource> dnElements = Simantics.getSession().syncRequest(new UniqueRead<List<Resource>>() {
42                                                 
43                                                 @Override
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 -> {
48                                                                 try {
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();
54                                                                         try {
55                                                                                 varURI = var.getURI(graph);
56                                                                         } catch (DatabaseException ex) {
57                                                                                 LOGGER.error("Unable to resole uri for {}", var, ex);
58                                                                         }
59                                                                         LOGGER.error("Could not get dn element for {}", varURI, e);
60                                                                         return null;
61                                                                 }
62                                                         }).collect(Collectors.toList());
63                                                 }
64                                         });
65                                         DistrictNetworkUIUtil.openDNDiagramWithSelection(Display.getDefault(), dnElements);
66                                 } catch (DatabaseException e) {
67                                         LOGGER.error("Could not convert variables to dn elements", propertyTableComponents, e);
68                                 }
69                         }
70                 }
71         }
72
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";
76
77         private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
78
79     public static final String PLUGIN_ID = "org.simantics.district.network.ui";
80
81     private static Activator instance;
82     private static BundleContext context;
83
84     private ServiceTracker<SubgraphProvider, SubgraphProvider> subgraphProviderTracker;
85     private ServiceTracker<RouteService, RouteService> routeServiceTracker;
86     private ServiceTracker<DistrictDiagramViewerListener, DistrictDiagramViewerListener> districtDiagramViewerListenerTracker;
87
88         private HighlightSelectionEventHandler eventHandler;
89
90     @Override
91     public void start(BundleContext context) throws Exception {
92         Activator.instance = this;
93         Activator.context = context;
94
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();
101         
102         initializeEventListener();
103     }
104
105     @Override
106     public void stop(BundleContext context) throws Exception {
107         subgraphProviderTracker.close();
108         routeServiceTracker.close();
109         deinitializeEventListener();
110         Activator.instance = null;
111         Activator.context = null;
112     }
113
114     public static Activator getInstance() {
115         return instance;
116     }
117
118     public static BundleContext getContext() {
119         return context;
120     }
121
122     public SubgraphProvider[] getSubgraphProviders() {
123         return subgraphProviderTracker.getServices(new SubgraphProvider[0]);
124     }
125
126     public RouteService getRouteService() {
127         return routeServiceTracker.getService();
128     }
129     
130     public DistrictDiagramViewerListener[] getDistrictDiagramViewerListeners() {
131         return districtDiagramViewerListenerTracker.getServices(new DistrictDiagramViewerListener[0]);
132     }
133
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);
141                 } else {
142                         LOGGER.info("EventBroker is somehow null for {}", this);
143                 }
144         }
145
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);
152                 } else {
153                         LOGGER.info("EventBroker is somehow null for {}", this);
154                 }
155         }
156 }