import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; import java.io.*; public class SongsAreUsMain extends JFrame { // Store the model as a vector of email buddies MusicStore musicStore; //model object the represents the collection of songs private Song selectedSong; //song currently selected in the GUI list private Song songBeingEdited; //song being edited by the dialog; // Store the view that contains the components SongListPanel view; //panel of GUI components for the main window // Here are the component listeners ActionListener theAddButtonListener; ActionListener theRemoveButtonListener; ListSelectionListener songListSelectionListener; // Here is the default constructor public SongsAreUsMain(String title) { super(title); musicStore = new MusicStore(); System.out.println(musicStore); selectedSong = null; // Make the main window view panel add(view = new SongListPanel()); // Add a listener for the add button theAddButtonListener = new ActionListener() { public void actionPerformed(ActionEvent event) { addSong(); }}; // Add a listener for the revove button theRemoveButtonListener = new ActionListener() { public void actionPerformed(ActionEvent event) { removeSong(); }}; // Add a listener to allow selection of buddies from the list songListSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { selectSong(); }}; setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(600,300); // Start off with everything updated properly to reflect the model state update(); } // Enable all listeners private void enableListeners() { view.getAddButton().addActionListener(theAddButtonListener); view.getRemoveButton().addActionListener(theRemoveButtonListener); view.getSongList().addListSelectionListener(songListSelectionListener); } // Disable all listeners private void disableListeners() { view.getAddButton().removeActionListener(theAddButtonListener); view.getRemoveButton().removeActionListener(theRemoveButtonListener); view.getSongList().removeListSelectionListener(songListSelectionListener); } // This is called when the user clicks the add button private void addSong() { selectedSong = null; songBeingEdited = new Song(); // Now bring up the dialog box SongDetailsDialog dialog = new SongDetailsDialog(this, this, "Song Details Dialog", true, songBeingEdited); dialog.setVisible(true); } // This is called when the user clicks the remove button private void removeSong() { // Remove the currently selected song from the list Song aSong = (Song)(view.getSongList().getSelectedValue()); if (aSong != null) { musicStore.removeSong(aSong); update(); selectSong(); } } // This is called when the user selects a song from the list private void selectSong() { selectedSong = (Song)(view.getSongList().getSelectedValue()); update(); } // Called when the dialog box is closed with the Ok button //DialogClient Iterface Methods //MISSING CODE // Update the remove button private void updateRemoveButton() { view.getRemoveButton().setEnabled(selectedSong != null); } // Update the list private void updateList() { boolean foundSelected = false; view.getSongList().setListData(musicStore.getCopyOfSongs()); if (selectedSong != null) view.getSongList().setSelectedValue(selectedSong, true); } // Update the components private void update() { disableListeners(); updateList(); updateRemoveButton(); enableListeners(); } // Code that starts the application public static void main(String args[]) { SongsAreUsMain frame = new SongsAreUsMain("Songs Are Us Music Store"); frame.setVisible(true); } }