ANT Task to Create a Java Source to Provide Access to Icons

Description

This task creates a Java class (source) which contrains public static constants referencing to Icon.class

The task scann's a directory for icon files and add all of them as constants in a Java class named ApplicationIcons in the same package as the icons.

How to use

Installation

One of the easiest way is simple copy the jar jugbbant-*.jar into the ANT runtime lib directory

The disadvantage of this method is, you collect a lot of different libs (over the time) into your ANT runtime and if you decided to change your ANT version you have to move all of them. The next problem is, especially in larger projects, all of the involved developer have to do that.

A fare better approach is to store the jugbbant-*.jar file into your project and reference it from your build script.

Task attributes

Class: org.jugbb.ant.iconcodegen.IconCodeGenTask

The iconCodeGen task has these attributes:

Attribute Description Required
sourceDir Java source root directory. Typically it is an ant property like ${src} or somethiong like that. Yes
iconPackage The package containing the icons. This is alos the target package for the created Java class (ApplicationIcons) Yes
iconFileExtensions A space delimited list of icon file extensions. The default list contains (png gif jpg). No
encoding File encoding fo the ApplicationIcon class. This should mathc to the encoding attribute of the javac task ! Yes

Example

This examples shows a typical usage.

The necessary lib for this (and other) task is located within the current project (example: build/).

<taskdef name="iconCodeGen" classname="org.jugbb.ant.iconcodegen.IconCodeGenTask" classpath="build/jugbbant-1.7.jar"/>
<iconCodeGen 
    sourceDir="${src}" 
    iconPackage="sqlrunner.resources"
    iconFileExtensions="png gif jpg"
    encoding="UTF-8"
/>
		

An example of the generated Java class

package sqlrunner.resources;

import javax.swing.ImageIcon;

/**
 * Class contains images as static variables.
 * This class is generated by the IconCodeGenerator
 *
 * author: jan
 * created at: Fri Jun 04 18:05:11 CEST 2010
 */
public class ApplicationIcons {

    public static final ImageIcon ADMINEDIT_PNG = createIcon("/sqlrunner/resources/adminedit.png");
    public static final ImageIcon ADMINRUN_PNG = createIcon("/sqlrunner/resources/adminrun.png");
    public static final ImageIcon AUSRUECK_GIF = createIcon("/sqlrunner/resources/ausrueck.gif");
    public static final ImageIcon COMMENTS_GIF = createIcon("/sqlrunner/resources/comments.gif");
    public static final ImageIcon COMMIT_GIF = createIcon("/sqlrunner/resources/commit.gif");
    public static final ImageIcon COPY_GIF = createIcon("/sqlrunner/resources/copy.gif");
    public static final ImageIcon COPY_PNG = createIcon("/sqlrunner/resources/copy.png");
    public static final ImageIcon CUT_GIF = createIcon("/sqlrunner/resources/cut.gif");
    public static final ImageIcon DATAMODEL_PNG = createIcon("/sqlrunner/resources/datamodel.png");
    public static final ImageIcon DB_GIF = createIcon("/sqlrunner/resources/db.gif");
    public static final ImageIcon DBCLOSE_GIF = createIcon("/sqlrunner/resources/dbclose.gif");
    public static final ImageIcon DBOPEN_GIF = createIcon("/sqlrunner/resources/dbopen.gif");
    public static final ImageIcon DOWN_GIF = createIcon("/sqlrunner/resources/down.gif");
    public static final ImageIcon EINRUECK_GIF = createIcon("/sqlrunner/resources/einrueck.gif");
    public static final ImageIcon FOLDER_GIF = createIcon("/sqlrunner/resources/folder.gif");
    public static final ImageIcon FOLDER_CLOSED_PNG = createIcon("/sqlrunner/resources/folder_closed.png");

    private static ImageIcon createIcon(String iconName) {
        ImageIcon icon = null;
        try {
            icon = new ImageIcon(ApplicationIcons.class.getResource(iconName));
        } catch (Exception e) {
            System.err.println("unable to load icon: " + iconName);
            icon = new ImageIcon();
        }
        return icon;
    }

}
		

Download

Binary: jugbbant-1.7.jar

Sources: jugbbant-1.7-src.zip