1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
package org.jfree.layouting.modules.output.graphics;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import org.pentaho.reporting.libraries.resourceloader.factory.drawable.DrawableWrapper;
/**
* Todo: Document Me
*
* @author Thomas Morgner
*/
public class DrawablePanel extends JPanel
{
private DrawableWrapper drawable;
public DrawablePanel()
{
setOpaque(false);
}
public DrawableWrapper getDrawable()
{
return drawable;
}
public void setDrawableAsRawObject(final Object o)
{
if (o == null)
{
setDrawable(null);
}
else if (o instanceof DrawableWrapper)
{
setDrawable((DrawableWrapper)o);
}
else
{
setDrawable(new DrawableWrapper(o));
}
}
public void setDrawable(final DrawableWrapper drawable)
{
this.drawable = drawable;
revalidate();
repaint();
}
/**
* If the <code>preferredSize</code> has been set to a non-<code>null</code>
* value just returns it. If the UI delegate's <code>getPreferredSize</code>
* method returns a non <code>null</code> value then return that; otherwise
* defer to the component's layout manager.
*
* @return the value of the <code>preferredSize</code> property
* @see #setPreferredSize
* @see javax.swing.plaf.ComponentUI
*/
public Dimension getPreferredSize()
{
if (drawable == null)
{
return new Dimension(0,0);
}
return drawable.getPreferredSize();
}
/**
* If the minimum size has been set to a non-<code>null</code> value just
* returns it. If the UI delegate's <code>getMinimumSize</code> method
* returns a non-<code>null</code> value then return that; otherwise defer to
* the component's layout manager.
*
* @return the value of the <code>minimumSize</code> property
* @see #setMinimumSize
* @see javax.swing.plaf.ComponentUI
*/
public Dimension getMinimumSize()
{
if (drawable == null)
{
return new Dimension(0,0);
}
return drawable.getPreferredSize();
}
/**
* Returns true if this component is completely opaque.
* <p/>
* An opaque component paints every pixel within its rectangular bounds. A
* non-opaque component paints only a subset of its pixels or none at all,
* allowing the pixels underneath it to "show through". Therefore, a
* component that does not fully paint its pixels provides a degree of
* transparency.
* <p/>
* Subclasses that guarantee to always completely paint their contents should
* override this method and return true.
*
* @return true if this component is completely opaque
* @see #setOpaque
*/
public boolean isOpaque()
{
if (drawable == null)
{
return false;
}
return super.isOpaque();
}
/**
* Calls the UI delegate's paint method, if the UI delegate is
* non-<code>null</code>. We pass the delegate a copy of the
* <code>Graphics</code> object to protect the rest of the paint code from
* irrevocable changes (for example, <code>Graphics.translate</code>).
* <p/>
* If you override this in a subclass you should not make permanent changes to
* the passed in <code>Graphics</code>. For example, you should not alter the
* clip <code>Rectangle</code> or modify the transform. If you need to do
* these operations you may find it easier to create a new
* <code>Graphics</code> from the passed in <code>Graphics</code> and
* manipulate it. Further, if you do not invoker super's implementation you
* must honor the opaque property, that is if this component is opaque, you
* must completely fill in the background in a non-opaque color. If you do not
* honor the opaque property you will likely see visual artifacts.
* <p/>
* The passed in <code>Graphics</code> object might have a transform other
* than the identify transform installed on it. In this case, you might get
* unexpected results if you cumulatively apply another transform.
*
* @param g the <code>Graphics</code> object to protect
* @see #paint
* @see javax.swing.plaf.ComponentUI
*/
protected void paintComponent(final Graphics g)
{
super.paintComponent(g);
if (drawable == null)
{
return;
}
final Graphics2D g2 = (Graphics2D) g.create
(0, 0, getWidth(), getHeight());
drawable.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
g2.dispose();
}
}
|