The
DownloadManager Class
The DownloadManager
class
is responsible for creating and running the Download Manager’s GUI. This class
has a main( ) method declared, so on execution it will be invoked
first. The main( ) method instantiates a new DownloadManager class instance and then calls its show( ) method, which causes it
to be displayed.
The DownloadManager Variables- DownloadManager starts off by declaring several instance variables, most of which hold references to the GUI controls. The selectedDownload variable holds a reference to the Download object represented by the selected row in the table.
Finally, the clearing instance variable
is a boolean flag that tracks whether or not a download is
currently being cleared from the Downloads
table.
The DownloadManager Constructor- When the DownloadManager is instantiated, all of the GUI’s controls are initialized inside its
constructor. First, the window’s
title is set with a call to setTitle( ). Next, the setSize( ) call establishes the window’s width and height in pixels. After that,
a window listener is added by calling addWindowListener( ), passing aWindowAdapter object that overrides the windowClosing( ) event handler. This
handler calls the actionExit( ) method when the application’s
window is closed. Then the “Add” panel, which has the Add Text field and button,
is set up. An ActionListener is added to
the “Add Download” button so that the actionAdd( ) method is called each
time the button is clicked.
The downloads table is constructed
next. A ListSelectionListener is added to the table so that each
time a row is selected in the table, the tableSelectionChanged( ) method is invoked. The
table’s selection mode is also updated to ListSelectionModel.SINGLE_SELECTION
so
that only one row at a time can be selected in the table. Limiting row selection
to only one row at a time simplifies the logic for determining which buttons
should be enabled in the GUI when a row in the download table is selected.
Next, a ProgressRenderer class is instantiated and registered with the table
to handle the “Progress” column. The table’s row height is updated to the ProgressRenderer’s height by calling table.setRowHeight( ). After the table has
been assembled and tweaked, it is wrapped in a JScrollPane
to
make it scrollable
and then added to a panel. Finally,
the buttons panel is created. The buttons panel has Pause, Resume, Cancel, and Clear
buttons. Each of the buttons adds an ActionListener that invokes its
respective action method when it is clicked. After creating the buttons panel,
all of the panels that have been created are added to the window
.
The verifyUrl( ) Method-
The verifyUrl(
) method
is called by the actionAdd( ) method each time a download is
added to the Download Manager. 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-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.
The updateButtons( ) Method-
The updateButtons(
) method
updates the state of all the buttons on the button panel basedon the state of
the selected download.
Compiling and Running the Download Manager
Compile DownloadManager like this:
javac DownloadManager.java
DownloadsTableModel.java ProgressRenderer.java Download.java
Run DownloadManager like this:
java DownloadManager
No comments:
Post a Comment