001: /*
002: * Main.java
003: *
004: * Created on 03 November 2006, 17:03
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.xoetrope.svgcomponentswizard;
011:
012: import com.kitfox.svg.SVGElement;
013: import com.kitfox.svg.SVGRoot;
014: import com.kitfox.svg.animation.AnimationElement;
015: import com.kitfox.svg.animation.TimeDiscrete;
016: import com.xoetrope.svg.XSvgImageMap;
017: import java.awt.BorderLayout;
018: import java.awt.Color;
019: import java.awt.Insets;
020: import java.awt.Point;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.ComponentAdapter;
024: import java.awt.event.ComponentEvent;
025: import java.awt.event.MouseEvent;
026: import java.awt.event.MouseListener;
027: import java.io.File;
028: import java.lang.reflect.Field;
029: import java.net.MalformedURLException;
030: import net.xoetrope.optional.svg.XSvgButton;
031: import net.xoetrope.swing.XPanel;
032: import net.xoetrope.xui.XPage;
033: import net.xoetrope.xui.XPageManager;
034: import net.xoetrope.xui.XProjectManager;
035:
036: /**
037: *
038: * @author luano
039: */
040: public class SvgGuage extends XPage {
041: protected XPanel guagePanel, buttonPanel;
042: protected XSvgButton svgButton;
043: protected boolean first;
044: protected int x, y, guageWidth, guageHeight, percentage;
045:
046: /**
047: * Set-up svg button and add component listener to guage panel.
048: */
049: public void pageCreated() {
050: guagePanel = (XPanel) findComponent("guagePanel");
051: guagePanel.setBackground(Color.white);
052:
053: buttonPanel = (XPanel) findComponent("buttonPanel");
054: buttonPanel.setBackground(Color.white);
055:
056: svgButton = new XSvgButton();
057: svgButton.setOpaque(false);
058: svgButton.setBackground(Color.white);
059: guagePanel.add(svgButton);
060:
061: try {
062: svgButton.setAnimate(true);
063: svgButton.setImage(new File("resources/animatedGuage.svg")
064: .toURL());
065:
066: final String[][] ids = { { "needle", null, null } };
067: svgButton.setElementIds(ids);
068: final RotationTransform rotationTransform = new RotationTransform();
069: svgButton.addActionListener(new ActionListener() {
070: public void actionPerformed(ActionEvent ae) {
071: SVGElement element = svgButton
072: .getElement("rotateNeedleTransform");
073: rotationTransform.setElement(element, 193.0, 190.0);
074:
075: if ((ae.getModifiers() & ae.CTRL_MASK) == 0)
076: rotationTransform
077: .setRotation(Math
078: .min(255.0, rotationTransform
079: .getRotation() + 20.0));
080: else
081: rotationTransform
082: .setRotation(Math
083: .max(0.0, rotationTransform
084: .getRotation() - 20.0));
085: svgButton.update();
086: svgButton.setElementIds(ids);
087: return;
088: }
089: });
090:
091: svgButton.setElementIds(ids);
092: svgButton.setup();
093: svgButton.play();
094: } catch (MalformedURLException ex) {
095: ex.printStackTrace();
096: }
097:
098: setVisible(true);
099:
100: svgButton.addActionListener(new ActionListener() {
101: public void actionPerformed(ActionEvent ae) {
102: System.out.println("Clicked");
103: }
104: });
105:
106: /**
107: * Re-position svg button if the guage panel's size changes.
108: */
109: guagePanel.addComponentListener(new ComponentAdapter() {
110: public void componentResized(ComponentEvent evt) {
111: int width = guagePanel.getWidth();
112: int height = guagePanel.getHeight();
113:
114: if (svgButton.getWidth() == 0
115: && svgButton.getHeight() == 0) {
116: guageWidth = height;
117: guageHeight = height;
118: } else {
119: if (width < height) {
120: guageWidth = width;
121: guageHeight = width;
122: } else {
123: guageWidth = height;
124: guageHeight = height;
125: }
126: }
127:
128: if (height < width) {
129: x = (width - guageWidth) / 2;
130: width = height;
131: } else {
132: y = (height - guageHeight) / 2;
133: height = width;
134: }
135:
136: svgButton.setBounds(x, y, width, height);
137: }
138: });
139: }
140:
141: /**
142: * Show the welcome page if the back button is pressed.
143: */
144: public void showPage() {
145: XPageManager pageMgr = XProjectManager.getPageManager();
146: pageMgr.showPage("Welcome");
147: }
148:
149: /**
150: * Inner class used to set the rotation transform for the svg guage.
151: */
152: class RotationTransform {
153: private SVGElement element;
154: private double rotation;
155: private Point.Double centre;
156:
157: /**
158: * Get the currently used svg element.
159: * @return <CODE>SVGElemnt</CODE> specifying the returned element.
160: */
161: public SVGElement getElement() {
162: return element;
163: }
164:
165: /**
166: * Set the currently used SVGElement.
167: * @param element <CODE>SVGElemnt</CODE> specifying the new element.
168: */
169: public void setElement(SVGElement element) {
170: this .element = element;
171: }
172:
173: /**
174: * Set location of the specified svg element.
175: * @param element <CODE>SVGElemnt</CODE> specifying the element to be modified.
176: * @param cx <CODE>double</CODE> specifying the new x co-ordinate.
177: * @param cy <CODE>double</CODE> specifying the new y co-ordinate.
178: */
179: public void setElement(SVGElement element, double cx, double cy) {
180: this .element = element;
181: centre = new Point.Double(cx, cy);
182: }
183:
184: /**
185: * Get the current rotation.
186: * @return <CODE>double</CODE> specifying the current rotation.
187: */
188: public double getRotation() {
189: return rotation;
190: }
191:
192: /**
193: * Set the amount of rotation to be applied to the needle.
194: * @param rotation <CODE>double</CODE> specifying the amount of rotation to be applied.
195: */
196: public void setRotation(double rotation) {
197: double oldRotation = Math.max(15.0, this .rotation);
198: this .rotation = rotation;
199: try {
200: // element.setAttribute( "transform", AnimationElement.AT_XML, "translate(195,198) rotate(" + Double.toString( rotation )+ ") translate(-195,-198)" );
201: Field f = element.getClass().getDeclaredField("values");
202: f.setAccessible(true);
203: double[][] values = (double[][]) f.get(element);
204: values[0][0] = oldRotation;
205: values[1][0] = rotation;
206: f.set(element, values);
207:
208: f = AnimationElement.class
209: .getDeclaredField("beginTime");
210: f.setAccessible(true);
211: TimeDiscrete value = (TimeDiscrete) f.get(element);
212: value = new TimeDiscrete(svgButton.getCurTime());
213: f.set(element, value);
214:
215: element.updateTime(0.0);
216: } catch (Exception ex) {
217: ex.printStackTrace();
218: }
219:
220: // element.setAttribute( "from", AnimationElement.AT_XML, Integer.toString( (int)oldRotation ) );
221: // element.setAttribute( "to", AnimationElement.AT_XML, Integer.toString( (int)rotation ) );
222: }
223:
224: /**
225: * Get the centre point.
226: * @return <CODE>Point.Double</CODE> specifying the current centre point.
227: */
228: public Point.Double getCentre() {
229: return centre;
230: }
231:
232: /**
233: * Set the centre point.
234: * @param centre <CODE>Point.Double</CODE> specifying the new centre point.
235: */
236: public void setCentre(Point.Double centre) {
237: this.centre = centre;
238: }
239: }
240: }
|