This is not a current assignment. If you are currently enrolled in SE1020, do not do this assignment expecting to receive credit for it.

Lab 5: Media Player Saving and Loading User Preferences

Objectives

  • incorporate additional UI elements into a GUI application
  • use Java file handling to read and write files
  • incorporate exception handling to effectively capture errors and handle unexpected behavior

Assignment

This is a two-week assignment with a milestone due after the first week.

In this lab, you will extend your Media Player to incorporate the following functionality:

  1. Add a menu to the MediaPlayer UI.
  2. Add a the capability to save (and subsequently load) user-specified UI preferences to/from a file.
  3. Incorporate exception handling into the UI to prevent users from crashing the program.

Shown below is a screenshot of the Media Player.

MediaPlayer User Interface with menu

UI with Menu

In this lab, you will continue the work you began on the Media Player in lab 2. In particular, you will add functionality to the various UI components. The following UML Class Diagram illustrates the basic structure of your program after making the modifications required for this lab:

Detailed Requirements

Options menu

Modify your existing MediaPlayerUI class to incorporate a menu bar containing both an Options menu and a Help menu. Within the Options menu:

  1. Place the menu items Background color and Text color as shown above.
  2. Add the necessary code to your existing ActionListener to handle the Options menu events.
  3. Handle the Background color event by displaying a JOptionPane-based input dialog (exactly as shown below) that prompts the user for RGB (red, green, blue) color values (valid values are 0 to 255). The example of valid input shown below specifies a pure blue color. Note the text in the title bar, as well as the ”?” icon. Reproduce this appearance. Background color dialog
  4. Extract the integer red, green, and blue values (hint: use the Scanner class to help do this). Implement error handling, including exception handling, to trap all errors in input. The type of errors to trap are those that arise from 1) pressing OK with nothing input, and 2) entering invalid input, such as negative values, 3) entering “garbage” input that cannot be parsed into three integer values. In such cases, post a JOptionPane-based message dialog informing the user that the input entered was invalid, and then repost the preceding dialog. In other words, keep prompting (via a loop) until the user enters valid data or…
  5. Abort the operation if the user presses the close button “X” or the Cancel button, either with or without data in the text field.
  6. Pass valid RGB values as arguments to a method of the PlaylistMangerUI class named setBackgroundColor that you must write. Within this method, use the color values to set the background color of the Playlist Manager's JTextArea. The normal sequence of calls for the preceding instructions is represented by the following UML Sequence Diagram:

Repeat the preceding steps, but modified for the Text color event. Respond to this event by setting the text (foreground) color of the Playlist Manager's JTextArea via a call to a method of PlaylistManagerUI named setTextColor that you must write. Background color dialog Note that 255,255,255 specifies the color white. The resulting Playlist Manager UI looks like this: Background color dialog

Help menu

Implement the Help menu:

  1. Place the menu item About as shown above.
  2. Add the necessary code to your existing ActionListener to handle the About menu item event.
  3. Handle the About event by displaying a JOptionPane-based message dialog that displays “SE1020 Media Player Application by <your name>”, as shown below (on two lines). Note the text in the title bar, as well as the “i” icon. Reproduce this appearance. Background color dialog

Loading User-defined Preferences

Implement functionality that automatically loads user-defined preferences for window positions, text color, and background colors. To do this, create a class called UserPreferences that will be used to store, load, and save user-defined preferences. The UserPreferences class:

  • contains appropriate private attributes representing the RGB values for both the Background color and Text color.
  • contains appropriate private attributes representing the current location (x,y screen coordinates) of both the Playlist Manager UI and the Media Player UI, as well as the width and height of the Playlist Manager UI (since it is resizable).
  • contains appropriate Accessor and Mutator methods for setting and getting the value of each of these attributes.
  1. Modify the MediaPlayerUI constructor to create an instance of the UserPreferences class. The default constructor for the UserPreferences class must initialize every attribute with reasonable values.
  2. From within the MediaPlayerUI constructor, call a UserPreferences method named loadPreferences (that you must write) that tries to load an existing mp_prefs.txt file containing the attributes that were saved last time the program was exited.
    • The file may not exist - this is to be expected the first time the program runs (since it is created only when the program exits). This is an expected situation which you should not report to the user; the attributes will just assume the values previously set by the default constructor.
    • If the file exists, read all the attribute values from the file, paying attention to the order in which they were written so that the correct values get loaded into the correct attributes. Again, use the Scanner class to help read in the values. Set the value of each attribute of the UserPreferences object equal to the value read from the file.
    • Handle all errors (exceptions) that might arise from reading the values from the file. Keep in mind that the file may become corrupted/invalid by manual editing, since it is a text file. Thus, check for negative values, and catch exceptions that may result from trying to scan “garbage” values. Think carefully about how you will handle such errors, and COMMENT your code such that it clearly explains how you handle each condition.
  3. Close the file before the loadPreferences method returns.
  4. From within the MediaPlayerUI constructor, use the Accessor methods of the UserPreferences class to retrieve the values of all the attributes, and use the retrieved values to set the various windows' properties. Note: In order to set the PlaylistMangerUIs window position and size, you'll have to implement a public method on that class called setPosition that accepts the x,y location as well as the width and height as method parameters.

The startup sequence for the Media Player application is graphically described by this UML Sequence Diagram:

Saving User-defined Preferences

After starting the Media Player, the user may reposition the windows, resize the Playlist Manager window, or use the menus to change background and text colors. Upon application exit, you'll save these parameters as UserPreferences attributes as follows:

  1. Capture the window close event. You'll have to modify your MediaPlayerUI's inner event handling class to <i>implement</i> WindowListener (or, alternately, <i>extend</i> WindowAdapter) to handle this event (via the windowClosing method).
  2. Within your implementation of the windowClosing() method, call a method of the UserPreferences class named savePreferences (that you must write) that creates a file called mp_prefs.txt and saves all the private attributes:
    1. The background RGB values must be written on the first line of the file with commas separating the values.
    2. The text (foreground) RGB values must be written on the second line of the file with commas separating the values.
    3. The x,y position of the Media Player UI must be written on the third line with a comma separating the values.
    4. The x,y position as well as the width and height of the Playlist Manager UI must be written on the fourth line with commas separating the values.
  3. Close the file before returning from the savePreferences method.

The application shutdown sequence is illustrated by the following UML Sequence Diagram:

Milestones and Report

Week 6 Milestone: Options/Help Menu Implementation

During the first week of this two week assignment, you must complete implementation of the Options and Help menu functionality as described above.

During the lab of week 6, you will demonstrate the features you implemented during the first week. Use the following steps to test your implementation:

  1. Start your Media Player application, and display the Playlist Manager UI.
  2. Use Options menu commands to specify valid colors for the Playlist Manager's JTextArea. When you press OK, the colors should immediately change.
  3. Use Options menu commands to specify invalid/garbage values. When you press OK, an error message dialog should be displayed and the prompt should reappear after the error message is dismissed.
  4. Close/Cancel the Options menu dialogs both with and without values entered in the text fields. This should simply terminate the command with no effect.
  5. Use the Help/About command to display the “About Media Player” dialog.

Submit your assignment according to your instructor's directions as Lab 5 Part 1: Options/Help Menu Implementation.

Week 7 Milestone: User Preferences Save/Load and Lab Report

During the second week of this two week assignment, you must complete the Load/Save User Preferences functionality described above. You will demonstrate this during the lab session of week 7 (during the Lab 6 session). Use the following steps to test your implementation:

  1. If the mp_prefs.txt file exists, delete it from your directory.
  2. Start the Media Player and display the Playlist Manager. Since the file does not exist, you should default to reasonable positions for the MediaPlayerUI and PlaylistManagerUI windows, as well as for the colors for the Playlist Manager JTextArea.
  3. Use Options commands to specify colors for the Playlist Manager JTextArea, as in the week 6 milestone.
  4. Move both windows to a different location on the screen, and resize the Playlist Manager window.
  5. Quit the application. A new file named mp_prefs.txt should exist in your directory. The file should contain four records as described in step 5 above.
  6. Restart the application. The newly created mp_prefs.txt file should be loaded. The locations of the windows should be placed according to their last saved values, and the color of the Playlist Manager's JTextArea should be according to the saved valud as well.

Submit your assignment, including a lab report, according to your instructor's directions as Lab 5 Part 2: Saving & Loading User Preferences.

FAST Data

You should indicate how much time (in minutes) you spend on this assignment in the FAST database. You are encouraged to log your activity as you work on the project. At a minimum, you should log all of the time spent on this assignment before the due date given above. Enter all time spent prior to the Week 6 demonstration in the week 5 column and all time spent prior to the Week 7 demonstration in the week 6 column.

se1020labs/lab5.txt · Last modified: 2009/06/03 11:22 (external edit)
 

This website is not owned or managed by the Milwaukee School of Engineering.

© 2003-2010 Dr. Christopher C. Taylor, et. al. • Office: L-343 • Phone: 277-7339 • npǝ˙ǝosɯ@ɹolʎɐʇ • -> RSS <-