Example usage for javax.swing UIDefaults.LazyValue UIDefaults.LazyValue

List of usage examples for javax.swing UIDefaults.LazyValue UIDefaults.LazyValue

Introduction

In this page you can find the example usage for javax.swing UIDefaults.LazyValue UIDefaults.LazyValue.

Prototype

UIDefaults.LazyValue

Source Link

Usage

From source file:Main.java

/**
 * Utility method that creates a <code>UIDefaults.LazyValue</code> that
 * creates an <code>ImageIcon</code> <code>UIResource</code> for the
 * specified image file name. The image is loaded using
 * <code>getResourceAsStream</code>, starting with a call to that method
 * on the base class parameter. If it cannot be found, searching will
 * continue through the base class' inheritance hierarchy, up to and
 * including <code>rootClass</code>.
 *
 * @param baseClass the first class to use in searching for the resource
 * @param rootClass an ancestor of <code>baseClass</code> to finish the
 *                  search at/* w  ww.j  ava2s . co m*/
 * @param imageFile the name of the file to be found
 * @return a lazy value that creates the <code>ImageIcon</code>
 *         <code>UIResource</code> for the image,
 *         or null if it cannot be found
 */
public static Object makeIcon(final Class<?> baseClass, final Class<?> rootClass, final String imageFile) {

    return new UIDefaults.LazyValue() {
        public Object createValue(UIDefaults table) {
            /* Copy resource into a byte array.  This is
             * necessary because several browsers consider
             * Class.getResource a security risk because it
             * can be used to load additional classes.
             * Class.getResourceAsStream just returns raw
             * bytes, which we can convert to an image.
             */
            byte[] buffer = java.security.AccessController
                    .doPrivileged(new java.security.PrivilegedAction<byte[]>() {
                        public byte[] run() {
                            try {
                                InputStream resource = null;
                                Class<?> srchClass = baseClass;

                                while (srchClass != null) {
                                    resource = srchClass.getResourceAsStream(imageFile);

                                    if (resource != null || srchClass == rootClass) {
                                        break;
                                    }

                                    srchClass = srchClass.getSuperclass();
                                }

                                if (resource == null) {
                                    return null;
                                }

                                BufferedInputStream in = new BufferedInputStream(resource);
                                ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
                                byte[] buffer = new byte[1024];
                                int n;
                                while ((n = in.read(buffer)) > 0) {
                                    out.write(buffer, 0, n);
                                }
                                in.close();
                                out.flush();
                                return out.toByteArray();
                            } catch (IOException ioe) {
                                System.err.println(ioe.toString());
                            }
                            return null;
                        }
                    });

            if (buffer == null) {
                return null;
            }
            if (buffer.length == 0) {
                System.err.println("warning: " + imageFile + " is zero-length");
                return null;
            }

            return new ImageIconUIResource(buffer);
        }
    };
}