SimpleBeanInfo.java revision 12903:b89f353e2f9a
1145510Sdarrenr/* 2145510Sdarrenr * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. 3145510Sdarrenr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4145510Sdarrenr * 5145510Sdarrenr * This code is free software; you can redistribute it and/or modify it 6145510Sdarrenr * under the terms of the GNU General Public License version 2 only, as 7145510Sdarrenr * published by the Free Software Foundation. Oracle designates this 8145510Sdarrenr * particular file as subject to the "Classpath" exception as provided 9145510Sdarrenr * by Oracle in the LICENSE file that accompanied this code. 10145510Sdarrenr * 11145510Sdarrenr * This code is distributed in the hope that it will be useful, but WITHOUT 12145510Sdarrenr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13145510Sdarrenr * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14145510Sdarrenr * version 2 for more details (a copy is included in the LICENSE file that 15145510Sdarrenr * accompanied this code). 16145510Sdarrenr * 17145510Sdarrenr * You should have received a copy of the GNU General Public License version 18145510Sdarrenr * 2 along with this work; if not, write to the Free Software Foundation, 19145510Sdarrenr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20145510Sdarrenr * 21145510Sdarrenr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package java.beans; 27 28import java.awt.Image; 29import java.awt.Toolkit; 30import java.io.InputStream; 31import java.security.AccessController; 32import java.security.PrivilegedAction; 33 34/** 35 * This is a support class to make it easier for people to provide 36 * BeanInfo classes. 37 * <p> 38 * It defaults to providing "noop" information, and can be selectively 39 * overriden to provide more explicit information on chosen topics. 40 * When the introspector sees the "noop" values, it will apply low 41 * level introspection and design patterns to automatically analyze 42 * the target bean. 43 * 44 * @since 1.1 45 */ 46public class SimpleBeanInfo implements BeanInfo { 47 48 /** 49 * Deny knowledge about the class and customizer of the bean. 50 * You can override this if you wish to provide explicit info. 51 */ 52 @Override 53 public BeanDescriptor getBeanDescriptor() { 54 return null; 55 } 56 57 /** 58 * Deny knowledge of properties. You can override this 59 * if you wish to provide explicit property info. 60 */ 61 @Override 62 public PropertyDescriptor[] getPropertyDescriptors() { 63 return null; 64 } 65 66 /** 67 * Deny knowledge of a default property. You can override this 68 * if you wish to define a default property for the bean. 69 */ 70 @Override 71 public int getDefaultPropertyIndex() { 72 return -1; 73 } 74 75 /** 76 * Deny knowledge of event sets. You can override this 77 * if you wish to provide explicit event set info. 78 */ 79 @Override 80 public EventSetDescriptor[] getEventSetDescriptors() { 81 return null; 82 } 83 84 /** 85 * Deny knowledge of a default event. You can override this 86 * if you wish to define a default event for the bean. 87 */ 88 @Override 89 public int getDefaultEventIndex() { 90 return -1; 91 } 92 93 /** 94 * Deny knowledge of methods. You can override this 95 * if you wish to provide explicit method info. 96 */ 97 @Override 98 public MethodDescriptor[] getMethodDescriptors() { 99 return null; 100 } 101 102 /** 103 * Claim there are no other relevant BeanInfo objects. You 104 * may override this if you want to (for example) return a 105 * BeanInfo for a base class. 106 */ 107 @Override 108 public BeanInfo[] getAdditionalBeanInfo() { 109 return null; 110 } 111 112 /** 113 * Claim there are no icons available. You can override 114 * this if you want to provide icons for your bean. 115 */ 116 @Override 117 public Image getIcon(final int iconKind) { 118 final BeanDescriptor descriptor = getBeanDescriptor(); 119 if (descriptor != null) { 120 final Class<?> type = descriptor.getBeanClass(); 121 if (type != null && type.getClassLoader() == null 122 && type.getAnnotation(JavaBean.class) != null) { 123 final String name = type.getName(); 124 final int index = name.lastIndexOf('.'); 125 if (name.substring(0, index).equals("javax.swing")) { 126 final String className = type.getSimpleName(); 127 switch (iconKind) { 128 case ICON_COLOR_32x32: 129 return loadImage(className, "Color32.gif"); 130 case ICON_COLOR_16x16: 131 return loadImage(className, "Color16.gif"); 132 case ICON_MONO_32x32: 133 return loadImage(className, "Mono32.gif"); 134 case ICON_MONO_16x16: 135 return loadImage(className, "Mono16.gif"); 136 } 137 } 138 } 139 } 140 return null; 141 } 142 143 /** 144 * This is a utility method to help in loading standard icon images. 145 * 146 * @param resourceName A pathname relative to the directory holding the 147 * class file of the current class 148 * @return an image object. May be null if the load failed. 149 * @see java.beans.SimpleBeanInfo#loadImage(String) 150 */ 151 private Image loadStandardImage(final String resourceName) { 152 return AccessController.doPrivileged( 153 (PrivilegedAction<Image>) () -> loadImage(resourceName)); 154 } 155 156 /** 157 * This is a utility method to help in loading standard icon images. 158 * 159 * @param resourceName A pathname relative to the directory holding the 160 * class file of the current class 161 * @param suffix A {@code String} containing a file suffix (<i>e.g.</i>, 162 * "Color32.gif" or "Mono32.gif") 163 * @return an image object. May be null if the load failed. 164 * @see java.beans.SimpleBeanInfo#loadImage(String) 165 */ 166 private Image loadImage(final String resourceName, final String suffix) { 167 final String prefix = "/javax/swing/beaninfo/images/"; 168 final Image image = loadStandardImage(prefix + resourceName + suffix); 169 return image == null ? loadStandardImage(prefix + "JComponent" + suffix) 170 : image; 171 } 172 173 /** 174 * This is a utility method to help in loading icon images. 175 * It takes the name of a resource file associated with the 176 * current object's class file and loads an image object 177 * from that file. Typically images will be GIFs. 178 * 179 * @param resourceName A pathname relative to the directory 180 * holding the class file of the current class. For example, 181 * "wombat.gif". 182 * @return an image object. May be null if the load failed. 183 */ 184 public Image loadImage(final String resourceName) { 185 try (InputStream in = getClass().getResourceAsStream(resourceName)) { 186 return Toolkit.getDefaultToolkit().createImage(in.readAllBytes()); 187 } catch (final Exception ignored) { 188 } 189 return null; 190 } 191} 192