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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs