OSDN Git Service

完了、キャンセル、エラーを通知するインタフェース追加
authoryukihane <yukihane.feather@gmail.com>
Sat, 27 Aug 2011 13:28:45 +0000 (22:28 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sat, 27 Aug 2011 13:28:45 +0000 (22:28 +0900)
12 files changed:
frontend/src/saccubus/worker/Worker.java
frontend/src/saccubus/worker/WorkerListener.java
frontend/src/saccubus/worker/WorkerStatus.java [deleted file]
frontend/src/saccubus/worker/convert/Convert.java
frontend/src/saccubus/worker/convert/ConvertProgress.java
frontend/src/saccubus/worker/convert/ConvertStatus.java [new file with mode: 0644]
frontend/src/saccubus/worker/download/Download.java
frontend/src/saccubus/worker/download/DownloadProgress.java
frontend/src/saccubus/worker/download/DownloadStatus.java [new file with mode: 0644]
frontend/src/yukihane/inqubus/gui/MainFrame.java
frontend/src/yukihane/inqubus/manager/Request.java
frontend/src/yukihane/inqubus/manager/TaskManage.java

index 463814a..928e3c0 100644 (file)
@@ -11,9 +11,9 @@ public abstract class Worker<T, V> implements Callable<T> {
 
     private static int serialNumber = 0;
     private final int id;
-    private final WorkerListener<V> listener;
+    private final WorkerListener<T, V> listener;
 
-    public Worker(WorkerListener<V> listener) {
+    public Worker(WorkerListener<T, V> listener) {
         this.id = ++serialNumber;
         this.listener = listener;
     }
@@ -22,12 +22,19 @@ public abstract class Worker<T, V> implements Callable<T> {
     public final T call() throws Exception {
         try {
             final T result = doInBackground();
-            // TODO
-            publish(null);
+            if (listener != null) {
+                listener.done(result);
+            }
             return result;
+        } catch (InterruptedException ex) {
+            if( listener != null){
+                listener.cancelled();
+            }
+            throw ex;
         } catch (Throwable th) {
-            // TODO
-            publish(null);
+            if(listener != null){
+                listener.error(th);
+            }
             throw th;
         }
     }
index 36f7e1b..2749d6d 100644 (file)
@@ -4,7 +4,13 @@ package saccubus.worker;
  *
  * @author yuki
  */
-public interface WorkerListener<V> {
+public interface WorkerListener<T, V> {
 
     void process(V progress);
+
+    void cancelled();
+
+    void done(T result);
+
+    void error(Throwable th);
 }
diff --git a/frontend/src/saccubus/worker/WorkerStatus.java b/frontend/src/saccubus/worker/WorkerStatus.java
deleted file mode 100644 (file)
index 8f0b30b..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-package saccubus.worker;
-
-/**
- *
- * @author yuki
- */
-public enum WorkerStatus {
-
-    READY, PROGRESS, DONE
-}
index c1f962d..01f4a58 100644 (file)
@@ -3,6 +3,7 @@ package saccubus.worker.convert;
 
 import static org.apache.commons.io.FilenameUtils.getBaseName;
 import static org.apache.commons.lang.StringUtils.*;
+import static saccubus.worker.convert.ConvertStatus.*;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -51,7 +52,7 @@ public class Convert extends Worker<ConvertResult, ConvertProgress> {
      * @param output 変換後出力動画.
      * @throws IOException 変換失敗.
      */
-    public Convert(ConvertProfile profile, File video, File comment, WorkerListener<ConvertProgress> listener) {
+    public Convert(ConvertProfile profile, File video, File comment, WorkerListener<ConvertResult, ConvertProgress> listener) {
         super(listener);
         this.profile = profile;
         this.videoFile = video;
@@ -83,18 +84,18 @@ public class Convert extends Worker<ConvertResult, ConvertProgress> {
             if (profile.isCommentOverlay()) {
                 transformedComment = File.createTempFile("vhk", ".tmp", profile.getTempDir());
                 final HideCondition hide = profile.getNgSetting();
-                publish(new ConvertProgress("コメントの中間ファイルへの変換中"));
+                publish(new ConvertProgress(PROCESS, "コメントの中間ファイルへの変換中"));
                 ConvertToVideoHook.convert(commentFile, transformedComment, hide.getId(), hide.getWord());
             }
 
             checkStop();
-            publish(new ConvertProgress("動画の変換を開始"));
+            publish(new ConvertProgress(PROCESS, "動画の変換を開始"));
 
             final int code = convert(transformedComment, outputFile);
             if (code != 0) {
                 throw new IOException("ffmpeg実行失敗: " + outputFile.getPath());
             }
-            publish(new ConvertProgress("変換が正常に終了しました。"));
+            publish(new ConvertProgress(PROCESS, "変換が正常に終了しました。"));
             return new ConvertResult(true, outputFile.getName());
         } finally {
             if (transformedComment != null && transformedComment.exists()) {
@@ -190,7 +191,7 @@ public class Convert extends Worker<ConvertResult, ConvertProgress> {
             String msg;
             while ((msg = ebr.readLine()) != null) {
                 if (msg.startsWith("frame=")) {
-                    publish(new ConvertProgress(msg));
+                    publish(new ConvertProgress(PROCESS, msg));
                 } else if (!msg.endsWith("No accelerated colorspace conversion found")) {
                     logger.log(Level.INFO, msg);
                 }
index b3ecdeb..7e697ba 100644 (file)
@@ -6,12 +6,18 @@ package saccubus.worker.convert;
  */
 public class ConvertProgress {
 
+    private final ConvertStatus status;
     private final String message;
 
-    ConvertProgress(String message) {
+    ConvertProgress(ConvertStatus status, String message) {
+        this.status = status;
         this.message = message;
     }
 
+    public ConvertStatus getStatus() {
+        return status;
+    }
+
     public String getMessage() {
         return message;
     }
diff --git a/frontend/src/saccubus/worker/convert/ConvertStatus.java b/frontend/src/saccubus/worker/convert/ConvertStatus.java
new file mode 100644 (file)
index 0000000..5f87899
--- /dev/null
@@ -0,0 +1,10 @@
+package saccubus.worker.convert;
+
+/**
+ *
+ * @author yuki
+ */
+public enum ConvertStatus {
+
+    READY, PROCESS, DONE, CANCELLED, ERROR;
+}
index 3f332b1..5c91d73 100644 (file)
@@ -1,5 +1,7 @@
 package saccubus.worker.download;
 
+import static saccubus.worker.download.DownloadStatus.*;
+
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
@@ -48,7 +50,7 @@ public class Download extends Worker<DownloadResult, DownloadProgress> {
      * @param listener
      * @param flag
      */
-    public Download(DownloadProfile profile, String videoId, WorkerListener<DownloadProgress> listener) {
+    public Download(DownloadProfile profile, String videoId, WorkerListener<DownloadResult, DownloadProgress> listener) {
         // TODO listener登録
         super(listener);
         this.videoId = videoId;
@@ -78,7 +80,7 @@ public class Download extends Worker<DownloadResult, DownloadProgress> {
     @Override
     public DownloadResult doInBackground() throws Exception {
 
-        publish(new DownloadProgress("ログイン中"));
+        publish(new DownloadProgress(PROCESS, "ログイン中"));
 
         NicoHttpClient client = null;
         nicobrowser.VideoInfo vi = null;
@@ -130,7 +132,7 @@ public class Download extends Worker<DownloadResult, DownloadProgress> {
                 @Override
                 public void progress(long fileSize, long downloadSize) {
                     final double vol = (double) downloadSize / (double) fileSize * 100.0;
-                    publish(new DownloadProgress(String.format("ダウンロード%.2f%%", vol)));
+                    publish(new DownloadProgress(PROCESS, String.format("ダウンロード%.2f%%", vol)));
                 }
             });
 
index 2fbb7a3..96a64db 100644 (file)
@@ -6,12 +6,18 @@ package saccubus.worker.download;
  */
 public class DownloadProgress {
 
+    private final DownloadStatus status;
     private final String message;
 
-    DownloadProgress(String message) {
+    DownloadProgress(DownloadStatus status, String message) {
+        this.status = status;
         this.message = message;
     }
 
+    public DownloadStatus getStatus() {
+        return status;
+    }
+
     public String getMessage() {
         return message;
     }
diff --git a/frontend/src/saccubus/worker/download/DownloadStatus.java b/frontend/src/saccubus/worker/download/DownloadStatus.java
new file mode 100644 (file)
index 0000000..66580ff
--- /dev/null
@@ -0,0 +1,10 @@
+package saccubus.worker.download;
+
+/**
+ *
+ * @author yuki
+ */
+public enum DownloadStatus {
+
+    READY, PROCESS, DONE, CANCELLED, ERROR;
+}
index bedeb76..8bc13a0 100644 (file)
@@ -52,6 +52,8 @@ import saccubus.util.WayBackTimeParser;
 import saccubus.worker.convert.ConvertProgress;
 import saccubus.worker.download.DownloadProgress;
 import saccubus.worker.WorkerListener;
+import saccubus.worker.convert.ConvertResult;
+import saccubus.worker.download.DownloadResult;
 import saccubus.worker.profile.CommentProfile;
 import saccubus.worker.profile.DownloadProfile;
 import saccubus.worker.profile.GeneralProfile;
@@ -578,20 +580,50 @@ public class MainFrame extends JFrame {
         return menuBar;
     }
 
-    private class DownloadProgressListener implements WorkerListener<DownloadProgress> {
+    private class DownloadProgressListener implements WorkerListener<DownloadResult, DownloadProgress> {
 
         @Override
         public void process(DownloadProgress progress) {
             throw new UnsupportedOperationException("Not supported yet.");
         }
+
+        @Override
+        public void cancelled() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void done(DownloadResult result) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void error(Throwable th) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
     }
 
-    private class ConvertProgressListener implements WorkerListener<ConvertProgress> {
+    private class ConvertProgressListener implements WorkerListener<ConvertResult, ConvertProgress> {
 
         @Override
         public void process(ConvertProgress progress) {
             throw new UnsupportedOperationException("Not supported yet.");
         }
+
+        @Override
+        public void cancelled() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void done(ConvertResult result) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void error(Throwable th) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
     }
 
 
index 45741c2..8a886af 100644 (file)
@@ -1,8 +1,5 @@
 package yukihane.inqubus.manager;
 
-import saccubus.worker.convert.ConvertProgress;
-import saccubus.worker.download.DownloadProgress;
-import saccubus.worker.WorkerListener;
 import saccubus.worker.profile.ConvertProfile;
 import saccubus.worker.profile.DownloadProfile;
 
@@ -16,18 +13,13 @@ public class Request {
     private final int rowId;
     private final DownloadProfile downloadProfile;
     private final String videoId;
-    private final WorkerListener<DownloadProgress> downloadProgressListener;
     private final ConvertProfile convertProfile;
-    private final WorkerListener<ConvertProgress> convertProgressListener;
 
-    public Request(DownloadProfile download, String videoId, WorkerListener<DownloadProgress> downListener,
-            ConvertProfile convert, WorkerListener<ConvertProgress> convListener) {
+    public Request(DownloadProfile download, String videoId, ConvertProfile convert) {
         this.rowId = ++serialId;
         this.downloadProfile = download;
         this.videoId = videoId;
-        this.downloadProgressListener = downListener;
         this.convertProfile = convert;
-        this.convertProgressListener = convListener;
     }
 
     public ConvertProfile getConvertProfile() {
@@ -45,12 +37,4 @@ public class Request {
     Integer getRowId() {
         return rowId;
     }
-
-    WorkerListener<DownloadProgress> getDownloadProgressListener() {
-        return downloadProgressListener;
-    }
-
-    WorkerListener<ConvertProgress> getConvertProgressListener() {
-        return convertProgressListener;
-    }
 }
index 70edbab..12865eb 100644 (file)
@@ -61,7 +61,7 @@ public class TaskManage {
         return false;
     }
 
-    private class DownloadListener implements WorkerListener<DownloadProgress> {
+    private class DownloadListener implements WorkerListener<DownloadResult, DownloadProgress> {
 
         private final int rowId;
 
@@ -76,9 +76,24 @@ public class TaskManage {
                 clientListner.process(rowId, TaskKind.DOWNLOAD, TaskStatus.DOING, 0.0, "");
             }
         }
+
+        @Override
+        public void cancelled() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void done(DownloadResult result) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void error(Throwable th) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
     }
 
-    private class ConvertListener implements WorkerListener<ConvertProgress> {
+    private class ConvertListener implements WorkerListener<ConvertResult, ConvertProgress> {
 
         private final int id;
 
@@ -93,6 +108,21 @@ public class TaskManage {
                 clientListner.process(id, TaskKind.CONVERT, TaskStatus.DOING, 0.0, "");
             }
         }
+
+        @Override
+        public void cancelled() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void done(ConvertResult result) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void error(Throwable th) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
     }
 }