/*
* Thumbnail.java
*
* Copyright (C) 2006-2007 Gabriel Burca (gburca dash virtmus at ebixio dot com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.ebixio.virtmus;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
/**
*
* @author Gabriel Burca <gburca dash virtmus at ebixio dot com>
*/
public class Thumbnail extends javax.swing.JPanel {
private boolean selected = false; // Show a different border when the thumbnail is selected
private MusicPage page;
private BufferedImage img = null;
private Color defaultLabelColor;
private Border defaultBorder;
/** Creates new form Thumbnail */
public Thumbnail() {
initComponents();
defaultLabelColor = this.label.getBackground();
defaultBorder = this.getBorder();
this.setBackground(new Color(255, 255, 255));
}
public Thumbnail(int w, int h) {
this(); // Call the default constructor first
this.setSize(w, h);
Border b = getBorder();
h = label.getPreferredSize().height;
if (b != null) {
Insets insets = b.getBorderInsets(this);
label.setSize(w - insets.left - insets.right, h);
} else {
label.setSize(w, h);
}
}
public Thumbnail(int w, int h, String description) {
this(w, h);
setName(description);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
label = new javax.swing.JLabel();
canvas = new Thumb();
setBorder(javax.swing.BorderFactory.createEtchedBorder());
setMinimumSize(new java.awt.Dimension(20, 20));
label.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
label.setText("jLabel1");
label.setMinimumSize(new java.awt.Dimension(20, 14));
label.setOpaque(true);
javax.swing.GroupLayout canvasLayout = new javax.swing.GroupLayout(canvas);
canvas.setLayout(canvasLayout);
canvasLayout.setHorizontalGroup(
canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 131, Short.MAX_VALUE)
);
canvasLayout.setVerticalGroup(
canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 132, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
.addComponent(canvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(canvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(0, 0, 0)
.addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel canvas;
private javax.swing.JLabel label;
// End of variables declaration//GEN-END:variables
@Override
public void setName(String name) {
String origName = name;
FontMetrics fm = label.getFontMetrics(label.getFont());
int chars = 0;
do {
name = Utils.shortenString(origName, chars++);
} while (fm.stringWidth(name) > label.getSize().getWidth()
&& chars < origName.length());
label.setText(name);
this.setToolTipText(origName);
}
@Override
public String getName() {
// The JPanel constructor can call this before label is created
return (label != null) ? label.getText() : super.getName();
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
if (selected) {
//this.setBorder(BorderFactory.createLineBorder(new Color(255, 0, 0), 5));
this.setBorder(BorderFactory.createEtchedBorder(new Color(255, 200, 200), new Color(100, 100, 100)));
this.label.setBackground(new Color(255, 102, 102));
} else {
//this.setBorder(BorderFactory.createEtchedBorder());
this.setBorder(defaultBorder);
this.label.setBackground(defaultLabelColor);
}
}
public MusicPage getPage() {
return page;
}
public void setPage(MusicPage page) {
this.page = page;
}
private class Thumb extends javax.swing.JPanel implements Renderer.JobRequester {
boolean imgRequested = false, imgReturned = false;
@Override
public void paint(Graphics g) {
super.paint(g);
if (img == null) {
if (page != null) {
if (imgRequested) {
if (imgReturned) {
paintMsg(g, "No image or OOM");
} else {
paintMsg(g, "Loading...");
}
} else {
Renderer.JobRequest req = new Renderer.JobRequest(this, 0, 10, this.getSize());
req.fillSize = true;
Renderer.requestRendering(req, page);
imgRequested = true;
paintMsg(g, "Loading...");
}
} else {
paintMsg(g, "No image.");
}
} else {
g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
}
}
private void paintMsg(Graphics g, String msg) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
int msgW = g.getFontMetrics().stringWidth(msg);
g.drawString(msg, getWidth()/2 - msgW/2, getHeight()/2);
}
@Override
public void renderingComplete(MusicPage mp, Renderer.JobRequest request) {
img = Renderer.getRenderedImage(this);
imgReturned = true;
if (this.isShowing()) this.repaint();
}
}
}