+ @Override
+ public double getTranslateAmount() {
+ return 15 * super.getTranslateAmount();
+ }
+
+ @Override
+ public double limitScaleFactor(double scaleFactor) {
+ return scaleFactor;
+ }
+
+ @Override
+ @EventHandler(priority = 1)
+ public boolean handleEvent(CommandEvent e) {
+ super.update();
+ Command c = e.command;
+ boolean zoomDisabled = Boolean.TRUE.equals(getHint(KEY_DISABLE_ZOOM)) ? true : false;
+ // custom handling of zoom to fit & selection
+ if (Commands.ZOOM_TO_FIT.equals(c) && !zoomDisabled) {
+ boolean result = zoomToFit();
+ if (!result)
+ result = zoomToPage();
+ return result;
+ }
+ if (Commands.ZOOM_TO_PAGE.equals(c) && !zoomDisabled) {
+ return zoomToPage();
+ }
+ if (Commands.ZOOM_TO_SELECTION.equals(c) && !zoomDisabled) {
+ return zoomToSelection();
+ }
+ return super.handleEvent(e);
+ }
+
+ private boolean zoomToFit() {
+ 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 = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(d.getElements()));
+ 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;
+ }
+
+ protected static List<IElement> getMapElements(Collection<IElement> elements) {
+ List<IElement> justMapElements = elements.stream()
+ .filter(e -> "DistrictNetworkEdgeElement".equals(e.getElementClass().getId())
+ || "DistrictNetworkVertexElement".equals(e.getElementClass().getId()))
+ .collect(Collectors.toList());
+ return justMapElements;
+ }
+
+ private boolean zoomToPage() {
+ util.setTransform(new AffineTransform(2,0,0,2,270,270));
+ return true;
+ }
+
+ private boolean zoomToSelection() {
+ CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
+ if (boundsParticipant == null)
+ return false;
+
+ final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
+ if (controlBounds == null || controlBounds.isEmpty())
+ return false;
+
+ Set<IElement> selections = selection.getAllSelections();
+ if (selections == null || selections.isEmpty()) {
+ // no can do,
+ return zoomToPage();
+ }
+ Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(selections));
+
+ // Make sure that even empty bounds can be zoomed into.
+ org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
+
+ util.fitArea(controlBounds, diagramRect, null);
+ return true;
+ }
+