001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.widget;
042:
043: import org.netbeans.api.visual.widget.Scene;
044:
045: import javax.swing.*;
046: import java.awt.*;
047: import java.awt.geom.AffineTransform;
048: import java.awt.event.*;
049:
050: /**
051: * @author David Kaspar
052: */
053: public final class SatelliteComponent extends JComponent implements
054: MouseListener, MouseMotionListener, Scene.SceneListener,
055: ComponentListener {
056:
057: private Scene scene;
058:
059: public SatelliteComponent(Scene scene) {
060: this .scene = scene;
061: setDoubleBuffered(true);
062: setPreferredSize(new Dimension(128, 128));
063: addMouseListener(this );
064: addMouseMotionListener(this );
065: }
066:
067: public void addNotify() {
068: super .addNotify();
069: scene.addSceneListener(this );
070: JComponent viewComponent = scene.getView();
071: if (viewComponent == null)
072: viewComponent = scene.createView();
073: viewComponent.addComponentListener(this );
074: repaint();
075: }
076:
077: public void removeNotify() {
078: scene.getView().removeComponentListener(this );
079: scene.removeSceneListener(this );
080: super .removeNotify();
081: }
082:
083: public void paint(Graphics g) {
084: Graphics2D gr = (Graphics2D) g;
085: super .paint(g);
086: Rectangle bounds = scene.getBounds();
087: Dimension size = getSize();
088:
089: double sx = bounds.width > 0 ? (double) size.width
090: / bounds.width : 0.0;
091: double sy = bounds.width > 0 ? (double) size.height
092: / bounds.height : 0.0;
093: double scale = Math.min(sx, sy);
094:
095: int vw = (int) (scale * bounds.width);
096: int vh = (int) (scale * bounds.height);
097: int vx = (size.width - vw) / 2;
098: int vy = (size.height - vh) / 2;
099:
100: AffineTransform previousTransform = gr.getTransform();
101: gr.translate(vx, vy);
102: gr.scale(scale, scale);
103:
104: scene.paint(gr);
105: gr.setTransform(previousTransform);
106:
107: JComponent component = scene.getView();
108: double zoomFactor = scene.getZoomFactor();
109: Rectangle viewRectangle = component != null ? component
110: .getVisibleRect() : null;
111: if (viewRectangle != null) {
112: Rectangle window = new Rectangle(
113: (int) ((double) viewRectangle.x * scale / zoomFactor),
114: (int) ((double) viewRectangle.y * scale / zoomFactor),
115: (int) ((double) viewRectangle.width * scale / zoomFactor),
116: (int) ((double) viewRectangle.height * scale / zoomFactor));
117: window.translate(vx, vy);
118: // Area area = new Area (new Rectangle (vx, vy, vw, vh));
119: // area.subtract (new Area (window));
120: gr.setColor(new Color(200, 200, 200, 128));
121: gr.fill(window);
122: gr.setColor(Color.BLACK);
123: gr.drawRect(window.x, window.y, window.width - 1,
124: window.height - 1);
125: }
126: }
127:
128: public void mouseClicked(MouseEvent e) {
129: }
130:
131: public void mousePressed(MouseEvent e) {
132: moveVisibleRect(e.getPoint());
133: }
134:
135: public void mouseReleased(MouseEvent e) {
136: moveVisibleRect(e.getPoint());
137: }
138:
139: public void mouseEntered(MouseEvent e) {
140: }
141:
142: public void mouseExited(MouseEvent e) {
143: }
144:
145: public void mouseDragged(MouseEvent e) {
146: moveVisibleRect(e.getPoint());
147: }
148:
149: public void mouseMoved(MouseEvent e) {
150: }
151:
152: private void moveVisibleRect(Point center) {
153: JComponent component = scene.getView();
154: if (component == null)
155: return;
156: double zoomFactor = scene.getZoomFactor();
157: Rectangle bounds = scene.getBounds();
158: Dimension size = getSize();
159:
160: double sx = bounds.width > 0 ? (double) size.width
161: / bounds.width : 0.0;
162: double sy = bounds.width > 0 ? (double) size.height
163: / bounds.height : 0.0;
164: double scale = Math.min(sx, sy);
165:
166: int vw = (int) (scale * bounds.width);
167: int vh = (int) (scale * bounds.height);
168: int vx = (size.width - vw) / 2;
169: int vy = (size.height - vh) / 2;
170:
171: int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
172: int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);
173:
174: Rectangle visibleRect = component.getVisibleRect();
175: visibleRect.x = cx - visibleRect.width / 2;
176: visibleRect.y = cy - visibleRect.height / 2;
177: component.scrollRectToVisible(visibleRect);
178:
179: }
180:
181: public void sceneRepaint() {
182: repaint();
183: }
184:
185: public void sceneValidating() {
186: }
187:
188: public void sceneValidated() {
189: }
190:
191: public void componentResized(ComponentEvent e) {
192: repaint();
193: }
194:
195: public void componentMoved(ComponentEvent e) {
196: repaint();
197: }
198:
199: public void componentShown(ComponentEvent e) {
200: }
201:
202: public void componentHidden(ComponentEvent e) {
203: }
204: }
|