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.view; 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 DiagramScene scene; 39 private Image image; 40 private int imageWidth; 41 private int imageHeight; 42 43 public ExtendedSatelliteComponent(DiagramScene scene) { 44 this.scene = scene; 45 setDoubleBuffered(true); 46 setPreferredSize(new Dimension(128, 128)); 47 addMouseListener(this); 48 addMouseMotionListener(this); 49 } 50 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 public void removeNotify() { 63 scene.getView().removeComponentListener(this); 64 scene.removeSceneListener(this); 65 super.removeNotify(); 66 } 67 68 public void update() { 69 this.image = null; 70 if (this.isVisible()) { 71 repaint(); 72 revalidate(); 73 validate(); 74 } 75 } 76 77 public void paint(Graphics g) { 78 Graphics2D gr = (Graphics2D) g; 79 super.paint(g); 80 Rectangle bounds = scene.getBounds(); 81 Dimension size = getSize(); 82 83 double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0; 84 double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0; 85 double scale = Math.min(sx, sy); 86 87 int vw = (int) (scale * bounds.width); 88 int vh = (int) (scale * bounds.height); 89 int vx = (size.width - vw) / 2; 90 int vy = (size.height - vh) / 2; 91 92 93 if (image == null || vw != imageWidth || vh != imageHeight) { 94 95 imageWidth = vw; 96 imageHeight = vh; 97 image = this.createImage(imageWidth, imageHeight); 98 Graphics2D ig = (Graphics2D) image.getGraphics(); 99 ig.scale(scale, scale); 100 scene.setRealZoomFactor(scale); 101 scene.paint(ig); 102 scene.setRealZoomFactor(0.0); 103 } 104 105 gr.drawImage(image, vx, vy, this); 106 107 JComponent component = scene.getView(); 108 double zoomFactor = scene.getZoomFactor(); 109 Rectangle viewRectangle = component != null ? component.getVisibleRect() : null; 110 if (viewRectangle != null) { 111 Rectangle window = new Rectangle( 112 (int) ((double) viewRectangle.x * scale / zoomFactor), 113 (int) ((double) viewRectangle.y * scale / zoomFactor), 114 (int) ((double) viewRectangle.width * scale / zoomFactor), 115 (int) ((double) viewRectangle.height * scale / zoomFactor)); 116 window.translate(vx, vy); 117 gr.setColor(new Color(200, 200, 200, 128)); 118 gr.fill(window); 119 gr.setColor(Color.BLACK); 120 gr.drawRect(window.x, window.y, window.width - 1, window.height - 1); 121 } 122 } 123 124 public void mouseClicked(MouseEvent e) { 125 } 126 127 public void mousePressed(MouseEvent e) { 128 moveVisibleRect(e.getPoint()); 129 } 130 131 public void mouseReleased(MouseEvent e) { 132 moveVisibleRect(e.getPoint()); 133 } 134 135 public void mouseEntered(MouseEvent e) { 136 } 137 138 public void mouseExited(MouseEvent e) { 139 } 140 141 public void mouseDragged(MouseEvent e) { 142 moveVisibleRect(e.getPoint()); 143 } 144 145 public void mouseMoved(MouseEvent e) { 146 } 147 148 private void moveVisibleRect(Point center) { 149 JComponent component = scene.getView(); 150 if (component == null) { 151 return; 152 } 153 double zoomFactor = scene.getZoomFactor(); 154 Rectangle bounds = scene.getBounds(); 155 Dimension size = getSize(); 156 157 double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0; 158 double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0; 159 double scale = Math.min(sx, sy); 160 161 int vw = (int) (scale * bounds.width); 162 int vh = (int) (scale * bounds.height); 163 int vx = (size.width - vw) / 2; 164 int vy = (size.height - vh) / 2; 165 166 int cx = (int) ((double) (center.x - vx) / scale * zoomFactor); 167 int cy = (int) ((double) (center.y - vy) / scale * zoomFactor); 168 169 Rectangle visibleRect = component.getVisibleRect(); 170 visibleRect.x = cx - visibleRect.width / 2; 171 visibleRect.y = cy - visibleRect.height / 2; 172 component.scrollRectToVisible(visibleRect); 173 174 this.repaint(); 175 } 176 177 public void sceneRepaint() { 178 //repaint (); 179 } 180 181 public void sceneValidating() { 182 } 183 184 public void sceneValidated() { 185 } 186 187 public void componentResized(ComponentEvent e) { 188 repaint(); 189 } 190 191 public void componentMoved(ComponentEvent e) { 192 repaint(); 193 } 194 195 public void componentShown(ComponentEvent e) { 196 } 197 198 public void componentHidden(ComponentEvent e) { 199 } 200} 201