📄 defaultelementrenderer.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner (taquera@sherito.org);
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* ------------------------------
* DefaultElementRenderer.java
* ------------------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: DefaultElementRenderer.java,v 1.2 2004/04/20 18:54:56 taqua Exp $
*
* Changes
* -------------------------
* 28.09.2003 : Initial version
*
*/
package org.jfree.designer.visualeditor.bandeditor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.border.LineBorder;
import org.jfree.report.Element;
import org.jfree.report.filter.templates.LabelTemplate;
import org.jfree.report.filter.templates.StringFieldTemplate;
import org.jfree.report.filter.templates.Template;
import org.jfree.report.modules.parser.ext.factory.templates.DefaultTemplateCollection;
import org.jfree.report.modules.parser.ext.factory.templates.TemplateCollector;
import org.jfree.report.style.ElementStyleSheet;
import org.jfree.report.util.Log;
public final class DefaultElementRenderer
extends JComponent
implements ElementRenderer
{
private Element element;
public DefaultElementRenderer ()
{
setDoubleBuffered(false);
}
public final JComponent getElementRenderer
(final Element element, final boolean isSelected, final boolean hasFocus,
final float detail)
{
if (isSelected)
{
setBackground(Color.blue);
}
else
{
setBackground(Color.white);
}
if (hasFocus)
{
setBorder(new LineBorder(Color.green));
}
else
{
setBorder(null);
}
setElement(element);
return this;
}
public final Element getElement ()
{
return element;
}
public final void setElement (final Element element)
{
this.element = element;
}
/**
* If the UI delegate is non-null, call its paint method. We pass the delegate a copy
* of the Graphics object to protect the rest of the paint code from irrevocable changes
* (e.g. Graphics.translate()).
*
* @see #paint
*/
protected final void paintComponent (final Graphics g)
{
final Graphics2D g2 = (Graphics2D) g;
Log.debug("Painting Component: " + getElement().getName());
final Rectangle2D relElementRect = (Rectangle2D)
element.getStyle().getStyleProperty(ElementStyleSheet.BOUNDS);
if (relElementRect == null)
{
super.paintComponent(g);
Log.debug("Aborted Painting Component: " + getElement().getName());
return;
}
// calculate the name for display, default is just the element name
String elementType = null;
if (element.getDataSource() instanceof Template)
{
final Template template = (Template) element.getDataSource();
if (template instanceof LabelTemplate)
{
elementType = "Label";
}
else if (template instanceof StringFieldTemplate)
{
final StringFieldTemplate sft = (StringFieldTemplate) template;
elementType = "String-Field: " + sft.getField();
}
else
{
elementType = template.getClass().getName();
}
// feeeeeed me : more element types here ...
// make this generic (OO!) instead of hardcoding ...
final TemplateCollector templateCollector = new TemplateCollector();
templateCollector.addTemplateCollection(new DefaultTemplateCollection());
}
// show the element name as information. Here a lot remains to be done:
// calc font, evaluate vertical and horizontal alignment,
// cut name at end of element etc.
// display label content instead of label name,
// display example or name (requires change in parser?), make this
// switchable in checkbox...
// we keep the color => a selected element will have text in red/blue
String elementName = element.getName();
if (elementName.startsWith(Element.ANONYMOUS_ELEMENT_PREFIX))
{
elementName = elementType;
}
else
{
elementName = elementType + ": " + elementName;
}
g2.setPaint((Paint) element.getStyle().getStyleProperty(ElementStyleSheet.PAINT));
g2.setFont(element.getStyle().getFontDefinitionProperty().getFont());
final float lineheight = g.getFontMetrics().getAscent();
g.drawString(elementName, 0, (int) (0 + lineheight));
// todo: beautify: text size from band heigt, display centered, if possible
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -