Theupdate( )method is shown here. It fulfills theObserverinterface contract allowing the
DowloadsTableModelclass to receive notifications fromDownloadobjects when they change.
/* Update is called when a Download notifies its observers of any changes. */
public void update(Observable o, Object arg) { int index = downloadList.indexOf(o);
// Fire table row update notification to table. fireTableRowsUpdated(index, index);
}
This method is passed a reference to theDownloadthat has changed, in the form of an
Observableobject. Next, an index to that download is looked up in the list of downloads, and that index is then used to fire a table row update event notification, which alerts the table that the given row has been updated. The table will then rerender the row with the given index, reflecting its new values.
The DownloadManager Class
Now that the foundation has been laid by explaining each of the Download Manager’s helper classes, we can look closely at theDownloadManagerclass. TheDownloadManagerclass is responsible for creating and running the Download Manager’s GUI. This class has amain( )
method declared, so on execution it will be invoked first. Themain( )method instantiates a newDownloadManagerclass instance and then calls itsshow( )method, which causes it to be displayed.
TheDownloadManagerclass is shown here. Notice that it extendsJFrameand implements
Observer. The following sections examine it in detail.
import java.awt.*; import java.awt.event.*; import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.event.*;
// The Download Manager.
public class DownloadManager extends JFrame implements Observer
{
// Add download text field. private JTextField addTextField;
// Download table's data model.
private DownloadsTableModel tableModel;
// Table listing downloads. private JTable table;
// These are the buttons for managing the selected download. private JButton pauseButton, resumeButton;
private JButton cancelButton, clearButton;
// Currently selected download. private Download selectedDownload;
// Flag for whether or not table selection is being cleared. private boolean clearing;
// Constructor for Download Manager. public DownloadManager()
{
// Set application title.
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 4
C h a p t e r 4 : C r e a t i n g a D o w n l o a d M a n a g e r i n J a v a
1 0 9
P:\010Comp\ApDev\971-3\ch04.vp Monday, July 07, 2003 10:03:36 AM
Color profile: Generic CMYK printer profile Composite Default screen
setTitle("Download Manager");
// Set window size. setSize(640, 480);
// Handle window closing events.
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
actionExit(); }
});
// Set up file menu.
JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
fileExitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionExit(); } }); fileMenu.add(fileExitMenuItem); menuBar.add(fileMenu); setJMenuBar(menuBar);
// Set up add panel.
JPanel addPanel = new JPanel(); addTextField = new JTextField(30); addPanel.add(addTextField);
JButton addButton = new JButton("Add Download"); addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { actionAdd();
} });
addPanel.add(addButton);
// Set up Downloads table.
tableModel = new DownloadsTableModel(); table = new JTable(tableModel);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
tableSelectionChanged(); }
});
// Allow only one row at a time to be selected.
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Set up ProgressBar as renderer for progress column. ProgressRenderer renderer = new ProgressRenderer(0, 100); renderer.setStringPainted(true); // show progress text table.setDefaultRenderer(JProgressBar.class, renderer);
// Set table's row height large enough to fit JProgressBar. table.setRowHeight(
(int) renderer.getPreferredSize().getHeight());
// Set up downloads panel.
JPanel downloadsPanel = new JPanel(); downloadsPanel.setBorder(
BorderFactory.createTitledBorder("Downloads")); downloadsPanel.setLayout(new BorderLayout()); downloadsPanel.add(new JScrollPane(table),
BorderLayout.CENTER);
// Set up buttons panel.
JPanel buttonsPanel = new JPanel(); pauseButton = new JButton("Pause");
pauseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionPause(); }
});
pauseButton.setEnabled(false); buttonsPanel.add(pauseButton);
resumeButton = new JButton("Resume");
resumeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionResume(); }
});
resumeButton.setEnabled(false); buttonsPanel.add(resumeButton); cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionCancel();
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 4
C h a p t e r 4 : C r e a t i n g a D o w n l o a d M a n a g e r i n J a v a
1 1 1
P:\010Comp\ApDev\971-3\ch04.vp Monday, July 07, 2003 10:03:36 AM
Color profile: Generic CMYK printer profile Composite Default screen
} });
cancelButton.setEnabled(false); buttonsPanel.add(cancelButton); clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
actionClear(); }
});
clearButton.setEnabled(false); buttonsPanel.add(clearButton);
// Add panels to display.
getContentPane().setLayout(new BorderLayout()); getContentPane().add(addPanel, BorderLayout.NORTH);
getContentPane().add(downloadsPanel, BorderLayout.CENTER); getContentPane().add(buttonsPanel, BorderLayout.SOUTH); }
// Exit this program. private void actionExit() {
System.exit(0); }
// Add a new download. private void actionAdd() {
URL verifiedUrl = verifyUrl(addTextField.getText()); if (verifiedUrl != null) {
tableModel.addDownload(new Download(verifiedUrl)); addTextField.setText(""); // reset add text field } else {
JOptionPane.showMessageDialog(this, "Invalid Download URL", "Error", JOptionPane.ERROR_MESSAGE); }
}
// Verify download URL.
private URL verifyUrl(String url) { // Only allow HTTP URLs.
if (!url.toLowerCase().startsWith("http://")) return null;
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; }
// 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) { selectedDownload = tableModel.getDownload(table.getSelectedRow()); selectedDownload.addObserver(DownloadManager.this); updateButtons(); } }
// Pause the selected download. private void actionPause() {
selectedDownload.pause(); updateButtons();
}
// Resume the selected download. private void actionResume() {
selectedDownload.resume(); updateButtons();
}
// Cancel the selected download.
C h a p t e r 4 : C r e a t i n g a D o w n l o a d M a n a g e r i n J a v a
1 1 3
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 4
P:\010Comp\ApDev\971-3\ch04.vp Monday, July 07, 2003 10:03:36 AM
Color profile: Generic CMYK printer profile Composite Default screen
private void actionCancel() { selectedDownload.cancel(); updateButtons();
}
// Clear the selected download. private void actionClear() {
clearing = true; tableModel.clearDownload(table.getSelectedRow()); clearing = false; selectedDownload = null; updateButtons(); }
/* Update each button's state based off of the currently selected download's status. */ private void updateButtons() {
if (selectedDownload != null) {
int status = selectedDownload.getStatus(); switch (status) { case Download.DOWNLOADING: pauseButton.setEnabled(true); resumeButton.setEnabled(false); cancelButton.setEnabled(true); clearButton.setEnabled(false); break; case Download.PAUSED: pauseButton.setEnabled(false); resumeButton.setEnabled(true); cancelButton.setEnabled(true); clearButton.setEnabled(false); break; case Download.ERROR: pauseButton.setEnabled(false); resumeButton.setEnabled(true); cancelButton.setEnabled(false); clearButton.setEnabled(true); break;
default: // COMPLETE or CANCELLED pauseButton.setEnabled(false); resumeButton.setEnabled(false); cancelButton.setEnabled(false); clearButton.setEnabled(true); } } else {
// No download is selected in table. pauseButton.setEnabled(false); resumeButton.setEnabled(false); cancelButton.setEnabled(false); clearButton.setEnabled(false); } }
/* Update is called when a Download notifies its observers of any changes. */
public void update(Observable o, Object arg) {
// Update buttons if the selected download has changed. if (selectedDownload != null && selectedDownload.equals(o))
updateButtons(); }
// Run the Download Manager.
public static void main(String[] args) {
DownloadManager manager = new DownloadManager(); manager.show();
} }
The DownloadManager Variables
DownloadManagerstarts off by declaring several instance variables, most of which hold references to the GUI controls. TheselectedDownloadvariable holds a reference to the
Downloadobject represented by the selected row in the table. Finally, theclearinginstance variable is abooleanflag that tracks whether or not a download is currently being cleared from the Downloads table.
The DownloadManager Constructor
When theDownloadManageris instantiated, all of the GUI’s controls are initialized inside its constructor. The constructor contains a lot of code, but most of it is straightforward. The following discussion gives an overview.
First, the window’s title is set with a call tosetTitle( ). Next, thesetSize( )call establishes the window’s width and height in pixels. After that, a window listener is added by calling
addWindowListener( ), passing aWindowAdapterobject that overrides thewindowClosing( )
event handler. This handler calls theactionExit( )method when the application’s window is closed. Next, a menu bar with a “File” menu is added to the application’s window. Then the “Add” panel, which has the Add Text field and button, is set up. AnActionListeneris added to the “Add Download” button so that theactionAdd( )method is called each time the button is clicked.
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 4
C h a p t e r 4 : C r e a t i n g a D o w n l o a d M a n a g e r i n J a v a
1 1 5
AppDev TIGHT/ The Art of Java / Schildt/Holmes / 222971-3 / Chapter 4
P:\010Comp\ApDev\971-3\ch04.vp Monday, July 07, 2003 10:03:36 AM
Color profile: Generic CMYK printer profile Composite Default screen
The downloads table is constructed next. AListSelectionListeneris added to the table so that each time a row is selected in the table thetableSelectionChanged( )method is invoked. The table’s selection mode is also updated toListSelectionModel.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, aProgressRendererclass is instantiated and registered with the table to handle the “Progress” column. The table’s row height is updated to theProgressRenderer’s height by callingtable.setRowHeight( ). After the table has been assembled and tweaked, it is wrapped in aJScrollPaneto 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 anActionListenerthat 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.