package net.sf.jpluck.apps.jpluckx.ui;
import net.sf.jpluck.ClientConfiguration;
import net.sf.jpluck.apps.jpluckx.JPluckX;
import net.sf.jpluck.http.HttpClient;
import net.sf.jpluck.http.HttpResponse;
import org.apache.commons.threadpool.DefaultThreadPool;
import org.apache.commons.threadpool.ThreadPool;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
class PreviewLabel extends JLabel {
public static final String LOCAL_ROOT = "http://127.0.0.1/showcase/images";
private static final String LOCAL_CLASSPATH = "showcase/images";
public static final String WEB_ROOT = "http://jpluck.sourceforge.net/showcase/images";
private Set notFoundSet = new HashSet();
private String rootPath;
private BufferedImage image;
private ThreadPool threadPool = new DefaultThreadPool(1);
private String name;
private File dir;
private BufferedImage noPreviewImage;
private BufferedImage downloadingImage;
private Map imageMap = new HashMap();
public PreviewLabel(String rootPath) {
this.rootPath = rootPath;
setBorder(new LineBorder(Color.BLACK, 1));
setPreferredSize(new Dimension(160, 160));
if (!JPluckX.getInstance().isWebStart()) {
URL url = PreviewLabel.class.getClassLoader().getResource(LOCAL_CLASSPATH);
URI uri = URI.create(url.toString());
dir = new File(uri);
}
try {
noPreviewImage = ImageIO.read(PreviewLabel.class.getClassLoader().getResource("net/sf/jpluck/res/showcase/nopreview.gif"));
downloadingImage = ImageIO.read(PreviewLabel.class.getClassLoader().getResource("net/sf/jpluck/res/showcase/downloadingpreview.gif"));
} catch (IOException e) {
}
}
public synchronized void showPreview(String name) {
if (name != null) {
this.name = name;
} else {
name = this.name;
}
byte[] data = (byte[]) imageMap.get(name);
if (data == null) {
File imageFile = new File(dir, convertToFilename(name));
if (imageFile.exists()) {
try {
data = new byte[(int) imageFile.length()];
FileInputStream in = new FileInputStream(imageFile);
in.read(data);
in.close();
} catch (IOException e) {
data = null;
}
} else if (notFoundSet.contains(name)) {
image = noPreviewImage;
repaint();
return;
}
}
if (data != null) {
try {
image = ImageIO.read(new ByteArrayInputStream(data));
repaint();
} catch (IOException e) {
}
} else {
if (ClientConfiguration.getDefault().isShowcasePreview()) {
image = downloadingImage;
repaint();
threadPool.invokeLater(new RetrieveTask(name));
} else {
image = noPreviewImage;
repaint();
}
}
}
private String convertToFilename(String name) {
name = name.toLowerCase();
name = name.replace(' ', '_');
name = name.replaceAll("[\\?:!&']", "");
return name + ".gif";
}
protected void paintComponent(Graphics g) {
if (image != null) {
g.drawImage(image, 0, 0, this);
} else {
g.clearRect(0, 0, 160, 160);
}
}
private class RetrieveTask implements Runnable {
private String name;
public RetrieveTask(String name) {
this.name = name;
}
public void run() {
try {
HttpClient client = new HttpClient();
String filename = convertToFilename(name);
HttpResponse response = client.doGet(rootPath + "/" + filename);
if (response.getStatusCode() == 200) {
byte[] data = response.getContent();
if (!JPluckX.getInstance().isWebStart()) {
FileOutputStream out = new FileOutputStream(new File(dir, filename));
out.write(data);
out.close();
}
imageMap.put(name, data);
if (PreviewLabel.this.name.equals(name)) {
synchronized (image) {
image = ImageIO.read(new ByteArrayInputStream(data));
repaint();
}
}
} else {
notFoundSet.add(name);
if (PreviewLabel.this.name.equals(name)) {
synchronized (image) {
image = noPreviewImage;
repaint();
}
}
}
} catch (Exception e) {
notFoundSet.add(name);
if (PreviewLabel.this.name.equals(name)) {
synchronized (image) {
image = noPreviewImage;
repaint();
}
}
}
}
}
}