001: /*
002: * PainterPropertyEditor.java
003: *
004: * Created on March 21, 2006, 11:26 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.jdesktop.swingx.editors;
011:
012: import java.awt.Color;
013: import java.awt.GradientPaint;
014: import java.awt.geom.AffineTransform;
015: import java.awt.geom.Arc2D;
016: import java.awt.geom.Area;
017: import java.awt.geom.CubicCurve2D;
018: import java.awt.geom.Ellipse2D;
019: import java.awt.geom.GeneralPath;
020: import java.awt.geom.Line2D;
021: import java.awt.geom.PathIterator;
022: import java.awt.geom.Point2D;
023: import java.awt.geom.QuadCurve2D;
024: import java.awt.geom.Rectangle2D;
025: import java.awt.geom.RoundRectangle2D;
026: import java.beans.DefaultPersistenceDelegate;
027: import java.beans.Encoder;
028: import java.beans.Expression;
029: import java.beans.PersistenceDelegate;
030: import java.beans.PropertyEditorSupport;
031: import java.beans.Statement;
032: import java.beans.XMLDecoder;
033: import java.beans.XMLEncoder;
034: import java.io.ByteArrayInputStream;
035: import java.io.ByteArrayOutputStream;
036: import javax.swing.JFrame;
037: import org.apache.batik.ext.awt.LinearGradientPaint;
038: import org.apache.batik.ext.awt.MultipleGradientPaint.ColorSpaceEnum;
039: import org.apache.batik.ext.awt.RadialGradientPaint;
040: import org.jdesktop.swingx.JXPanel;
041: import org.jdesktop.swingx.painter.BackgroundPainter;
042: import org.jdesktop.swingx.painter.CheckerboardPainter;
043: import org.jdesktop.swingx.painter.CompoundPainter;
044: import org.jdesktop.swingx.painter.GlossPainter;
045: import org.jdesktop.swingx.painter.MattePainter;
046: import org.jdesktop.swingx.painter.Painter;
047: import org.jdesktop.swingx.painter.PinstripePainter;
048: import org.jdesktop.swingx.painter.ShapePainter;
049: import org.jdesktop.swingx.painter.TextPainter;
050: import org.jdesktop.swingx.painter.gradient.BasicGradientPainter;
051: import org.jdesktop.swingx.painter.gradient.LinearGradientPainter;
052: import org.jdesktop.swingx.painter.gradient.RadialGradientPainter;
053:
054: /**
055: * Two parts to this property editor. The first part is a simple dropdown.
056: * The second part is a complicated UI for constructing multiple "layers" of
057: * various different Painters, including gradient painters.
058: *
059: * @author Richard
060: */
061: public class PainterPropertyEditor extends PropertyEditorSupport {
062: /** Creates a new instance of PainterPropertyEditor */
063: public PainterPropertyEditor() {
064: }
065:
066: public Painter getValue() {
067: return (Painter) super .getValue();
068: }
069:
070: public String getJavaInitializationString() {
071: Painter painter = getValue();
072: //TODO!!!
073: return painter == null ? "null"
074: : "new org.jdesktop.swingx.painter.CheckerboardPainter()";
075: }
076:
077: public static void main(String... args) {
078: ByteArrayOutputStream baos = new ByteArrayOutputStream(300);
079: XMLEncoder e = new XMLEncoder(baos);
080:
081: e.setPersistenceDelegate(GradientPaint.class,
082: new GradientPaintDelegate());
083: e
084: .setPersistenceDelegate(Arc2D.Float.class,
085: new Arc2DDelegate());
086: e.setPersistenceDelegate(Arc2D.Double.class,
087: new Arc2DDelegate());
088: e.setPersistenceDelegate(CubicCurve2D.Float.class,
089: new CubicCurve2DDelegate());
090: e.setPersistenceDelegate(CubicCurve2D.Double.class,
091: new CubicCurve2DDelegate());
092: e.setPersistenceDelegate(Ellipse2D.Float.class,
093: new Ellipse2DDelegate());
094: e.setPersistenceDelegate(Ellipse2D.Double.class,
095: new Ellipse2DDelegate());
096: e.setPersistenceDelegate(Line2D.Float.class,
097: new Line2DDelegate());
098: e.setPersistenceDelegate(Line2D.Double.class,
099: new Line2DDelegate());
100: e.setPersistenceDelegate(Point2D.Float.class,
101: new Point2DDelegate());
102: e.setPersistenceDelegate(Point2D.Double.class,
103: new Point2DDelegate());
104: e.setPersistenceDelegate(QuadCurve2D.Float.class,
105: new QuadCurve2DDelegate());
106: e.setPersistenceDelegate(QuadCurve2D.Double.class,
107: new QuadCurve2DDelegate());
108: e.setPersistenceDelegate(Rectangle2D.Float.class,
109: new Rectangle2DDelegate());
110: e.setPersistenceDelegate(Rectangle2D.Double.class,
111: new Rectangle2DDelegate());
112: e.setPersistenceDelegate(RoundRectangle2D.Float.class,
113: new RoundRectangle2DDelegate());
114: e.setPersistenceDelegate(RoundRectangle2D.Double.class,
115: new RoundRectangle2DDelegate());
116: e.setPersistenceDelegate(Area.class, new AreaDelegate());
117: e.setPersistenceDelegate(GeneralPath.class,
118: new GeneralPathDelegate());
119: e.setPersistenceDelegate(AffineTransform.class,
120: new AffineTransformDelegate());
121: e.setPersistenceDelegate(RadialGradientPaint.class,
122: new RadialGradientPaintDelegate());
123: e.setPersistenceDelegate(LinearGradientPaint.class,
124: new LinearGradientPaintDelegate());
125:
126: Area a = new Area(new RoundRectangle2D.Double(20, 20, 50, 50,
127: 4, 4));
128: a.add(new Area(new Ellipse2D.Double(10, 10, 100, 20)));
129:
130: TextPainter textPainter = new TextPainter("Yo dude");
131: textPainter.setPaint(Color.WHITE);
132:
133: e
134: .writeObject(new CompoundPainter(
135: new BackgroundPainter(),
136: new CheckerboardPainter(),
137: new MattePainter(Color.BLACK),
138: new BasicGradientPainter(
139: BasicGradientPainter.RED_XP),
140: new LinearGradientPainter(
141: LinearGradientPainter.BLACK_PERSPECTIVE),
142: new RadialGradientPainter(
143: new RadialGradientPaint(
144: new Point2D.Double(.5, .5),
145: .2f,
146: new float[] { 0f, .5f, 1f },
147: new Color[] { Color.BLACK,
148: Color.WHITE, Color.RED })),
149: new ShapePainter(a), new PinstripePainter(),
150: textPainter, new GlossPainter()
151: // new IconPainter(),
152: // new ImagePainter(),
153: ));
154: e.close();
155: System.out.println(baos.toString());
156:
157: XMLDecoder d = new XMLDecoder(new ByteArrayInputStream(baos
158: .toByteArray()));
159:
160: Painter p = (Painter) d.readObject();
161:
162: JFrame frame = new JFrame("Yo momma");
163: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
164: JXPanel panel = new JXPanel();
165: panel.setBackgroundPainter(p);
166: frame.add(panel);
167: frame.setSize(800, 600);
168: frame.setVisible(true);
169: }
170:
171: public static final class GradientPaintDelegate extends
172: DefaultPersistenceDelegate {
173: public GradientPaintDelegate() {
174: super (
175: new String[] { "point1", "color1", "point2",
176: "color2" });
177: }
178: }
179:
180: public static final class LinearGradientPaintDelegate extends
181: DefaultPersistenceDelegate {
182: public LinearGradientPaintDelegate() {
183: super (new String[] { "startPoint", "endPoint", "fractions",
184: "colors" });
185: //these 3 not yet supported. The problem is the
186: //colorspace. I haven't figured out how to transfer that one yet
187: //, "cycleMethod", "colorSpace", "transform"});
188: }
189:
190: protected Expression instantiate(Object oldInstance, Encoder out) {
191:
192: ColorSpaceEnum e = ((LinearGradientPaint) oldInstance)
193: .getColorSpace();
194: Expression retValue;
195:
196: retValue = super .instantiate(oldInstance, out);
197: return retValue;
198: }
199: }
200:
201: public static final class RadialGradientPaintDelegate extends
202: DefaultPersistenceDelegate {
203: public RadialGradientPaintDelegate() {
204: super (new String[] { "centerPoint", "radius", "focusPoint",
205: "fractions", "colors" });
206: //these 3 not yet supported. The problem is the
207: //colorspace. I haven't figured out how to transfer that one yet
208: //, "cycleMethod", "colorSpace", "transform"});
209: }
210: }
211:
212: public static final class Arc2DDelegate extends
213: DefaultPersistenceDelegate {
214: public Arc2DDelegate() {
215: super (new String[] { "x", "y", "width", "height",
216: "angleStart", "angleExtent", "arcType" });
217: }
218: }
219:
220: public static final class CubicCurve2DDelegate extends
221: DefaultPersistenceDelegate {
222: public CubicCurve2DDelegate() {
223: super (new String[] { "x1", "y1", "ctrlX1", "ctrlY1",
224: "ctrlX2", "ctrlY2", "x2", "y2" });
225: }
226: }
227:
228: public static final class Ellipse2DDelegate extends
229: DefaultPersistenceDelegate {
230: public Ellipse2DDelegate() {
231: super (new String[] { "x", "y", "width", "height" });
232: }
233: }
234:
235: public static final class Line2DDelegate extends
236: DefaultPersistenceDelegate {
237: public Line2DDelegate() {
238: super (new String[] { "x1", "y1", "x2", "y2" });
239: }
240: }
241:
242: public static final class Point2DDelegate extends
243: DefaultPersistenceDelegate {
244: public Point2DDelegate() {
245: super (new String[] { "x", "y" });
246: }
247: }
248:
249: public static final class QuadCurve2DDelegate extends
250: DefaultPersistenceDelegate {
251: public QuadCurve2DDelegate() {
252: super (new String[] { "x1", "y1", "ctrlX", "ctrlY", "x2",
253: "y2" });
254: }
255: }
256:
257: public static final class Rectangle2DDelegate extends
258: DefaultPersistenceDelegate {
259: public Rectangle2DDelegate() {
260: super (new String[] { "x", "y", "width", "height" });
261: }
262: }
263:
264: public static final class RoundRectangle2DDelegate extends
265: DefaultPersistenceDelegate {
266: public RoundRectangle2DDelegate() {
267: super (new String[] { "x", "y", "width", "height",
268: "arcWidth", "arcHeight" });
269: }
270: }
271:
272: public static final class AreaDelegate extends PersistenceDelegate {
273: protected Expression instantiate(Object oldInstance, Encoder out) {
274: Area a = (Area) oldInstance;
275:
276: //use the default constructor
277: AffineTransform tx = new AffineTransform();
278: PathIterator itr = a.getPathIterator(tx);
279:
280: GeneralPath path = new GeneralPath();
281: out.writeExpression(new Expression(path, GeneralPath.class,
282: "new", new Object[0]));
283:
284: while (!itr.isDone()) {
285: float[] segment = new float[6]; //must use floats because lineTo etc use floats
286: int pathType = itr.currentSegment(segment);
287:
288: switch (pathType) {
289: case PathIterator.SEG_CLOSE:
290: out.writeStatement(new Statement(path, "closePath",
291: new Object[0]));
292: break;
293: case PathIterator.SEG_CUBICTO:
294: out.writeStatement(new Statement(path, "curveTo",
295: new Object[] { segment[0], segment[1],
296: segment[2], segment[3], segment[4],
297: segment[5] }));
298: break;
299: case PathIterator.SEG_LINETO:
300: out.writeStatement(new Statement(path, "lineTo",
301: new Object[] { segment[0], segment[1] }));
302: break;
303: case PathIterator.SEG_MOVETO:
304: out.writeStatement(new Statement(path, "moveTo",
305: new Object[] { segment[0], segment[1] }));
306: break;
307: case PathIterator.SEG_QUADTO:
308: out.writeStatement(new Statement(path, "quadTo",
309: new Object[] { segment[0], segment[1],
310: segment[2], segment[3] }));
311: break;
312: }
313: itr.next();
314: }
315:
316: return new Expression(a, Area.class, "new",
317: new Object[] { path });
318: }
319: }
320:
321: public static final class AffineTransformDelegate extends
322: DefaultPersistenceDelegate {
323: public AffineTransformDelegate() {
324: super (new String[] { "scaleX", "shearY", "shearX",
325: "scaleY", "translateX", "translateY" });
326: }
327: }
328:
329: public static final class GeneralPathDelegate extends
330: PersistenceDelegate {
331: protected Expression instantiate(Object oldInstance, Encoder out) {
332: return new Expression(oldInstance, GeneralPath.class,
333: "new", new Object[0]);
334: }
335:
336: protected void initialize(Class<?> type, Object oldInstance,
337: Object newInstance, Encoder out) {
338: GeneralPath a = (GeneralPath) oldInstance;
339:
340: AffineTransform tx = new AffineTransform();
341: PathIterator itr = a.getPathIterator(tx);
342:
343: out.writeStatement(new Statement(a, "setWindingRule",
344: new Object[] { a.getWindingRule() }));
345:
346: while (!itr.isDone()) {
347: float[] segment = new float[6]; //must use floats because lineTo etc use floats
348: int pathType = itr.currentSegment(segment);
349:
350: switch (pathType) {
351: case PathIterator.SEG_CLOSE:
352: out.writeStatement(new Statement(a, "closePath",
353: new Object[0]));
354: break;
355: case PathIterator.SEG_CUBICTO:
356: out.writeStatement(new Statement(a, "curveTo",
357: new Object[] { segment[0], segment[1],
358: segment[2], segment[3], segment[4],
359: segment[5] }));
360: break;
361: case PathIterator.SEG_LINETO:
362: out.writeStatement(new Statement(a, "lineTo",
363: new Object[] { segment[0], segment[1] }));
364: break;
365: case PathIterator.SEG_MOVETO:
366: out.writeStatement(new Statement(a, "moveTo",
367: new Object[] { segment[0], segment[1] }));
368: break;
369: case PathIterator.SEG_QUADTO:
370: out.writeStatement(new Statement(a, "quadTo",
371: new Object[] { segment[0], segment[1],
372: segment[2], segment[3] }));
373: break;
374: }
375: itr.next();
376: }
377: }
378: }
379: // public static final class PaintDelegate extends PersistenceDelegate {
380: // protected Expression instantiate(Object oldInstance, Encoder out) {
381: // if (oldInstance instanceof GradientPaint) {
382: //
383: // }
384: // }
385: // }
386: }
|