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