| | | | Browse by category |
Question
How do I change the behavior of the IlvDiagrammerViewBar
's zoom buttons to zoom and focus on the selected graphic objects?
Answer
To override the behavior of the zoom buttons, you can set a custom action handler and redefine its perform method. Inside the perform method, you can get the bounding box of the selected graphic objects and use the box's dimensions and coordinates to help translate the view where the objects are focused at the center. Then you can zoom the view by calling the zoomIn()
or zoomOut()
methods of IlvDiagrammer
. Here is an example of an action handler that focuses the selected graphic objects at the center while zooming in or out of the view:
public class ZoomActionHandler extends IlvDiagrammerAction.Handler { private boolean zoomIn; // The zoomIn parameter determines whether to zoom in or out the view. // true = zoom in // false = zoom out public ZoomActionHandler(boolean zoomIn) { super(); this.zoomIn = zoomIn; } public void perform(IlvDiagrammerAction action, IlvDiagrammer diagrammer, ActionEvent event) throws Exception { IlvManagerView managerView = diagrammer.getView(); Iterator it = diagrammer.getSelectedObjects(); IlvGraphic selGraphic = null; IlvRect rect = null; while (it.hasNext()) { IlvDefaultSDMNode sdmNode = (IlvDefaultSDMNode)it.next(); selGraphic = diagrammer.getEngine().getGraphic(sdmNode, false); // Getting the transformer that allows converting from the // coordinate system of this manager to the coordinate system // of the top most manager. IlvTransformer t = ((IlvManager)selGraphic.getGraphicBag()).getDrawingTransformer(managerView); // Get the bounding box of the selected graphic object. If there are // more than one selected graphic objects then include their bounding // boxes to form one bounding box that encompasses all the selected // graphic objects. IlvRect graphicRect = selGraphic.boundingBox(t); if (rect == null) { rect = graphicRect; } else { rect.add(graphicRect); } } if (selGraphic != null) { // Getting information on the visible area of the view. Rectangle visibleArea = managerView.visibleRect(); float width = (float)visibleArea.getWidth(); float height = (float)visibleArea.getHeight(); // Get the center coordinates of the bounding box of all selected graphic // objects. float centerX = rect.x + rect.width / 2; float centerY = rect.y + rect.height / 2; // Performing centering... // Note that the 1st and 2nd paremeters are the deltaX and deltaY // needed to get to this centerX and centerY point // and NOT the actual X and Y coordinates of the point // you want to center in. // // Also note that all coordinates are in the View coordinate system. managerView.translate((width / 2 - centerX), (height / 2 - centerY), false); } if (zoomIn) { diagrammer.zoomIn(); } else { diagrammer.zoomOut(); } } }
To set the custom handler for the zoom in and zoom out actions, call:
IlvDiagrammerAction.zoomIn.setHandler(new ZoomActionHandler(true)); IlvDiagrammerAction.zoomOut.setHandler(new ZoomActionHandler(false));
Here is a sample that demonstrates this technique.