We want to add a toolbar to our application with some actions to support zooming operations in the view. Zooming in and out, and a fit content action also, are natural actions in a viewer type application.
Figure 1.5, “SimpleGraphViewer4 with toolbar and zooming actions” shows the GUI of our SimpleGraphViewer4 application with the new toolbar.
Creating the toolbar and adding the actions is standard Java Swing code as presented in Example 1.8, “Creating a toolbar with zooming-related actions”.
Example 1.8. Creating a toolbar with zooming-related actions
/** Creates a toolbar for this demo. */ protected JToolBar createToolBar() { JToolBar toolbar = new JToolBar(); toolbar.add(new FitContent(getView())); toolbar.add(new Zoom(getView(), 1.25)); toolbar.add(new Zoom(getView(), 0.8)); return toolbar; }
We add the call to create the toolbar in our createApplicationFrame method.
Example 1.9. Adding the toolbar to the application GUI
/** Creates a JFrame that will show the demo graph. */ private JFrame createApplicationFrame(Dimension size, String title, JComponent view) { JPanel panel = new JPanel(new BorderLayout()); panel.setPreferredSize(size); // Add the given view to the panel. panel.add(view, BorderLayout.CENTER); // Add a toolbar with some actions to the panel, too. panel.add(createToolBar(), BorderLayout.NORTH); ...
Example 1.10, “Zoom class” presents the Zoom class, an Action implementation that enables both zooming in and zooming out in a given Graph2DView.
Example 1.10. Zoom class
/** Action that applies a specified zoom level to the given view. */ protected static class Zoom extends AbstractAction { Graph2DView view; double factor; public Zoom(Graph2DView view, double factor) { super("Zoom " + (factor > 1.0 ? "In" : "Out")); this.view = view; this.factor = factor; this.putValue(Action.SHORT_DESCRIPTION, "Zoom " + (factor > 1.0 ? "In" : "Out")); } public void actionPerformed(ActionEvent e) { view.setZoom(view.getZoom() * factor); // Adjusts the size of the view's world rectangle. The world rectangle // defines the region of the canvas that is accessible by using the // scrollbars of the view. Rectangle box = view.getGraph2D().getBoundingBox(); view.setWorldRect(box.x - 20, box.y - 20, box.width + 40, box.height + 40); view.updateView(); } }
In the actionPerformed method the view's current zoom value is multiplied by the double parameter given at instantiation. If that parameter is greater than 1.0 (which is the initial value for Graph2DView's zoom property), each time the action is triggered we zoom in, otherwise we zoom out.
In addition, the view's world rectangle is also adjusted, keeping Graph2DView's
scrollbars properly in sync, and an immediate repaint of the view (method updateView())
is triggered.
In Figure 1.6, “Zooming in on the graph” you can see the result of zooming in on the graph by clicking the "Zoom In" button twice. Note that the scrollbars have appeared in order to provide the user with a means to navigate all of the world rectangle.
Example 1.11, “FitContent class” presents the FitContent class, an Action implementation that allows to conveniently present the entire contents of a given Graph2DView.
Example 1.11. FitContent class
/** Action that fits the content nicely inside the view. */ protected static class FitContent extends AbstractAction { Graph2DView view; public FitContent(Graph2DView view) { super("Fit Content"); this.view = view; this.putValue(Action.SHORT_DESCRIPTION, "Fit Content"); } public void actionPerformed(ActionEvent e) { view.fitContent(); view.updateView(); } }
Fit content is a feature provided by Graph2DView itself. It changes the view's zoom level (and at the same time also the viewpoint, i.e., the world coordinate that is at the viewport's origin) such that the view's current graph is displayed in its entirety, centered in the viewport.
The FitContent class's actionPerformed method shows the necessary code that also triggers an immediate repaint of the view (method updateView).
Invoking the fitContent method of a Graph2DView when there is a graph that is "small" compared to the available viewport area, the view's zoom level will not change beyond the value 1.0. In other words, a graph that easily fits inside the available viewport area is presented in its original size at maximum.
When we trigger the zoom out action multiple times, the rendering of the graph suddenly changes. This is due to a feature of Graph2DView which switches to a less-detail rendering strategy beyond a certain threshold for its zoom property. The default value for this threshold is 0.3.
Setting a value of 0.0 for this threshold, as shown in Example 1.12, “Disabling less-detail rendering in the view”, effectively prevents the view from switching to less-detail rendering.
You will find related information in the yFiles for Java Developer's Guide:
In the yFiles for Java source code demos:
Copyright ©2008-2009, yWorks GmbH. All rights reserved. |