ResourceLoaders

From Java CoG Kit

Jump to: navigation, search

This page pertains to the new multi-module Maven/SVN repository only (trunk/five).

Contents

Finding Resources with the Resource Loaders

Where to put the resources

Resources should be placed within their respective module. For instance, if the cogkit-core module needs to access a file cog.properties, then this file should placed within org-cogkit/cogkit-core/src/main/resources. A list of all the resource directories follows:

org-cogkit/cogkit-core/src/main/resources
org-cogkit/util/src/main/resources
org-cogkit/karajan/src/main/resources
org-cogkit/karajan-viewer/src/main/resources
org-cogkit/ui/src/main/resources
org-cogkit/qstat/src/main/resources

Any resources placed in these directories will be in the root of the jar file of their respective module. See below for more information.

The Generic Resource Loader

The generic resource loader, found in org.cogkit.util.ResourceLoader allows programmers to obtain a URL object pointing to any resource accessible by the class loader. See the example below.


Directory Layout:

org-cogkit/cogkit-core/src/main/resources/
                                      .../my_first_file
                                      .../my_directory/my_second_file


Code to access resources:

ResourceLoader loader = new ResourceLoader();
URL firstURL = loader.getResource("my_first_file");
URL secondURL = loader.getResource("my_directory/my_second_file");

The Image Loader

The image loader, a subclass of the generic resource loader found in org.cogkit.util.ImageLoader, allows programmers to obtain an ImageIcon object of the specified image file. This ImageIcon object may then be used in a JPanel, JButton or some other Swing component. See the example below.

Directory Layout:

org-cogkit/ui/src/main/resources/
                             .../images/my_icon.png


Code to access resources:

ImageLoader loader = new ImageLoader();
ImageIcon myIcon = loader.loadImage("images/my_icon.png")

The Text Loader

The text loader, a subclass of the generic resource loader found in org.cogkit.util.TextFileLoader, allows programmers to easily load the contents of file into a regular Java String. See the example below.

Directory Layout:

org-cogkit/ui/src/main/resources/
                             .../text/my_story.txt


Code to access resources:

TextFileLoader loader = new TextFileLoader();
String myStory = loader.loadFromResource("text/my_story.txt")
Personal tools
Collaboration and Jobs