001    /**
002     * ========================================
003     * JCommon : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://www.jfree.org/jcommon/
007     * Project Lead:  Thomas Morgner;
008     *
009     * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
010     *
011     * This library is free software; you can redistribute it and/or modify it under the terms
012     * of the GNU Lesser General Public License as published by the Free Software Foundation;
013     * either version 2.1 of the License, or (at your option) any later version.
014     *
015     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
016     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
017     * See the GNU Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public License along with this
020     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021     * Boston, MA 02111-1307, USA.
022     *
023     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
024     * in the United States and other countries.]
025     *
026     * ------------
027     * DrawablePanel.java
028     * ------------
029     * (C) Copyright 2002-2006, by Object Refinery Limited.
030     *
031     * Original Author:  Thomas Morgner;
032     * Contributor(s):   -;
033     *
034     * $Id: DrawablePanel.java,v 1.3 2007/10/15 10:03:35 taqua Exp $
035     *
036     * Changes
037     * -------
038     *
039     *
040     */
041    package org.jfree.ui;
042    
043    import java.awt.Dimension;
044    import java.awt.Graphics;
045    import java.awt.Graphics2D;
046    import java.awt.geom.Rectangle2D;
047    import javax.swing.JPanel;
048    
049    /**
050     * A component, that accepts a drawable and which draws that drawable.
051     *
052     * @author Thomas Morgner
053     */
054    public class DrawablePanel extends JPanel
055    {
056      private Drawable drawable;
057    
058      public DrawablePanel()
059      {
060        setOpaque(false);
061      }
062    
063      public Drawable getDrawable()
064      {
065        return drawable;
066      }
067    
068      public void setDrawable(final Drawable drawable)
069      {
070        this.drawable = drawable;
071        revalidate();
072        repaint();
073      }
074    
075      /**
076       * If the <code>preferredSize</code> has been set to a non-<code>null</code>
077       * value just returns it. If the UI delegate's <code>getPreferredSize</code>
078       * method returns a non <code>null</code> value then return that; otherwise
079       * defer to the component's layout manager.
080       *
081       * @return the value of the <code>preferredSize</code> property
082       * @see #setPreferredSize
083       * @see javax.swing.plaf.ComponentUI
084       */
085      public Dimension getPreferredSize()
086      {
087        if (drawable instanceof ExtendedDrawable)
088        {
089          final ExtendedDrawable ed = (ExtendedDrawable) drawable;
090          return ed.getPreferredSize();
091        }
092        return super.getPreferredSize();
093      }
094    
095      /**
096       * If the minimum size has been set to a non-<code>null</code> value just
097       * returns it.  If the UI delegate's <code>getMinimumSize</code> method
098       * returns a non-<code>null</code> value then return that; otherwise defer to
099       * the component's layout manager.
100       *
101       * @return the value of the <code>minimumSize</code> property
102       * @see #setMinimumSize
103       * @see javax.swing.plaf.ComponentUI
104       */
105      public Dimension getMinimumSize()
106      {
107        if (drawable instanceof ExtendedDrawable)
108        {
109          final ExtendedDrawable ed = (ExtendedDrawable) drawable;
110          return ed.getPreferredSize();
111        }
112        return super.getMinimumSize();
113      }
114    
115      /**
116       * Returns true if this component is completely opaque.
117       * <p/>
118       * An opaque component paints every pixel within its rectangular bounds. A
119       * non-opaque component paints only a subset of its pixels or none at all,
120       * allowing the pixels underneath it to "show through".  Therefore, a
121       * component that does not fully paint its pixels provides a degree of
122       * transparency.
123       * <p/>
124       * Subclasses that guarantee to always completely paint their contents should
125       * override this method and return true.
126       *
127       * @return true if this component is completely opaque
128       * @see #setOpaque
129       */
130      public boolean isOpaque()
131      {
132        if (drawable == null)
133        {
134          return false;
135        }
136        return super.isOpaque();
137      }
138    
139      /**
140       * Calls the UI delegate's paint method, if the UI delegate is
141       * non-<code>null</code>.  We pass the delegate a copy of the
142       * <code>Graphics</code> object to protect the rest of the paint code from
143       * irrevocable changes (for example, <code>Graphics.translate</code>).
144       * <p/>
145       * If you override this in a subclass you should not make permanent changes to
146       * the passed in <code>Graphics</code>. For example, you should not alter the
147       * clip <code>Rectangle</code> or modify the transform. If you need to do
148       * these operations you may find it easier to create a new
149       * <code>Graphics</code> from the passed in <code>Graphics</code> and
150       * manipulate it. Further, if you do not invoker super's implementation you
151       * must honor the opaque property, that is if this component is opaque, you
152       * must completely fill in the background in a non-opaque color. If you do not
153       * honor the opaque property you will likely see visual artifacts.
154       * <p/>
155       * The passed in <code>Graphics</code> object might have a transform other
156       * than the identify transform installed on it.  In this case, you might get
157       * unexpected results if you cumulatively apply another transform.
158       *
159       * @param g the <code>Graphics</code> object to protect
160       * @see #paint
161       * @see javax.swing.plaf.ComponentUI
162       */
163      protected void paintComponent(Graphics g)
164      {
165        super.paintComponent(g);
166        if (drawable == null)
167        {
168          return;
169        }
170    
171        final Graphics2D g2 = (Graphics2D) g.create
172                (0, 0, getWidth(), getHeight());
173    
174        drawable.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
175        g2.dispose();
176      }
177    
178    }