made GUI stay in place on db refresh
This commit is contained in:
parent
e165367133
commit
a93598d8f6
@ -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] = {
|
||||
|
@ -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] = {
|
||||
|
@ -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] = {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user