📄 elementstylesheet.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner;
*
* (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.
*
* ----------------------
* ElementStyleSheet.java
* ----------------------
* (C)opyright 2002, 2003 by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: ElementStyleSheet.java,v 1.12 2003/11/12 22:40:03 taqua Exp $
*
* Changes
* -------
* 05-Dec-2002 : Added Javadocs (DG);
* 12-Dec-2002 : First BugFix: setFontStyle must use font.getName instead of font.getFontName
* or a totally different font family is used.
* 03-Jan-2003 : Javadoc updates (DG);
* 15-Jun-2003 : Cloning did not register the clone as change listener with the parents
*/
package org.jfree.report.style;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.jfree.report.ElementAlignment;
import org.jfree.report.util.InstanceID;
import org.jfree.report.util.SerializerHelper;
/**
* An element style-sheet contains zero, one or many attributes that affect the appearance of
* report elements. For each attribute, there is a predefined key that can be used to access
* that attribute in the style sheet.
* <p>
* Every report element has an associated style-sheet.
* <p>
* A style-sheet maintains a list of parent style-sheets. If an attribute is not defined in a
* style-sheet, the code refers to the parent style-sheets to see if the attribute is defined
* there.
* <p>
* All StyleSheet entries are checked against the StyleKeyDefinition for validity.
*
* @author Thomas Morgner
*/
public class ElementStyleSheet implements Serializable, StyleChangeListener, Cloneable
{
/**
* Internal helper class to handle the style sheet collection properly.
*/
private static class ElementStyleSheetCollectionHelper
extends StyleSheetCollectionHelper
{
/** The ElementStyleSheet for which we handle the stylesheet collection. */
private ElementStyleSheet es;
/**
* Creates a new helper for the given ElementStyleSheet.
*
* @param es the ElementStyleSheet whose stylesheet collection should be managed.
*/
public ElementStyleSheetCollectionHelper(final ElementStyleSheet es)
{
if (es == null)
{
throw new NullPointerException("ElementStyleSheet must not be null.");
}
this.es = es;
}
/**
* Handles the stylesheet collection registration for the ElementStyleSheet and
* all parent and default parent stylesheets.
*/
protected void handleRegisterStyleSheetCollection()
{
this.getStyleSheetCollection().addStyleSheet(es);
}
/**
* Handles the stylesheet collection unregistration for the ElementStyleSheet and
* all parent and default parent stylesheets.
*/
protected void handleUnregisterStyleSheetCollection()
{
this.getStyleSheetCollection().remove(es);
}
}
/**
* A marker to indicate that none of the parent stylesheets defines this
* value.
*/
private static class UndefinedValue
{
/**
* DefaultConstructor.
*/
public UndefinedValue()
{
}
}
/** A singleton marker for the cache. */
private static final UndefinedValue UNDEFINED_VALUE = new UndefinedValue();
/** A key for the 'minimum size' of an element. */
public static final StyleKey MINIMUMSIZE = StyleKey.getStyleKey("min-size", Dimension2D.class);
/** A key for the 'maximum size' of an element. */
public static final StyleKey MAXIMUMSIZE = StyleKey.getStyleKey("max-size", Dimension2D.class);
/** A key for the 'preferred size' of an element. */
public static final StyleKey PREFERREDSIZE = StyleKey.getStyleKey("preferred-size",
Dimension2D.class);
/** A key for the 'bounds' of an element. */
public static final StyleKey BOUNDS = StyleKey.getStyleKey("bounds", Rectangle2D.class);
/** A key for an element's 'visible' flag. */
public static final StyleKey VISIBLE = StyleKey.getStyleKey("visible", Boolean.class);
/** A key for the 'paint' used to color an element. */
public static final StyleKey PAINT = StyleKey.getStyleKey("paint", Color.class);
/** A key for the 'stroke' used to draw an element. */
public static final StyleKey STROKE = StyleKey.getStyleKey("stroke", Stroke.class);
/** A key for the 'font name' used to draw element text. */
public static final StyleKey FONT = StyleKey.getStyleKey("font", String.class);
/** A key for the 'font size' used to draw element text. */
public static final StyleKey FONTSIZE = StyleKey.getStyleKey("font-size", Integer.class);
/** A key for the 'font size' used to draw element text. */
public static final StyleKey LINEHEIGHT = StyleKey.getStyleKey("line-height", Float.class);
/** A key for an element's 'bold' flag. */
public static final StyleKey BOLD = StyleKey.getStyleKey("font-bold", Boolean.class);
/** A key for an element's 'italic' flag. */
public static final StyleKey ITALIC = StyleKey.getStyleKey("font-italic", Boolean.class);
/** A key for an element's 'underlined' flag. */
public static final StyleKey UNDERLINED = StyleKey.getStyleKey("font-underline",
Boolean.class);
/** A key for an element's 'strikethrough' flag. */
public static final StyleKey STRIKETHROUGH = StyleKey.getStyleKey("font-strikethrough",
Boolean.class);
/** A key for an element's 'embedd' flag. */
public static final StyleKey EMBEDDED_FONT = StyleKey.getStyleKey("font-embedded", Boolean.class);
/** A key for an element's 'embedd' flag. */
public static final StyleKey FONTENCODING = StyleKey.getStyleKey("font-encoding", String.class);
/** A key for the horizontal alignment of an element. */
public static final StyleKey ALIGNMENT = StyleKey.getStyleKey("alignment",
ElementAlignment.class);
/** A key for the vertical alignment of an element. */
public static final StyleKey VALIGNMENT = StyleKey.getStyleKey("valignment",
ElementAlignment.class);
/** A key for an element's 'scale' flag. */
public static final StyleKey SCALE = StyleKey.getStyleKey("scale", Boolean.class);
/** A key for an element's 'keep aspect ratio' flag. */
public static final StyleKey KEEP_ASPECT_RATIO = StyleKey.getStyleKey("keepAspectRatio",
Boolean.class);
/** A key for the dynamic height flag for an element. */
public static final StyleKey DYNAMIC_HEIGHT = StyleKey.getStyleKey("dynamic_height",
Boolean.class);
/**
* The Layout Cacheable stylekey. Set this stylekey to false, to define that the element
* is not cachable. This key defaults to true.
*/
public static final StyleKey ELEMENT_LAYOUT_CACHEABLE = StyleKey.getStyleKey("layout-cacheable",
Boolean.class);
/** The string that is used to end a text if not all text fits into the element. */
public static final StyleKey RESERVED_LITERAL = StyleKey.getStyleKey
("reserved-literal", String.class);
/**
* The Layout Cacheable stylekey. Set this stylekey to false, to define that the element
* is not cachable. This key defaults to true.
*/
public static final StyleKey TRIM_TEXT_CONTENT = StyleKey.getStyleKey("trim-text-content",
Boolean.class);
/** The instance id of this ElementStyleSheet. This id is shared among all clones. */
private InstanceID id;
/** The style-sheet name. */
private String name;
/** The style-sheet properties. */
private transient HashMap properties;
/** Storage for the parent style sheets (if any). */
private ArrayList parents;
/** Storage for readonly style sheets. */
private ArrayList defaultSheets;
/** Parent style sheet cache. */
private transient ElementStyleSheet[] parentsCached;
/** Default style sheet cache. */
private transient ElementStyleSheet[] defaultCached;
/** Style cache. */
private transient HashMap styleCache;
/** Style change support. */
private transient StyleChangeSupport styleChangeSupport;
/** A flag that controls whether or not caching is allowed. */
private boolean allowCaching;
/** An unmodifiable list, chaching the return value from getParents(). */
private transient List parentsListCached;
/** An unmodifiable list, chaching the return value from getDefaultParents(). */
private transient List defaultParentsListCached;
/** The cached font definition instance from this stylessheet. */
private transient FontDefinition fontDefinition;
/**
* The stylesheet collection helper implementation that manages the
* stylesheet collection for this ElementStyleSheet.
*/
private transient ElementStyleSheetCollectionHelper collectionHelper;
/**
* Creates a new element style-sheet with the given name. The style-sheet initially contains
* no attributes, and has no parent style-sheets.
*
* @param name the name (<code>null</code> not permitted).
*/
public ElementStyleSheet(final String name)
{
if (name == null)
{
throw new NullPointerException("ElementStyleSheet constructor: name is null.");
}
this.id = new InstanceID();
this.name = name;
this.properties = new HashMap();
this.parents = new ArrayList(5);
this.defaultSheets = new ArrayList(5);
this.styleChangeSupport = new StyleChangeSupport(this);
this.collectionHelper = new ElementStyleSheetCollectionHelper(this);
}
/**
* Returns <code>true</code> if caching is allowed, and <code>false</code> otherwise.
*
* @return A boolean.
*/
public boolean isAllowCaching()
{
return allowCaching;
}
/**
* Returns true, if the given key is locally defined, false otherwise.
* @param key the key to test
* @return true, if the key is local, false otherwise.
*/
public boolean isLocalKey(StyleKey key)
{
return properties.containsKey(key);
}
/**
* Sets the flag that controls whether or not caching is allowed.
*
* @param allowCaching the flag value.
*/
public void setAllowCaching(final boolean allowCaching)
{
this.allowCaching = allowCaching;
}
/**
* Returns the name of the style-sheet.
*
* @return the name (never <code>null</code>).
*/
public String getName()
{
return name;
}
/**
* Adds a parent style-sheet. This method adds the parent to the beginning of the
* list, and guarantees, that this parent is queried first.
*
* @param parent the parent (<code>null</code> not permitted).
*/
public synchronized void addParent(final ElementStyleSheet parent)
{
addParent(0, parent);
}
/**
* Adds a parent style-sheet. This method adds the parent to the beginning of the
* list, and guarantees, that this parent is queried first.
* <p>
* The default parents operations are reserved for the system internal stylesheet
* operations. If you want to add own stylesheets, use the addParent methods.
*
* @param parent the parent (<code>null</code> not permitted).
*/
public synchronized void addDefaultParent(final ElementStyleSheet parent)
{
addDefaultParent(0, parent);
}
/**
* Adds a parent style-sheet. Parents on a lower position are queried before any
* parent with an higher position in the list.
*
* @param position the position where to insert the parent style sheet
* @param parent the parent (<code>null</code> not permitted).
*
* @throws IndexOutOfBoundsException if the position is invalid (pos < 0 or pos >=
* numberOfParents)
*/
public synchronized void addParent(final int position, final ElementStyleSheet parent)
{
if (parent == null)
{
throw new NullPointerException("ElementStyleSheet.addParent(...): parent is null.");
}
if (parent.isSubStyleSheet(this) == false)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -