made GUI stay in place on db refresh

This commit is contained in:
Louis Heredero 2024-05-07 15:37:47 +02:00
parent e165367133
commit a93598d8f6
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 47 additions and 28 deletions

View File

@ -5,7 +5,7 @@ import scala.collection.mutable.ArrayBuffer
class Album(val name: String) extends Serializable { class Album(val name: String) extends Serializable {
private var _songs: ArrayBuffer[Song] = new ArrayBuffer() private var _songs: ArrayBuffer[Song] = new ArrayBuffer()
def addSong(song: Song): Unit = _songs.addOne(song) 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 getSongs(): Array[Song] = _songs.toArray
def getSongByTitle(title: String): Option[Song] = { def getSongByTitle(title: String): Option[Song] = {

View File

@ -6,7 +6,7 @@ class Artist(val name: String) extends Serializable {
private var _albums: ArrayBuffer[Album] = new ArrayBuffer() private var _albums: ArrayBuffer[Album] = new ArrayBuffer()
def addAlbum(album: Album): Unit = _albums.addOne(album) 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 getAlbums(): Array[Album] = _albums.toArray
def getAlbumByName(name: String): Option[Album] = { def getAlbumByName(name: String): Option[Album] = {

View File

@ -5,7 +5,7 @@ import scala.collection.mutable.ArrayBuffer
class DataBase extends Serializable { class DataBase extends Serializable {
private var _artists: ArrayBuffer[Artist] = new ArrayBuffer() private var _artists: ArrayBuffer[Artist] = new ArrayBuffer()
def addArtist(artist: Artist): Unit = _artists.addOne(artist) 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 getArtists(): Array[Artist] = _artists.toArray
def getArtistByName(name: String): Option[Artist] = { def getArtistByName(name: String): Option[Artist] = {

View File

@ -7,12 +7,12 @@ object DataBaseHelper {
val db = new DataBase() val db = new DataBase()
for (a <- new File(directory).listFiles() if a.isDirectory) { for (a <- new File(directory).listFiles() if a.isDirectory) {
val artistName = a.getName val artistName = a.getName
println(s"found new artist : $artistName") //println(s"found new artist : $artistName")
val artist: Artist = new Artist(artistName) val artist: Artist = new Artist(artistName)
for (b <- a.listFiles() if b.isDirectory ) { for (b <- a.listFiles() if b.isDirectory ) {
val albumName = b.getName 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) val album: Album = new Album(albumName)
for (c <- b.listFiles() if c.isFile if c.getName.toLowerCase().endsWith(".mp3") ) { for (c <- b.listFiles() if c.isFile if c.getName.toLowerCase().endsWith(".mp3") ) {
@ -25,17 +25,17 @@ object DataBaseHelper {
fileName match { fileName match {
case format1(nr, name) => { 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 songName = name
songNumber = Integer.parseInt(nr) songNumber = Integer.parseInt(nr)
} }
case format2(cd, nr, name) => { 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 songName = name
songNumber = Integer.parseInt(nr) songNumber = Integer.parseInt(nr)
} }
case format3(nr, name) => { 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 songName = name
songNumber = Integer.parseInt(nr) songNumber = Integer.parseInt(nr)
} }

View File

@ -1,12 +1,18 @@
package ch.hevs.isc.slopify_v2 package ch.hevs.isc.slopify_v2
import io.methvin.watcher.{DirectoryChangeEvent, DirectoryChangeListener, DirectoryWatcher}
import java.awt.event.{KeyEvent, KeyListener} import java.awt.event.{KeyEvent, KeyListener}
import java.awt.{GridBagConstraints, GridBagLayout, LayoutManager} import java.awt.{GridBagConstraints, GridBagLayout, LayoutManager}
import java.nio.file.Paths
import java.util import java.util
import javax.swing._ import javax.swing._
import javax.swing.table.{DefaultTableModel, TableModel, TableRowSorter} import javax.swing.table.{DefaultTableModel, TableModel, TableRowSorter}
class GUI extends JFrame { class GUI extends JFrame {
val DIRECTORY: String = "res/songs/"
val DB_PATH: String = "res/songs_db.bin"
setSize(800, 600) setSize(800, 600)
setTitle("Slopify V2") setTitle("Slopify V2")
@ -81,21 +87,7 @@ class GUI extends JFrame {
tableSorter.setSortKeys(songsSortKeys) tableSorter.setSortKeys(songsSortKeys)
songsTable.setRowSorter(tableSorter) 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) 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 val songsSelectionModel: ListSelectionModel = songsTable.getSelectionModel
songsSelectionModel.addListSelectionListener(e => { songsSelectionModel.addListSelectionListener(e => {
val row: Int = e.getFirstIndex val row: Int = e.getFirstIndex
@ -131,6 +123,11 @@ class GUI extends JFrame {
var curAlbum: Option[Album] = None var curAlbum: Option[Album] = None
var curSong: Option[Song] = None var curSong: Option[Song] = None
DirectoryWatcher.builder()
.path(Paths.get(DIRECTORY))
.listener(e => onDirectoryChange(e))
.build().watchAsync()
refreshDatabase() refreshDatabase()
setVisible(true) setVisible(true)
@ -145,28 +142,30 @@ class GUI extends JFrame {
} }
def clearSongs(): Unit = { def clearSongs(): Unit = {
songsTableModel.setRowCount(0) songsTableModel.setRowCount(0)
//songsListModel.clear()
curSong = None curSong = None
} }
def addArtist(artist: Artist): Unit = { def addArtist(artist: Artist): Unit = {
println(s"Adding $artist") //println(s"Adding $artist")
artistsListModel.addElement(artist.name) artistsListModel.addElement(artist.name)
} }
def addAlbum(album: Album): Unit = { def addAlbum(album: Album): Unit = {
println(s"Adding $album") //println(s"Adding $album")
albumsListModel.addElement(album.name) albumsListModel.addElement(album.name)
} }
def addSong(song: Song): Unit = { def addSong(song: Song): Unit = {
println(s"Adding $song") //println(s"Adding $song")
val row: Array[String] = Array( val row: Array[String] = Array(
song.number.toString.reverse.padTo(2, '0').reverse, song.number.toString.reverse.padTo(2, '0').reverse,
song.title song.title
) )
songsTableModel.addRow(row.asInstanceOf[Array[AnyRef]]) songsTableModel.addRow(row.asInstanceOf[Array[AnyRef]])
//songsListModel.addElement(s"${song.title} (n° ${song.number})")
} }
def refreshDatabase(): Unit = { 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() clearArtists()
clearAlbums() clearAlbums()
clearSongs() clearSongs()
@ -174,6 +173,19 @@ class GUI extends JFrame {
for (artist: Artist <- artists) { for (artist: Artist <- artists) {
addArtist(artist) 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 = { def selectArtist(artist: Artist): Unit = {
curArtist = Some(artist) curArtist = Some(artist)
@ -208,6 +220,13 @@ class GUI extends JFrame {
addArtist(artist) addArtist(artist)
} }
} }
def onDirectoryChange(event: DirectoryChangeEvent): Unit = {
println("Changed " + event)
SwingUtilities.invokeLater(() => {
refreshDatabase()
})
}
} }
object GUI { object GUI {