📄 propertymanager.java
字号:
if (propertyList.overridden)
return object.getProperty(property);
if (propertyList.size() > 0)
return propertyList.get(propertyList.size() - 1).getValue();
return NO_STYLE;
}
/**
* Applies inherited values to an object's children. Has no effect if the
* values have already been inherited.
*
*@param object the object which should pass inherited values to its children
*@throws StylesheetException if an error occurs
*/
public static void cascadeFrom(Styleable object) throws StylesheetException {
for (Styleable child : object.getStyleableChildren())
cascadeTo(child, true);
}
/**
* Applies inherited values from the object's parent. Has no effect if the
* values have already been inherited.
*
*@param object the object which should inherit values from its parent
*@param recurse true to recursively call <code>cascadeTo</code> on the
* object's children
*@throws StylesheetException if an error occurs
*/
public static void cascadeTo(Styleable object, boolean recurse)
throws StylesheetException {
Styleable parent = object.getStyleableParent();
if (parent != null) {
List<PropertyValue> propertyList =
getAllPropertiesForObject(parent);
Set<PropertyValue> cascaded = null;
for (PropertyValue property : propertyList) {
if (property.source != null &&
object.isPropertyInherited(property.getPropertyName())) {
Priority oldPriority = property.getPriority();
Priority newPriority = (Priority) oldPriority.clone();
newPriority.setDepth(oldPriority.getDepth() + 1);
applyProperty(object, property.getPropertyName(),
property.getValue(), property.getSource(),
newPriority, property.getAnimation(), true);
if (cascaded == null)
cascaded = new HashSet<PropertyValue>();
cascaded.add(property);
}
}
// remove defunct cascaded properties
propertyList = getAllPropertiesForObject(object);
for (PropertyValue property : propertyList) {
if (property.wasInherited()) {
boolean matched = false;
for (PropertyValue cascadedProperty : cascaded) {
if (cascadedProperty.getPropertyName().equals(
property.getPropertyName()) &&
cascadedProperty.getSource() ==
property.getSource())
matched = true;
}
if (!matched)
removeProperty(object, property.getPropertyName(),
property.getValue(), property.getSource(),
property.getPriority(), property.getAnimation(),
true);
}
}
}
if (recurse) {
for (Styleable child : object.getStyleableChildren())
cascadeTo(child, true);
}
}
/**
* Returns a list of all pseudoclass listeners currently applied to an
* object.
*/
private static List<PseudoclassValue> getPseudoclassListForObject(
Styleable object) {
List<PseudoclassValue> pseudoclassList = pseudoclasses.get(object);
if (pseudoclassList == null) {
pseudoclassList = new ArrayList<PseudoclassValue>();
pseudoclasses.put(object, pseudoclassList);
}
return pseudoclassList;
}
/**
* Adds a pseudoclass listener to a styleable object.
*
*@param object the object to listen to
*@param pseudoclass the name of the pseudoclass to listen for
*@param listener the listener which should be notified when the pseudoclass
* is added or removed
*@param source the source applying the listener
*@throws StylesheetException if the pseudoclass is unsupported
*/
public static void addPseudoclassListener(Styleable object,
String pseudoclass, PseudoclassListener listener, Rule source)
throws StylesheetException {
List<PseudoclassValue> list = getPseudoclassListForObject(object);
list.add(new PseudoclassValue(pseudoclass, listener, source));
object.addPseudoclassListener(pseudoclass, listener);
}
/**
* Removes a pseudoclass listener from a styleable object. Has no effect if
* the listener is not present.
*
*@param object the object to which the listener was attached
*@param pseudoclass the name of the pseudoclass
*@param listener the listener to remove
*@param source the source which applied the listener
*@throws StylesheetException if the pseudoclass is unsupported
*/
public static void removePseudoclassListener(Styleable object,
String pseudoclass, PseudoclassListener listener, Rule source)
throws StylesheetException {
List<PseudoclassValue> list = getPseudoclassListForObject(object);
list.remove(new PseudoclassValue(pseudoclass, listener, source));
object.removePseudoclassListener(pseudoclass, listener);
}
/**
* Removes all properties and pseudoclass listeners in effect for the
* specified object and all of its descendents.
*
*@param object the object from which to remove styles
*@throws StylesheetException if an error occurs
*/
public static void removeAllStyles(Styleable object) throws StylesheetException {
List<PropertyValue> propertyList = getAllPropertiesForObject(object);
for (PropertyValue property : propertyList) {
if (property.getSource() != null) {
removeProperty(object, property.getPropertyName(),
property.getValue(), property.getSource(),
property.getPriority(), property.getAnimation());
}
}
List<PseudoclassValue> pseudoclassList =
new ArrayList<PseudoclassValue>(
getPseudoclassListForObject(object));
for (PseudoclassValue pseudoclass : pseudoclassList) {
removePseudoclassListener(object, pseudoclass.getPseudoclass(),
pseudoclass.getPseudoclassListener(), pseudoclass.getSource());
}
for (Styleable child : object.getStyleableChildren())
removeAllStyles(child);
}
/**
* Removes all properties applied by a given stylesheet from the specified
* object and all of its descendents.
*
*@param stylesheet the stylesheet whose properties should be removed
*@param object the object from which properties should be removed
*@throws StylesheetException if an error occurs
*/
public static void removeStylesheet(Stylesheet stylesheet, Styleable object)
throws StylesheetException {
List<PropertyValue> propertyList = getAllPropertiesForObject(object);
for (PropertyValue property : propertyList) {
if (property.getSource() != null &&
property.getSource().getStylesheet() == stylesheet) {
removeProperty(object, property.getPropertyName(),
property.getValue(), property.getSource(),
property.getPriority(), property.getAnimation(),
property.wasInherited());
}
}
List<PseudoclassValue> pseudoclassList =
new ArrayList<PseudoclassValue>(
getPseudoclassListForObject(object));
for (PseudoclassValue pseudoclass : pseudoclassList) {
if (pseudoclass.getSource() != null &&
pseudoclass.getSource().getStylesheet() == stylesheet) {
removePseudoclassListener(object, pseudoclass.getPseudoclass(),
pseudoclass.getPseudoclassListener(),
pseudoclass.getSource());
}
}
for (Styleable child : object.getStyleableChildren())
removeStylesheet(stylesheet, child);
}
/**
* Applies a property value to an object. The new value will only be
* visible if it is currently the highest-priority value in effect for the
* given property. For example, if you assign a high-priority green
* foreground and a low-priority red foreground to a given object, the
* object's foreground will be green. If you later remove the green
* foreground, the foreground will change to red. Removing both foreground
* values will cause the foreground to revert to its original value.
*
*@param object the object to which the property should be assigned
*@param property the name of the property to affect, e.g. "foreground"
*@param newValue the new value of the property
*@param source the source applying this value
*@param priority the value's priority
*@see #removeProperty
*/
public static void applyProperty(Styleable object, String property,
Object newValue, Rule source, Priority priority,
Animation animation) throws StylesheetException {
applyProperty(object, property, newValue, source, priority, animation,
false);
}
private static void applyProperty(Styleable object, String property,
Object newValue, Rule source, Priority priority,
Animation animation, boolean wasInherited)
throws StylesheetException {
if (newValue instanceof Size && !((Size) newValue).isComputed())
throw new IllegalArgumentException("must compute size '" +
newValue + "' before applying");
Map<String, Object> split = object.splitCompoundProperty(property,
newValue);
if (split != null) {
for (Map.Entry<String, Object> e : split.entrySet())
applyProperty(object, e.getKey(), e.getValue(), source,
priority, animation, wasInherited);
}
else if (!isPropertyApplied(object, property, source, priority,
wasInherited)) {
Object value = getCurrentValue(object, property);
if (value == NO_STYLE) {
if (AnimationManager.isAnimating(object, property))
value = AnimationManager.getTargetValue(object, property);
else
value = object.getProperty(property);
propertyApplied(object, property,
value,
null, new Priority(false, -1, -1, -1, -1, -1, -1),
null, false);
}
propertyApplied(object, property, newValue, source, priority,
animation, wasInherited);
if (animation != null)
AnimationManager.animateTransition(object, property,
newValue, animation);
else
object.setProperty(property, getCurrentValue(object, property));
}
}
/**
* Removes a property value from an object. Has no effect if the property
* was not actually in effect, and if a higher-priority value was masking
* the removed value the visible property value will not actually change.
*
*@param object the object from which the property should be removed
*@param property the name of the property to affect, e.g. "foreground"
*@param oldValue the value to remove
*@param source the source which applied this value
*@param priority the value's priority
*@see #applyProperty
*/
public static void removeProperty(Styleable object, String property,
Object oldValue, Rule source, Priority priority,
Animation animation) throws StylesheetException {
removeProperty(object, property, oldValue, source, priority,
animation, false);
}
private static void removeProperty(Styleable object, String property,
Object oldValue, Rule source, Priority priority,
Animation animation, boolean wasInherited)
throws StylesheetException {
Map<String, Object> split = object.splitCompoundProperty(property,
oldValue);
if (split != null) {
for (Map.Entry<String, Object> e : split.entrySet())
removeProperty(object, e.getKey(), e.getValue(), source,
priority, animation, wasInherited);
}
if (isPropertyApplied(object, property, source, priority,
wasInherited)) {
propertyRemoved(object, property, oldValue, source, priority,
animation, wasInherited);
Object value = getCurrentValue(object, property);
if (value == NO_STYLE)
throw new java.lang.IllegalStateException(
"found unexpected NO_STYLE value");
if (animation != null)
AnimationManager.animateTransition(object, property,
getCurrentValue(object, property), animation);
else
object.setProperty(property, getCurrentValue(object, property));
Map<String, PropertyList> propertyMap = properties.get(object);
if (propertyMap != null) {
PropertyList propertyList = propertyMap.get(property);
if (propertyList.size() == 1)
propertyMap.remove(property); // no styles left
}
}
else if (debug)
System.err.println("WARNING: attempted to remove property " +
object + "." + property + " (" + oldValue + "), but it was " +
"not present");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -