001 /* ======================================================
002 * Orson : a free chart beans library based on JFreeChart
003 * ======================================================
004 *
005 * (C) Copyright 2007, by Object Refinery Limited.
006 *
007 * Project Info: not-yet-released
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 */
027
028 package org.jfree.beans.editors;
029
030 import java.awt.Color;
031 import java.awt.Component;
032 import java.awt.GradientPaint;
033 import java.awt.Graphics;
034 import java.awt.Graphics2D;
035 import java.awt.Paint;
036 import java.awt.Rectangle;
037 import java.beans.PropertyChangeEvent;
038 import java.beans.PropertyChangeListener;
039 import java.beans.PropertyEditorSupport;
040
041 /**
042 * A JavaBeans property editor for {@link Paint} instances. Obviously, we
043 * can't provide editing for every type of <code>Paint</code>, but we'll try
044 * to cover {@link Paint} and {@link GradientPaint}.
045 */
046 public class PaintEditor extends PropertyEditorSupport
047 implements PropertyChangeListener {
048
049
050 /* (non-Javadoc)
051 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
052 */
053 public void propertyChange(PropertyChangeEvent evt) {
054 firePropertyChange();
055 }
056
057 PaintEditorGUI customEditor;
058
059 /**
060 * Creates a new instance.
061 */
062 public PaintEditor() {
063 this.customEditor = new PaintEditorGUI();
064 this.customEditor.addPropertyChangeListener(this);
065 }
066
067 public boolean isPaintable() {
068 return true;
069 }
070
071 public void paintValue(Graphics g, Rectangle clipRect) {
072 Graphics2D g2 = (Graphics2D) g;
073 Paint p = this.customEditor.getPaint();
074 if (p != null) {
075 g2.setPaint(p);
076 int cy = (int) clipRect.getCenterY();
077 int x = (int) clipRect.getMinX() + 2;
078 Rectangle box = new Rectangle(x, cy - 4, 8, 8);
079 g2.fill(box);
080 g2.setPaint(Color.black);
081 g2.draw(box);
082 }
083 }
084
085 /* (non-Javadoc)
086 * @see java.beans.PropertyEditorSupport#getValue()
087 */
088 public Object getValue() {
089 return this.customEditor.getPaint();
090 }
091
092 /* (non-Javadoc)
093 * @see java.beans.PropertyEditorSupport#setValue(java.lang.Object)
094 */
095 public void setValue(Object value) {
096 this.customEditor.setPaint((Paint) value);
097 }
098
099 /**
100 * Returns a string for the property value.
101 *
102 * @return A string for the property value.
103 */
104 public String getJavaInitializationString() {
105 Paint p = (Paint) getValue();
106 if (p == null) {
107 return "null";
108 }
109 else if (p instanceof Color) {
110 return "Color.black";
111 }
112 return "new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.blue);";
113 }
114
115
116 /**
117 * Returns a component for editing a <code>Paint</code> instance.
118 *
119 * @return A component for editing.
120 */
121 public Component getCustomEditor() {
122 return this.customEditor;
123 }
124
125 /**
126 * Returns <code>true</code> to indicate that we provide a custom editor
127 * via the {@link #getCustomEditor()} method.
128 *
129 * @return <code>true</code>.
130 */
131 public boolean supportsCustomEditor() {
132 return true;
133 }
134
135 }