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