1 /*
   2  * Copyright (c) 1996, 2011, 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 /**
  29  * This is a support class to make it easier for people to provide
  30  * BeanInfo classes.
  31  * <p>
  32  * It defaults to providing "noop" information, and can be selectively
  33  * overriden to provide more explicit information on chosen topics.
  34  * When the introspector sees the "noop" values, it will apply low
  35  * level introspection and design patterns to automatically analyze
  36  * the target bean.
  37  *
  38  * @since 1.1
  39  */
  40 
  41 public class SimpleBeanInfo implements BeanInfo {
  42 
  43     /**
  44      * Deny knowledge about the class and customizer of the bean.
  45      * You can override this if you wish to provide explicit info.
  46      */
  47     public BeanDescriptor getBeanDescriptor() {
  48         return null;
  49     }
  50 
  51     /**
  52      * Deny knowledge of properties. You can override this
  53      * if you wish to provide explicit property info.
  54      */
  55     public PropertyDescriptor[] getPropertyDescriptors() {
  56         return null;
  57     }
  58 
  59     /**
  60      * Deny knowledge of a default property. You can override this
  61      * if you wish to define a default property for the bean.
  62      */
  63     public int getDefaultPropertyIndex() {
  64         return -1;
  65     }
  66 
  67     /**
  68      * Deny knowledge of event sets. You can override this
  69      * if you wish to provide explicit event set info.
  70      */
  71     public EventSetDescriptor[] getEventSetDescriptors() {
  72         return null;
  73     }
  74 
  75     /**
  76      * Deny knowledge of a default event. You can override this
  77      * if you wish to define a default event for the bean.
  78      */
  79     public int getDefaultEventIndex() {
  80         return -1;
  81     }
  82 
  83     /**
  84      * Deny knowledge of methods. You can override this
  85      * if you wish to provide explicit method info.
  86      */
  87     public MethodDescriptor[] getMethodDescriptors() {
  88         return null;
  89     }
  90 
  91     /**
  92      * Claim there are no other relevant BeanInfo objects.  You
  93      * may override this if you want to (for example) return a
  94      * BeanInfo for a base class.
  95      */
  96     public BeanInfo[] getAdditionalBeanInfo() {
  97         return null;
  98     }
  99 
 100     /**
 101      * Claim there are no icons available.  You can override
 102      * this if you want to provide icons for your bean.
 103      */
 104     public java.awt.Image getIcon(int iconKind) {
 105         BeanDescriptor descriptor = getBeanDescriptor();
 106         if (descriptor != null) {
 107             Class<?> type = descriptor.getBeanClass();
 108             if ((type != null) && (null != type.getAnnotation(JavaBean.class))) {
 109                 String name = type.getName();
 110                 int index = name.lastIndexOf('.');
 111                 if (name.substring(0, index).equals("javax.swing")) {
 112                     switch (iconKind) {
 113                         case ICON_COLOR_32x32:
 114                             return loadImage(name.substring(index + 1), "Color32.gif");
 115                         case ICON_COLOR_16x16:
 116                             return loadImage(name.substring(index + 1), "Color16.gif");
 117                         case ICON_MONO_32x32:
 118                             return loadImage(name.substring(index + 1), "Mono32.gif");
 119                         case ICON_MONO_16x16:
 120                             return loadImage(name.substring(index + 1), "Mono16.gif");
 121                     }
 122                 }
 123             }
 124         }
 125         return null;
 126     }
 127 
 128     /**
 129      * This is a utility method to help in loading icon images.
 130      * It takes the name of a resource file associated with the
 131      * current object's class file and loads an image object
 132      * from that file.  Typically images will be GIFs.
 133      *
 134      * @param resourceName  A pathname relative to the directory
 135      *          holding the class file of the current class.  For example,
 136      *          "wombat.gif".
 137      * @return  an image object.  May be null if the load failed.
 138      */
 139     public java.awt.Image loadImage(final String resourceName) {
 140         try {
 141             final Class<?> c = getClass();
 142             java.awt.image.ImageProducer ip = (java.awt.image.ImageProducer)
 143                 java.security.AccessController.doPrivileged(
 144                 new java.security.PrivilegedAction<Object>() {
 145                     public Object run() {
 146                         java.net.URL url;
 147                         if ((url = c.getResource(resourceName)) == null) {
 148                             return null;
 149                         } else {
 150                             try {
 151                                 return url.getContent();
 152                             } catch (java.io.IOException ioe) {
 153                                 return null;
 154                             }
 155                         }
 156                     }
 157             });
 158 
 159             if (ip == null)
 160                 return null;
 161             java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
 162             return tk.createImage(ip);
 163         } catch (Exception ex) {
 164             return null;
 165         }
 166     }
 167 
 168     private java.awt.Image loadImage(String name, String postfix) {
 169         java.awt.Image image = loadImage("/javax/swing/beaninfo/images/" + name + postfix);
 170         return (image != null) ? image : loadImage("/javax/swing/beaninfo/images/JComponent" + postfix);
 171     }
 172 }