From 95b04796000caa551ff273a9a1d14194f4083011 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 7 May 2024 14:09:47 +0200 Subject: [PATCH] task 3 --- src/ch/hevs/isc/slopify_v2/Album.scala | 9 ++ src/ch/hevs/isc/slopify_v2/Artist.scala | 9 ++ src/ch/hevs/isc/slopify_v2/DataBase.scala | 9 ++ src/ch/hevs/isc/slopify_v2/GUI.scala | 171 ++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 src/ch/hevs/isc/slopify_v2/GUI.scala diff --git a/src/ch/hevs/isc/slopify_v2/Album.scala b/src/ch/hevs/isc/slopify_v2/Album.scala index 63cb5ed..6572a92 100644 --- a/src/ch/hevs/isc/slopify_v2/Album.scala +++ b/src/ch/hevs/isc/slopify_v2/Album.scala @@ -8,5 +8,14 @@ class Album(val name: String) extends Serializable { def containsSong(song: Song): Boolean = _songs.contains(song) def getSongs(): Array[Song] = _songs.toArray + def getSongByTitle(title: String): Option[Song] = { + for (song: Song <- _songs) { + if (song.title == title) { + return Some(song) + } + } + return None + } + override def toString: String = s"" } diff --git a/src/ch/hevs/isc/slopify_v2/Artist.scala b/src/ch/hevs/isc/slopify_v2/Artist.scala index 5d24378..3001cc4 100644 --- a/src/ch/hevs/isc/slopify_v2/Artist.scala +++ b/src/ch/hevs/isc/slopify_v2/Artist.scala @@ -9,5 +9,14 @@ class Artist(val name: String) extends Serializable { def hasAlbum(album: Album): Boolean = _albums.contains(album) def getAlbums(): Array[Album] = _albums.toArray + def getAlbumByName(name: String): Option[Album] = { + for (album: Album <- _albums) { + if (album.name == name) { + return Some(album) + } + } + return None + } + override def toString: String = s"" } diff --git a/src/ch/hevs/isc/slopify_v2/DataBase.scala b/src/ch/hevs/isc/slopify_v2/DataBase.scala index 8b630f1..9ba4d92 100644 --- a/src/ch/hevs/isc/slopify_v2/DataBase.scala +++ b/src/ch/hevs/isc/slopify_v2/DataBase.scala @@ -8,5 +8,14 @@ class DataBase extends Serializable { def containsArtist(artist: Artist): Boolean = _artists.contains(artist) def getArtists(): Array[Artist] = _artists.toArray + def getArtistByName(name: String): Option[Artist] = { + for (artist: Artist <- _artists) { + if (artist.name == name) { + return Some(artist) + } + } + return None + } + override def toString: String = s"" } diff --git a/src/ch/hevs/isc/slopify_v2/GUI.scala b/src/ch/hevs/isc/slopify_v2/GUI.scala new file mode 100644 index 0000000..7d9b8dd --- /dev/null +++ b/src/ch/hevs/isc/slopify_v2/GUI.scala @@ -0,0 +1,171 @@ +package ch.hevs.isc.slopify_v2 + +import java.util +import javax.swing._ +import javax.swing.table.{DefaultTableModel, TableModel, TableRowSorter} + +class GUI extends JFrame { + setSize(800, 600) + setTitle("Slopify V2") + + // Artists + val artistsListModel: DefaultListModel[String] = new DefaultListModel[String]() + val artistsList: JList[String] = new JList(artistsListModel) + artistsList.setLayoutOrientation(JList.VERTICAL) + val col1: JScrollPane = new JScrollPane(artistsList) + artistsList.addListSelectionListener(_ => { + val artistName: String = artistsList.getSelectedValue + val artist: Option[Artist] = db.getArtistByName(artistName) + if (artist.isDefined) { + selectArtist(artist.get) + } else { + clearAlbums() + } + }) + + // Albums + val albumsListModel: DefaultListModel[String] = new DefaultListModel[String]() + val albumsList: JList[String] = new JList(albumsListModel) + val col2: JScrollPane = new JScrollPane(albumsList) + albumsList.addListSelectionListener(_ => { + if (curArtist.isDefined) { + val albumName: String = albumsList.getSelectedValue + val album: Option[Album] = curArtist.get.getAlbumByName(albumName) + if (album.isDefined) { + selectAlbum(album.get) + } else { + clearSongs() + } + } + }) + + // Songs + val songsTableHeaders: Array[Object] = Array("Track no", "Title") + val songsTable: JTable = new JTable(Array.empty[Array[Object]], songsTableHeaders) + val songsTableModel: DefaultTableModel = new DefaultTableModel() + songsTableModel.addColumn("Track no") + songsTableModel.addColumn("Title") + songsTable.setModel(songsTableModel) + songsTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN) + + val tableSorter: TableRowSorter[TableModel] = new TableRowSorter(songsTable.getModel) + val songsSortKeys: util.List[RowSorter.SortKey] = new util.ArrayList[RowSorter.SortKey]() + songsSortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)) + 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 + val songName: String = songsTableModel.getValueAt(row, 0).asInstanceOf[String] + val song: Option[Song] = curAlbum.get.getSongByTitle(songName) + if (song.isDefined) { + selectSong(song.get) + } + }) + + val split1: JSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, col1, col2) + val split2: JSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, split1, col3) + split1.setResizeWeight(0.5) + split2.setResizeWeight(0.66) + + val myMenuBar: JMenuBar = new JMenuBar() + val fileMenu: JMenu = new JMenu("File") + val refreshDbBtn: JMenuItem = new JMenuItem("Refresh Database") + refreshDbBtn.addActionListener(_ => refreshDatabase()) + + fileMenu.add(refreshDbBtn) + myMenuBar.add(fileMenu) + setJMenuBar(myMenuBar) + getContentPane.add(split2) + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) + + var db: DataBase = _ + var curArtist: Option[Artist] = None + var curAlbum: Option[Album] = None + var curSong: Option[Song] = None + + refreshDatabase() + setVisible(true) + + + def clearArtists(): Unit = { + artistsListModel.clear() + curArtist = None + } + def clearAlbums(): Unit = { + albumsListModel.clear() + curAlbum = None + } + def clearSongs(): Unit = { + songsTableModel.setRowCount(0) + //songsListModel.clear() + curSong = None + } + def addArtist(artist: Artist): Unit = { + println(s"Adding $artist") + artistsListModel.addElement(artist.name) + } + def addAlbum(album: Album): Unit = { + println(s"Adding $album") + albumsListModel.addElement(album.name) + } + def addSong(song: Song): Unit = { + 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") + clearArtists() + clearAlbums() + clearSongs() + val artists: Array[Artist] = db.getArtists().sortBy(_.name) + for (artist: Artist <- artists) { + addArtist(artist) + } + } + def selectArtist(artist: Artist): Unit = { + curArtist = Some(artist) + clearAlbums() + val albums: Array[Album] = artist.getAlbums().sortBy(_.name) + for (album: Album <- albums) { + addAlbum(album) + } + } + def selectAlbum(album: Album): Unit = { + curAlbum = Some(album) + clearSongs() + for (song: Song <- album.getSongs()) { + addSong(song) + } + } + def selectSong(song: Song): Unit = { + curSong = Some(song) + } +} + +object GUI { + def main(args: Array[String]): Unit = { + val gui: GUI = new GUI() + } +} \ No newline at end of file