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

Opening and closing the folders

To handle the "open/closed" status of a directory we first add a new member variable and a couple of methods to MyDir:


public class MyDir implements Comparable {
  private boolean open = true; // directory is open
  . . .
	
  public boolean isOpen() { return open; }  
  public void toggleOpen() { this.open = !this.open; } 
  . . .

"toggleOpen" simply switches "true" to "false" and vice versa. By setting "open" to "true" as default the whole directory structure will be shown initially. Using "false" will only show the top directory. It's dependant upon the application as to which option will be the best default value.

The next thing is to add JavaScript code to the HTML-page that will issue requests to open and close the directories. If you look in the code for MyFileStructureExplorer above you'll see that I've added a link around each folder. Furthermore I've added an onclick event that calls a JavaScript function "toggle" to open or close the folder. The "toggle" function must be defined in the HTML-page, and in the first solution we'll use a simple design, where "toggle" requests a jsp-page with an id of the folder that should be opened or closed. A simple jsp-page, toggle.jsp, that contains "toggle" can look like this:

<jsp:useBean id="file"
 class="hansen.playground.MyFileStructureExplorer" 
 scope="session" />
     
<html>
<head>
<script>
function toggle(key) {
  location.href="ctltoggle.jsp?key="+key;
}
</script>
<style type="text/css">
  body { font-family: Verdana; font-size:10px}
</style>
</head>

<body>

<%
String name = file.getDirname();
// If first time then build the directory structure
if (name == null) {
  file.setDirname("c:\\Dir-A");
  file.build();
}  
file.list();
out.println(file.getResult());
%>

The "toggle" function simply calls another jsp-page, ctltoggle.jsp , which gets the id of the folder, calls a new method "toggleDir", which flips the open/close status of the folder, and then returns to the first jsp-page again:

<jsp:useBean id="file" 
  class="hansen.playground.MyFileStructureExplorer" 
  scope="session" />

<%
String key = request.getParameter("key");
if (key != null) {
  int k = Integer.parseInt(key);
  file.toggleDir(k);
} 
pageContext.forward("toggle.jsp");
%>

Before explaining how the id for the directory--the "key"--is implemented, let's see how the output from the new jsp-pages looks:

- Figure 3 -

Nice, right? It's not very far from figure 1. To get a good result you'll have to use a small font like the one given in the style sheet above. A larger font may cause the vertical lines to break.

Clicking on the folders will make them open or close. As mentioned above the click event calls "toggle" with the id of the directory, e.g. <a href="#" onclick="toggle(1)">. All directories are numbered 0,1,2,… and the id is used to look up the directory in a Vector of MyDir's held by MyFileStructure. The few new lines to handle this in MyFileStructure are shown here:

public class MyFileStructure{ 
  . . .
  private int lastKey;  // last used value for dir-id
  private Vector keyStore;  // holds MyDir's
  
  . . .  
  public void toggleDir(int key) {
    MyDir m = (MyDir)keyStore.elementAt(key);
    m.toggleOpen();
  }

  . . . 
  public void build() {
    lastKey = 0;
    keyStore = new Vector(); 
    . . .

  protected MyDir build(File f) {  
    . . . 
    MyDir mdir = new MyDir(path, name, lastKey);
    keyStore.addElement(mdir);
    lastKey++;
    . . .

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