]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java
Add refresh to context menu in district network breakdown view
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DistrictPanZoomRotateHandler.java
index f9d9fccca449b8a265d4f0983d41952138cd39e8..fecc17548e98c5f995eef4d912112c354007a5f0 100644 (file)
@@ -7,6 +7,9 @@ import java.util.Set;
 import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
 import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;
+import org.simantics.g2d.diagram.DiagramHints;
+import org.simantics.g2d.diagram.DiagramUtils;
+import org.simantics.g2d.diagram.IDiagram;
 import org.simantics.g2d.diagram.participant.Selection;
 import org.simantics.g2d.element.ElementUtils;
 import org.simantics.g2d.element.IElement;
@@ -74,14 +77,36 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
     }
 
     private boolean zoomToFit() {
-        return false;
+        CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
+        if (boundsParticipant == null)
+            return false;
+
+        final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
+        if (controlBounds == null || controlBounds.isEmpty())
+            return false;
+
+        IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
+        if (d == null)
+            return false;
+
+        Rectangle2D diagramRect = DiagramUtils.getContentRect(d);
+        if (diagramRect == null)
+            return false;
+        if (diagramRect.isEmpty())
+            return false;
+
+        org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
+        
+        // System.out.println("zoomToFit(" + controlArea + ", " + diagramRect + ")");
+        util.fitArea(controlBounds, diagramRect, null);
+
+        return true;
     }
 
     private boolean zoomToPage() {
         int currentZoomLevel = MapScalingTransform.zoomLevel(util.getTransform());
         
         util.setTransform(new AffineTransform(2,0,0,2,270,270));
-//        util.setTransform(new AffineTransform(MapScalingTransform.INSTANCE));
         return true;
     }
     
@@ -95,6 +120,10 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
             return false;
         
         Set<IElement> selections = selection.getAllSelections();
+        if (selections == null || selections.isEmpty()) {
+            // no can do, 
+            return zoomToPage();
+        }
         Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections);
         
         // Make sure that even empty bounds can be zoomed into.
@@ -136,26 +165,34 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
         @Override
         public boolean mouseWheelMoved(MouseWheelMovedEvent me) {
             if (navigationEnabled && zoomEnabled) {
-                double z;
-                if (me.wheelRotation > 0) {
-                    z = DISTRICT_TRANSLATE_AMOUNT;
+                // check if min/max zoom already
+                AffineTransform transform = getTransform();
+                int zoomLevel = MapScalingTransform.zoomLevel(transform);
+                
+                if (0 < zoomLevel && zoomLevel < 20 || (zoomLevel == 0 && me.wheelRotation > 0) || (zoomLevel == 20 && me.wheelRotation < 0)) {
+                    double z;
+                    if (me.wheelRotation > 0) {
+                        z = DISTRICT_TRANSLATE_AMOUNT;
+                    } else {
+                        z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
+                    }
+                    //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
+                    //double z = 1 - scroll;
+                    double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
+                    double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
+                    dx = dx * (1 - z);
+                    dy = dy * (1 - z);
+//                    double limitedScale = limitScaleFactor(z);
+//                    if (limitedScale != 1.0) {
+                        translate(dx, dy);
+                        scale(z, z);
+                        transformChanged();
+                        dropQuality();
+                        repaint();
+//                    }
                 } else {
-                    z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
+                    // max zoom level reached
                 }
-                //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
-                //double z = 1 - scroll;
-                double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
-                double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
-                dx = dx * (1 - z);
-                dy = dy * (1 - z);
-//                double limitedScale = limitScaleFactor(z);
-//                if (limitedScale != 1.0) {
-                    translate(dx, dy);
-                    scale(z, z);
-                    transformChanged();
-                    dropQuality();
-                    repaint();
-//                }
             }
             return false;
         }