📄 stylesheetcollection.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.
*
* ------------------------------
* StyleSheetCollection.java
* ------------------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: StyleSheetCollection.java,v 1.5 2003/11/01 19:52:29 taqua Exp $
*
* Changes
* -------------------------
* 12-Jun-2003 : Initial version
*
*/
package org.jfree.report.style;
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.jfree.report.util.HashNMap;
import org.jfree.report.util.InstanceID;
/**
* The StyleSheet collection is assigned to all Elements, all StyleSheets and
* to the JFreeReport and the ReportDefinition objects. This collection is used
* to coordinate the deep cloning of the stylesheets of an report. As bonus
* functionality it allows simplified access to the stylesheets.
*
* @author Thomas Morgner
*/
public class StyleSheetCollection implements Cloneable, Serializable
{
/**
* A style collection entry. This class holds the stylesheet and an reference
* count to the contained stylesheet. A stylesheet can only be removed if
* the reference count is 0.
*/
private static class StyleCollectionEntry implements Serializable
{
/** The reference count of the stylesheet. */
private int referenceCount;
/** The stylesheet. */
private ElementStyleSheet styleSheet;
/**
* Creates a new StyleCollectionEntry for the stylesheet with an reference
* count of 0.
*
* @param styleSheet The ElementStyleSheet that should be stored in that entry.
*/
public StyleCollectionEntry(final ElementStyleSheet styleSheet)
{
this(0, styleSheet);
}
/**
* Creates a new StyleCollectionEntry for the stylesheet and with the given
* reference count.
*
* @param styleSheet The ElementStyleSheet that should be stored in that entry.
* @param referenceCount the initial reference count.
* @throws IllegalArgumentException if the reference count is negative.
* @throws NullPointerException if the given stylesheet is null.
*/
public StyleCollectionEntry(final int referenceCount, final ElementStyleSheet styleSheet)
{
if (referenceCount < 0)
{
throw new IllegalArgumentException("Initial reference count may not be negative.");
}
if (styleSheet == null)
{
throw new NullPointerException("StyleSheet for the entry must not be null.");
}
this.referenceCount = referenceCount;
this.styleSheet = styleSheet;
}
/**
* Returns the reference count.
*
* @return the reference count.
*/
public int getReferenceCount()
{
return referenceCount;
}
/**
* Defines the new reference count for this stylesheet.
*
* @param referenceCount the reference count.
* @throws IllegalArgumentException if the reference count is negative.
*/
public void setReferenceCount(final int referenceCount)
{
if (referenceCount < 0)
{
throw new IllegalArgumentException("Initial reference count may not be negative.");
}
this.referenceCount = referenceCount;
}
/**
* Returns the stylesheet stored in that entry.
*
* @return the stylesheet.
*/
public ElementStyleSheet getStyleSheet()
{
return styleSheet;
}
}
/** The stylesheet storage. */
private HashNMap styleSheets;
/**
* DefaultConstructor.
*/
public StyleSheetCollection()
{
styleSheets = new HashNMap();
}
/**
* Adds the given stylesheet to this collection. This throw an
* IllegalArgumentException if the stylesheet is already added to another
* stylesheet collection.
*
* @param es the element stylesheet
* @throws NullPointerException if the given stylesheet is null.
*/
public void addStyleSheet(final ElementStyleSheet es)
{
addStyleSheet(es, true);
}
/**
* Adds the given stylesheet to this collection. This throw an
* IllegalArgumentException if the stylesheet is already added to another
* stylesheet collection.
*
* @param es the element stylesheet that should be added.
* @param updateRefs true, if the reference counts should be recomputed,
* false otherwise.
* @throws IllegalStateException if the stylesheet is already added, but has
* a different stylesheet collection assigned.
* @throws NullPointerException if the given element stylesheet is null.
*/
protected void addStyleSheet(final ElementStyleSheet es, final boolean updateRefs)
{
if (es == null)
{
throw new NullPointerException("Element stylesheet to be added must not be null.");
}
if (contains(es) == false)
{
styleSheets.add(es.getName(), new StyleCollectionEntry(es));
es.registerStyleSheetCollection(this);
addParents(es);
if (updateRefs)
{
updateReferences();
}
}
// /** assert: stylesheet collection set ...
else
{
if (es.isGlobalDefault() == false)
{
if (es.getStyleSheetCollection() != this)
{
throw new IllegalStateException("Invalid StyleSheet detected!" + es.getClass());
}
}
}
// */
}
/**
* Tests, whether the stylesheet is contained in that collection.
*
* @param es the stylesheet that is searched.
* @return true, if the stylesheet is contained in that collection, false otherwise.
*/
private boolean contains(final ElementStyleSheet es)
{
if (styleSheets.containsKey(es.getName()))
{
final Iterator it = styleSheets.getAll(es.getName());
while (it.hasNext())
{
final StyleCollectionEntry se = (StyleCollectionEntry) it.next();
final ElementStyleSheet ces = se.getStyleSheet();
if (ces.getId() == es.getId())
{
return true;
}
}
}
return false;
}
/**
* Returns all stylesheets for a given name or null, if there is no such stylesheet
* registered.
*
* @param name the name of the stylesheet.
* @return the found stylesheets as object array or null.
*/
public ElementStyleSheet[] getAll(final String name)
{
final StyleCollectionEntry[] data = (StyleCollectionEntry[])
styleSheets.toArray(name, new StyleCollectionEntry[styleSheets.getValueCount(name)]);
if (data.length == 0)
{
return null;
}
final ElementStyleSheet[] retval = new ElementStyleSheet[data.length];
for (int i = 0; i < data.length; i++)
{
final StyleCollectionEntry se = data[i];
retval[i] = se.getStyleSheet();
}
return retval;
}
/**
* Returns the first element stylesheet with that name.
*
* @param name the name of the searched stylesheet.
* @return the stylesheet or null, if there is no such stylesheet.
*/
public ElementStyleSheet getFirst(final String name)
{
final StyleCollectionEntry se = (StyleCollectionEntry) styleSheets.getFirst(name);
if (se == null)
{
return null;
}
return se.getStyleSheet();
}
/**
* Clones this stylesheet collection and all stylesheets contained in that
* collection. The stylesheets of this collection get cloned and reassigned
* after the cloning.
*
* @return the cloned collection.
* @throws CloneNotSupportedException if cloning failed.
*/
public Object clone() throws CloneNotSupportedException
{
final StyleSheetCollection col = (StyleSheetCollection) super.clone();
col.styleSheets = new HashNMap();
// clone all contained stylesheets ...
Iterator it = styleSheets.keySet().iterator();
StyleCollectionEntry[] allElements = new StyleCollectionEntry[0];
while (it.hasNext())
{
final Object key = it.next();
final int len = styleSheets.getValueCount(key);
allElements = (StyleCollectionEntry[]) styleSheets.toArray(key, allElements);
for (int i = 0; i < len; i++)
{
final StyleCollectionEntry se = allElements[i];
final ElementStyleSheet es = se.getStyleSheet();
final ElementStyleSheet esCopy = es.getCopy();
col.styleSheets.add(esCopy.getName(),
new StyleCollectionEntry(se.getReferenceCount(), esCopy));
}
}
// next reconnect the stylesheets
// the default parents dont need to be updated, as they are shared among all
// stylesheets ...
it = col.styleSheets.keySet().iterator();
while (it.hasNext())
{
final Object key = it.next();
final int len = col.styleSheets.getValueCount(key);
allElements = (StyleCollectionEntry[]) col.styleSheets.toArray(key, allElements);
for (int ai = 0; ai < len; ai++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -