]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/ElementSelectionView.java
Finder visualisation coloring to diagram & checkbox for enable/disable
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / parts / ElementSelectionView.java
index 16e6bb159ed2938a0a1281602b3628b7b08dab94..31d1c6e3b7a64ad915551dec6a46e95714989d47 100644 (file)
@@ -1,9 +1,13 @@
 package org.simantics.district.selection.ui.parts;
 
+import java.awt.Color;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
@@ -34,6 +38,7 @@ 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.network.ui.participants.DistrictFinderVisualisationParticipant;
 import org.simantics.district.selection.ElementSelector;
 import org.simantics.district.selection.ElementSelector.DiagramGenerator;
 import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
@@ -41,6 +46,7 @@ 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.simantics.scl.runtime.tuple.Tuple2;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -123,18 +129,28 @@ public class ElementSelectionView {
        public void setFocus() {
                table.setFocus();
        }
-
+       
        public ElementSelector getSelectedItem() {
                return table.getSelectedItem();
        }
+       
+       private Map<ElementSelector, Tuple2> resultCache = new HashMap<>();
 
        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();
                        
+                       Tuple2 selectionResult = resultCache.get(query);
+                       if (selectionResult != null) {
+                               // clear existing result and visualisation
+                               clearResultVisualisation(query);
+                       }
+                       
                        SelectionResult result = performQuery(query, model);
                        
+                       resultCache.put(query, new Tuple2(result, model));
+                       
                        if (result.tailCount != result.tailSize) {
                                showArbitraryResultWarning(query, result);
                        }
@@ -143,20 +159,32 @@ public class ElementSelectionView {
                                openDiagramWithSelection(display, result);
                        }
                        
-                       StructuredSelection selection = makeSelection(model, result);
+                       StructuredSelection selection = makeSelection(model, result, query.getColor());
                        selectionService.setPostSelection(selection);
                        sendSelectionEvent(selection);
                } catch (DatabaseException e) {
                        LOGGER.error("Element selection query failed", e);
                }
        }
+       
+       public void clearResultVisualisation(ElementSelector query) {
+               Tuple2 selectionResult = resultCache.get(query);
+               if (selectionResult != null) {
+                       SelectionResult result = (SelectionResult) selectionResult.c0;
+                       Resource model = (Resource) selectionResult.c1;
+                       eventBroker.send(SELECTION_EVENT_ID, makeSelection(model, result, null));
+               }
+       }
 
-       private StructuredSelection makeSelection(final Resource model, SelectionResult result) {
+       private StructuredSelection makeSelection(final Resource model, SelectionResult result, float[] fs) {
                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);
+                                       if (fs != null) {
+                                               selectionElement.setHint(DistrictFinderVisualisationParticipant.COLOR_KEY, new Color(fs[2], fs[1], fs[0], fs[3]));
+                                       }
                                        return selectionElement;
                                })
                                .toArray());
@@ -191,4 +219,11 @@ public class ElementSelectionView {
                });
                return result;
        }
+
+       public void dispose() {
+               for (Entry<ElementSelector, Tuple2> entry : resultCache.entrySet()) {
+                       ElementSelector query = entry.getKey();
+                       clearResultVisualisation(query);
+               }
+       }
 }