Path: blob/master/src/java.desktop/share/classes/java/beans/SimpleBeanInfo.java
41152 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.beans;2627import java.awt.Image;28import java.awt.Toolkit;29import java.awt.image.ImageProducer;30import java.net.URL;31import java.security.AccessController;32import java.security.PrivilegedAction;3334/**35* This is a support class to make it easier for people to provide36* BeanInfo classes.37* <p>38* It defaults to providing "noop" information, and can be selectively39* overriden to provide more explicit information on chosen topics.40* When the introspector sees the "noop" values, it will apply low41* level introspection and design patterns to automatically analyze42* the target bean.43*44* @since 1.145*/46public class SimpleBeanInfo implements BeanInfo {4748/**49* Constructs a {@code SimpleBeanInfo}.50*/51public SimpleBeanInfo() {}5253/**54* Deny knowledge about the class and customizer of the bean.55* You can override this if you wish to provide explicit info.56*/57@Override58public BeanDescriptor getBeanDescriptor() {59return null;60}6162/**63* Deny knowledge of properties. You can override this64* if you wish to provide explicit property info.65*/66@Override67public PropertyDescriptor[] getPropertyDescriptors() {68return null;69}7071/**72* Deny knowledge of a default property. You can override this73* if you wish to define a default property for the bean.74*/75@Override76public int getDefaultPropertyIndex() {77return -1;78}7980/**81* Deny knowledge of event sets. You can override this82* if you wish to provide explicit event set info.83*/84@Override85public EventSetDescriptor[] getEventSetDescriptors() {86return null;87}8889/**90* Deny knowledge of a default event. You can override this91* if you wish to define a default event for the bean.92*/93@Override94public int getDefaultEventIndex() {95return -1;96}9798/**99* Deny knowledge of methods. You can override this100* if you wish to provide explicit method info.101*/102@Override103public MethodDescriptor[] getMethodDescriptors() {104return null;105}106107/**108* Claim there are no other relevant BeanInfo objects. You109* may override this if you want to (for example) return a110* BeanInfo for a base class.111*/112@Override113public BeanInfo[] getAdditionalBeanInfo() {114return null;115}116117/**118* Claim there are no icons available. You can override119* this if you want to provide icons for your bean.120*/121@Override122public Image getIcon(final int iconKind) {123final BeanDescriptor descriptor = getBeanDescriptor();124if (descriptor != null) {125final Class<?> type = descriptor.getBeanClass();126if (type != null && type.getClassLoader() == null127&& type.getAnnotation(JavaBean.class) != null) {128final String name = type.getName();129final int index = name.lastIndexOf('.');130if (name.substring(0, index).equals("javax.swing")) {131final String className = type.getSimpleName();132switch (iconKind) {133case ICON_COLOR_32x32:134return loadImage(className, "Color32.gif");135case ICON_COLOR_16x16:136return loadImage(className, "Color16.gif");137case ICON_MONO_32x32:138return loadImage(className, "Mono32.gif");139case ICON_MONO_16x16:140return loadImage(className, "Mono16.gif");141}142}143}144}145return null;146}147148/**149* This is a utility method to help in loading standard icon images.150*151* @param resourceName A pathname relative to the directory holding the152* class file of the current class153* @return an image object. May be null if the load failed.154* @see java.beans.SimpleBeanInfo#loadImage(String)155*/156@SuppressWarnings("removal")157private Image loadStandardImage(final String resourceName) {158return AccessController.doPrivileged(159(PrivilegedAction<Image>) () -> loadImage(resourceName));160}161162/**163* This is a utility method to help in loading standard icon images.164*165* @param resourceName A pathname relative to the directory holding the166* class file of the current class167* @param suffix A {@code String} containing a file suffix (<i>e.g.</i>,168* "Color32.gif" or "Mono32.gif")169* @return an image object. May be null if the load failed.170* @see java.beans.SimpleBeanInfo#loadImage(String)171*/172private Image loadImage(final String resourceName, final String suffix) {173final String prefix = "/javax/swing/beaninfo/images/";174final Image image = loadStandardImage(prefix + resourceName + suffix);175return image == null ? loadStandardImage(prefix + "JComponent" + suffix)176: image;177}178179/**180* This is a utility method to help in loading icon images. It takes the181* name of a resource file associated with the current object's class file182* and loads an image object from that file. Typically images will be GIFs.183*184* @param resourceName A pathname relative to the directory holding the185* class file of the current class. For example, "wombat.gif".186* @return an image object or null if the resource is not found or the187* resource could not be loaded as an Image188*/189public Image loadImage(final String resourceName) {190try {191final URL url = getClass().getResource(resourceName);192if (url != null) {193final ImageProducer ip = (ImageProducer) url.getContent();194if (ip != null) {195return Toolkit.getDefaultToolkit().createImage(ip);196}197}198} catch (final Exception ignored) {199}200return null;201}202}203204205