📄 beanstylesupport.java
字号:
/* * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.stylesheet.styleable;import java.awt.*;import java.awt.event.*;import java.beans.*;import java.io.IOException;import java.lang.reflect.*;import java.util.*;import org.w3c.dom.*;import org.w3c.dom.Node;import org.jdesktop.beansbinding.*;import com.sun.stylesheet.*;import com.sun.stylesheet.types.*;/** * Provides basic <code>Styleable</code> support for all instances of a * JavaBeans class. Java Beans introspection is used to determine the object's * supported properties, and features which cannot be deduced automatically * (such as finding parents and children) are stubbed out. * <p> * This class does not itself implement <code>Styleable</code>; it is instead * used by the generic wrapper class {@link DefaultStyleable}. * *@author Ethan Nicholas */public class BeanStyleSupport implements StyleSupport { /** The class that this handler provides support for. */ private Class beanClass; /** The BeanInfo for the beanClass. */ protected BeanInfo beanInfo; /** All matching Java classes. */ private Class[] classes; /** Maps property names to their respective PropertyHandlers. */ protected Map<String, PropertyHandler> properties; /** Maps EL expressions to ELProperties. */ private Map<String, ELProperty> elProperties; /** Maps PseudoclassListeners to their PropertyStateListener adapters. */ private Map<PseudoclassListener, EventListener> propertyStateListeners; /** * Creates a new <code>BeanStyleSupport</code> which provides support for * the specified class. * *@param beanClass the class which this handler supports */ public BeanStyleSupport(Class beanClass) { this.beanClass = beanClass; } /** Performs introspection on the beanClass and stores the results. */ protected void init() throws IntrospectionException { if (beanInfo == null) { // perform introspection & cache the results beanInfo = getBeanInfo(beanClass); PropertyDescriptor[] propertiesArray = beanInfo.getPropertyDescriptors(); properties = new HashMap<String, PropertyHandler>(); for (int i = propertiesArray.length - 1; i >= 0; i--) createPropertyHandler(propertiesArray[i]); Class[] interfaces = beanClass.getInterfaces(); classes = new Class[interfaces.length + 1]; classes[0] = beanClass; System.arraycopy(interfaces, 0, classes, 1, interfaces.length); } } protected void createPropertyHandler(PropertyDescriptor descriptor) { String name = descriptor.getName(); properties.put(name, new DefaultPropertyHandler(descriptor)); if (descriptor.getPropertyType() == Font.class) { properties.put(name + "-size", new FontSizeHandler(descriptor)); properties.put(name + "-family", new FontFamilyHandler(descriptor)); properties.put(name + "-style", new FontStyleHandler(descriptor)); properties.put(name + "-weight", new FontWeightHandler(descriptor)); } } /** * Returns the class which this <code>BeanStyleSupport</code> object * supports. */ public Class getBeanClass() { return beanClass; } /** Returns the <code>BeanInfo</code> for the class which this * <code>BeanStyleSupport</code> supports. */ public BeanInfo getBeanInfo() { try { init(); } catch (IntrospectionException e) { throw new RuntimeException(e); } return beanInfo; } /** Returns the <code>BeanInfo</code> for the specified class. * *@param beanClass the bean class for which to retrieve * <code>BeanInfo</code> *@return the class' <code>BeanInfo</code> */ public static BeanInfo getBeanInfo(Class beanClass) throws IntrospectionException { return Introspector.getBeanInfo(beanClass); } /** Returns the type of the named property. This is the return type of the * property's <code>get</code> method; for instance <code>JLabel</code>'s * <code>text</code> property is a <code>String</code>. * *@param object the object being examined *@param propertyName the simple JavaBeans-style name of the property *@return the property's type *@throws CompilerException if the type cannot be determined */ public Class getPropertyType(Object object, String propertyName) throws StylesheetException { try { init(); } catch (IntrospectionException e) { throw new RuntimeException(e); } PropertyHandler property = properties.get(propertyName); if (property != null) return property.getPropertyType(object); else throw new UnsupportedPropertyException("property '" + propertyName + "' not found in " + object); } /** * Returns the value of a property for the specified object. * *@param object the object being queried *@param propertyName the property name *@return the property's value *@throws StylesheetException if the property does not exist or could not be read *@see #setProperty */ public Object getProperty(Object object, String propertyName) throws StylesheetException { try { init(); } catch (IntrospectionException e) { throw new RuntimeException(e); } PropertyHandler property = properties.get(propertyName); if (property != null) return property.getProperty(object); else throw new UnsupportedPropertyException("property '" + propertyName + "' could not be found in class " + getBeanClass().getName()); } /** * Sets the value of a property for the specified object. * *@param object the object being modified *@param propertyName the property name *@param value the property's new value *@throws StylesheetException if the property does not exist or could not be * written *@see #getProperty */ public void setProperty(Object object, String propertyName, Object value) throws StylesheetException { try { init(); } catch (IntrospectionException e) { throw new RuntimeException(e); } PropertyHandler property = properties.get(propertyName); if (property != null) property.setProperty(object, value); else throw new UnsupportedPropertyException("property '" + propertyName + "' could not be found in class " + getBeanClass().getName()); } public PropertyHandler getPropertyHandler(String name) { return properties.get(name); } /** * Returns <code>true</code> if the specified property should be inherited * when found on a parent object. */ public boolean isPropertyInherited(Object object, String property) throws UnsupportedPropertyException { return false; // property.equals("font") || property.startsWith("font-") || //property.equals("foreground") || property.equals("enabled"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -