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++;
. . .
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.
|