001: package com.xoetrope.svg;
002:
003: import com.kitfox.svg.SVGDiagram;
004: import java.awt.BorderLayout;
005: import java.awt.Container;
006: import java.awt.FlowLayout;
007: import java.awt.Graphics2D;
008: import java.awt.GridBagConstraints;
009: import java.awt.GridBagLayout;
010: import java.awt.Image;
011: import java.awt.Point;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014: import javax.swing.BorderFactory;
015: import javax.swing.ButtonGroup;
016: import javax.swing.JCheckBox;
017: import javax.swing.JComponent;
018: import javax.swing.JFrame;
019: import javax.swing.JPanel;
020: import javax.swing.JRadioButton;
021: import javax.swing.SwingUtilities;
022: import javax.swing.border.BevelBorder;
023:
024: /**
025: *
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.2 $</p>
031: */
032: public class XSvgMagnifier implements ActionListener {
033: private JComponent glassPane;
034: private JFrame frame;
035: private JPanel buttonPanel, zoomPanel;
036: private JRadioButton windowButton, glassButton;
037: private JCheckBox rotationBox, qualityBox;
038: private ButtonGroup buttonGroup;
039: private SVGDiagram diagram;
040: private Point zoomLocation;
041: private Image lensImage;
042:
043: private XSvgMagnifyingWindow magWindow;
044: private XSvgMagnifyingGlass magGlass;
045: private boolean mode, rotationMode, qualityMode;
046:
047: /** Class constructor.
048: * @param diagram provides a means of rendering the document as a Graphics2D object
049: * @param imageFrame a <code>JFrame</code> that should point to the frame that contains the XSvgImageMap instance.
050: */
051: public XSvgMagnifier(JComponent glassPane, XSvgImageMap svgImage) {
052: this .diagram = svgImage.getSvgDiagram();
053: this .glassPane = glassPane;
054:
055: frame = new JFrame("Svg Magnify");
056: frame.setLayout(new BorderLayout());
057: frame.setSize(275, 250);
058:
059: buttonPanel = new JPanel();
060: buttonPanel.setBorder(BorderFactory
061: .createBevelBorder(BevelBorder.RAISED));
062: buttonPanel.setLayout(new GridBagLayout());
063: GridBagConstraints c = new GridBagConstraints();
064: zoomPanel = new JPanel();
065:
066: windowButton = new JRadioButton("Window");
067: windowButton.setFocusable(false);
068: windowButton.setActionCommand("window");
069: windowButton.addActionListener(this );
070:
071: glassButton = new JRadioButton("Magnifying Glass");
072: glassButton.setFocusable(false);
073: glassButton.setActionCommand("glass");
074: glassButton.addActionListener(this );
075:
076: windowButton.setSelected(true);
077:
078: buttonGroup = new ButtonGroup();
079: buttonGroup.add(windowButton);
080: buttonGroup.add(glassButton);
081:
082: rotationBox = new JCheckBox("Rotation On");
083: rotationBox.setFocusable(false);
084: rotationBox.setActionCommand("isRotational");
085: rotationBox.addActionListener(this );
086:
087: qualityBox = new JCheckBox("High Quality Rendering");
088: qualityBox.setFocusable(false);
089: qualityBox.setActionCommand("quality");
090: qualityBox.addActionListener(this );
091:
092: //buttonPanel.setBackground( SystemColor.control );
093: c.anchor = GridBagConstraints.LINE_START;
094: c.gridx = 0;
095: c.gridy = 0;
096: buttonPanel.add(windowButton, c);
097:
098: c.gridx = 1;
099: c.gridy = 0;
100: buttonPanel.add(glassButton, c);
101:
102: c.gridx = 0;
103: c.gridy = 1;
104: buttonPanel.add(rotationBox, c);
105:
106: c.gridx = 1;
107: c.gridy = 1;
108: buttonPanel.add(qualityBox, c);
109:
110: magWindow = new XSvgMagnifyingWindow();
111: magWindow.setDiagram(diagram);
112: magWindow.setImageMap(svgImage);
113:
114: magGlass = new XSvgMagnifyingGlass();
115: magGlass.setDiagram(diagram);
116: magGlass.setImageMap(svgImage);
117:
118: frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
119: frame.getContentPane().add(magWindow, BorderLayout.CENTER);
120:
121: mode = true;
122: frame.setLocation(800, 350);
123:
124: magWindow.setVisible(true);
125: frame.setVisible(true);
126:
127: magGlass.setRotational(rotationMode);
128: }
129:
130: /**
131: * Used to set which part of the SVG image will be zoomed.
132: * @param p the Point object specifying the area to be zoomed.
133: */
134: public void setZoomPoint(Point p) {
135: if (mode == true) {
136: magWindow.setZoomPoint(p);
137: } else {
138: magGlass.setZoomPoint(p, p);
139: }
140: }
141:
142: /**
143: * Sets the scale factor of the XSvgMagnifyingWindow object used within this class.
144: * @param scale <code>float</code> specifing the scale factor.
145: */
146: public void setWindowScale(float scale) {
147: magWindow.setScaleFactor(scale);
148: }
149:
150: /**
151: * Sets the scale factor of the XSvgMagnifyingWindow object used within this class.
152: * @param scale <code>float</code> specifing the scale factor.
153: */
154: public void setGlassScale(float scale) {
155: magGlass.setScaleFactor(scale);
156: }
157:
158: /**
159: * Provides an action listener for the JRadioButton objects used to select which magnification mode is currently in use.
160: * @param e the <code>ActionEvent</code> generated by the component.
161: */
162: public void actionPerformed(ActionEvent e) {
163: if (e.getActionCommand().equals("isRotational")) {
164: if (rotationMode == false)
165: rotationMode = true;
166: else
167: rotationMode = false;
168:
169: magGlass.setRotational(rotationMode);
170: } else if (e.getActionCommand().equals("quality")) {
171: if (qualityMode == false)
172: qualityMode = true;
173: else
174: qualityMode = false;
175:
176: magGlass.setQuality(qualityMode);
177: }
178: // if currently in window mode
179: else if (e.getActionCommand().equals("window")) {
180: mode = true;
181: glassPane.remove(magGlass);
182: glassPane.repaint();
183: magGlass.stopThreads();
184: magWindow.setZoomPoint(null);
185: magWindow.setVisible(true);
186: } else if (e.getActionCommand().equals("glass")) {
187: mode = false;
188: magWindow.stopRenderer();
189: magWindow.setVisible(false);
190: magGlass.setBounds(0, 0, glassPane.getWidth(), glassPane
191: .getHeight());
192: glassPane.add(magGlass, 0);
193: glassPane.setVisible(true);
194: }
195: }
196:
197: /**
198: * Sets whether a fade-in or fade-out animation is to be run.
199: * animationMode an <code>int</code> specifying the animation type.
200: */
201: public void setMagGlassAnimation(int animationMode,
202: float animationSpeed) {
203: magGlass.setAnimationMode(animationMode, animationSpeed);
204: new Thread(magGlass).start();
205: }
206: }
|