Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Quickly access files and directories you use repeatedly

Quickly access files and directories you use repeatedly

Tutorial Details:

Quickly access files and directories you use repeatedly
Quickly access files and directories you use repeatedly
By: By Slav Boleslawski
The JFileChooser shortcuts accessory organizes your documents
evelopers have suggested many approaches for accelerating the selection of a directory or file in JFileChooser . TypeAheadSelector allows you to quickly select a specific file in the current directory by typing just the initial letters of its name (suitable for JFileChooser in J2SE (Java 2 Platform, Standard Edition) 1.3 and earlier versions?the chooser in J2SE 1.4 sports a built-in type-ahead feature). A history mechanism maintains a list of recently visited directories, allowing you to easily revisit the same directory. Some applications allow you to create and maintain a list of files that you can access from a menu. For instance, Microsoft Word XP allows you to add a Work menu to the Main menu and then add currently opened documents to the Work menu for quick access in the future.
In this article, I describe a JFileChooser shortcuts accessory that goes further than the Work menu. The accessory allows both files and directories in the list. In addition, you can define a short name (alias) for a given shortcut that proves easier to read than an absolute path. You can further improve your shortcuts' readability by coding them in a specific color. Finally, the accessory also updates the chooser's dialog title to show the path to the currently selected directory, which helps you navigate the file system.
Note: You can download this article's source code from Resources .
The shortcuts accessory
The accessory consists of a list of shortcuts and, below that, a control panel, as shown in the figure below.
Shortcuts accessory
The list's shortcuts are sorted alphabetically. A shortcut displays as either an absolute path to a directory/file or, for shortcuts with defined aliases, as its alias enclosed in brackets. For instance, four aliases are defined in the figure above: Flights Retriever, Java Source, Tomcat-Apps, and Tomcat-Work. When the mouse moves over an alias, a tool-tip text displays for that alias, showing the path to a file or directory represented by that alias.
To change the chooser's current directory to the one represented by the shortcut, you click on a shortcut in the list. If the selected shortcut represents a file, the accessory selects that file in the chooser, and the file's name appears in the File Name field. The accessory modifies the chooser's dialog title to include the chooser's current directory path in parentheses.
You can easily add/remove the list's shortcuts and set/reset your aliases. Clicking the Add button adds the chooser's current directory to the list or, if the accessory selects a file in the chooser, the selected file is added to the shortcuts list. The accessory ensures no duplicates appear in the list. Clicking the Delete button removes the currently selected shortcut from the list. To set a given shortcut's alias, first select this shortcut, then type any text in the Alias field, and either press Enter on your keyboard or click the Set button. To change a shortcut's color, add a color name and hash character (#) to an alias (for example, blue#Tomcat). When a shortcut with a nonempty alias is selected in the list, the alias also displays in the Alias field so you can easily rename or remove it (again, press Enter or click Set for confirmation). If you delete the shortcut's alias in the Alias field, this shortcut again displays as its absolute path.
Shortcuts model
Let's look closer at the implementation of the shortcuts list. Shortcuts are elements of JList , and each element is a Shortcut class instance. This class encapsulates three attributes of a shortcut, namely its alias (name), absolute path, and color for displaying on the screen. Shortcut contains accessor methods for those attributes, an important method getDisplayName() that defines how a shortcut displays in the list, and two helper methods for converting between Color and String .
DefaultListModel is the list's model. The model is created in the createModel() method, where shortcut information is read from a file in your home directory. The shortcut file's name is unique for a given application. An application's name is passed as a parameter to the accessory and becomes part of the shortcut file's name. Each line in the file represents one shortcut, except for lines beginning with two slashes (Java-style single-line comments), which are treated as comment lines and thus ignored. Each shortcut line contains a color name, hash character ( # ), alias (or empty string, if no alias is set), comma character, and shortcut's absolute path. For instance: blue#Java,c:\jdk1.3\bin represents a shortcut whose alias is Java , path is c:\jdk1.3\bin , and which displays in blue. Shortcut recognizes many colors by their name (red, blue, and so on). You can specify other colors with a six-digit hexadecimal number representing a color's RGB (red-green-blue) parameter. For instance, if you want to change the color of the shortcut defined above from blue to teal, you could replace blue with 66cc99 : 66cc99#Java,c:\jdk1.3\bin .
The model's shortcuts are sorted alphabetically by shortcut's path or name as defined in Shortcut 's getName() method. The insertShortcut() method implements this sorting. Initially, this method checks whether a new shortcut already appears in the list. If it does not, the method finds an index in the list where it can insert a new shortcut so the list's sorted order is preserved.
All shortcuts are written back to the same file when you close the chooser. The accessory registers with the chooser as an ancestor listener (in the addListeners() method), and shortcuts are saved to a file when listener's ancestorRemoved() method is called.
Render on the screen
The accessory displays shortcuts as either a path or an alias in a specified color. To implement a custom rendering of shortcuts on the screen, you create a new cell renderer for the list:
list.setCellRenderer(new ListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
Shortcut shortcut = (Shortcut)value;
String name = shortcut.getDisplayName();
JLabel label = new JLabel(name);
label.setBorder(new EmptyBorder(0,3,0,3));
label.setOpaque(true);
if (!isSelected) {
label.setBackground(list.getBackground());
label.setForeground(shortcut.getColor());
} else {
label.setBackground(list.getSelectionBackground());
label.setForeground(list.getSelectionForeground());
}
return label;
}
});
The renderer implements the only method defined in the ListCellRenderer interface, getListCellRendererComponent() . It creates a label component that draws the list's cells. The label displays a shortcut's name as defined in the getDisplayName() method. This method's current implementation returns an alias enclosed in brackets or a shortcut's path if the alias is empty. You can easily redefine getDisplayName() to render shortcuts differently. The renderer uses the default colors defined in JList to paint the currently selected cell. For all other cells, it sets the label's foreground color to the shortcut's color. You must set the label's opaque attribute to true , otherwise the label's text of the selected cell will not show up on the screen.
When you set an alias for a given shortcut, that alias replaces a shortcut's path on the screen. In that case, a tool-tip displays a shortcut's path when you position the mouse pointer over an alias in the list. A tool-tip also displays for the shortcuts with empty aliases whose paths are longer than the display area (horizontal scrolling of the accessory would be required).
To implement a smooth and quick display of tool-tips when you move the mouse from one shortcut to another, the accessory changes a tool-tip's initial and dismiss-delay times as defined in ToolTipManager , so that tool-tips display quickly (after 0.3 seconds) and remain on the screen for a short time (2 seconds). When you close a chooser's dialog, those delay times restore to their original values.
A tool-tip's text changes as you move the mouse pointer from shortcut to shortcut. We achieve that functionality by extending JList and overriding JComponent 's getToolTipText() method:
list = new JList(model) {
public String getToolTipText(MouseEvent me) {
if (model.size() == 0)
return null;
Point p = me.getPoint();
Rectangle bounds = list.getCellBounds(model.size()-1,
model.size()-1);
int lastElementBaseline = bounds.y + bounds.height;
// Is the mouse pointer below the last element in the list?
if (lastElementBaseline < p.y)
return null;
int index = list.locationToIndex(p);
if (index == -1) // For compatibility with J2SE 1.3
return null;
Shortcut shortcut = (Shortcut)model.get(index);
String path = shortcut.getPath();
if (shortcut.hasAlias())
return path;
FontMetrics fm = list.getFontMetrics(list.getFont());
int textWidth = SwingUtilities.computeStringWidth(fm, path);
if (textWidth <= listScrollPane.getSize().width)
return null;
return path;
}
};
The locationToIndex() method converts the mouse-click point to the list's index. This index's shortcut is inspected, and getToolTipText() returns the shortcut's path if the shortcut has an alias set or when a shortcut's path is longer than the display area. Computing a string's width can be tricky; you should delegate this task to SwingUtilities 's computeStringWidth() method.
The case when the list's display area extends below its last element needs special consideration. We would like to display tool-tips for the list's relevant shortcuts, but when the mouse pointer moves below the last shortcut, which represents an empty region of JList , no tool-tip should display. To add such behavior to the accessory, we must detect when the mouse pointer crosses the list's last shortcut. It is a s


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Quickly access files and directories you use repeatedly

View Tutorial:
Quickly access files and directories you use repeatedly

Related Tutorials:

GNU offers a new kind of Java IDE - JavaWorld - July 1998
GNU offers a new kind of Java IDE - JavaWorld - July 1998
 
Java Tip 83: Use filters to access resources in Java archives - JavaWorld
Java Tip 83: Use filters to access resources in Java archives - JavaWorld
 
Tweak your IO performance for faster runtime - JavaWorld November 2000
Tweak your IO performance for faster runtime - JavaWorld November 2000
 
Get the app out - JavaWorld January 2001
Get the app out - JavaWorld January 2001
 
Automate your build process using Java and Ant - JavaWorld October 2000
Automate your build process using Java and Ant - JavaWorld October 2000
 
Develop a generic caching service to improve performance - JavaWorld July 2001
Develop a generic caching service to improve performance - JavaWorld July 2001
 
Sir, what is your preference?
Sir, what is your preference?
 
Deploy code servers in Jini systems
Deploy code servers in Jini systems
 
Cache SOAP services on the client side
Cache SOAP services on the client side
 
J2EE or J2SE? JNDI works with both
J2EE or J2SE? JNDI works with both
 
Make a statement with javac!
Make a statement with javac!
 
Quickly access files and directories you use repeatedly
Quickly access files and directories you use repeatedly
 
Once again, only introduction
Once again, only introduction
 
Java and Security, Part 1
Java and Security WebLogic provides a comprehensive suite of security services that can be used to protect all aspects of a domain and its deployments. These security services affect all aspects of your domain: from the lowest level provided by the Jav
 
Smokescreen Introduction
Smokescreen is a Java obfuscator. Aside from being able to change symbolic names, it can also modify the bytecode instructions in methods thereby obfuscating control flow. This makes the resulting obfuscated classes much more difficult to decompile.
 
Smokescreen 3.4 has been released
Smokescreen is a Java obfuscator. Aside from being able to change symbolic names, it can also modify the bytecode instructions in methods thereby obfuscating control flow. This makes the resulting obfuscated classes much more difficult to decompile.
 
Backing Up and Restoring A MySQL Database
Backing Up and Restoring A MySQL Database Backing Up and Restoring A MySQL Database This tutorial explains the how to backup and restore the MySQL Database. Databases are used to store large amount of precious data and it becomes very important to
 
Programming Jakarta Struts: Using Tiles, Part 2
In part two in this series of book excerpts on using tiles from Programming Jakarta Struts, learn how to install and configure tiles, as well as get an overview on tiles.
 
Reader-Submitted Tech Tip: Peeking Under Mount Points
Find out how to use NFS to inspect the underlying directory structure if the reported disk usage seems inconsistent.
 

Free Web Site Hosting Services Below is the listing of the hosting providers providing free web hosting services. These services helps you building your sites even if you have no experience in HTML writing. Zero
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.