Advertisement
AL4ST4I2

Untitled

Oct 8th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.21 KB | None | 0 0
  1.         table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  2.         fileColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
  3.         titleColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
  4.         artistColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
  5.         albumColumn.prefWidthProperty().bind(table.widthProperty().multiply(0.25));
  6.  
  7.         fileColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("fileName"));
  8.         artistColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("artist"));
  9.         albumColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("album"));
  10.         titleColumn.setCellValueFactory(new PropertyValueFactory<Metadata, String>("title"));
  11.  
  12.         table.setItems(Util.MUSIC_LIB.musicLibAsObservableList());
  13.  
  14.         genere.setItems(Util.getGenereOptions());
  15.         genere.getItems().add(null);
  16.  
  17.         table.getSelectionModel().selectedItemProperty().addListener((v, old, now) -> {
  18.             //<editor-fold desc="table listener">
  19.             ObservableList<Metadata> selectedItems = table.getSelectionModel().getSelectedItems();
  20.  
  21.             ArrayList<String> templist = new ArrayList<String>();
  22.             String[] attributeList = {
  23.                     "Artist",
  24.                     "Album",
  25.                     "Author",
  26.                     "FileName",
  27.                     "Year"
  28.             };
  29.            
  30.             switch (selectedItems.size()) {
  31.                 case 0:
  32.                     fileName.setText("");
  33.                     title.setText("");
  34.                     artist.setText("");
  35.                     album.setText("");
  36.                     author.setText("");
  37.                     year.setText("");
  38.                     track.setText("");
  39.                     genere.getSelectionModel().select(-1);
  40.                     break;
  41.                 case 1:
  42.                     fileName.setText(now.getFileName());
  43.                     title.setText(now.getTitle());
  44.                     artist.setText(now.getArtist());
  45.                     album.setText(now.getAlbum());
  46.                     author.setText(now.getAuthor());
  47.                     year.setText(now.getYear());
  48.                     track.setText(now.getTrack());
  49.                     break;
  50.  
  51.                 default:
  52.                     title.setText(MULTIPLE_SEL);
  53.                     track.setText("MULT");
  54.                     cover.setImage(null);
  55.                     cover.setAccessibleText(MULTIPLE_SEL);
  56.  
  57.                     for (String attribute : attributeList) {
  58.                         for (Metadata canzone : selectedItems) {
  59.  
  60.                             Method method = null;
  61.                             try {
  62.                                 method = canzone.getClass().getMethod("get".concat(attribute));
  63.                             } catch (NoSuchMethodException e) {
  64.                                 e.printStackTrace();
  65.                             }
  66.  
  67.                             try {
  68.                                 assert method != null;
  69.                                 String methodReturn = method.invoke(canzone) == null ?
  70.                                         null : method.invoke(canzone).toString();
  71.  
  72.                                 if (!templist.contains(methodReturn)) {
  73.                                     templist.add(methodReturn);
  74.                                 }
  75.  
  76.                             } catch (IllegalAccessException | InvocationTargetException e) {
  77.                                 e.printStackTrace();
  78.                             }
  79.    
  80.                             if (templist.size() != 1)
  81.                             {
  82.                                 try {
  83.                                     this.getClass().getMethod("set_t_"+ attribute, String.class).invoke(this, "MULTIPLE SELECTION");
  84.                                 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  85.                                     e.printStackTrace();
  86.                                 }
  87.                             }
  88.                             else
  89.                             {
  90.                                 try {
  91.                                     this.getClass().getMethod("set_t_"+attribute, String.class).invoke(this, templist.get(0));
  92.                                 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  93.                                     e.printStackTrace();
  94.                                 }
  95.                             }
  96.    
  97.                             templist.clear();
  98.  
  99.                         }
  100.                     }
  101.                     break;
  102.  
  103.             }
  104.             //</editor-fold>
  105.         });
  106.  
  107.  
  108.     }
  109.  
  110.     public void saveEvent(ActionEvent e) {
  111.  
  112.         for (Metadata song : table.getSelectionModel().getSelectedItems()){
  113.             ID3v2 tags = new ID3v23Tag();
  114.             if (title.getText() != null && !title.getText().trim().equals(MULTIPLE_SEL)) {
  115.                 tags.setTitle(Util.allWordsFirstLetterCapitalized(title.getText().trim()));
  116.             }
  117.             if (artist.getText() != null && !artist.getText().trim().equals(MULTIPLE_SEL)) {
  118.                 tags.setArtist(Util.allWordsFirstLetterCapitalized(artist.getText().trim()));
  119.             }
  120.             if (album.getText() != null && !album.getText().trim().equals(MULTIPLE_SEL)) {
  121.                 tags.setAlbum(Util.allWordsFirstLetterCapitalized(album.getText().trim()));
  122.             }
  123.             if (author.getText() != null && !author.getText().trim().equals(MULTIPLE_SEL)) {
  124.                 tags.setAlbumArtist(Util.allWordsFirstLetterCapitalized(author.getText().trim()));
  125.             }
  126.             if (year.getText() != null && !year.getText().trim().equals(MULTIPLE_SEL)) {
  127.                 tags.setYear(year.getText().trim());
  128.             }
  129.             if (track.getText() != null && !track.getText().trim().equals(MULTIPLE_SEL)) {
  130.                 tags.setTrack(track.getText().trim());
  131.             }
  132.  
  133.  
  134.             Canzone.save(song.getSource(), tags);
  135.         }
  136.         table.setItems(new MusicLib(Util.LIBDIR).musicLibAsObservableList());
  137.         table.getSelectionModel().clearSelection();
  138.         table.refresh();
  139.  
  140.     }
  141.  
  142. public static String allWordsFirstLetterCapitalized(String input) {
  143.         if (input.length() == 0) return "";
  144.         String[] arr = input.split(" ");
  145.         StringBuilder fin = new StringBuilder();
  146.         for (int i = 0; i < arr.length; i++) {
  147.             fin.append(arr[i].substring(0,1).toUpperCase()).append(arr[i].substring(1, arr[i].length()));
  148.             if (i != arr.length -1)
  149.             {
  150.                 fin.append(" ");
  151.             }
  152.         }
  153.         return fin.toString();
  154.     }
  155.  
  156. /**
  157.      *  The file name of the saved mp3 is generated according to the artist and albumArtist
  158.      *  of the new ID3 tags. If such values are both null the method will return the old name
  159.      *  of the mp3. I think that the old name would be more significative than a casual:
  160.      *  ".../unknown artist - unknown title.mp3"
  161.      *
  162.      * @param tags the new local name will be "tags.getAlbumArtist - tags.getTitle.mp3.new"
  163.      * @param oldName old name used if tags is missing albumArtist and title
  164.      * @return the name(also his local path on the machine) of the saved mp3
  165.      */
  166.     static String retrieveNewTempFileName(ID3v2 tags, String oldName) {
  167.         String ret;
  168.         if (tags.getTitle() != null && tags.getAlbumArtist() != null) {
  169.             ret =
  170.                     Util.LIBDIR
  171.                             .concat(tags.getAlbumArtist().replace("?", ""))
  172.                             .concat(" - ")
  173.                             .concat(tags.getTitle().replace("?", ""))
  174.                             .concat(".mp3.new");
  175.         } else {
  176.             ret = oldName.concat(".new");
  177.         }
  178.  
  179.         return ret;
  180.     }
  181.  
  182. public void save(ID3v2 tags) {
  183.         song.removeCustomTag();
  184.         song.removeId3v1Tag();
  185.  
  186.         String oldFileName = song.getFilename();
  187.         String newTempFile =
  188.                 Util.LIBDIR
  189.                         + tags.getAlbumArtist().replace("?", "")
  190.                         + " - "
  191.                         + tags.getTitle().replace("?", "")
  192.                         + ".mp3.new";
  193.         String newFile = newTempFile.substring(0, newTempFile.length() - 4);
  194.  
  195.         song.setId3v2Tag(tags);
  196.  
  197.         try {
  198.             song.save(newTempFile);
  199.             System.out.println("Saved as: " + new File(newTempFile).getName()); // TODO at the end delete this statement
  200.  
  201.             if (!new File(oldFileName).delete()) {
  202.                 System.err.printf("ERROR DELETING>>Couldn't delete the old file %s\n", oldFileName);
  203.             }
  204.             if (!new File(newTempFile).renameTo(new File(newFile))) {
  205.                 System.err.printf("ERROR RENAMING>>Couldn't rename %s to %s\n", newTempFile, newFile);
  206.             }
  207.  
  208.         } catch (IOException | NotSupportedException e) {
  209.             System.err.printf("ERROR SAVING>>Couldn't save %s to %s\n", oldFileName, newTempFile);
  210.             System.err.printf("ERROE SAVING MESSAGE>>%s\n", e.getMessage());
  211.             // e.printStackTrace();
  212.         }
  213.  
  214.  
  215.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement