diff --git a/src/ch/hevs/isc/slopify_v2/Album.scala b/src/ch/hevs/isc/slopify_v2/Album.scala index 6572a92..a2a8839 100644 --- a/src/ch/hevs/isc/slopify_v2/Album.scala +++ b/src/ch/hevs/isc/slopify_v2/Album.scala @@ -5,7 +5,7 @@ import scala.collection.mutable.ArrayBuffer class Album(val name: String) extends Serializable { private var _songs: ArrayBuffer[Song] = new ArrayBuffer() def addSong(song: Song): Unit = _songs.addOne(song) - def containsSong(song: Song): Boolean = _songs.contains(song) + def containsSong(song: Song): Boolean = _songs.exists(s => s.number == song.number && s.title == song.title) def getSongs(): Array[Song] = _songs.toArray def getSongByTitle(title: String): Option[Song] = { diff --git a/src/ch/hevs/isc/slopify_v2/Artist.scala b/src/ch/hevs/isc/slopify_v2/Artist.scala index 3001cc4..afdffad 100644 --- a/src/ch/hevs/isc/slopify_v2/Artist.scala +++ b/src/ch/hevs/isc/slopify_v2/Artist.scala @@ -6,7 +6,7 @@ class Artist(val name: String) extends Serializable { private var _albums: ArrayBuffer[Album] = new ArrayBuffer() def addAlbum(album: Album): Unit = _albums.addOne(album) - def hasAlbum(album: Album): Boolean = _albums.contains(album) + def hasAlbum(album: Album): Boolean = _albums.exists(_.name == album.name) def getAlbums(): Array[Album] = _albums.toArray def getAlbumByName(name: String): Option[Album] = { diff --git a/src/ch/hevs/isc/slopify_v2/DataBase.scala b/src/ch/hevs/isc/slopify_v2/DataBase.scala index 9ba4d92..d90aeb4 100644 --- a/src/ch/hevs/isc/slopify_v2/DataBase.scala +++ b/src/ch/hevs/isc/slopify_v2/DataBase.scala @@ -5,7 +5,7 @@ import scala.collection.mutable.ArrayBuffer class DataBase extends Serializable { private var _artists: ArrayBuffer[Artist] = new ArrayBuffer() def addArtist(artist: Artist): Unit = _artists.addOne(artist) - def containsArtist(artist: Artist): Boolean = _artists.contains(artist) + def containsArtist(artist: Artist): Boolean = _artists.exists(_.name == artist.name) def getArtists(): Array[Artist] = _artists.toArray def getArtistByName(name: String): Option[Artist] = { diff --git a/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala b/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala index f0ccc62..470503a 100644 --- a/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala +++ b/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala @@ -7,12 +7,12 @@ object DataBaseHelper { val db = new DataBase() for (a <- new File(directory).listFiles() if a.isDirectory) { val artistName = a.getName - println(s"found new artist : $artistName") + //println(s"found new artist : $artistName") val artist: Artist = new Artist(artistName) for (b <- a.listFiles() if b.isDirectory ) { val albumName = b.getName - println(s"found new album $albumName for artist : $artistName") + //println(s"found new album $albumName for artist : $artistName") val album: Album = new Album(albumName) for (c <- b.listFiles() if c.isFile if c.getName.toLowerCase().endsWith(".mp3") ) { @@ -25,17 +25,17 @@ object DataBaseHelper { fileName match { case format1(nr, name) => { - println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '${a.getName}'") + //println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '${a.getName}'") songName = name songNumber = Integer.parseInt(nr) } case format2(cd, nr, name) => { - println(s"found song nr #$nr on cd#$cd name:'$name' in album '$albumName' for artist : '${a.getName}'") + //println(s"found song nr #$nr on cd#$cd name:'$name' in album '$albumName' for artist : '${a.getName}'") songName = name songNumber = Integer.parseInt(nr) } case format3(nr, name) => { - println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '$artistName'") + //println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '$artistName'") songName = name songNumber = Integer.parseInt(nr) } diff --git a/src/ch/hevs/isc/slopify_v2/GUI.scala b/src/ch/hevs/isc/slopify_v2/GUI.scala index 37d8cf5..29c8535 100644 --- a/src/ch/hevs/isc/slopify_v2/GUI.scala +++ b/src/ch/hevs/isc/slopify_v2/GUI.scala @@ -1,12 +1,18 @@ package ch.hevs.isc.slopify_v2 +import io.methvin.watcher.{DirectoryChangeEvent, DirectoryChangeListener, DirectoryWatcher} + import java.awt.event.{KeyEvent, KeyListener} import java.awt.{GridBagConstraints, GridBagLayout, LayoutManager} +import java.nio.file.Paths import java.util import javax.swing._ import javax.swing.table.{DefaultTableModel, TableModel, TableRowSorter} class GUI extends JFrame { + val DIRECTORY: String = "res/songs/" + val DB_PATH: String = "res/songs_db.bin" + setSize(800, 600) setTitle("Slopify V2") @@ -81,21 +87,7 @@ class GUI extends JFrame { tableSorter.setSortKeys(songsSortKeys) songsTable.setRowSorter(tableSorter) - //val songsListModel: DefaultListModel[String] = new DefaultListModel[String]() - //val songsList: JList[String] = new JList(songsListModel) - //val col3: JScrollPane = new JScrollPane(songsList) val col3: JScrollPane = new JScrollPane(songsTable) - /* - songsList.addListSelectionListener(_ => { - if (curAlbum.isDefined) { - val songName: String = songsList.getSelectedValue - val song: Option[Song] = curAlbum.get.getSongByTitle(songName) - if (song.isDefined) { - selectSong(song.get) - } - } - }) - */ val songsSelectionModel: ListSelectionModel = songsTable.getSelectionModel songsSelectionModel.addListSelectionListener(e => { val row: Int = e.getFirstIndex @@ -131,6 +123,11 @@ class GUI extends JFrame { var curAlbum: Option[Album] = None var curSong: Option[Song] = None + DirectoryWatcher.builder() + .path(Paths.get(DIRECTORY)) + .listener(e => onDirectoryChange(e)) + .build().watchAsync() + refreshDatabase() setVisible(true) @@ -145,28 +142,30 @@ class GUI extends JFrame { } def clearSongs(): Unit = { songsTableModel.setRowCount(0) - //songsListModel.clear() curSong = None } def addArtist(artist: Artist): Unit = { - println(s"Adding $artist") + //println(s"Adding $artist") artistsListModel.addElement(artist.name) } def addAlbum(album: Album): Unit = { - println(s"Adding $album") + //println(s"Adding $album") albumsListModel.addElement(album.name) } def addSong(song: Song): Unit = { - println(s"Adding $song") + //println(s"Adding $song") val row: Array[String] = Array( song.number.toString.reverse.padTo(2, '0').reverse, song.title ) songsTableModel.addRow(row.asInstanceOf[Array[AnyRef]]) - //songsListModel.addElement(s"${song.title} (n° ${song.number})") } def refreshDatabase(): Unit = { - db = DataBaseHelper.create("res/songs") + db = DataBaseHelper.create(DIRECTORY) + val oldArtist: Option[Artist] = curArtist + val oldAlbum: Option[Album] = curAlbum + val oldSong: Option[Song] = curSong + clearArtists() clearAlbums() clearSongs() @@ -174,6 +173,19 @@ class GUI extends JFrame { for (artist: Artist <- artists) { addArtist(artist) } + + if (oldArtist.isDefined && db.containsArtist(oldArtist.get)) { + artistsList.setSelectedIndex(artistsListModel.indexOf(oldArtist.get.name)) + + if (oldAlbum.isDefined && curArtist.get.hasAlbum(oldAlbum.get)) { + albumsList.setSelectedIndex(albumsListModel.indexOf(oldAlbum.get.name)) + + if (oldSong.isDefined && curAlbum.get.containsSong(oldSong.get)) { + selectSong(oldSong.get) + } + } + } + DataBaseHelper.save(DB_PATH, db) } def selectArtist(artist: Artist): Unit = { curArtist = Some(artist) @@ -208,6 +220,13 @@ class GUI extends JFrame { addArtist(artist) } } + + def onDirectoryChange(event: DirectoryChangeEvent): Unit = { + println("Changed " + event) + SwingUtilities.invokeLater(() => { + refreshDatabase() + }) + } } object GUI {