📄 elementstylesheet.java
字号:
cloneDefaultCached[i] = defaultCached[i];
}
sc.parentsCached = cloneParentsCached;
sc.defaultCached = cloneDefaultCached;
return sc;
}
catch (CloneNotSupportedException cne)
{
throw new IllegalStateException("Clone failed.");
}
}
/**
* Creates the cached object array for the default element style sheets.
*/
private void defaultToCache()
{
if (defaultCached == null)
{
defaultCached = (ElementStyleSheet[])
defaultSheets.toArray(new ElementStyleSheet[defaultSheets.size()]);
}
}
/**
* Creates the cached object array for the parent element style sheets.
*
*/
private void parentsToCache()
{
if (parentsCached == null)
{
parentsCached = (ElementStyleSheet[])
parents.toArray(new ElementStyleSheet[parents.size()]);
}
}
/**
* Returns a boolean style (defaults to false if the style is not found).
*
* @param key the style key.
*
* @return <code>true</code> or <code>false</code>.
*/
public boolean getBooleanStyleProperty(final StyleKey key)
{
return getBooleanStyleProperty(key, false);
}
/**
* Returns a boolean style.
*
* @param key the style key.
* @param defaultValue the default value.
*
* @return true or false.
*/
public boolean getBooleanStyleProperty(final StyleKey key, final boolean defaultValue)
{
final Boolean b = (Boolean) getStyleProperty(key, null);
if (b == null)
{
return defaultValue;
}
return b.booleanValue();
}
/**
* Returns an integer style.
*
* @param key the style key.
* @param def the default value.
*
* @return the style value.
*/
public int getIntStyleProperty(final StyleKey key, final int def)
{
final Integer i = (Integer) getStyleProperty(key, new Integer(def));
return i.intValue();
}
/**
* Checks, whether the given key is one of the keys used to define the
* font definition from this stylesheet.
*
* @param key the key that should be checked.
* @return true, if the key is a font definition key, false otherwise.
*/
private boolean isFontDefinitionProperty (final StyleKey key)
{
if (key.equals(FONT))
{
return true;
}
if (key.equals(FONTSIZE))
{
return true;
}
if (key.equals(BOLD))
{
return true;
}
if (key.equals(ITALIC))
{
return true;
}
if (key.equals(UNDERLINED))
{
return true;
}
if (key.equals(STRIKETHROUGH))
{
return true;
}
if (key.equals(EMBEDDED_FONT))
{
return true;
}
if (key.equals(FONTENCODING))
{
return true;
}
return false;
}
/**
* Returns the font for this style-sheet.
*
* @return the font.
*/
public FontDefinition getFontDefinitionProperty()
{
if (fontDefinition == null)
{
final String name = (String) getStyleProperty(FONT);
final int size = getIntStyleProperty(FONTSIZE, -1);
final boolean bold = getBooleanStyleProperty(BOLD);
final boolean italic = getBooleanStyleProperty(ITALIC);
final boolean underlined = getBooleanStyleProperty(UNDERLINED);
final boolean strike = getBooleanStyleProperty(STRIKETHROUGH);
final boolean embed = getBooleanStyleProperty(EMBEDDED_FONT);
final String encoding = (String) getStyleProperty(FONTENCODING);
final FontDefinition retval = new FontDefinition(name, size, bold, italic, underlined, strike,
encoding, embed);
if (isAllowCaching())
{
fontDefinition = retval;
}
else
{
return retval;
}
}
return fontDefinition;
}
/**
* Sets the font for this style-sheet.
*
* @param font the font (<code>null</code> not permitted).
*/
public void setFontDefinitionProperty(final FontDefinition font)
{
if (font == null)
{
throw new NullPointerException("ElementStyleSheet.setFontStyleProperty: font is null.");
}
setStyleProperty(FONT, font.getFontName());
setStyleProperty(FONTSIZE, new Integer(font.getFontSize()));
setBooleanStyleProperty(BOLD, font.isBold());
setBooleanStyleProperty(ITALIC, font.isItalic());
setBooleanStyleProperty(UNDERLINED, font.isUnderline());
setBooleanStyleProperty(STRIKETHROUGH, font.isStrikeThrough());
setBooleanStyleProperty(EMBEDDED_FONT, font.isEmbeddedFont());
setStyleProperty(FONTENCODING, font.getFontEncoding(null));
}
/**
* Returns an enumeration of all local property keys.
*
* @return an enumeration of all localy defined style property keys.
*/
public Iterator getDefinedPropertyNames()
{
return properties.keySet().iterator();
}
/**
* Adds a {@link StyleChangeListener}.
*
* @param l the listener.
*/
public void addListener(final StyleChangeListener l)
{
styleChangeSupport.addListener(l);
}
/**
* Removes a {@link StyleChangeListener}.
*
* @param l the listener.
*/
public void removeListener(final StyleChangeListener l)
{
styleChangeSupport.removeListener(l);
}
/**
* Forwards a change event notification to all registered {@link StyleChangeListener} objects.
*
* @param source the source of the change.
* @param key the style key.
* @param value the new value.
*/
public void styleChanged(final ElementStyleSheet source, final StyleKey key, final Object value)
{
if (styleCache != null)
{
styleCache.remove(key);
if (isFontDefinitionProperty(key))
{
fontDefinition = null;
}
}
styleChangeSupport.fireStyleChanged(key, value);
}
/**
* Forwards a change event notification to all registered {@link StyleChangeListener} objects.
*
* @param source the source of the change.
* @param key the style key.
*/
public void styleRemoved(final ElementStyleSheet source, final StyleKey key)
{
if (styleCache != null)
{
styleCache.remove(key);
if (isFontDefinitionProperty(key))
{
fontDefinition = null;
}
}
styleChangeSupport.fireStyleRemoved(key);
}
/**
* Helper method for serialization.
*
* @param out the output stream where to write the object.
* @throws IOException if errors occur while writing the stream.
*/
private void writeObject(final ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
final int size = properties.size();
out.writeInt(size);
final Iterator it = properties.keySet().iterator();
while (it.hasNext())
{
final Object key = it.next();
out.writeObject(key);
final Object value = properties.get(key);
SerializerHelper.getInstance().writeObject(value, out);
}
}
/**
* Helper method for serialization.
*
* @param in the input stream from where to read the serialized object.
* @throws IOException when reading the stream fails.
* @throws ClassNotFoundException if a class definition for a serialized object
* could not be found.
*/
private void readObject(final ObjectInputStream in)
throws IOException, ClassNotFoundException
{
styleChangeSupport = new StyleChangeSupport(this);
in.defaultReadObject();
final int size = in.readInt();
properties = new HashMap(size);
for (int i = 0; i < size; i++)
{
final Object key = in.readObject();
final Object value = SerializerHelper.getInstance().readObject(in);
properties.put(key, value);
}
}
/**
* Creates and returns a copy of this object. This method calls getCopy().
*
* @return a clone of this instance.
* @see Cloneable
*/
public Object clone()
{
return getCopy();
}
/**
* Returns the stylesheet collection of this element stylesheet, or null,
* if this stylessheet is not assigned with an collection.
*
* @return the collection or null.
*/
public StyleSheetCollection getStyleSheetCollection()
{
return collectionHelper.getStyleSheetCollection();
}
/**
* Registers the given StyleSheet collection with this ElementStyleSheet.
* If there is already another stylesheet collection registered, this method
* will throw an <code>InvalidStyleSheetCollectionException</code>.
*
* @param styleSheetCollection the stylesheet collection that should be registered.
* @throws InvalidStyleSheetCollectionException if there is already an other
* stylesheet registered.
* @throws NullPointerException if the given stylesheet collection is null.
*/
public void registerStyleSheetCollection(final StyleSheetCollection styleSheetCollection)
throws InvalidStyleSheetCollectionException
{
collectionHelper.registerStyleSheetCollection(styleSheetCollection);
}
/**
* Unregisters the given stylesheet collection from this ElementStyleSheet. If this stylesheet
* collection is not registered with this ElementStyleSheet, this method will throw an
* <code>InvalidStyleSheetCollectionException</code>
*
* @param styleSheetCollection the stylesheet collection that should be unregistered.
* @throws InvalidStyleSheetCollectionException if there is already an other stylesheet
* registered.
* @throws NullPointerException if the given stylesheet collection is null.
*/
public void unregisterStyleSheetCollection(final StyleSheetCollection styleSheetCollection)
throws InvalidStyleSheetCollectionException
{
collectionHelper.unregisterStyleSheetCollection(styleSheetCollection);
}
/**
* Returns the ID of the stylesheet. The ID does identify an element stylesheet an
* all all cloned instances of that stylesheet.
*
* @return the ID of this stylesheet.
*/
public InstanceID getId()
{
return id;
}
/**
* Returns true, if this stylesheet is one of the global default stylesheets.
* Global default stylesheets are unmodifiable and shared among all element stylesheets.
*
* @return true, if this is one of the unmodifiable global default stylesheets,
* false otherwise.
*/
public boolean isGlobalDefault()
{
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -