comparison visualizer/Util/src/com/sun/hotspot/igv/util/BoundedZoomAction.java @ 4512:015fb895586b

Moved visualizer to new directory.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 07 Feb 2012 22:41:09 +0100
parents
children
comparison
equal deleted inserted replaced
4511:6cb549627941 4512:015fb895586b
1 /*
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 package com.sun.hotspot.igv.util;
25
26 import java.awt.Container;
27 import java.awt.Dimension;
28 import java.awt.Point;
29 import java.awt.Rectangle;
30 import javax.swing.JComponent;
31 import javax.swing.JScrollPane;
32 import org.netbeans.api.visual.action.WidgetAction;
33 import org.netbeans.api.visual.action.WidgetAction.State;
34 import org.netbeans.api.visual.action.WidgetAction.WidgetMouseWheelEvent;
35 import org.netbeans.api.visual.animator.SceneAnimator;
36 import org.netbeans.api.visual.widget.Scene;
37 import org.netbeans.api.visual.widget.Widget;
38
39 /**
40 *
41 * @author Thomas Wuerthinger
42 */
43 public class BoundedZoomAction extends WidgetAction.Adapter {
44
45 private double minFactor = 0.0;
46 private double maxFactor = Double.MAX_VALUE;
47 private double zoomMultiplier;
48 private boolean useAnimator;
49
50 public BoundedZoomAction(double zoomMultiplier, boolean useAnimator) {
51 this.zoomMultiplier = zoomMultiplier;
52 this.useAnimator = useAnimator;
53 }
54
55 public double getMinFactor() {
56 return minFactor;
57 }
58
59 public void setMinFactor(double d) {
60 minFactor = d;
61 }
62
63 public double getMaxFactor() {
64 return maxFactor;
65 }
66
67 public void setMaxFactor(double d) {
68 maxFactor = d;
69 }
70
71 private JScrollPane findScrollPane(JComponent component) {
72 for (;;) {
73 if (component == null) {
74 return null;
75 }
76 if (component instanceof JScrollPane) {
77 return ((JScrollPane) component);
78 }
79 Container parent = component.getParent();
80 if (!(parent instanceof JComponent)) {
81 return null;
82 }
83 component = (JComponent) parent;
84 }
85 }
86
87 @Override
88 public State mouseWheelMoved(Widget widget, WidgetMouseWheelEvent event) {
89 final Scene scene = widget.getScene();
90 int amount = event.getWheelRotation();
91 JScrollPane scrollPane = findScrollPane(scene.getView());
92 Point viewPosition = null;
93 Point mouseLocation = scene.convertSceneToView(event.getPoint());
94 int xOffset = 0;
95 int yOffset = 0;
96 Rectangle bounds = new Rectangle(scene.getBounds());
97 Dimension componentSize = new Dimension(scene.getView().getPreferredSize());
98
99 if (scrollPane != null) {
100 viewPosition = new Point(scrollPane.getViewport().getViewPosition());
101 xOffset = (mouseLocation.x - viewPosition.x);
102 yOffset = (mouseLocation.y - viewPosition.y);
103 viewPosition.x += xOffset;
104 viewPosition.y += yOffset;
105 }
106
107 if (useAnimator) {
108 SceneAnimator sceneAnimator = scene.getSceneAnimator();
109 synchronized (sceneAnimator) {
110 double zoom = sceneAnimator.isAnimatingZoomFactor() ? sceneAnimator.getTargetZoomFactor() : scene.getZoomFactor();
111 while (amount > 0 && zoom / zoomMultiplier >= minFactor && zoom / zoomMultiplier <= maxFactor) {
112 zoom /= zoomMultiplier;
113 if (viewPosition != null) {
114 viewPosition.x /= zoomMultiplier;
115 viewPosition.y /= zoomMultiplier;
116 bounds.width /= zoomMultiplier;
117 bounds.height /= zoomMultiplier;
118 componentSize.width /= zoomMultiplier;
119 componentSize.height /= zoomMultiplier;
120 }
121 amount--;
122 }
123 while (amount < 0 && zoom * zoomMultiplier >= minFactor && zoom * zoomMultiplier <= maxFactor) {
124 zoom *= zoomMultiplier;
125 if (viewPosition != null) {
126 viewPosition.x *= zoomMultiplier;
127 viewPosition.y *= zoomMultiplier;
128 bounds.width *= zoomMultiplier;
129 bounds.height *= zoomMultiplier;
130 componentSize.width *= zoomMultiplier;
131 componentSize.height *= zoomMultiplier;
132 }
133 amount++;
134 }
135 sceneAnimator.animateZoomFactor(zoom);
136 }
137 } else {
138 double zoom = scene.getZoomFactor();
139 while (amount > 0 && zoom / zoomMultiplier >= minFactor && zoom / zoomMultiplier <= maxFactor) {
140 zoom /= zoomMultiplier;
141 if (viewPosition != null) {
142 viewPosition.x /= zoomMultiplier;
143 viewPosition.y /= zoomMultiplier;
144 bounds.width /= zoomMultiplier;
145 bounds.height /= zoomMultiplier;
146 componentSize.width /= zoomMultiplier;
147 componentSize.height /= zoomMultiplier;
148 }
149 amount--;
150 }
151 while (amount < 0 && zoom * zoomMultiplier >= minFactor && zoom * zoomMultiplier <= maxFactor) {
152 zoom *= zoomMultiplier;
153 if (viewPosition != null) {
154 viewPosition.x *= zoomMultiplier;
155 viewPosition.y *= zoomMultiplier;
156 bounds.width *= zoomMultiplier;
157 bounds.height *= zoomMultiplier;
158 componentSize.width *= zoomMultiplier;
159 componentSize.height *= zoomMultiplier;
160 }
161 amount++;
162 }
163 scene.setZoomFactor(zoom);
164 }
165
166 if (scrollPane != null) {
167 scene.validate(); // Call validate to update size of scene
168 Dimension size = scrollPane.getViewport().getExtentSize();
169 viewPosition.x -= xOffset;
170 viewPosition.y -= yOffset;
171 scene.resolveBounds(scene.getLocation(), bounds);
172 scene.getView().setPreferredSize(componentSize);
173 scene.getView().revalidate();
174 scene.getView().addNotify();
175 scrollPane.getViewport().setViewPosition(viewPosition);
176 }
177
178 return WidgetAction.State.CONSUMED;
179 }
180 }