001: /*
002: * XSelectionZoom.java
003: *
004: * Created on 14 August 2007, 10:07
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.xoetrope.svg;
011:
012: import com.kitfox.svg.SVGRoot;
013: import com.kitfox.svg.animation.AnimationElement;
014: import com.xoetrope.carousel.build.BuildProperties;
015: import com.xoetrope.svg.XPointSystem;
016: import com.xoetrope.svg.XSvgImageMap;
017: import java.awt.BasicStroke;
018: import java.awt.Color;
019: import java.awt.Graphics;
020: import java.awt.Graphics2D;
021: import java.awt.Point;
022: import java.awt.event.ComponentAdapter;
023: import java.awt.event.ComponentEvent;
024: import java.awt.event.KeyEvent;
025: import java.awt.event.KeyListener;
026: import java.awt.event.MouseEvent;
027: import java.awt.event.MouseListener;
028: import java.awt.event.MouseMotionListener;
029: import java.awt.geom.Point2D;
030: import java.awt.geom.Rectangle2D;
031: import java.beans.PropertyChangeEvent;
032: import java.beans.PropertyChangeListener;
033: import java.text.DecimalFormat;
034: import java.text.NumberFormat;
035: import javax.swing.JComponent;
036:
037: /**
038: *
039: *
040: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
041: * the GNU Public License (GPL), please see license.txt for more details. If
042: * you make commercial use of this software you must purchase a commercial
043: * license from Xoetrope.</p>
044: * <p> $Revision: 1.2 $</p>
045: */
046: public class XSelectionZoom extends JComponent implements
047: MouseListener, MouseMotionListener {
048: protected boolean dragged, firstZoom;
049: protected XSvgImageMap imageMap;
050: protected Point startPt, endPt;
051: protected Rectangle2D.Double rect;
052: protected XPointSystem pointSystem;
053: protected NumberFormat formatter;
054:
055: /**
056: * Creates a new instance of XZoomSelection
057: * @param iMap <CODE>XSvgImageMap</CODE>
058: */
059: public XSelectionZoom(XSvgImageMap iMap) {
060: imageMap = iMap;
061: formatter = new DecimalFormat(".00000");
062: imageMap.addMouseListener(this );
063: imageMap.addMouseMotionListener(this );
064:
065: imageMap.getGlassPane().add(this );
066: imageMap.addComponentListener(new ComponentAdapter() {
067: public void componentResized(ComponentEvent evt) {
068: setBounds(0, 0, imageMap.getWidth(), imageMap
069: .getHeight());
070: firstZoom = true;
071: }
072: });
073:
074: pointSystem = new XPointSystem(imageMap);
075: }
076:
077: /**
078: * Displays the selection box which will be used to select the area to be zoomed.
079: * @param e the passed <CODE>MouseEvent</CODE> instance.
080: */
081: public void mousePressed(MouseEvent e) {
082: if ((e.getModifiers() & e.CTRL_MASK) != 0) {
083: startPt = e.getPoint();
084: setVisible(true);
085: }
086: }
087:
088: /**
089: * Zooms to the selected region of the image once the mouse is released
090: * @param e the passed <CODE>MouseEvent</CODE> instance.
091: */
092: public void mouseReleased(MouseEvent e) {
093: if (dragged) {
094: dragged = false;
095:
096: setVisible(false);
097: SVGRoot root = imageMap.getSvgDiagram().getRoot();
098: try {
099: Point2D p1 = pointSystem.glassPaneToSvg(rect.getX(),
100: rect.getY());
101: Point2D p2 = pointSystem.glassPaneToSvg(rect.getX()
102: + rect.getWidth(), rect.getY()
103: + rect.getHeight());
104:
105: double x = p1.getX();
106: double y = p1.getY();
107: double rectW = p2.getX() - p1.getX();
108: double rectH = p2.getY() - p1.getY();
109:
110: Point2D start = pointSystem.glassPaneToSvg(0, 0);
111: Point2D end = pointSystem.glassPaneToSvg(imageMap
112: .getWidth(), imageMap.getHeight());
113:
114: double totalWidth = end.getX() - start.getX();
115: double totalHeight = end.getY() - start.getY();
116:
117: double zoom;
118: if (rect.getWidth() > rect.getHeight())
119: zoom = totalWidth / rectW;
120: else
121: zoom = totalHeight / rectH;
122:
123: imageMap.startCoalesce();
124: imageMap.zoom(zoom);
125:
126: double[] attr = root.getPresAbsolute("viewBox")
127: .getDoubleList();
128:
129: double newX = x - attr[0];
130: double newY = y - attr[1];
131:
132: newX = newX / attr[2];
133: newY = newY / attr[3];
134:
135: if (newX > 0)
136: newX = newX + 1.0;
137: else
138: newX = newX - 1.0;
139:
140: if (newY > 0)
141: newY = newY + 1.0;
142: else
143: newY = newY - 1.0;
144:
145: imageMap.endCoalesce();
146: imageMap.pan(newX, newY);
147: } catch (Exception ex) {
148: if (BuildProperties.DEBUG)
149: ex.printStackTrace();
150: }
151: }
152: }
153:
154: /**
155: * Re-sizes the selection box as the mouse is dragged.
156: * @param e the passed <CODE>MouseEvent</CODE> instance.
157: */
158: public void mouseDragged(MouseEvent e) {
159: if ((e.getModifiers() & e.CTRL_MASK) != 0) {
160: endPt = e.getPoint();
161: dragged = true;
162: repaint();
163: }
164: }
165:
166: /**
167: * Paints the selection box.
168: * @param g the delegate <CODE>Graphics</Code> object.
169: */
170: public void paintComponent(Graphics g) {
171: if (dragged) {
172: Graphics2D g2d = (Graphics2D) g.create();
173: g2d.setColor(new Color(0, 148, 255));
174: g2d.setStroke(new BasicStroke(2.0F));
175:
176: double width = endPt.getX() - startPt.getX();
177: double height = endPt.getY() - startPt.getY();
178: rect = new Rectangle2D.Double(startPt.getX(), startPt
179: .getY(), width, height);
180: g2d.draw(rect);
181:
182: g2d.dispose();
183: }
184: }
185:
186: public void mouseClicked(MouseEvent e) {
187: }
188:
189: public void mouseEntered(MouseEvent e) {
190: }
191:
192: public void mouseExited(MouseEvent e) {
193: }
194:
195: public void mouseMoved(MouseEvent e) {
196: }
197: }
|