Java Reference
In-Depth Information
This class ensures that all of the views have access to the application's SongModel instance. It also provides a
pattern for easily writing new views. Each new view only has to provide a concrete implementation of the initView
method. This method returns a top-level Node that is the root node of a scene graph for that view.
With those two pieces of infrastructure in place, we can proceed to create some actual views. The obvious place
to start is to move the GridPane , Label s, and ImageView that display the metadata in AudioPlayer2 into a new view
class named MetadataView . The source code for this class is not presented here because it is virtually the same UI
code that was shown in the createGridPane and createControls methods of Listing 9-9. If you are curious, the view
can be found in the source code of the AudioPlayer3 project.
We now return to the original goal of letting the user select a media resource to play by means of a FileChooser
dialog box or by dragging and dropping a file or URL onto the application. A new view is created to hold controls for
our audio player. Its first component is a button that shows the FileChooser when clicked. The button's event handler
will get the file chosen by the user and pass the file's URL to the SongModel . This view, called PlayerControlsView ,
is shown in Listing 9-12.
Listing 9-12. The PlayerControlsView Class
class PlayerControlsView extends AbstractView {
public PlayerControlsView(SongModel songModel) {
super(songModel);
}
@Override
protected Node initView() {
final HBox hbox = new HBox();
hbox.setPadding(new Insets(10));
final Button openButton = new Button();
openButton.setId("openButton");
openButton.setOnAction(new OpenHandler());
openButton.setPrefWidth(32);
openButton.setPrefHeight(32);
hbox.getChildren().addAll(openButton);
return hbox;
}
private class OpenHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
FileChooser fc = new FileChooser();
fc.setTitle("Pick a Sound File");
File song = fc.showOpenDialog(viewNode.getScene().getWindow());
if (song != null) {
songModel.setURL(song.toURI().toString());
songModel.getMediaPlayer().play();
}
}
}
}
 
Search WWH ::




Custom Search