+
+ public void performSelection(Display display, ElementSelector query) {
+ try {
+ Collection<Resource> models = Simantics.getSession().syncRequest(new ActiveModels(Simantics.getProjectResource()));
+ final Resource model = models.isEmpty() ? null : models.iterator().next();
+
+ SelectionResult result = performQuery(query, model);
+
+ if (result.tailCount != result.tailSize) {
+ showArbitraryResultWarning(query, result);
+ }
+
+ if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
+ openDiagramWithSelection(display, result);
+ }
+
+ StructuredSelection selection = makeSelection(model, result);
+ selectionService.setPostSelection(selection);
+ sendSelectionEvent(selection);
+ } catch (DatabaseException e) {
+ LOGGER.error("Element selection query failed", e);
+ }
+ }
+
+ private StructuredSelection makeSelection(final Resource model, SelectionResult result) {
+ return new StructuredSelection(result.elements.stream()
+ .map(p0 -> {
+ AdaptableHintContext selectionElement = new ElementSelectionTools.SelectionElement(SelectionHints.STD_KEYS);
+ selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
+ selectionElement.setHint(SelectionHints.KEY_MODEL, model);
+ return selectionElement;
+ })
+ .toArray());
+ }
+
+ private void openDiagramWithSelection(Display display, SelectionResult result) throws DatabaseException {
+ DistrictNetworkUIUtil.openDNDiagramWithSelection(display, new ArrayList<>(result.elements));
+ }
+
+ private void sendSelectionEvent(Object selection) {
+ eventBroker.send(SELECTION_EVENT_ID, selection);
+ }
+
+ private void showArbitraryResultWarning(ElementSelector query, SelectionResult result) {
+ String name = query.getSelector() != null && query.getSelector() instanceof PropertySelector ? ((PropertySelector)query.getSelector()).propertyName : null;
+ String msg = "Last " + result.tailCount + " of the " + result.elements.size() + " selected elements are an arbitraty subset of " + result.tailSize + " elements with equal values" +
+ (name != null ? " for " + name : "");
+ MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Note", msg);
+ }
+
+ private SelectionResult performQuery(ElementSelector query, final Resource model) throws DatabaseException {
+ SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
+ @Override
+ public SelectionResult perform(ReadGraph graph) throws DatabaseException {
+ if (model == null) {
+ LOGGER.warn("No active model");
+ return new SelectionResult(Collections.emptyList(), 0, 0);
+ }
+
+ return query.selectElementsFrom(graph, model);
+ }
+ });
+ return result;
+ }