001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: StructurePanelPanWindow.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.old;
009:
010: import java.awt.*;
011: import java.awt.event.MouseEvent;
012: import java.awt.event.MouseMotionListener;
013: import java.awt.image.BufferedImage;
014:
015: import javax.swing.*;
016:
017: import com.uwyn.rife.swing.BorderEtched;
018:
019: public class StructurePanelPanWindow extends JWindow implements
020: MouseMotionListener {
021: private StructurePanel mStructurePanel = null;
022: private JComponent mRelativeComponent = null;
023: private Thumbnail mThumbnail = null;
024: private int mViewWidth = 0;
025: private int mViewHeight = 0;
026: private ViewRectangle mViewRectangle = null;
027: private Point mInitialViewPosition = null;
028: private Point mStartPoint = null;
029:
030: public StructurePanelPanWindow(StructurePanel structurePanel,
031: JComponent relativeComponent, Point startPoint) {
032: mStructurePanel = structurePanel;
033: mRelativeComponent = relativeComponent;
034: mStartPoint = startPoint;
035:
036: mViewWidth = (int) mStructurePanel.getScrollPane()
037: .getViewport().getViewRect().getWidth();
038: mViewHeight = (int) mStructurePanel.getScrollPane()
039: .getViewport().getViewRect().getHeight();
040: mInitialViewPosition = mStructurePanel.getScrollPane()
041: .getViewport().getViewPosition();
042:
043: mThumbnail = new Thumbnail();
044: mViewRectangle = new ViewRectangle();
045:
046: getContentPane().setLayout(null);
047: getContentPane().add(mViewRectangle);
048: getContentPane().add(mThumbnail);
049: mViewRectangle.updateLocation();
050:
051: setSize(mThumbnail.getSize());
052:
053: updateLocation();
054:
055: setVisible(true);
056: }
057:
058: private void updateLocation() {
059: Point relative_component_location = mRelativeComponent
060: .getLocationOnScreen();
061:
062: int base_offset_x = relative_component_location.x
063: + (mRelativeComponent.getWidth() / 2);
064: int base_offset_y = relative_component_location.y
065: + (mRelativeComponent.getHeight() / 2);
066:
067: Point view_rectangle_center = mViewRectangle.getCenterPoint();
068:
069: base_offset_x -= view_rectangle_center.x;
070: base_offset_y -= view_rectangle_center.y;
071:
072: Dimension screen_size = Toolkit.getDefaultToolkit()
073: .getScreenSize();
074: if (base_offset_x + getWidth() > screen_size.width) {
075: int new_offset_x = screen_size.width - getWidth();
076: mStartPoint.x += new_offset_x - base_offset_x;
077: base_offset_x = new_offset_x;
078: }
079: if (base_offset_y + getHeight() > screen_size.height) {
080: int new_offset_y = screen_size.height - getHeight();
081: mStartPoint.y += new_offset_y - base_offset_y;
082: base_offset_y = new_offset_y;
083: }
084:
085: setLocation(base_offset_x, base_offset_y);
086: }
087:
088: private class ViewRectangle extends JPanel {
089: private int mWidth = 0;
090: private int mHeight = 0;
091: private int mX = 0;
092: private int mY = 0;
093:
094: public ViewRectangle() {
095: super ();
096: setOpaque(false);
097: mWidth = (int) (mViewWidth * mThumbnail.getScaleFactor());
098: mHeight = (int) (mViewHeight * mThumbnail.getScaleFactor());
099: ViewRectangle.this .setSize(mWidth, mHeight);
100: }
101:
102: public Point getCenterPoint() {
103: return new Point(mX + (mWidth / 2), mY + (mHeight / 2));
104: }
105:
106: protected void paintComponent(Graphics g) {
107: super .paintComponent(g);
108:
109: g.setColor(Color.black);
110: g.drawRect(0, 0, mWidth - 1, mHeight - 1);
111: }
112:
113: public void updateLocation() {
114: Point view_position = mStructurePanel.getScrollPane()
115: .getViewport().getViewPosition();
116: mX = (int) (view_position.x * mThumbnail.getScaleFactor() + mThumbnail
117: .getInsets().left);
118: mY = (int) (view_position.y * mThumbnail.getScaleFactor() + mThumbnail
119: .getInsets().top);
120: ViewRectangle.this .setLocation(mX, mY);
121: }
122: }
123:
124: private class Thumbnail extends JLabel {
125: private int mWidth = 200;
126: private int mHeight = 200;
127: private float mScaleFactor = 0;
128: private BufferedImage mThumbnailImage = null;
129: private BorderEtched mBorder = null;
130:
131: public Thumbnail() {
132: float width_ratio = 200f / (float) mStructurePanel
133: .getWidth();
134: float height_ratio = 200f / (float) mStructurePanel
135: .getHeight();
136:
137: if (width_ratio < height_ratio) {
138: mHeight = (int) (200 * (width_ratio / height_ratio));
139: mScaleFactor = width_ratio;
140: } else {
141: mWidth = (int) (200 * (height_ratio / width_ratio));
142: mScaleFactor = height_ratio;
143: }
144:
145: mThumbnailImage = new BufferedImage(mWidth, mHeight,
146: BufferedImage.TYPE_INT_ARGB);
147: Graphics2D g2d = mThumbnailImage.createGraphics();
148: ElementStyle.setRenderingHints(g2d, mScaleFactor);
149: g2d.setColor(Color.white);
150: g2d.fillRect(0, 0, mWidth, mHeight);
151: g2d.scale(mScaleFactor, mScaleFactor);
152:
153: Component[] elements = mStructurePanel.getComponents();
154: Element element = null;
155: for (int i = elements.length - 1; i >= 0; i--) {
156: element = (Element) elements[i];
157: g2d.translate(element.getX(), element.getY());
158: element.drawElement(g2d);
159: g2d.translate(-1 * element.getX(), -1 * element.getY());
160: }
161:
162: setIcon(new ImageIcon(mThumbnailImage));
163: mBorder = new BorderEtched(BorderEtched.RAISED);
164: setBorder(mBorder);
165: Insets insets = Thumbnail.this .getInsets();
166: Thumbnail.this .setSize(mWidth + insets.left + insets.right,
167: mHeight + insets.top + insets.bottom);
168: }
169:
170: public float getScaleFactor() {
171: return mScaleFactor;
172: }
173: }
174:
175: public void mouseClicked(MouseEvent e) {
176: }
177:
178: public void mousePressed(MouseEvent e) {
179: }
180:
181: public void mouseReleased(MouseEvent e) {
182: }
183:
184: public void mouseEntered(MouseEvent e) {
185: }
186:
187: public void mouseDragged(MouseEvent e) {
188: Point current_point = e.getPoint();
189: int offset_x = (int) ((current_point.x - mStartPoint.x) / mThumbnail
190: .getScaleFactor());
191: int offset_y = (int) ((current_point.y - mStartPoint.y) / mThumbnail
192: .getScaleFactor());
193: int new_view_position_x = 0;
194: int new_view_position_y = 0;
195:
196: if (mViewWidth < mStructurePanel.getWidth()) {
197: new_view_position_x = mInitialViewPosition.x + offset_x;
198: if (new_view_position_x + mViewWidth > mStructurePanel
199: .getWidth()) {
200: new_view_position_x = mStructurePanel.getWidth()
201: - mViewWidth;
202: }
203: if (new_view_position_x < 0) {
204: new_view_position_x = 0;
205: }
206: }
207:
208: if (mViewHeight < mStructurePanel.getHeight()) {
209: new_view_position_y = mInitialViewPosition.y + offset_y;
210: if (new_view_position_y + mViewHeight > mStructurePanel
211: .getHeight()) {
212: new_view_position_y = mStructurePanel.getHeight()
213: - mViewHeight;
214: }
215: if (new_view_position_y < 0) {
216: new_view_position_y = 0;
217: }
218: }
219:
220: mStructurePanel.getScrollPane().getViewport().setViewPosition(
221: new Point(new_view_position_x, new_view_position_y));
222: mViewRectangle.updateLocation();
223: }
224:
225: public void mouseMoved(MouseEvent e) {
226: }
227: }
|