comparison visualizer/Util/src/com/sun/hotspot/igv/util/ExtendedSatelliteComponent.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.*;
27 import java.awt.event.*;
28 import javax.swing.JComponent;
29 import org.netbeans.api.visual.widget.Scene;
30
31 /**
32 * @author David Kaspar
33 * @author Thomas Wuerthinger
34 */
35 public class ExtendedSatelliteComponent extends JComponent implements MouseListener, MouseMotionListener, Scene.SceneListener, ComponentListener {
36
37 private Scene scene;
38 private Image image;
39 private int imageWidth;
40 private int imageHeight;
41
42 public ExtendedSatelliteComponent(Scene scene) {
43 this.scene = scene;
44 setDoubleBuffered(true);
45 setPreferredSize(new Dimension(128, 128));
46 addMouseListener(this);
47 addMouseMotionListener(this);
48 }
49
50 @Override
51 public void addNotify() {
52 super.addNotify();
53 scene.addSceneListener(this);
54 JComponent viewComponent = scene.getView();
55 if (viewComponent == null) {
56 viewComponent = scene.createView();
57 }
58 viewComponent.addComponentListener(this);
59 repaint();
60 }
61
62 @Override
63 public void removeNotify() {
64 scene.getView().removeComponentListener(this);
65 scene.removeSceneListener(this);
66 super.removeNotify();
67 }
68
69 public void update() {
70 this.image = null;
71 repaint();
72 }
73
74 @Override
75 public void paint(Graphics g) {
76 Graphics2D gr = (Graphics2D) g;
77 super.paint(g);
78 Rectangle bounds = scene.getBounds();
79 Dimension size = getSize();
80
81 double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
82 double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
83 double scale = Math.min(sx, sy);
84
85 int vw = (int) (scale * bounds.width);
86 int vh = (int) (scale * bounds.height);
87 int vx = (size.width - vw) / 2;
88 int vy = (size.height - vh) / 2;
89
90
91 if (image == null || vw != imageWidth || vh != imageHeight) {
92
93 imageWidth = vw;
94 imageHeight = vh;
95 image = this.createImage(imageWidth, imageHeight);
96 Graphics2D ig = (Graphics2D) image.getGraphics();
97 ig.scale(scale, scale);
98 scene.paint(ig);
99 }
100
101 gr.drawImage(image, vx, vy, this);
102
103 JComponent component = scene.getView();
104 double zoomFactor = scene.getZoomFactor();
105 Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
106 if (viewRectangle != null) {
107 Rectangle window = new Rectangle(
108 (int) ((double) viewRectangle.x * scale / zoomFactor),
109 (int) ((double) viewRectangle.y * scale / zoomFactor),
110 (int) ((double) viewRectangle.width * scale / zoomFactor),
111 (int) ((double) viewRectangle.height * scale / zoomFactor));
112 window.translate(vx, vy);
113 gr.setColor(new Color(200, 200, 200, 128));
114 gr.fill(window);
115 gr.setColor(Color.BLACK);
116 gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
117 }
118 }
119
120 @Override
121 public void mouseClicked(MouseEvent e) {
122 }
123
124 @Override
125 public void mousePressed(MouseEvent e) {
126 moveVisibleRect(e.getPoint());
127 }
128
129 @Override
130 public void mouseReleased(MouseEvent e) {
131 moveVisibleRect(e.getPoint());
132 }
133
134 @Override
135 public void mouseEntered(MouseEvent e) {
136 }
137
138 @Override
139 public void mouseExited(MouseEvent e) {
140 }
141
142 @Override
143 public void mouseDragged(MouseEvent e) {
144 moveVisibleRect(e.getPoint());
145 }
146
147 @Override
148 public void mouseMoved(MouseEvent e) {
149 }
150
151 private void moveVisibleRect(Point center) {
152 JComponent component = scene.getView();
153 if (component == null) {
154 return;
155 }
156 double zoomFactor = scene.getZoomFactor();
157 Rectangle bounds = scene.getBounds();
158 Dimension size = getSize();
159
160 double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
161 double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
162 double scale = Math.min(sx, sy);
163
164 int vw = (int) (scale * bounds.width);
165 int vh = (int) (scale * bounds.height);
166 int vx = (size.width - vw) / 2;
167 int vy = (size.height - vh) / 2;
168
169 int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
170 int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);
171
172 Rectangle visibleRect = component.getVisibleRect();
173 visibleRect.x = cx - visibleRect.width / 2;
174 visibleRect.y = cy - visibleRect.height / 2;
175 component.scrollRectToVisible(visibleRect);
176
177 }
178
179 @Override
180 public void sceneRepaint() {
181 }
182
183 @Override
184 public void sceneValidating() {
185 }
186
187 @Override
188 public void sceneValidated() {
189 }
190
191 @Override
192 public void componentResized(ComponentEvent e) {
193 repaint();
194 }
195
196 @Override
197 public void componentMoved(ComponentEvent e) {
198 repaint();
199 }
200
201 @Override
202 public void componentShown(ComponentEvent e) {
203 }
204
205 @Override
206 public void componentHidden(ComponentEvent e) {
207 }
208 }