001: /*
002: * ViewBoxListener.java
003: *
004: * Created on 30 January 2007, 17:27
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.xoetrope.svg.XSvgImageMap;
014: import java.awt.Component;
015: import java.awt.Container;
016: import java.awt.Rectangle;
017: import java.awt.event.ComponentAdapter;
018: import java.awt.event.ComponentEvent;
019: import java.awt.geom.Point2D;
020: import java.beans.PropertyChangeEvent;
021: import java.beans.PropertyChangeListener;
022: import java.util.ArrayList;
023: import javax.swing.JComponent;
024:
025: /**
026: *
027: *
028: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
029: * the GNU Public License (GPL), please see license.txt for more details. If
030: * you make commercial use of this software you must purchase a commercial
031: * license from Xoetrope.</p>
032: * <p> $Revision: 1.2 $</p>
033: */
034: public class XViewBoxListener {
035: protected ArrayList components;
036: protected final XSvgImageMap imageMap;
037: protected XPointSystem pointSystem;
038:
039: /**
040: * Creates a new instance of ViewBoxListener.
041: * @param imageMap the <CODE>XSvgImageMap</CODE> component used by this class.
042: */
043: public XViewBoxListener(final XSvgImageMap imageMap) {
044: this .imageMap = imageMap;
045: components = new ArrayList();
046: pointSystem = new XPointSystem(imageMap);
047:
048: imageMap
049: .addPropertyChangeListener(new PropertyChangeListener() {
050: public void propertyChange(PropertyChangeEvent evt) {
051: SVGRoot root = imageMap.getSvgDiagram()
052: .getRoot();
053: double[] attr = root.getPresAbsolute("viewBox")
054: .getDoubleList();
055:
056: Rectangle imageBounds = new Rectangle(imageMap
057: .getX(), imageMap.getY(), imageMap
058: .getWidth(), imageMap.getHeight());
059:
060: for (int i = 0; i < components.size(); i++) {
061: ViewBoxComponent c = (ViewBoxComponent) components
062: .get(i);
063: Point2D p1 = pointSystem.svgToGlassPane(c
064: .getViewBoxLocation().getX(), c
065: .getViewBoxLocation().getY());
066:
067: if (imageBounds
068: .contains(new Point2D.Double(p1
069: .getX()
070: + imageMap.getX(), p1
071: .getY()))) {
072: c.getComponent().setVisible(true);
073: c.getComponent().setLocation(
074: (int) p1.getX()
075: + imageMap.getX(),
076: (int) p1.getY()
077: + imageMap.getY());
078: c.getComponent().repaint();
079: } else {
080: c.getComponent().setVisible(false);
081: }
082: }
083: }
084: });
085: }
086:
087: /**
088: * Register a component to be updated if the viewBox is modified.
089: * @param component the added <CODE>Component</CODE>
090: */
091: public void registerComponent(Component component) {
092: components.add(new ViewBoxComponent(component));
093: }
094:
095: /**
096: * Remove a component from the list of registered components.
097: * @param component the removed <CODE>Component</CODE>
098: */
099: public void removeComponent(Component component) {
100: for (int i = 0; i < components.size(); i++) {
101: if (((ViewBoxComponent) components.get(i)).getComponent()
102: .equals(component)) {
103: components.remove(i);
104: }
105: }
106: }
107:
108: /**
109: * Private class used to store information for each registered component.
110: */
111: private class ViewBoxComponent {
112: protected Component component;
113: protected Point2D viewBoxlocation;
114:
115: /**
116: * Create a new instance of ViewBoxComponent.
117: * @param component the stored <CODE>Component</CODE>.
118: */
119: public ViewBoxComponent(Component component) {
120: this .component = component;
121: viewBoxlocation = pointSystem.glassPaneToSvg(component
122: .getX(), component.getY());
123: }
124:
125: /**
126: * Get this class's stored component.
127: * @return the returned <CODE>Component</CODE>
128: */
129: public Component getComponent() {
130: return component;
131: }
132:
133: /**
134: * Get the stored component's view-box location.
135: * @return the <CODE>Point2D</CODE> object which contains the components location.
136: */
137: public Point2D getViewBoxLocation() {
138: return viewBoxlocation;
139: }
140: }
141: }
|