Search in sources :

Example 6 with MediaFile

use of com.negativevr.media_library.files.MediaFile in project Media-Library by The-Rain-Goddess.

the class ApplicationWindow method getWindowComponents.

private List<Control> getWindowComponents(Stage parent) {
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Media Selection.");
    //Defining path text field
    final TextField songPath = new TextField();
    songPath.setPromptText("C:\\... *");
    songPath.setPrefColumnCount(30);
    songPath.setEditable(false);
    songPath.setFocusTraversable(false);
    GridPane.setConstraints(songPath, 1, 0);
    final Label songPathLabel = new Label("Path: ");
    GridPane.setConstraints(songPathLabel, 0, 0);
    //Defining the Name text field
    final TextField songName = new TextField();
    songName.setPromptText("Enter the song's name. *");
    songName.setPrefColumnCount(20);
    GridPane.setConstraints(songName, 1, 2);
    final Label songNameLabel = new Label("Name: ");
    GridPane.setConstraints(songNameLabel, 0, 2);
    //Defining the Last Name text field
    final TextField artistNames = new TextField();
    artistNames.setPromptText("Enter the artist name(s). *");
    GridPane.setConstraints(artistNames, 1, 3);
    final Label songArtistLabel = new Label("Artist(s): ");
    GridPane.setConstraints(songArtistLabel, 0, 3);
    //Defining the Comment text field
    final TextField albumName = new TextField();
    albumName.setPrefColumnCount(15);
    albumName.setPromptText("Enter the album name. *");
    GridPane.setConstraints(albumName, 1, 4);
    final Label songAlbumLabel = new Label("Album: ");
    GridPane.setConstraints(songAlbumLabel, 0, 4);
    //album number
    final TextField albumNumber = new TextField();
    albumNumber.setPrefColumnCount(20);
    albumNumber.setPromptText("Enter the song's album number.");
    albumNumber.textProperty().addListener(new ChangeListener<String>() {

        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            if (!newValue.matches("\\d*")) {
                albumNumber.setText(newValue.replaceAll("[^\\d]", ""));
            }
        }
    });
    GridPane.setConstraints(albumNumber, 1, 5);
    final Label songNumberLabel = new Label("Number: ");
    GridPane.setConstraints(songNumberLabel, 0, 5);
    //genre
    final TextField genre = new TextField();
    genre.setPrefColumnCount(20);
    genre.setPromptText("Enter the song's genre.");
    GridPane.setConstraints(genre, 1, 6);
    final Label songGenreLabel = new Label("Genre: ");
    GridPane.setConstraints(songGenreLabel, 0, 6);
    //required label
    final Label requiredLabel = new Label("* Required");
    GridPane.setConstraints(requiredLabel, 1, 7);
    //Defining the Submit button
    Button submit = new Button("Submit");
    GridPane.setConstraints(submit, 2, 3);
    submit.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            if (songName.textProperty() != null && albumName.textProperty() != null && artistNames.textProperty() != null && !songName.textProperty().getValue().equals("") && !albumName.textProperty().getValue().equals("") && !artistNames.textProperty().getValue().equals("") && !songPath.textProperty().getValue().equals("") && songPath.textProperty() != null) {
                MediaFile newFile = new MediaFile();
                String number = (albumNumber.textProperty().getValue().equals("")) ? "0" : albumNumber.textProperty().getValue();
                MediaFileAttribute mfa = new MediaFileAttribute().setAlbum(albumName.textProperty()).setName(songName.textProperty()).setArtists(artistNames.textProperty()).setGenre(genre.textProperty()).setDateCreated(new Date().toString()).setNumber(Integer.parseInt(number)).setPath(songPath.textProperty().getValue()).setPlays(0);
                try {
                    mfa.setLength(AudioFileIO.read(new File(songPath.textProperty().getValue())).getAudioHeader().getTrackLength());
                } catch (IOException | NumberFormatException | CannotReadException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
                    e.printStackTrace();
                    mfa.setLength(Double.NaN);
                }
                newFile = new MediaFile(mfa, Main.getNextUUID());
                System.out.println("Successfuly Added:");
                System.out.println(newFile);
                String[] attrs = newFile.getFilePath().split("\\.");
                newFile.setLibraryFilePath("C:\\Music\\" + newFile.getArtistName() + "\\" + newFile.getAlbumName() + "\\" + newFile.getSongName() + "." + attrs[attrs.length - 1]);
                try {
                    newFile.writeToDisk();
                    Files.copy(Paths.get(songPath.textProperty().getValue()), Paths.get(newFile.getLibraryFilePath()), StandardCopyOption.COPY_ATTRIBUTES);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Main.getMasterData().put(newFile.getUUID(), newFile);
                ((Stage) ((Button) t.getSource()).getScene().getWindow()).close();
                updateDataTable();
                updateFileSystem();
            }
        }
    });
    //Defining the Clear button
    Button clear = new Button("Clear");
    clear.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            List<TextField> fields = Arrays.asList(songPath, songName, artistNames, albumName, albumNumber, genre);
            for (TextField s : fields) s.textProperty().set("");
        }
    });
    GridPane.setConstraints(clear, 2, 4);
    //Open file button
    final Button openFile = new Button("Add media files...");
    openFile.setOnAction(new EventHandler<ActionEvent>() {

        final TextField path = songPath;

        @Override
        public void handle(final ActionEvent e) {
            List<File> list = fileChooser.showOpenMultipleDialog(parent);
            if (list != null) {
                for (File file : list) {
                    path.appendText(file.getAbsolutePath().toString());
                }
            }
        }
    });
    GridPane.setConstraints(openFile, 2, 0);
    return Arrays.asList(songPathLabel, songNameLabel, songArtistLabel, songAlbumLabel, songNumberLabel, songGenreLabel, requiredLabel, songPath, songName, artistNames, albumName, albumNumber, genre, openFile, submit, clear);
}
Also used : MediaFile(com.negativevr.media_library.files.MediaFile) ActionEvent(javafx.event.ActionEvent) Label(javafx.scene.control.Label) IOException(java.io.IOException) Date(java.util.Date) Button(javafx.scene.control.Button) MouseButton(javafx.scene.input.MouseButton) FileChooser(javafx.stage.FileChooser) TextField(javafx.scene.control.TextField) SortedList(javafx.collections.transformation.SortedList) FilteredList(javafx.collections.transformation.FilteredList) List(java.util.List) ArrayList(java.util.ArrayList) MediaFileAttribute(com.negativevr.media_library.files.MediaFileAttribute) MediaFile(com.negativevr.media_library.files.MediaFile) File(java.io.File)

Aggregations

MediaFile (com.negativevr.media_library.files.MediaFile)6 File (java.io.File)4 ActionEvent (javafx.event.ActionEvent)3 Label (javafx.scene.control.Label)3 VBox (javafx.scene.layout.VBox)3 Insets (javafx.geometry.Insets)2 Button (javafx.scene.control.Button)2 MouseButton (javafx.scene.input.MouseButton)2 Media (javafx.scene.media.Media)2 MediaPlayer (javafx.scene.media.MediaPlayer)2 MediaFileAttribute (com.negativevr.media_library.files.MediaFileAttribute)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1