Allow PickSorter access to the PickRequest used for pick operation 13/3813/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 24 Jan 2020 12:39:05 +0000 (14:39 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 24 Jan 2020 12:39:05 +0000 (14:39 +0200)
New default-method on the PickSorter interface keeps the API/ABI
backwards compatible.

gitlab #454

Change-Id: Icef2d4b5349bbbbfb275d9c1e6a2ecaac8d610b7

bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/PickRequest.java
bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/impl/PickContextImpl.java

index 912e79cd9ce3e8caec89606ba61c495e797c0923..8d9d814d8eeb3c5599a08a4113e380752a1863b6 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2020 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -8,6 +8,7 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - gitlab #454
  *******************************************************************************/
 package org.simantics.g2d.diagram.handler;
 
@@ -143,8 +144,33 @@ public class PickRequest {
     }
 
     public static interface PickSorter {
+        /**
+         * Sorts the specified element list.
+         * 
+         * @param elements the element list to sort
+         */
         void sort(List<IElement> elements);
 
+        /**
+         * Extended interface-method that receives the pick request in addition to the
+         * picked elements to be sorted. Allows e.g. looking at the pick area in the
+         * sorter.
+         * 
+         * <p>
+         * The default implementation just invokes {@link #sort(List)} ignoring the pick
+         * request. The default implementation also keeps PickSorter API/ABI-compatible.
+         * 
+         * @param request  the original pick request that produced the hits listed in
+         *                 <code>elements</code>
+         * @param elements the element list to sort
+         * 
+         * @author Tuukka Lehtonen
+         * @since 1.43.0, 1.35.3
+         */
+        default void sort(PickRequest request, List<IElement> elements) {
+            sort(elements);
+        }
+
         //
         public static final PickSorter CONNECTIONS_LAST = new PickSorter() {
             @Override
@@ -204,12 +230,8 @@ public class PickRequest {
                     }
                     return minDistanceSq;
                 }
-                
-                @Override
-                public void sort(List<IElement> elements) {
-                    if (sorter != null)
-                        sorter.sort(elements);
 
+                private void sortConnections(List<IElement> elements) {
                     List<Pair<Double, IElement>> connections = new ArrayList<>(elements.size());
                     int tail = 0;
                     for (int i = 0; i < elements.size(); i++) {
@@ -217,7 +239,7 @@ public class PickRequest {
                         RouteGraphNode rgn = element.getHint(RouteGraphConnectionClass.KEY_RG_NODE);
                         if (rgn != null) {
                             double distanceSq = getDistanceSqToRouteGraphConnection(rgn, x, y);
-                            connections.add(new Pair<Double, IElement>(distanceSq, element));
+                            connections.add(Pair.make(distanceSq, element));
                         }
                         
                         if (rgn == null || i == elements.size() - 1) {
@@ -236,6 +258,20 @@ public class PickRequest {
                         }
                     }
                 }
+
+                @Override
+                public void sort(PickRequest request, List<IElement> elements) {
+                    if (sorter != null)
+                        sorter.sort(request, elements);
+                    sortConnections(elements);
+                }
+
+                @Override
+                public void sort(List<IElement> elements) {
+                    if (sorter != null)
+                        sorter.sort(elements);
+                    sortConnections(elements);
+                }
             }; 
         }
     }
index 6a77af9b80e0cb0463abe78f5d8e0962f9b66c2a..5d42cebb8970273841cdac2488f2fdf200a9e357 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2018 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2020 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -8,7 +8,7 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
- *     Semantum Oy - gitlab #66 - parallel/spatial optimization
+ *     Semantum Oy - gitlab #60, #454 - parallel/spatial optimization
  *******************************************************************************/
 package org.simantics.g2d.diagram.handler.impl;
 
@@ -97,7 +97,7 @@ public class PickContextImpl implements PickContext {
                if (!result.isEmpty()) {
                        if (request.pickSorter != null) {
                                List<IElement> elems = new ArrayList<>(result);
-                               request.pickSorter.sort(elems);
+                               request.pickSorter.sort(request, elems);
                                finalResult.addAll(elems);
                        } else {
                                finalResult.addAll(result);