]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java
Change selection logic for n lowest/highest value queries
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / ElementSelectorTableUI.java
index 09f34609497f5e0b792597932848edf541355e5c..001c8730970895461b67ffba3e766c9a4be5dae8 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
 import org.eclipse.jface.viewers.DoubleClickEvent;
@@ -21,6 +22,7 @@ import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Tree;
 import org.simantics.Simantics;
 import org.simantics.browsing.ui.common.AdaptableHintContext;
@@ -39,9 +41,9 @@ import org.simantics.district.selection.ElementSelectionResource;
 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.layer0.Layer0;
-import org.simantics.scl.runtime.Lists;
-import org.simantics.scl.runtime.function.FunctionImpl1;
 import org.simantics.ui.selection.AnyResource;
 import org.simantics.ui.selection.AnyVariable;
 import org.simantics.ui.selection.WorkbenchSelectionContentType;
@@ -233,13 +235,13 @@ public class ElementSelectorTableUI extends Composite {
                        TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
                        ElementSelector query = (ElementSelector) selection.getFirstElement();
                        try {
-                               List<Resource> result = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
+                               SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
                                        @Override
-                                       public List<Resource> perform(ReadGraph graph) throws DatabaseException {
+                                       public SelectionResult perform(ReadGraph graph) throws DatabaseException {
                                                model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
                                                if (model == null) {
                                                        LOGGER.warn("No active model");
-                                                       return Collections.emptyList();
+                                                       return new SelectionResult(Collections.emptyList(), 0, 0);
                                                }
                                                
                                                return query.selectElementsFrom(graph, model);
@@ -247,17 +249,24 @@ public class ElementSelectorTableUI extends Composite {
                                });
                                
                                if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
-                                       DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), result);
+                                       DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), new ArrayList<>(result.elements));
                                }
                                else {
-                                       selectionService.setPostSelection(new StructuredSelection(Lists.map(new FunctionImpl1<Resource, AdaptableHintContext>() {
-                                               public AdaptableHintContext apply(Resource p0) {
-                                                       AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS);
-                                                       selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
-                                                       selectionElement.setHint(SelectionHints.KEY_MODEL, model);
-                                                       return selectionElement;
-                                               }
-                                       }, result)));
+                                       selectionService.setPostSelection(new StructuredSelection(result.elements.stream()
+                                                       .map(p0 -> {
+                                                               AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS);
+                                                               selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
+                                                               selectionElement.setHint(SelectionHints.KEY_MODEL, model);
+                                                               return selectionElement;
+                                                       })
+                                                       .toArray()));
+                               }
+                               
+                               if (result.tailCount != result.tailSize) {
+                                       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);
                                }
                        } catch (DatabaseException e) {
                                LOGGER.error("Element selection query failed", e);