OSDN Git Service

サムネイル表示情報を再考
authoryukihane <yukihane.feather@gmail.com>
Sun, 18 Sep 2011 07:37:00 +0000 (16:37 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sun, 18 Sep 2011 07:37:00 +0000 (16:37 +0900)
frontend/src/yukihane/inqubus/gui/MainFrame.java
frontend/src/yukihane/inqubus/thumbnail/Repository.java
frontend/src/yukihane/inqubus/thumbnail/Thumbnail.java

index 8f76527..f22e5b5 100644 (file)
@@ -22,6 +22,7 @@ import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
@@ -162,11 +163,22 @@ public class MainFrame extends JFrame {
                 int row = convertRowIndexToModel(rowAtPoint(e.getPoint()));
                 TableModel m = getModel();
                 final String videoId = (String) m.getValueAt(row, 0);
-                final Thumbnail thumbnail = thumbRepository.getThumnail(videoId);
-                if(thumbnail == null){
-                    return "動画情報取得中...";
+                try {
+                    final Thumbnail thumbnail = thumbRepository.getThumnail(videoId);
+                    if (thumbnail == null) {
+                        return videoId + ": 動画情報未取得";
+                    }
+
+                    final URL imageUrl = thumbnail.getImageFile().toURI().toURL();
+
+                    return "<html>" + videoId + ": " + thumbnail.getTitle()
+                            + " (" + thumbnail.getLength() + ")" + "<br/>"
+                            + "<img src=\"" + imageUrl + "\"/>"
+                            + "</html>";
+                } catch (Throwable ex) {
+                    logger.warn(null, ex);
+                    return videoId + ": 情報取得できません";
                 }
-                return "<html>" + videoId + ":" + thumbnail.getTitle() + "</html>";
             }
         };
         tblDisplay.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
@@ -595,7 +607,8 @@ public class MainFrame extends JFrame {
                 logger.debug(downProf.toString());
                 logger.debug(convProf.toString());
 
-                thumbRepository.request(downProf.getProxyProfile(), id);
+                final File tempDir = new File(Config.INSTANCE.getSystemTempDir());
+                thumbRepository.request(downProf.getProxyProfile(), tempDir, id);
 
                 final RequestProcess rp = new RequestProcess(downProf, id, convProf);
                 taskManager.add(rp);
index adc9370..c0a89c1 100644 (file)
@@ -1,6 +1,7 @@
 package yukihane.inqubus.thumbnail;
 
 import java.awt.image.BufferedImage;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -9,7 +10,6 @@ import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import javax.imageio.ImageIO;
-import javax.swing.ImageIcon;
 import nicobrowser.NicoHttpClient;
 import nicobrowser.util.RssContent;
 import nicobrowser.util.RssContentParser;
@@ -27,7 +27,7 @@ public class Repository {
     private final ExecutorService executorService = Executors.newCachedThreadPool();
     private final Map<String, Thumbnail> thumbnailMap = new HashMap<>();
 
-    public void request(ProxyProfile proxy, String videoId) {
+    public void request(ProxyProfile proxy, File tempDir, String videoId) {
         synchronized (thumbnailMap) {
             final Thumbnail value = thumbnailMap.get(videoId);
             if (value != null) {
@@ -35,7 +35,7 @@ public class Repository {
             }
         }
 
-        executorService.execute(new ThumnailFetchWorker(proxy, videoId));
+        executorService.execute(new ThumnailFetchWorker(proxy, tempDir, videoId));
     }
 
     public Thumbnail getThumnail(String videoId) {
@@ -48,10 +48,12 @@ public class Repository {
 
         private final ProxyProfile proxy;
         private final String videoId;
+        private final File tempDir;
 
-        private ThumnailFetchWorker(ProxyProfile proxy, String videoId) {
+        private ThumnailFetchWorker(ProxyProfile proxy, File tempDir, String videoId) {
             this.proxy = proxy;
             this.videoId = videoId;
+            this.tempDir = tempDir;
         }
 
         @Override
@@ -66,12 +68,14 @@ public class Repository {
                 final RssContent cont = new RssContentParser().parse(is);
                 final URL thumbUrl = new URL(cont.getThumbnail_url());
 
-                ImageIcon icon;
+                final File imageFile;
                 try (InputStream imageStream = client.get(thumbUrl)) {
                     final BufferedImage image = ImageIO.read(imageStream);
-                    icon = new ImageIcon(image);
+                    imageFile = File.createTempFile("thumb", ".jpg", tempDir);
+                    imageFile.deleteOnExit();
+                    ImageIO.write(image, "jpeg", imageFile);
                 }
-                final Thumbnail thumb = new Thumbnail(cont.getTitle(), icon);
+                final Thumbnail thumb = new Thumbnail(cont.getTitle(), cont.getLength(), imageFile);
 
                 synchronized (thumbnailMap) {
                     thumbnailMap.put(videoId, thumb);
index 707bdfd..0d8572e 100644 (file)
@@ -1,5 +1,6 @@
 package yukihane.inqubus.thumbnail;
 
+import java.io.File;
 import javax.swing.ImageIcon;
 
 /**
@@ -9,18 +10,24 @@ import javax.swing.ImageIcon;
 public class Thumbnail {
 
     private final String title;
-    private final ImageIcon image;
+    private final String length;
+    private final File imageFile;
 
-    public Thumbnail(String title, ImageIcon image) {
+    Thumbnail(String title, String length, File imageFile) {
         this.title = title;
-        this.image = image;
+        this.length = length;
+        this.imageFile = imageFile;
     }
 
     public String getTitle() {
         return title;
     }
 
-    public ImageIcon getImage() {
-        return image;
+    public String getLength() {
+        return length;
+    }
+
+    public File getImageFile() {
+        return imageFile;
     }
 }