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.

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

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

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