Advertisement
Guest User

Controller.java

a guest
Jun 13th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.13 KB | None | 0 0
  1. package controller;
  2.  
  3. import classes.Playlist;
  4. import classes.SerializableStrategy;
  5. import classes.Song;
  6. import javafx.beans.value.ChangeListener;
  7. import javafx.beans.value.ObservableValue;
  8. import javafx.collections.ModifiableObservableListBase;
  9. import javafx.scene.control.ListCell;
  10. import javafx.scene.control.ListView;
  11. import javafx.scene.input.MouseEvent;
  12. import javafx.scene.media.Media;
  13. import javafx.scene.media.MediaPlayer;
  14. import javafx.util.Duration;
  15. import model.Model;
  16. import view.View;
  17.  
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.rmi.RemoteException;
  21.  
  22. public class Controller implements interfaces.Controller {
  23.  
  24.     // Variablen
  25.  
  26.     private Model model;
  27.     private Media file;
  28.     private MediaPlayer player;
  29.  
  30.     private Boolean isplaying = false;
  31.     private Boolean ishalted = false;
  32.  
  33.     private long counter = -1;
  34.  
  35.     private Duration fullduration;
  36.     private Duration currentduration;
  37.  
  38.  
  39.     private Song s;
  40.     private Song startSong;
  41.  
  42.     private File f;
  43.     private File startFile;
  44.     private File[] paths;
  45.     private File songFile;
  46.  
  47.  
  48.     public String titleCell, idCell;
  49.  
  50.     //Methoden
  51.  
  52.     @Override
  53.     public void link(Model model, View view) {
  54.         this.model = model;
  55.         view.addController(this);
  56.     }
  57.  
  58.     @Override
  59.     public void loadbtn() {
  60.         System.out.println(model.getPlaylist().size());
  61.     }
  62.  
  63.     @Override
  64.     public void savebtn() {
  65.         System.out.println("Speichern");
  66.     }
  67.  
  68.     // TODO: 05.06.2018 model.updateplaylistview auslagern -> ggf unnötig
  69.  
  70.     public void addallbtn(View view) {
  71.         SerializableStrategy strat = new SerializableStrategy();
  72.  
  73.         try {
  74.             strat.openWritableLibrary();
  75.             strat.writeLibrary(model.getAllSongs());
  76.             strat.closeWritableLibrary();
  77.             strat.openReadableLibrary();
  78.             model.setPlaylist((Playlist) strat.readLibrary());
  79.             strat.closeReadableLibrary();
  80.         } catch (IOException | ClassNotFoundException e) {
  81.             e.printStackTrace();
  82.         }
  83.         updatePlaylistView(view);
  84.  
  85.     }
  86.  
  87.  
  88.     // TODO: 05.06.2018 model.updateplaylistview un LibviewUpdate auslagern -> ggf unnötig
  89.     @Override
  90.     public void addtoplaylistbtn(View view) {
  91.  
  92.         try {
  93.             SerializableStrategy strat = new SerializableStrategy();
  94.  
  95.             initClickedSong(view, model.getAllSongs());
  96.  
  97.  
  98.             strat.writeSong(s);
  99.  
  100.             Song s = (Song) strat.readSong();
  101.             s.setId(counter);
  102.  
  103.             model.getPlaylist().addSong(s);
  104.  
  105.             strat.openWritablePlaylist();
  106.             strat.writePlaylist(model.getPlaylist()); //playlist in datei schreiben und somit abspeichern
  107.             strat.closeWritablePlaylist();
  108.  
  109.             strat.openReadablePlaylist();
  110.             model.setPlaylist((Playlist) strat.readPlaylist());
  111.             strat.closeReadablePlaylist();
  112.  
  113.  
  114.             System.out.println(model.getPlaylist().size());
  115.             counter--;
  116.  
  117.             updateLibView(view);
  118.             updatePlaylistView(view);
  119.         } catch (IOException e) {
  120.             e.printStackTrace();
  121.         } catch (ClassNotFoundException e) {
  122.             e.printStackTrace();
  123.         }
  124.  
  125.  
  126.     }
  127.  
  128.     @Override
  129.     public void nextbtn(View view) {
  130.         if (s.getId() < 0) { //wenn s ids negativ ist (also in playlist geeklickzt wure
  131.             if (s.getId() - 1 >= -model.getPlaylist().size()) { //solange es noch nächsten song gibt
  132.                 playSong((Song) model.getPlaylist().findSongByID(s.getId() - 1), view); //nächsten Song abpielen
  133.                 s = (Song) model.getPlaylist().findSongByID(s.getId() - 1); //s = aktueller songs setzen, damit wir immer next drücken könnten
  134.  
  135.             }
  136.         }
  137.         if (s.getId() > 0) {
  138.             if (s.getId() + 1 <= model.getAllSongs().size()) {
  139.                 playSong((Song) model.getAllSongs().findSongByID(s.getId() + 1), view);
  140.                 s = (Song) model.getAllSongs().findSongByID(s.getId() + 1);
  141.             }
  142.         }
  143.         view.setbtnplaypause("||");
  144.         currentduration = null;
  145.     }
  146.  
  147.     @Override
  148.     public void playpauseSong(View view) {
  149.         if (isplaying == false) {
  150.             playSong(s, view);
  151.             view.setbtnplaypause("||");
  152.             isplaying = true;
  153.         } else if (isplaying == true) {
  154.             pauseSong(s);
  155.             view.setbtnplaypause("|>");
  156.             isplaying = false;
  157.         }
  158.     }
  159.  
  160.     @Override
  161.     public void loadPlaylist(Playlist playlist) throws RemoteException {
  162.         model.setPlaylist(playlist);
  163.     }
  164.  
  165.     // TODO: 05.06.2018 unnötig wegen getSelectionmodel oder so
  166.  
  167.  
  168.     @Override
  169.     public void backbtn(View view) {
  170.         if (s.getId() < 0) {
  171.             if (s.getId() + 1 < 0) { //solange es noch nächsten song gibt
  172.                 playSong((Song) model.getPlaylist().findSongByID(s.getId() + 1), view);
  173.                 s = (Song) model.getPlaylist().findSongByID(s.getId() + 1);
  174.             }
  175.         }
  176.         if (s.getId() > 0) {
  177.             if (s.getId() - 1 > 0) {
  178.                 playSong((Song) model.getAllSongs().findSongByID(s.getId() - 1), view);
  179.                 s = (Song) model.getAllSongs().findSongByID(s.getId() - 1);
  180.             }
  181.         }
  182.         currentduration = null;
  183.     }
  184.  
  185.     // TODO: 05.06.2018 updatePlaylistview weg
  186.  
  187.     @Override
  188.     public void deleteplaylist(View view) {
  189.         model.getPlaylist().clearPlaylist();
  190.         updatePlaylistView(view);
  191.     }
  192.  
  193.     // TODO: 05.06.2018 updateviews weg
  194.     @Override
  195.     public void commitbtn(View view) {
  196.         String title = view.getTxtTitle().getText();
  197.         String interpret = view.getTxtInterpret().getText();
  198.         String album = view.getTxtAlbum().getText();
  199.         s.setTitle(title);
  200.         s.setInterpret(interpret);
  201.         s.setAlbum(album);
  202.  
  203.         updateLibView(view);
  204.         updatePlaylistView(view);
  205.         updateLibView(view);
  206.     }
  207.  
  208.     // TODO: 05.06.2018
  209.  
  210.     // TODO: 05.06.2018
  211.     @Override
  212.     public void loadlib(String path, Playlist allsongs, View view) throws RemoteException {
  213.         model.setAllSongs(new Playlist());
  214.         f = new File(path);
  215.         int counter = 0;
  216.         paths = f.listFiles();
  217.         for (File file : paths) {
  218.             if (file.getPath().endsWith(".mp3")) {
  219.                 counter++;
  220.                 s = new Song();
  221.                 s.setTitle(file.getName().replace(".mp3", ""));
  222.                 s.setAlbum("");
  223.                 s.setPath(file.getPath());
  224.                 s.setInterpret("");
  225.                 s.setId(counter);
  226.                 model.getAllSongs().addSong(s);
  227.             }
  228.         }
  229.         //play sound bei programmstart
  230.         startFile = new File("res/iceicebaby.mp3");
  231.         startSong = new Song();
  232.         startSong.setPath(startFile.getPath());
  233.         playSong(startSong, view);
  234.  
  235.  
  236.         updateLibView(view);
  237.         updatePlaylistView(view);
  238.     }
  239.  
  240.     // TODO: 05.06.2018
  241.  
  242.  
  243.     @Override
  244.     public void stopsong(View view) {
  245.         player.stop();
  246.         isplaying = false;
  247.         view.setbtnplaypause("|>");
  248.         currentduration = null;
  249.     }
  250.  
  251.     @Override
  252.     public void playSong(Song s, View view) {
  253.         songFile = new File(s.pathProperty().getValue());
  254.         file = new Media(songFile.toURI().toString());
  255.         player = new MediaPlayer(file);
  256.         player.setOnEndOfMedia(() -> {
  257.             nextbtn(view);
  258.             view.setbtnplaypause("|>");
  259.         });
  260.  
  261.         player.setOnReady(new Runnable() {
  262.             @Override
  263.             public void run() {
  264.                 if (ishalted == true) {
  265.                     player.setStartTime(currentduration);
  266.                     player.play();
  267.  
  268.  
  269.                 } else {
  270.                     player.stop();
  271.                     player.play();
  272.                 }
  273.                 player.currentTimeProperty().addListener(new ChangeListener<Duration>() {
  274.                     @Override
  275.                     public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
  276.                         fullduration = player.getMedia().getDuration();
  277.                         view.getSlider().setValue(newValue.divide(fullduration.toMillis()).toMillis() * 100.0);
  278.                         view.setLblcurrentduration(String.valueOf(newValue.toSeconds()));
  279.                         view.setLblfinalduration(String.valueOf("/   " + player.getTotalDuration().toSeconds()));
  280.                     }
  281.                 });
  282.             }
  283.         });
  284.  
  285.  
  286.     }
  287.  
  288.     public void updatePlaylistView(View view) {
  289.         s = new Song();
  290.         // TODO: 20.05.18 nur wenn song noch nicht vorhanden ist!
  291.         view.getPlaylistView().setCellFactory(new javafx.util.Callback<ListView<Song>, ListCell<Song>>() {
  292.             @Override
  293.             public ListCell<Song> call(ListView<Song> param) {
  294.                 ListCell<Song> cell = new ListCell<Song>() {
  295.                     @Override
  296.                     protected void updateItem(Song s, boolean bln) {
  297.                         super.updateItem(s, bln);
  298.                         if (s != null) {
  299.                             String tmps = s.titleProperty().getValue();
  300.                             tmps.replace(".mp3", "");
  301.                             setText(tmps);
  302.                             setId(s.getTitle());
  303.                         }
  304.                     }
  305.                 };
  306.                 cell.setOnMouseClicked((MouseEvent event) -> {
  307.                     if (cell.isEmpty()) {
  308.                         event.consume();
  309.                     } else {
  310.                         titleCell = cell.getText();
  311.                         idCell = cell.getId();
  312.                         s = cell.getItem();
  313.                         view.setTxtTitle(s.getTitle());
  314.  
  315.                     }
  316.                 });
  317.                 return cell;
  318.  
  319.             }
  320.         });
  321.  
  322.  
  323.         view.getPlaylistView().setItems((ModifiableObservableListBase) model.getPlaylist());
  324.  
  325.         updateLibView(view);
  326.  
  327.     }
  328.  
  329.     @Override
  330.     public void pauseSong(Song s) {
  331.         player.pause();
  332.         currentduration = player.getCurrentTime();
  333.         isplaying = false;
  334.         ishalted = true;
  335.     }
  336.  
  337.  
  338.     @Override
  339.     public void updateLibView(View view) {
  340.         s = new Song();
  341.         view.getSongListView().setCellFactory(new javafx.util.Callback<ListView<Song>, ListCell<Song>>() {
  342.             @Override
  343.             public ListCell<Song> call(ListView<Song> param) {
  344.                 ListCell<Song> cell = new ListCell<Song>() {
  345.                     @Override
  346.                     protected void updateItem(Song s, boolean bln) {
  347.                         super.updateItem(s, bln);
  348.                         if (s != null) {
  349.                             String tmps = s.getTitle();
  350.                             tmps.replace(".mp3", "");
  351.                             setText(tmps);
  352.                             setId(s.getTitle());
  353.                         }
  354.                     }
  355.                 };
  356.                 //set for each sell a MouseEvent
  357.                 cell.setOnMouseClicked((MouseEvent event) -> {
  358.                     if (cell.isEmpty()) {
  359.                         event.consume();
  360.                     } else {
  361.                         titleCell = cell.getText();
  362.                         idCell = cell.getId();
  363.                         initClickedSong(view, model.getAllSongs());
  364.                         view.setTxtTitle(s.getTitle());
  365.                     }
  366.                 });
  367.                 return cell;
  368.             }
  369.         });
  370.         view.getSongListView().setItems((ModifiableObservableListBase) model.getAllSongs());
  371.  
  372.     }
  373.  
  374.  
  375.     public void initClickedSong(View view, Playlist playlistLib) {
  376.         //get the Song from allSongs which is represented by the clicked cell
  377.         for (int i = 0; i < playlistLib.size(); i++) {
  378.             s = (Song) playlistLib.get(i);
  379.             if (s.getTitle().contains(idCell)) {
  380.                 view.setTxtAlbum(s.getAlbum());
  381.                 view.setTxtInterpret(s.getInterpret());
  382.                 return;
  383.             }
  384.         }
  385.     }
  386.  
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement