Improved open book panel and added cover art

This commit is contained in:
2018-09-13 15:29:02 +01:00
parent 8cc7614a68
commit 6157eea4da
2 changed files with 139 additions and 36 deletions

View File

@@ -0,0 +1,108 @@
package uk.co.majenko.audiobookrecorder;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;
public class BookPanel extends JPanel {
String name;
String author;
String genre;
String comment;
ImageIcon cover;
Image resizedCover;
JLabel iconLabel;
JLabel titleLabel;
JLabel authorLabel;
JLabel otherLabel;
JPanel panel;
File root;
boolean highlight = false;
public BookPanel(File r) {
try {
root = r;
Properties props = new Properties();
props.loadFromXML(new FileInputStream(new File(root, "audiobook.abk")));
name = props.getProperty("book.name");
author = props.getProperty("book.author");
genre = props.getProperty("book.genre");
comment = props.getProperty("book.comment");
File icon = new File(root, "coverart.png");
if (icon.exists()) {
cover = new ImageIcon(icon.getAbsolutePath());
resizedCover = getScaledImage(cover.getImage(), 75, 75);
iconLabel = new JLabel(new ImageIcon(resizedCover));
} else {
cover = null;
resizedCover = null;
iconLabel = new JLabel("");
}
iconLabel.setSize(new Dimension(75, 75));
iconLabel.setPreferredSize(new Dimension(75, 75));
titleLabel = new JLabel(name);
authorLabel = new JLabel(author);
otherLabel = new JLabel(genre + " :: " + comment);
authorLabel.setForeground(new Color(0x80, 0x80, 0x80));
otherLabel.setForeground(new Color(0x80, 0x80, 0x80));
setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
panel.add(titleLabel);
panel.add(authorLabel);
panel.add(otherLabel);
add(iconLabel, BorderLayout.WEST);
add(panel, BorderLayout.CENTER);
panel.setBackground(new Color(0x20, 0x20, 0x20));
panel.setOpaque(true);
setBackground(new Color(0x20, 0x20, 0x20));
setOpaque(true);
} catch (Exception e) {
}
}
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
public File getConfigFile() {
return new File(root, "audiobook.abk");
}
public void highlight() {
setBackground(new Color(0x00, 0x20, 0x40));
panel.setBackground(new Color(0x00, 0x20, 0x40));
}
public void lowlight() {
setBackground(new Color(0x20, 0x20, 0x20));
panel.setBackground(new Color(0x20, 0x20, 0x20));
}
}

View File

@@ -28,13 +28,28 @@ public class OpenBookPanel extends JPanel {
}
}
public class BookCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) return null;
BookPanel p = (BookPanel)value;
if (isSelected) {
p.highlight();
} else {
p.lowlight();
}
return p;
}
};
class BookTableModel extends AbstractTableModel {
ArrayList<BookInfo> books;
ArrayList<BookPanel> books;
public BookTableModel() {
super();
books = new ArrayList<BookInfo>();
books = new ArrayList<BookPanel>();
}
public int getRowCount() {
@@ -42,38 +57,27 @@ public class OpenBookPanel extends JPanel {
}
public int getColumnCount() {
return 4;
return 1;
}
public boolean isCellEditable(int row, int column) {
return false;
}
public void addBook(BookInfo b) {
public void addBook(BookPanel b) {
books.add(b);
}
public Object getValueAt(int r, int c) {
if (c > 3) return null;
if (r > books.size()) return null;
BookInfo b = books.get(r);
switch (c) {
case 0: return b.name;
case 1: return b.author;
case 2: return b.genre;
case 4: return b.comment;
}
return null;
return books.get(r);
}
public String getColumnName(int i) {
switch(i) {
case 0: return "Name";
case 1: return "Author";
case 2: return "Genre";
case 3: return "Comment";
}
return null;
return "Book";
}
public Class getColumnClass(int i) {
return BookPanel.class;
}
}
@@ -85,7 +89,6 @@ public class OpenBookPanel extends JPanel {
model = new BookTableModel();
setLayout(new BorderLayout());
scroll = new JScrollPane();
add(scroll, BorderLayout.CENTER);
@@ -97,22 +100,16 @@ public class OpenBookPanel extends JPanel {
if (!b.isDirectory()) continue;
File xml = new File(b, "audiobook.abk");
if (xml.exists()) {
Properties props = new Properties();
props.loadFromXML(new FileInputStream(xml));
BookInfo book = new BookInfo(
props.getProperty("book.name"),
props.getProperty("book.author"),
props.getProperty("book.genre"),
props.getProperty("book.comment")
);
BookPanel book = new BookPanel(b);
model.addBook(book);
}
}
table = new JTable(model);
table.setDefaultRenderer(BookPanel.class, new BookCellRenderer());
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowHeight(80);
scroll.setViewportView(table);
} catch (Exception e) {
e.printStackTrace();
@@ -126,9 +123,7 @@ public class OpenBookPanel extends JPanel {
return null;
}
String name = (String)table.getValueAt(sel, 0);
File d = new File(Options.get("path.storage"), name);
File f = new File(d, "audiobook.abk");
return f;
BookPanel b = (BookPanel)table.getValueAt(sel, 0);
return b.getConfigFile();
}
}