// Verify download URL.
private URL verifyUrl(String url) {
// Only allow HTTP URLs.
if (!url.toLowerCase().startsWith("http://"))
return null;
// Verify format of URL.
URL verifiedUrl = null;
try {
verifiedUrl = new URL(url);
} catch (Exception e) {
return null;
}
// Make sure URL specifies a file.
if (verifiedUrl.getFile().length() < 2)
return null;
return verifiedUrl;
}
This method first verifies that the URL entered is an HTTP URL since only HTTP is supported.
Next, the URL being verified is used to construct a new URL class instance. If the URL is
malformed, the URL class constructor will throw an exception. Finally, this method verifies
that a file is actually specified in the URL.
The tableSelectionChanged( ) Method
The tableSelectionChanged( ) method, shown here, is called each time a row is selected
in the downloads table:
// Called when table row selection changes.
private void tableSelectionChanged() {
/* Unregister from receiving notifications
from the last selected download. */
if (selectedDownload != null)
selectedDownload.deleteObserver(DownloadManager.this);
/* If not in the middle of clearing a download,
set the selected download and register to
receive notifications from it. */
if (!clearing && table.getSelectedRow() > -1) {
selectedDownload =
tableModel.getDownload(table.getSelectedRow());
selectedDownload.addObserver(DownloadManager.this);
updateButtons();
}
}
This method starts by seeing if there is already a row currently selected by checking if
the selectedDownload variable is null. If the selectedDownload variable is not null,
DownloadManager removes itself as an observer of the download so that it no longer
receives change notifications. Next the clearing flag is checked. If the table is not empty
and the clearing flag is false, then first the selectedDownload variable is updated with the
Download corresponding to the row selected. Second, the DownloadManager is registered
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home