Home · Examples 


Resource System Example

Code:

Introduction

The resource system example illustrates how you can easily bundle resources with your Java application by using the class path file engine. The class path file engine searches for the files or directories you specify in all directories or .jar files in the current class path. It also allows you to list the contents of directories in the class path, even if they are actually distributed over several different actual locations.

You can tell Qt Jambi to use the class path file engine when specifying the file name. Simply prefix the name with classpath:, and Qt Jambi will automatically invoke the class path file engine. If you wish to search for a file at a specific location in the class path, then a second request format is available: classpath:<absolute path>#<filename>. The first variable, the absolute path, can be the full path of a directory or .jar file, while the filename should be the resource you want to request. Qt Jambi will then only search the specific directory or .jar file for the file. This can be useful in cases when you want to work with .jar files inside of Qt Jambi.

The Example

The Resource System (com.trolltech.examples.ResourceSystem) example shows off the different features of the class path file engine.

The example has two modes:

In either mode, it will show these contents in a
QTreeWidget and let the user select images to show them in the window.

In the default mode, the application should list the contents of the entire class path. We do this by specifying the root directory and prefixing it with classpath: with the following assignment:

searchPath = "classpath:/";
Every entry of the class path has a root directory, so this directory is distributed over all parts of the class path; i.e. if the classpath contains two entries: /my_java_files/:/java_sdk/rt.jar, the root directory will contain all contents of /my_java_files as well as all contents in the root of rt.jar. We simply use QDir on the root directory and entryList() to get its contents.

In the alternative mode, we only want to search a single .jar file specified by the user. This is accomplished by asking the user to select a file, and then requesting the root from th specific .jar file selected:

String fileName = QFileDialog.getOpenFileName(this, "Select a .jar file",
        null, new QFileDialog.Filter("Jar Files (*.jar)"));

if (fileName.length() == 0) {
    browseClassPath = true;
    browseClassPathButton.setChecked(true);
}

QFileInfo jarInfo = new QFileInfo(fileName);
searchPath = "classpath:" + jarInfo.canonicalFilePath() + "#/";
After the search path has been selected, the application recursively fills the tree widget with any directory or image file it finds. From this point on it works as you would expect it to if it was a normal image viewer application that searched the main file system. When the user selects a file from the tree widget, the application will attempt to load it as a pixmap and set it on its label at the bottom of the window:
QFileInfo info = selected_item.getInfo();
if (info.exists() && !info.isDir()) {
    QPixmap pm = new QPixmap(info.absoluteFilePath());
    m_currentImage.setPixmap(pm);
}


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01