]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/ElementSelectionView.java
Send event for performed queries + some refactoring
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / parts / ElementSelectionView.java
index 1cddde9f7034c62c2c37c4ce186a375707f4f78b..5a0767f8020c38708847f6aabe2fa0425b31c473 100644 (file)
@@ -1,10 +1,14 @@
 package org.simantics.district.selection.ui.parts;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 
+import org.eclipse.e4.core.services.events.IEventBroker;
 import org.eclipse.e4.ui.di.Focus;
 import org.eclipse.e4.ui.model.application.MApplication;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
@@ -16,9 +20,27 @@ import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
 import org.eclipse.e4.ui.model.application.ui.menu.MToolBarElement;
 import org.eclipse.e4.ui.services.EMenuService;
 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.e4.ui.workbench.modeling.ISelectionListener;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.common.AdaptableHintContext;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.request.ActiveModels;
+import org.simantics.db.request.Read;
+import org.simantics.district.network.ui.DistrictNetworkUIUtil;
 import org.simantics.district.selection.ElementSelector;
+import org.simantics.district.selection.ElementSelector.DiagramGenerator;
+import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
+import org.simantics.district.selection.ElementSelector.PropertySelector;
+import org.simantics.district.selection.ElementSelector.SelectionResult;
+import org.simantics.district.selection.ui.ElementSelectionTools;
 import org.simantics.district.selection.ui.ElementSelectorTableUI;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,6 +57,12 @@ public class ElementSelectionView {
        private static final String DELETE_ID = "org.simantics.district.selection.ui.command.deleteElementSelector";
        private static final String DELETE_LABEL = "Delete Element Selection Query";
        private static final String DELETE_ICON = "platform:/plugin/com.famfamfam.silk/icons/cross.png";
+
+       /**
+        * Name of an event that is posted to IEventBroker when a selection is made. The data value is
+        * an instance of Collection<Resource>.
+        */
+       public static final String SELECTION_EVENT_ID = "org/simantics/district/selection/elementQuerySelection";
        
        private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectionView.class);
 
@@ -42,7 +70,10 @@ public class ElementSelectionView {
 
        @Inject
        private ESelectionService selectionService;
-
+       
+       @Inject
+       private IEventBroker eventBroker;
+       
        @Inject
        public void init(MPart part, MApplication app) {
                // Command is contributed via fragment
@@ -84,7 +115,7 @@ public class ElementSelectionView {
 
        @PostConstruct
        public void createPartControl(Composite parent, EMenuService menuService) {
-               table = new ElementSelectorTableUI(selectionService, parent, SWT.BORDER);
+               table = new ElementSelectorTableUI(parent, SWT.BORDER, this);
                if (!(menuService.registerContextMenu(this.table.getTree(), POPUP_ID)))
                        LOGGER.warn("Could not register context menu {}", POPUP_ID);
        }
@@ -97,4 +128,68 @@ public class ElementSelectionView {
        public ElementSelector getSelectedItem() {
                return table.getSelectedItem();
        }
+
+       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;
+       }
 }