Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
- fileColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
- titleColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
- artistColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
- albumColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
- fileColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("fileName"));
- artistColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("artist"));
- albumColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("album"));
- titleColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("title"));
- table.setItems(Util.MUSIC_LIB.musicLibAsObservableList());
- genere.setItems(Util.getGenereOptions());
- genere.getItems().add(null);
- table.getSelectionModel().selectedItemProperty().addListener((v, old, now) -> {
- //<editor-fold desc="table listener">
- ObservableList<Metadata> selectedItems = table.getSelectionModel().getSelectedItems();
- ArrayList<String> templist = new ArrayList<String>();
- String[] attributeList = {
- "Artist",
- "Album",
- "Author",
- "FileName",
- "Year"
- };
- switch (selectedItems.size()) {
- case 0:
- fileName.setText("");
- title.setText("");
- artist.setText("");
- album.setText("");
- author.setText("");
- year.setText("");
- track.setText("");
- genere.getSelectionModel().select(-1);
- break;
- case 1:
- fileName.setText(now.getFileName());
- title.setText(now.getTitle());
- artist.setText(now.getArtist());
- album.setText(now.getAlbum());
- author.setText(now.getAuthor());
- year.setText(now.getYear());
- track.setText(now.getTrack());
- break;
- default:
- title.setText(MULTIPLE_SEL);
- track.setText("MULT");
- cover.setImage(null);
- cover.setAccessibleText(MULTIPLE_SEL);
- for (String attribute : attributeList) {
- for (Metadata canzone : selectedItems) {
- Method method = null;
- try {
- method = canzone.getClass().getMethod("get".concat(attribute));
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- try {
- assert method != null;
- String methodReturn = method.invoke(canzone) == null ?
- null : method.invoke(canzone).toString();
- if (!templist.contains(methodReturn)) {
- templist.add(methodReturn);
- }
- } catch (IllegalAccessException | InvocationTargetException e) {
- e.printStackTrace();
- }
- if (templist.size() != 1)
- {
- try {
- this.getClass().getMethod("set_t_"+ attribute, String.class).invoke(this, "MULTIPLE SELECTION");
- } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- else
- {
- try {
- this.getClass().getMethod("set_t_"+attribute, String.class).invoke(this, templist.get(0));
- } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- templist.clear();
- }
- }
- break;
- }
- //</editor-fold>
- });
- }
- public void saveEvent(ActionEvent e) {
- for (Metadata song : table.getSelectionModel().getSelectedItems()){
- ID3v2 tags = new ID3v23Tag();
- if (title.getText() != null && !title.getText().trim().equals(MULTIPLE_SEL)) {
- tags.setTitle(Util.allWordsFirstLetterCapitalized(title.getText().trim()));
- }
- if (artist.getText() != null && !artist.getText().trim().equals(MULTIPLE_SEL)) {
- tags.setArtist(Util.allWordsFirstLetterCapitalized(artist.getText().trim()));
- }
- if (album.getText() != null && !album.getText().trim().equals(MULTIPLE_SEL)) {
- tags.setAlbum(Util.allWordsFirstLetterCapitalized(album.getText().trim()));
- }
- if (author.getText() != null && !author.getText().trim().equals(MULTIPLE_SEL)) {
- tags.setAlbumArtist(Util.allWordsFirstLetterCapitalized(author.getText().trim()));
- }
- if (year.getText() != null && !year.getText().trim().equals(MULTIPLE_SEL)) {
- tags.setYear(year.getText().trim());
- }
- if (track.getText() != null && !track.getText().trim().equals(MULTIPLE_SEL)) {
- tags.setTrack(track.getText().trim());
- }
- Canzone.save(song.getSource(), tags);
- }
- table.setItems(new MusicLib(Util.LIBDIR).musicLibAsObservableList());
- table.getSelectionModel().clearSelection();
- table.refresh();
- }
- public static String allWordsFirstLetterCapitalized(String input) {
- if (input.length() == 0) return "";
- String[] arr = input.split(" ");
- StringBuilder fin = new StringBuilder();
- for (int i = 0; i < arr.length; i++) {
- fin.append(arr[i].substring(0,1).toUpperCase()).append(arr[i].substring(1, arr[i].length()));
- if (i != arr.length -1)
- {
- fin.append(" ");
- }
- }
- return fin.toString();
- }
- /**
- * The file name of the saved mp3 is generated according to the artist and albumArtist
- * of the new ID3 tags. If such values are both null the method will return the old name
- * of the mp3. I think that the old name would be more significative than a casual:
- * ".../unknown artist - unknown title.mp3"
- *
- * @param tags the new local name will be "tags.getAlbumArtist - tags.getTitle.mp3.new"
- * @param oldName old name used if tags is missing albumArtist and title
- * @return the name(also his local path on the machine) of the saved mp3
- */
- static String retrieveNewTempFileName(ID3v2 tags, String oldName) {
- String ret;
- if (tags.getTitle() != null && tags.getAlbumArtist() != null) {
- ret =
- Util.LIBDIR
- .concat(tags.getAlbumArtist().replace("?", ""))
- .concat(" - ")
- .concat(tags.getTitle().replace("?", ""))
- .concat(".mp3.new");
- } else {
- ret = oldName.concat(".new");
- }
- return ret;
- }
- public void save(ID3v2 tags) {
- song.removeCustomTag();
- song.removeId3v1Tag();
- String oldFileName = song.getFilename();
- String newTempFile =
- Util.LIBDIR
- + tags.getAlbumArtist().replace("?", "")
- + " - "
- + tags.getTitle().replace("?", "")
- + ".mp3.new";
- String newFile = newTempFile.substring(0, newTempFile.length() - 4);
- song.setId3v2Tag(tags);
- try {
- song.save(newTempFile);
- System.out.println("Saved as: " + new File(newTempFile).getName()); // TODO at the end delete this statement
- if (!new File(oldFileName).delete()) {
- System.err.printf("ERROR DELETING>>Couldn't delete the old file %s\n", oldFileName);
- }
- if (!new File(newTempFile).renameTo(new File(newFile))) {
- System.err.printf("ERROR RENAMING>>Couldn't rename %s to %s\n", newTempFile, newFile);
- }
- } catch (IOException | NotSupportedException e) {
- System.err.printf("ERROR SAVING>>Couldn't save %s to %s\n", oldFileName, newTempFile);
- System.err.printf("ERROE SAVING MESSAGE>>%s\n", e.getMessage());
- // e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment