Graphics
To make a nice, graphical view we'll need some graphics elements to put
inside <img> HTML-tags. It's like collecting a jig saw puzzle. If you look
at figure 1 then you'll see that we only need these 6 pieces:
|
open folder
|
closed folder
|
file
|
|
|
|
|
|
v-bar
|
t-bar
|
e-bar
|
|
|
|
|
I have made two directory images - since we'll soon implement an
open/close-directory function in our application. As you can see I've dropped
the plus/minus-signs on the directory-pictures to simplify things a bit.
The pictures are made with a simple graphics utility (like Paint Shop Pro)
using a few screen dumps with FrontPage pages. Be prepared to use some time to
get the size of the pictures right -- if you don't simply use mine from the
zip-file. Now it's simply a matter of putting these pieces together to make a
directory structure. To show you how it works let's create figure 1 (without the
text). The border of each picture has been made visible to show how it's
done:
To make the graphics view we'll have to code a new version of MyFileStructure
- the class that builds the output. A couple of notes about how this is
done:
- the "level", which is one of the parameters to the methods in
MyFileStructure, is used to determine how many of the vertical lines to place
first. If level=4 then you get this result:
- in front of a file or folder we must place an "e-bar" or "t-bar". The
"e-bar" is used if the file or folder is the last in its directory
- the very first folder doesn't have any bar in front of it.
- to determine if one uses an open or closed folder we must add a new
parameter to the methods in MyFileStructure.
The "Explorer" class
The new class -- called MyFileStructureExplorer (since it has a Windows File
Explorer look) -- may be coded like this:
package hansen.playground;
public class MyFileStructureExplorer extends MyFileStructure {
protected void outFile(MyFile f, int level, boolean last) {
String name = f.getName();
String horizontalGif = (last) ? imageTag("ebar") : imageTag("tbar");
String fileGif = horizontalGif + imageTag("file");
result += verticalGifs(level-2) + fileGif + " " + name + "<br>\n";
}
protected void outDir(MyDir d, int level, boolean open, boolean last) {
String name = d.getName();
int key = d.getKey();
String folderGif;
if (open) {
folderGif = "open";
} else {
folderGif = "closed";
}
// Show image of a directory
folderGif = "<img src=\"images/" + folderGif +
".gif\" border=0 align=center>";
// If not first directory then show a "branch" before the directory
String horizontalGif = "";
if (level > 1)
horizontalGif = (last) ? imageTag("ebar") : imageTag("tbar");
// Put a link around the directory image
folderGif = horizontalGif + "<a href=\"#\" onclick=\"toggle(" + key +
")\">" + folderGif + "</a>";
result += verticalGifs(level-2) + folderGif + " " + name + "<br>\n";
}
/*
* Return "n" vertical-bar Gifs
*/
private String verticalGifs(int n) {
String s = "";
for (int i=0; i < n; i++) {
s += imageTag("vbar");
}
return s;
}
/*
* Return an IMG-tag
*/
private String imageTag(String name) {
return "<img src=\"images/" + name + ".gif\" align=center>";
}
}
Since the outFile and outDir methods now have a couple of new parameters we
also need to update the base class MyFileStructure. The code can be seen in the
zip-file.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|