Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

988 Part IV: Applying Java


as anObserverwith the newly selectedDownload. Finally,updateButtons( )is called to
update the button states based on the selectedDownload’s state.

The updateButtons( ) Method

TheupdateButtons( )method updates the state of all the buttons on the button panel based
on the state of the selected download. TheupdateButtons( )method is shown here:

/* Update each button's state based on 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);
}
}

If no download is selected in the downloads table, all of the buttons are disabled, giving
them a grayed-out appearance. However, if there is a selected download, each button’s state
will be set based on whether theDownloadobject has a status ofDOWNLOADING,PAUSED,
ERROR,COMPLETE, orCANCELLED.
Free download pdf