| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ImageIconFactory.groovy)

This example Groovy source code file (ImageIconFactory.groovy) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

awt, file, gstring, gui, image, imageicon, imageiconfactory, in, in, map, object, object, runtimeexception, runtimeexception, string, string, swing

The Groovy ImageIconFactory.groovy source code

 * Copyright 2007 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package groovy.swing.factory

import java.awt.Image
import javax.swing.ImageIcon

class ImageIconFactory extends AbstractFactory {

    public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) {
        if (value == null) {
            if (attributes.containsKey("image")) {
                value = attributes.remove("image")
                if (!(value instanceof Image)) {
                    throw new RuntimeException("In $name image: attributes must be of type java.awt.Image")
            } else if (attributes.containsKey("url")) {
                value = attributes.remove("url")
                if (!(value instanceof URL)) {
                    throw new RuntimeException("In $name url: attributes must be of type")
            } else if (attributes.containsKey("file")) {
                value = attributes.remove("file")
                if (value instanceof GString) value = value as String
                if (value instanceof File) {
                    value = value.toURL()
                } else if (!(value instanceof String)) {
                    throw new RuntimeException("In $name file: attributes must be of type or a string")
        } else if (value instanceof GString) {
            value = value as String

        // not else if so we can adjust for the case of file string where the file does not exist
        def resource = null
        if ((value == null) && (attributes.containsKey("resource"))) {
            resource = attributes.remove('resource')
        } else if ((value instanceof String) && !(new File(value).exists())) {
            resource = value
        if (resource != null) {
            def klass = builder.context.owner
            def origValue = value
            if (attributes.containsKey("class")) {
                klass = attributes.remove("class")
            if (klass == null) {
                klass = ImageIconFactory
            } else if (!(klass instanceof Class)) {
                klass = klass.class
            // for now try URL approach.
            // we may need to extract the byte[] for some packaging cases
            value = klass.getResource(resource)
            if (value == null) {
                throw new RuntimeException("In $name the value argument '$origValue' does not refer to a file or a class resource")

        if (value == null) {
            throw new RuntimeException("$name has neither a value argument or one of image:, url:, file:, or resource:")
        // exploit multi-dispatch in constructor calls
        if (attributes.containsKey("description")) {
            return new ImageIcon(value, attributes.remove("description"))
        } else {
            return new ImageIcon(value)


Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ImageIconFactory.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.