Source for org.jfree.ui.DrawablePanel

   1: /**
   2:  * ========================================
   3:  * JCommon : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://www.jfree.org/jcommon/
   7:  * Project Lead:  Thomas Morgner;
   8:  *
   9:  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
  10:  *
  11:  * This library is free software; you can redistribute it and/or modify it under the terms
  12:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  13:  * either version 2.1 of the License, or (at your option) any later version.
  14:  *
  15:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17:  * See the GNU Lesser General Public License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public License along with this
  20:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21:  * Boston, MA 02111-1307, USA.
  22:  *
  23:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24:  * in the United States and other countries.]
  25:  *
  26:  * ------------
  27:  * DrawablePanel.java
  28:  * ------------
  29:  * (C) Copyright 2002-2006, by Object Refinery Limited.
  30:  *
  31:  * Original Author:  Thomas Morgner;
  32:  * Contributor(s):   -;
  33:  *
  34:  * $Id: DrawablePanel.java,v 1.4 2008/09/10 09:25:09 mungady Exp $
  35:  *
  36:  * Changes
  37:  * -------
  38:  *
  39:  *
  40:  */
  41: package org.jfree.ui;
  42: 
  43: import java.awt.Dimension;
  44: import java.awt.Graphics;
  45: import java.awt.Graphics2D;
  46: import java.awt.geom.Rectangle2D;
  47: import javax.swing.JPanel;
  48: 
  49: /**
  50:  * A component, that accepts a drawable and which draws that drawable.
  51:  *
  52:  * @author Thomas Morgner
  53:  */
  54: public class DrawablePanel extends JPanel
  55: {
  56:   private Drawable drawable;
  57: 
  58:   /**
  59:    * Creates a new instance.
  60:    */
  61:   public DrawablePanel()
  62:   {
  63:     setOpaque(false);
  64:   }
  65: 
  66:   /**
  67:    * Returns the drawable item.
  68:    *
  69:    * @return The drawable item.
  70:    */
  71:   public Drawable getDrawable()
  72:   {
  73:     return this.drawable;
  74:   }
  75: 
  76:   /**
  77:    * Sets the drawable item.
  78:    *
  79:    * @param drawable  the drawable item.
  80:    */
  81:   public void setDrawable(final Drawable drawable)
  82:   {
  83:     this.drawable = drawable;
  84:     revalidate();
  85:     repaint();
  86:   }
  87: 
  88:   /**
  89:    * If the <code>preferredSize</code> has been set to a non-<code>null</code>
  90:    * value just returns it. If the UI delegate's <code>getPreferredSize</code>
  91:    * method returns a non <code>null</code> value then return that; otherwise
  92:    * defer to the component's layout manager.
  93:    *
  94:    * @return the value of the <code>preferredSize</code> property
  95:    * @see #setPreferredSize
  96:    * @see javax.swing.plaf.ComponentUI
  97:    */
  98:   public Dimension getPreferredSize()
  99:   {
 100:     if (this.drawable instanceof ExtendedDrawable)
 101:     {
 102:       final ExtendedDrawable ed = (ExtendedDrawable) this.drawable;
 103:       return ed.getPreferredSize();
 104:     }
 105:     return super.getPreferredSize();
 106:   }
 107: 
 108:   /**
 109:    * If the minimum size has been set to a non-<code>null</code> value just
 110:    * returns it.  If the UI delegate's <code>getMinimumSize</code> method
 111:    * returns a non-<code>null</code> value then return that; otherwise defer to
 112:    * the component's layout manager.
 113:    *
 114:    * @return the value of the <code>minimumSize</code> property
 115:    * @see #setMinimumSize
 116:    * @see javax.swing.plaf.ComponentUI
 117:    */
 118:   public Dimension getMinimumSize()
 119:   {
 120:     if (this.drawable instanceof ExtendedDrawable)
 121:     {
 122:       final ExtendedDrawable ed = (ExtendedDrawable) this.drawable;
 123:       return ed.getPreferredSize();
 124:     }
 125:     return super.getMinimumSize();
 126:   }
 127: 
 128:   /**
 129:    * Returns true if this component is completely opaque.
 130:    * <p/>
 131:    * An opaque component paints every pixel within its rectangular bounds. A
 132:    * non-opaque component paints only a subset of its pixels or none at all,
 133:    * allowing the pixels underneath it to "show through".  Therefore, a
 134:    * component that does not fully paint its pixels provides a degree of
 135:    * transparency.
 136:    * <p/>
 137:    * Subclasses that guarantee to always completely paint their contents should
 138:    * override this method and return true.
 139:    *
 140:    * @return true if this component is completely opaque
 141:    * @see #setOpaque
 142:    */
 143:   public boolean isOpaque()
 144:   {
 145:     if (this.drawable == null)
 146:     {
 147:       return false;
 148:     }
 149:     return super.isOpaque();
 150:   }
 151: 
 152:   /**
 153:    * Calls the UI delegate's paint method, if the UI delegate is
 154:    * non-<code>null</code>.  We pass the delegate a copy of the
 155:    * <code>Graphics</code> object to protect the rest of the paint code from
 156:    * irrevocable changes (for example, <code>Graphics.translate</code>).
 157:    * <p/>
 158:    * If you override this in a subclass you should not make permanent changes to
 159:    * the passed in <code>Graphics</code>. For example, you should not alter the
 160:    * clip <code>Rectangle</code> or modify the transform. If you need to do
 161:    * these operations you may find it easier to create a new
 162:    * <code>Graphics</code> from the passed in <code>Graphics</code> and
 163:    * manipulate it. Further, if you do not invoker super's implementation you
 164:    * must honor the opaque property, that is if this component is opaque, you
 165:    * must completely fill in the background in a non-opaque color. If you do not
 166:    * honor the opaque property you will likely see visual artifacts.
 167:    * <p/>
 168:    * The passed in <code>Graphics</code> object might have a transform other
 169:    * than the identify transform installed on it.  In this case, you might get
 170:    * unexpected results if you cumulatively apply another transform.
 171:    *
 172:    * @param g the <code>Graphics</code> object to protect
 173:    * @see #paint
 174:    * @see javax.swing.plaf.ComponentUI
 175:    */
 176:   protected void paintComponent(Graphics g)
 177:   {
 178:     super.paintComponent(g);
 179:     if (this.drawable == null)
 180:     {
 181:       return;
 182:     }
 183: 
 184:     final Graphics2D g2 = (Graphics2D) g.create
 185:             (0, 0, getWidth(), getHeight());
 186: 
 187:     this.drawable.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
 188:     g2.dispose();
 189:   }
 190: 
 191: }