advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement


Tutorials : Java Tutorial: Working with files and directories in Java Part 2 :

Contents
Recap & Sorting the Files and Directories
Graphics
Opening and Closing the Folders
A Solution Using JavaScript

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:

  1. 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:
  2. 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
  3. the very first folder doesn't have any bar in front of it.
  4. 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.

How to Add Java Applets to Your Site

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.

XML error: undefined entity at line 19
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Linux Vendors Head to the Cloud in Search of Cash
iPhone 3GS: Overheating Fears, OS Update Nears
PostgreSQL 8.4 Revs Up Database Admin, Security
PHP 5.3 Accelerates PHP
Sun Releases NetBeans 6.7 IDE for Java, PHP
Why Firefox Doesn't Take Google Chrome Features
First Major PHP Update in Years Coming Soon
Red Hat CEO Calls on Oracle to Keep Java Open
Google Widens AdSense for iPhone, Android Apps
Eclipse Galileo Releases 33 Open Source Projects

A Taste of JavaFX for the Uninitiated
A Guide to Caching and Compression for High Performance Web Applications
How User-Centered Design Can Put User Stories in Proper Context
Explore C# 4's New Dynamic Types and Named/Optional Parameters
Enterprise Architecture: The Journey Begins Here, Part 2
Create a Syslog Sender/Receiver Using the MS Winsock Control
AMD CodeAnalyst Helps Developers Optimize and Tune Applications
Securing Microsoft's Cloud Infrastructure
Introducing the Azure Services Platform
An Introduction to Microsoft .NET Services for Developers

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs