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 028package org.jfree.beans.editors; 029 030import java.awt.Color; 031import java.awt.Component; 032import java.awt.GradientPaint; 033import java.awt.Graphics; 034import java.awt.Graphics2D; 035import java.awt.Paint; 036import java.awt.Rectangle; 037import java.beans.PropertyChangeEvent; 038import java.beans.PropertyChangeListener; 039import 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 */ 046public 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 Color c = (Color) p; 111 return "new java.awt.Color(" + c.getRed() + ", " + c.getGreen() 112 + ", " + c.getBlue() + ", " + c.getAlpha() + ")"; 113 } 114 // FIXME: not a color 115 return "new java.awt.GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.blue);"; 116 } 117 118 119 /** 120 * Returns a component for editing a <code>Paint</code> instance. 121 * 122 * @return A component for editing. 123 */ 124 public Component getCustomEditor() { 125 return this.customEditor; 126 } 127 128 /** 129 * Returns <code>true</code> to indicate that we provide a custom editor 130 * via the {@link #getCustomEditor()} method. 131 * 132 * @return <code>true</code>. 133 */ 134 public boolean supportsCustomEditor() { 135 return true; 136 } 137 138}