OSDN Git Service

タスクマネージャからのイベントを受け取りテーブルモデルを更新
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / MainFrame.java
1 /*
2  * MainFrame.java
3  *
4  * Created on 2011/05/28, 18:14:51
5  */
6 package yukihane.inqubus.gui;
7
8 import java.awt.Image;
9 import java.awt.ItemSelectable;
10 import java.awt.Toolkit;
11 import java.awt.datatransfer.DataFlavor;
12 import java.awt.datatransfer.Transferable;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.event.ItemEvent;
16 import java.awt.event.ItemListener;
17 import java.awt.event.KeyEvent;
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import javax.swing.BorderFactory;
30 import javax.swing.DropMode;
31 import javax.swing.GroupLayout;
32 import javax.swing.GroupLayout.Alignment;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBox;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuBar;
39 import javax.swing.JMenuItem;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTable;
43 import javax.swing.JTextField;
44 import javax.swing.KeyStroke;
45 import javax.swing.LayoutStyle.ComponentPlacement;
46 import javax.swing.SwingUtilities;
47 import javax.swing.TransferHandler;
48 import javax.swing.WindowConstants;
49 import org.apache.commons.lang.StringUtils;
50 import org.apache.commons.lang.builder.ToStringBuilder;
51 import saccubus.MainFrame_AboutBox;
52 import saccubus.util.WayBackTimeParser;
53 import saccubus.worker.convert.ConvertProgress;
54 import saccubus.worker.download.DownloadProgress;
55 import saccubus.worker.WorkerListener;
56 import saccubus.worker.convert.ConvertResult;
57 import saccubus.worker.download.DownloadResult;
58 import saccubus.worker.profile.CommentProfile;
59 import saccubus.worker.profile.ConvertProfile;
60 import saccubus.worker.profile.DownloadProfile;
61 import saccubus.worker.profile.FfmpegProfile;
62 import saccubus.worker.profile.GeneralProfile;
63 import saccubus.worker.profile.LoginProfile;
64 import saccubus.worker.profile.OutputProfile;
65 import saccubus.worker.profile.ProxyProfile;
66 import saccubus.worker.profile.VideoProfile;
67 import yukihane.Util;
68 import yukihane.inqubus.Config;
69 import yukihane.inqubus.manager.RequestProcess;
70 import yukihane.inqubus.manager.TaskKind;
71 import yukihane.inqubus.manager.TaskManage;
72 import yukihane.inqubus.manager.TaskManageListener;
73 import yukihane.inqubus.manager.TaskStatus;
74 import yukihane.inqubus.model.Target;
75 import yukihane.inqubus.model.TargetsTableModel;
76
77 /**
78  *
79  * @author yuki
80  */
81 public class MainFrame extends JFrame {
82
83     private static final long serialVersionUID = 1L;
84     private static final Logger logger = Logger.getLogger(MainFrame.class.getName());
85     private static final String ID_FIELD_TOOLTIP = "動画のIDまたはURLを入力します。";
86     private static final String FILE_LOCALBUTTON_TOOLTIP
87             = "ダウンロードする場合はチェックを外します。ローカルファイルを使用する場合はチェックを入れます。";
88     private static final String FILE_INPUTFIELD_TOOLTIP
89             = "ダウンロードする場合はファイル命名規則を入力します。"
90             + "ローカルファイルを使用する場合はパスを含むファイル名を入力します。";
91     private static final String FILE_OUTPUTFIELD_TOOLTIP
92             = "ファイル命名規則入力します。";
93     private final TargetsTableModel targetModel = new TargetsTableModel();
94     private final TaskManage taskManager;
95
96     /** Creates new form MainFrame */
97     public MainFrame() {
98         final URL url = MainFrame_AboutBox.class.getResource("icon.png");
99         final Image icon1 = Toolkit.getDefaultToolkit().createImage(url);
100         final URL url32 = MainFrame_AboutBox.class.getResource("icon32.png");
101         final Image icon2 = Toolkit.getDefaultToolkit().createImage(url32);
102         final List<Image> images = new ArrayList<Image>(2);
103         images.add(icon1);
104         images.add(icon2);
105         setIconImages(images);
106
107         final JPanel pnlMain = new JPanel();
108         final JScrollPane scrDisplay = new JScrollPane();
109         tblDisplay = new JTable();
110         final JPanel pnlButton = new JPanel();
111         btnStart = new JButton();
112         btnStop = new JButton();
113         btnDeselect = new JButton();
114         final JPanel pnlInputMain = new JPanel();
115         final JLabel lblId = new JLabel();
116         fldId = new JTextField();
117         fldId.setToolTipText(ID_FIELD_TOOLTIP);
118         final JLabel lblVideo = new JLabel();
119         cbVideoLocal = new JCheckBox();
120         cbVideoLocal.setToolTipText(FILE_LOCALBUTTON_TOOLTIP);
121         fldVideo = new JTextField();
122         fldVideo.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
123         btnVideo = new JButton();
124         final JLabel lblComment = new JLabel();
125         cbCommentLocal = new JCheckBox();
126         cbCommentLocal.setToolTipText(FILE_LOCALBUTTON_TOOLTIP);
127         cbCommentLocal.addItemListener(new ItemListener() {
128
129             @Override
130             public void itemStateChanged(ItemEvent e) {
131                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
132                 cbBackLogReduce.setEnabled(!selected);
133                 cbBackLog.setEnabled(!selected);
134                 if(selected) {
135                     cbBackLog.setSelected(false);
136                 }
137             }
138         });
139         fldComment = new JTextField();
140         fldComment.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
141         btnComment = new JButton();
142         final JLabel lblOutput = new JLabel();
143         cbOutputEnable = new JCheckBox();
144         fldOutput = new JTextField();
145         fldOutput.setToolTipText(FILE_OUTPUTFIELD_TOOLTIP);
146
147         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
148
149         btnApply.addActionListener(new ApplyActionListener());
150
151         pnlMain.setBorder(BorderFactory.createEtchedBorder());
152
153         tblDisplay.setModel(targetModel);
154         tblDisplay.setDropMode(DropMode.INSERT_ROWS);
155         scrDisplay.setViewportView(tblDisplay);
156
157         pnlButton.setBorder(BorderFactory.createEtchedBorder());
158
159         btnStart.setText("開始");
160
161         btnStop.setText("停止");
162
163         btnDeselect.setText("選択解除");
164
165         GroupLayout gl_pnlButton = new GroupLayout(pnlButton);
166         pnlButton.setLayout(gl_pnlButton);
167         gl_pnlButton.setHorizontalGroup(
168             gl_pnlButton.createParallelGroup(Alignment.LEADING)
169             .addGroup(gl_pnlButton.createSequentialGroup()
170                 .addContainerGap()
171                 .addComponent(btnStart)
172                 .addPreferredGap(ComponentPlacement.RELATED)
173                 .addComponent(btnStop)
174                 .addPreferredGap(ComponentPlacement.RELATED, 250, Short.MAX_VALUE)
175                 .addComponent(btnDeselect)
176                 .addContainerGap())
177         );
178         gl_pnlButton.setVerticalGroup(
179             gl_pnlButton.createParallelGroup(Alignment.LEADING)
180             .addGroup(gl_pnlButton.createSequentialGroup()
181                 .addContainerGap()
182                 .addGroup(gl_pnlButton.createParallelGroup(Alignment.BASELINE)
183                     .addComponent(btnStart)
184                     .addComponent(btnStop)
185                     .addComponent(btnDeselect))
186                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
187         );
188
189         pnlInputMain.setBorder(BorderFactory.createEtchedBorder());
190
191         lblId.setText("ID");
192
193         fldId.addFocusListener(new java.awt.event.FocusAdapter() {
194
195             public void focusLost(java.awt.event.FocusEvent evt) {
196                 idFieldFocusLost(evt);
197             }
198         });
199
200         lblVideo.setText("動画");
201
202         cbVideoLocal.setText("local");
203         cbVideoLocal.addItemListener(new java.awt.event.ItemListener() {
204
205             public void itemStateChanged(java.awt.event.ItemEvent evt) {
206                 useMovieLocalCheckBoxItemStateChanged(evt);
207             }
208         });
209
210         btnVideo.setText("...");
211
212         lblComment.setText("コメント");
213
214         cbCommentLocal.setText("local");
215         cbCommentLocal.addItemListener(new java.awt.event.ItemListener() {
216
217             public void itemStateChanged(java.awt.event.ItemEvent evt) {
218                 useMovieLocalCheckBoxItemStateChanged(evt);
219             }
220         });
221
222         btnComment.setText("...");
223
224         lblOutput.setText("出力");
225
226         cbOutputEnable.setText("変換");
227         cbOutputEnable.addItemListener(new java.awt.event.ItemListener() {
228
229             public void itemStateChanged(java.awt.event.ItemEvent evt) {
230                 outputConvertCheckBoxItemStateChanged(evt);
231             }
232         });
233
234
235         GroupLayout gl_pnlInputMain = new GroupLayout(pnlInputMain);
236         pnlInputMain.setLayout(gl_pnlInputMain);
237         gl_pnlInputMain.setHorizontalGroup(
238             gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
239             .addGroup(gl_pnlInputMain.createSequentialGroup()
240                 .addContainerGap()
241                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
242                     .addGroup(gl_pnlInputMain.createSequentialGroup()
243                         .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
244                             .addComponent(lblComment)
245                             .addComponent(lblVideo)
246                             .addComponent(lblId)
247                             .addComponent(lblOutput))
248                         .addPreferredGap(ComponentPlacement.RELATED)
249                         .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
250                             .addGroup(gl_pnlInputMain.createSequentialGroup()
251                                 .addComponent(cbVideoLocal)
252                                 .addPreferredGap(ComponentPlacement.RELATED)
253                                 .addComponent(fldVideo, GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
254                                 .addPreferredGap(ComponentPlacement.RELATED)
255                                 .addComponent(btnVideo))
256                             .addGroup(gl_pnlInputMain.createSequentialGroup()
257                                 .addComponent(fldId, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
258                                 .addPreferredGap(ComponentPlacement.UNRELATED)
259                                 .addComponent(cbBackLogReduce)
260                                 .addComponent(cbBackLog)
261                                 .addComponent(fldBackLog, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
262                             )
263                             .addGroup(Alignment.TRAILING, gl_pnlInputMain.createSequentialGroup()
264                                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.TRAILING)
265                                     .addGroup(Alignment.LEADING, gl_pnlInputMain.createSequentialGroup()
266                                         .addComponent(cbOutputEnable)
267                                         .addPreferredGap(ComponentPlacement.RELATED)
268                                         .addComponent(fldOutput, GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
269                                     .addGroup(gl_pnlInputMain.createSequentialGroup()
270                                         .addComponent(cbCommentLocal)
271                                         .addPreferredGap(ComponentPlacement.RELATED)
272                                         .addComponent(fldComment, GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)))
273                                 .addPreferredGap(ComponentPlacement.RELATED)
274                                 .addComponent(btnComment))))
275                     .addComponent(btnApply, Alignment.TRAILING))
276                 .addContainerGap())
277         );
278         gl_pnlInputMain.setVerticalGroup(
279             gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
280             .addGroup(gl_pnlInputMain.createSequentialGroup()
281                 .addContainerGap()
282                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
283                     .addComponent(fldId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
284                     .addComponent(lblId)
285                     .addComponent(cbBackLogReduce)
286                     .addComponent(cbBackLog)
287                     .addComponent(fldBackLog)
288                 )
289                 .addPreferredGap(ComponentPlacement.RELATED)
290                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
291                     .addComponent(lblVideo)
292                     .addComponent(fldVideo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
293                     .addComponent(btnVideo)
294                     .addComponent(cbVideoLocal))
295                 .addPreferredGap(ComponentPlacement.RELATED)
296                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
297                     .addComponent(lblComment)
298                     .addComponent(fldComment, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
299                     .addComponent(btnComment)
300                     .addComponent(cbCommentLocal))
301                 .addPreferredGap(ComponentPlacement.RELATED)
302                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
303                     .addComponent(lblOutput)
304                     .addComponent(fldOutput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
305                     .addComponent(cbOutputEnable))
306                 .addPreferredGap(ComponentPlacement.UNRELATED)
307                 .addComponent(btnApply)
308                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
309         );
310
311         GroupLayout gl_pnlMain = new GroupLayout(pnlMain);
312         pnlMain.setLayout(gl_pnlMain);
313         gl_pnlMain.setHorizontalGroup(
314             gl_pnlMain.createParallelGroup(Alignment.LEADING)
315             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
316                 .addContainerGap()
317                 .addGroup(gl_pnlMain.createParallelGroup(Alignment.TRAILING)
318                     .addComponent(pnlInputMain, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
319                     .addComponent(scrDisplay, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
320                     .addComponent(pnlButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
321                 .addContainerGap())
322         );
323         gl_pnlMain.setVerticalGroup(
324             gl_pnlMain.createParallelGroup(Alignment.LEADING)
325             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
326                 .addContainerGap()
327                 .addComponent(scrDisplay, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
328                 .addPreferredGap(ComponentPlacement.RELATED)
329                 .addComponent(pnlButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
330                 .addPreferredGap(ComponentPlacement.RELATED)
331                 .addComponent(pnlInputMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
332                 .addGap(24, 24, 24))
333         );
334
335
336         JMenuBar menuBar = initMenuBar();
337         setJMenuBar(menuBar);
338
339         GroupLayout layout = new GroupLayout(getContentPane());
340         getContentPane().setLayout(layout);
341         layout.setHorizontalGroup(
342             layout.createParallelGroup(Alignment.LEADING)
343             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
344         );
345         layout.setVerticalGroup(
346             layout.createParallelGroup(Alignment.LEADING)
347             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
348         );
349
350         pack();
351         initInputPanel();
352         pnlMain.setTransferHandler(new DownloadListTransferHandler());
353         tblDisplay.setTransferHandler(new TableTransferHandler());
354
355         final Config p = Config.INSTANCE;
356         // TODO コンフィグからスレッド数
357         taskManager = new TaskManage(1, 1, new GuiTaskManageListener());
358     }
359
360     private class GuiTaskManageListener implements TaskManageListener {
361
362         @Override
363         public void process(final int id, final TaskKind kind, final TaskStatus status, final double percentage,
364                 final String message) {
365             SwingUtilities.invokeLater(new Runnable() {
366
367                 @Override
368                 public void run() {
369                     targetModel.setStatus(id, kind, status, percentage, message);
370                 }
371             });
372         }
373     }
374
375     private class ApplyActionListener implements ActionListener {
376
377         @Override
378         public void actionPerformed(ActionEvent e) {
379             final DownloadProfile downProf = new InqubusDownloadProfile();
380             final String id = Util.getVideoId(fldId.getText());
381             final InqubusConvertProfile convProf = new InqubusConvertProfile();
382             logger.log(Level.INFO, downProf.toString());
383             logger.log(Level.INFO, convProf.toString());
384             final RequestProcess rp = new RequestProcess(downProf, id, convProf);
385             taskManager.add(rp);
386             targetModel.addTarget(new Target(rp));
387         }
388     }
389     /** This method is called from within the constructor to
390      * initialize the form.
391      * WARNING: Do NOT modify this code. The content of this method is
392      * always regenerated by the Form Editor.
393      */
394     @SuppressWarnings("unchecked")
395     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
396     private void initComponents() {
397     }// </editor-fold>//GEN-END:initComponents
398
399     private File searchFileMatchId(final File dir, final String id) {
400         // TODO 候補は複数返すようにして、その後の対処は呼び出しもとで行ってもらった方が良いかも
401         if (id.isEmpty()) {
402             return null;
403         }
404
405         final File[] lists = dir.listFiles(new FilenameFilter() {
406
407             final Pattern pattern = Pattern.compile(id + "\\D");
408
409             @Override
410             public boolean accept(File dir, String name) {
411                 return pattern.matcher(name).find();
412             }
413         });
414
415         if (lists.length == 1) {
416             return lists[0];
417         } else if (lists.length > 1) {
418             throw new UnsupportedOperationException();
419         } else {
420             return null;
421         }
422     }
423
424     private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
425         final Config p = Config.INSTANCE;
426
427         final ItemSelectable source = evt.getItemSelectable();
428
429         JButton btn;
430         JTextField field;
431         File dir;
432         if (source == cbVideoLocal) {
433             btn = btnVideo;
434             field = fldVideo;
435             dir = new File(p.getVideoDir());
436         } else {
437             btn = btnComment;
438             field = fldComment;
439             dir = new File(p.getCommentDir());
440         }
441
442         final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
443         btn.setEnabled(useLocal);
444
445         String text;
446         if (useLocal) {
447             final File f = searchFileMatchId(dir, fldId.getText());
448             if (f != null) {
449                 text = f.getPath();
450             } else {
451                 text = "";
452             }
453         } else {
454             text = p.getVideoFileNamePattern();
455         }
456         field.setText(text);
457
458         fldId.setEnabled(!(cbVideoLocal.isSelected() && cbCommentLocal.isSelected()));
459
460     }//GEN-LAST:event_useMovieLocalCheckBoxItemStateChanged
461
462     private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
463         final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
464         fldOutput.setEnabled(convert);
465     }//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
466
467     private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
468         final Config p = Config.INSTANCE;
469         final String id = fldId.getText();
470         if (id.isEmpty()) {
471             return;
472         }
473
474         if (cbVideoLocal.isSelected() && fldVideo.getText().isEmpty()) {
475             final File dir = new File(p.getVideoDir());
476             final File file = searchFileMatchId(dir, id);
477             if (file != null) {
478                 fldVideo.setText(file.getPath());
479             }
480         }
481
482         if (cbCommentLocal.isSelected() && fldComment.getText().isEmpty()) {
483             final File dir = new File(p.getCommentDir());
484             final File file = searchFileMatchId(dir, id);
485             if (file != null) {
486                 fldComment.setText(file.getPath());
487             }
488         }
489
490     }//GEN-LAST:event_idFieldFocusLost
491     // Variables declaration - do not modify//GEN-BEGIN:variables
492     private final JTable tblDisplay;
493     // ボタン領域
494     private final JButton btnStart;
495     private final JButton btnStop;
496     private final JButton btnDeselect;
497     //入力領域 - 標準
498     private final JTextField fldId;
499     private final JCheckBox cbBackLogReduce = new JCheckBox("コメ数減少");
500     private final JCheckBox cbBackLog = new JCheckBox("過去ログ");
501     private final JTextField fldBackLog = new JTextField();
502     private final JCheckBox cbVideoLocal;
503     private final JTextField fldVideo;
504     private final JButton btnVideo;
505     private final JCheckBox cbCommentLocal;
506     private final JTextField fldComment;
507     private final JButton btnComment;
508     private final JCheckBox cbOutputEnable;
509     private final JTextField fldOutput;
510     // 適用
511     private final JButton btnApply = new JButton("適用");
512     // End of variables declaration//GEN-END:variables
513
514     private void initInputPanel() {
515         final Config p = Config.INSTANCE;
516
517         fldId.setText("");
518         fldBackLog.setEnabled(false);
519         fldBackLog.setToolTipText("YYYY/MM/DD hh:mm:ss形式、あるいは1970/01/01からの経過秒を入力します。");
520         cbBackLog.setEnabled(true);
521         cbBackLog.addItemListener(new ItemListener() {
522
523             @Override
524             public void itemStateChanged(ItemEvent e) {
525                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
526                 fldBackLog.setEnabled(selected);
527             }
528         });
529         cbBackLogReduce.setToolTipText("「コメントの量を減らす」場合はチェックを付けます。");
530
531         final boolean movieLocal = p.getVideoUseLocal();
532         cbVideoLocal.setSelected(movieLocal);
533         btnVideo.setEnabled(movieLocal);
534         if (!movieLocal) {
535             fldVideo.setText(p.getVideoFileNamePattern());
536         }
537
538         final boolean commentLocal = p.getCommentUseLocal();
539         cbCommentLocal.setSelected(commentLocal);
540         btnComment.setEnabled(commentLocal);
541         if (!commentLocal) {
542             fldComment.setText(p.getCommentFileNamePattern());
543         }
544
545         final boolean convert = p.getOutputEnable();
546         cbOutputEnable.setSelected(convert);
547         fldOutput.setEnabled(convert);
548         fldOutput.setText(p.getOutputFileNamePattern());
549
550     }
551
552     private JMenuBar initMenuBar() {
553         final JMenuBar menuBar = new JMenuBar();
554
555         final JMenu mnFile = new JMenu("ファイル(F)");
556         menuBar.add(mnFile);
557
558         final JMenuItem itExit = new JMenuItem("終了(X)", KeyEvent.VK_X);
559         itExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
560         itExit.addActionListener(new ActionListener() {
561
562             @Override
563             public void actionPerformed(ActionEvent e) {
564                 throw new UnsupportedOperationException("Not supported yet.");
565             }
566         });
567         mnFile.add(itExit);
568
569         final JMenu mnTool = new JMenu("ツール(T)");
570         menuBar.add(mnTool);
571
572         final JMenuItem itOption = new JMenuItem("オプション(O)...", KeyEvent.VK_O);
573         // TODO ショートカットキー
574         itOption.addActionListener(new ActionListener() {
575
576             @Override
577             public void actionPerformed(ActionEvent e) {
578                 final yukihane.inqubus.gui.ConfigDialog dlg = new yukihane.inqubus.gui.ConfigDialog(MainFrame.this);
579                 dlg.setLocationRelativeTo(MainFrame.this);
580                 dlg.setModal(true);
581                 dlg.setVisible(true);
582             }
583         });
584         mnTool.add(itOption);
585
586         final JMenu mnHelp = new JMenu("ヘルプ(H)");
587         menuBar.add(mnHelp);
588
589         final JMenuItem itAbout = new JMenuItem("このソフトウェアについて(A)...", KeyEvent.VK_A);
590         itAbout.addActionListener(new ActionListener() {
591
592             @Override
593             public void actionPerformed(ActionEvent e) {
594                 MainFrame_AboutBox dlg = new MainFrame_AboutBox(MainFrame.this);
595                 dlg.pack();
596                 dlg.setLocationRelativeTo(MainFrame.this);
597                 dlg.setModal(true);
598                 dlg.setVisible(true);
599             }
600         });
601         mnHelp.add(itAbout);
602
603         return menuBar;
604     }
605
606     private class DownloadProgressListener implements WorkerListener<DownloadResult, DownloadProgress> {
607
608         @Override
609         public void process(DownloadProgress progress) {
610             throw new UnsupportedOperationException("Not supported yet.");
611         }
612
613         @Override
614         public void cancelled() {
615             throw new UnsupportedOperationException("Not supported yet.");
616         }
617
618         @Override
619         public void done(DownloadResult result) {
620             throw new UnsupportedOperationException("Not supported yet.");
621         }
622
623         @Override
624         public void error(Throwable th) {
625             throw new UnsupportedOperationException("Not supported yet.");
626         }
627     }
628
629     private class ConvertProgressListener implements WorkerListener<ConvertResult, ConvertProgress> {
630
631         @Override
632         public void process(ConvertProgress progress) {
633             throw new UnsupportedOperationException("Not supported yet.");
634         }
635
636         @Override
637         public void cancelled() {
638             throw new UnsupportedOperationException("Not supported yet.");
639         }
640
641         @Override
642         public void done(ConvertResult result) {
643             throw new UnsupportedOperationException("Not supported yet.");
644         }
645
646         @Override
647         public void error(Throwable th) {
648             throw new UnsupportedOperationException("Not supported yet.");
649         }
650     }
651
652
653
654     private class DownloadListTransferHandler extends TransferHandler {
655
656         private static final long serialVersionUID = 1L;
657         private final Pattern movieIdPattern = Pattern.compile("(\\w\\w\\d+)");
658
659         @Override
660         public boolean canImport(TransferHandler.TransferSupport support) {
661             Transferable transferable = support.getTransferable();
662             if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
663                     || transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
664                 return true;
665             }
666             return false;
667         }
668
669         @Override
670         public boolean importData(TransferHandler.TransferSupport support) {
671 //            try {
672 //                Transferable transferable = support.getTransferable();
673 //                if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
674 //                    @SuppressWarnings("unchecked")
675 //                    final List<File> data = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
676 //                    Collection<Target> targets = Target.from(data);
677 //                    targetModel.addTarget(targets);
678 //                } else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
679 //                    String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
680 //                    Matcher matcher = movieIdPattern.matcher(data);
681 //                    if (matcher.find()) {
682 //                        String movieId = matcher.group(1);
683 //                        Target target = Target.fromId(movieId);
684 //                        targetModel.addTarget(target);
685 //                    } else {
686 //                        return false;
687 //                    }
688 //
689 //                }
690 //                return false;
691 //            } catch (Exception e) {
692 //                logger.log(Level.SEVERE, null, e);
693 //                return false;
694 //            }
695             // TODO 上記実装見直し(Locationは削除された)
696             return false;
697         }
698     }
699
700     private class TableTransferHandler extends DownloadListTransferHandler {
701
702         private static final long serialVersionUID = 1L;
703
704         @Override
705         public boolean canImport(TransferHandler.TransferSupport support) {
706             return super.canImport(support);
707         }
708
709         @Override
710         public boolean importData(TransferHandler.TransferSupport support) {
711             return super.importData(support);
712         }
713     }
714
715     /*
716      * ここからDownloadProfile作成用クラスの定義
717      */
718
719     private class InqubusDownloadProfile implements DownloadProfile {
720
721         private final LoginProfile loginProfile;
722         private final ProxyProfile proxyProfile;
723         private final VideoProfile videoProfile;
724         private final CommentProfile commentProfile;
725         private final GeneralProfile generalProfile;
726
727         private InqubusDownloadProfile() {
728             this.loginProfile = new InqubusLoginProfile();
729             this.proxyProfile = new InqubusProxyProfile();
730             this.videoProfile = new InqubusVideoProfile();
731             this.commentProfile = new InqubusCommentProfile();
732             this.generalProfile = new InqubusGeneralProfile();
733         }
734
735         @Override
736         public LoginProfile getLoginInfo() {
737             return this.loginProfile;
738         }
739
740         @Override
741         public ProxyProfile getProxyProfile() {
742             return this.proxyProfile;
743         }
744
745         @Override
746         public VideoProfile getVideoProfile() {
747             return this.videoProfile;
748         }
749
750         @Override
751         public CommentProfile getCommentProfile() {
752             return this.commentProfile;
753         }
754
755         @Override
756         public GeneralProfile getGeneralProfile() {
757             return this.generalProfile;
758         }
759
760         @Override
761         public String toString(){
762             return ToStringBuilder.reflectionToString(this);
763         }
764     }
765
766     private class InqubusLoginProfile implements LoginProfile {
767         private final String mail;
768         private final String password;
769
770         private InqubusLoginProfile(){
771             final Config p = Config.INSTANCE;
772             this.mail = p.getId();
773             this.password = p.getPassword();
774         }
775
776         @Override
777         public String getMail() {
778             return this.mail;
779         }
780
781         @Override
782         public String getPassword() {
783             return this.password;
784         }
785
786         @Override
787         public String toString(){
788             return ToStringBuilder.reflectionToString(this);
789         }
790     }
791
792     private class InqubusProxyProfile implements ProxyProfile {
793         private final boolean use;
794         private final String host;
795         private final int port;
796
797         private InqubusProxyProfile(){
798             final Config p = Config.INSTANCE;
799             this.use = p.getProxyUse();
800             this.host = p.getProxyHost();
801             final String pp = p.getProxyPort();
802             this.port = StringUtils.isBlank(pp) ? -1 : Integer.parseInt(pp);
803         }
804
805         @Override
806         public boolean use() {
807             return this.use;
808         }
809
810         @Override
811         public String getHost() {
812             return this.host;
813         }
814
815         @Override
816         public int getPort() {
817             return this.port;
818         }
819
820         @Override
821         public String toString(){
822             return ToStringBuilder.reflectionToString(this);
823         }
824     }
825
826     private class InqubusVideoProfile implements VideoProfile {
827         private final boolean download;
828         private final File dir;
829         private final String fileName;
830         private final File localFile;
831
832         private InqubusVideoProfile(){
833             final Config p = Config.INSTANCE;
834             this.download = !cbVideoLocal.isSelected();
835             if (this.download) {
836                 this.dir = new File(p.getVideoDir());
837                 this.fileName = fldVideo.getText();
838                 this.localFile = null;
839             } else {
840                 this.dir = null;
841                 this.fileName = null;
842                 this.localFile = new File(fldVideo.getText());
843             }
844         }
845
846         @Override
847         public boolean isDownload() {
848             return this.download;
849         }
850
851         @Override
852         public File getDir() {
853             return this.dir;
854         }
855
856         @Override
857         public String getFileName() {
858             return this.fileName;
859         }
860
861         @Override
862         public File getLocalFile() {
863             return this.localFile;
864         }
865
866         @Override
867         public String toString(){
868             return ToStringBuilder.reflectionToString(this);
869         }
870     }
871
872     private class InqubusCommentProfile implements CommentProfile {
873         private final boolean download;
874         private final File dir;
875         private final String fileName;
876         private final File localFile;
877         private final int lengthRelatedCommentSize;
878         private final boolean disablePerMinComment;
879         private final int perMinCommentSize;
880         private final long backLogPoint;
881
882         private InqubusCommentProfile() {
883             final Config p = Config.INSTANCE;
884             this.download = !cbVideoLocal.isSelected();
885             if (this.download) {
886                 this.dir = new File(p.getVideoDir());
887                 this.fileName = fldVideo.getText();
888                 this.localFile = null;
889             } else {
890                 this.dir = null;
891                 this.fileName = null;
892                 this.localFile = new File(fldVideo.getText());
893             }
894
895             if(cbBackLog.isSelected()) {
896                 try {
897                     this.backLogPoint = WayBackTimeParser.parse(fldBackLog.getText());
898                 } catch (IOException ex) {
899                     throw new IllegalArgumentException("過去ログ時刻指定が誤っています。", ex);
900                 }
901             } else {
902                 this.backLogPoint = -1L;
903             }
904
905             this.disablePerMinComment = cbBackLogReduce.isSelected();
906             this.lengthRelatedCommentSize
907                     = (p.getCommentSizeAutosize()) ? -1 : Integer.parseInt(p.getCommentSizeManual());
908             this.perMinCommentSize
909                     = (p.getCommentMinSizeAutosize()) ? -1 : Integer.parseInt(p.getCommentMinSizeManual());
910         }
911
912         @Override
913         public boolean isDownload() {
914             return this.download;
915         }
916
917         @Override
918         public File getDir() {
919             return this.dir;
920         }
921
922         @Override
923         public String getFileName() {
924             return this.fileName;
925         }
926
927         @Override
928         public File getLocalFile() {
929             return this.localFile;
930         }
931
932         @Override
933         public int getLengthRelatedCommentSize() {
934             return this.lengthRelatedCommentSize;
935         }
936
937         @Override
938         public boolean isDisablePerMinComment() {
939             return this.disablePerMinComment;
940         }
941
942         @Override
943         public int getPerMinCommentSize() {
944             return this.perMinCommentSize;
945         }
946
947         @Override
948         public long getBackLogPoint() {
949             return this.backLogPoint;
950         }
951
952         @Override
953         public String toString(){
954             return ToStringBuilder.reflectionToString(this);
955         }
956     }
957
958     private class InqubusGeneralProfile implements GeneralProfile {
959         private final String replaceFrom;
960         private final String replaceTo;
961         private InqubusGeneralProfile() {
962             final Config p = Config.INSTANCE;
963             this.replaceFrom = p.getReplaceFrom();
964             this.replaceTo = p.getReplaceTo();
965         }
966
967         @Override
968         public String getReplaceFrom() {
969             return this.replaceFrom;
970         }
971
972         @Override
973         public String getReplaceTo() {
974             return this.replaceTo;
975         }
976
977         @Override
978         public String toString(){
979             return ToStringBuilder.reflectionToString(this);
980         }
981     }
982
983     /*
984      * ここからConvertProfile作成用クラスの定義
985      */
986     private class InqubusConvertProfile implements ConvertProfile {
987         private final OutputProfile outputProfile;
988         private final GeneralProfile generalProfile;
989         private final FfmpegProfile ffmpegProfile;
990         private final boolean convert;
991         private final File ffmpeg;
992         private final boolean vhookDisabled;
993         private final boolean commentOverlay;
994         private final File vhook;
995         private final File tmpDir;
996         private final File font;
997         private final int fontIndex;
998         private final boolean commentOpaque;
999         private final boolean disableFontSizeArrange;
1000         private final int shadowIndex;
1001         private final boolean showConvrting;
1002         private final int maxNumOfComment;
1003         private final HideCondition ngSetting;
1004
1005         private InqubusConvertProfile() {
1006             final Config p = Config.INSTANCE;
1007             this.outputProfile = new InqubusOutputProfile();
1008             this.generalProfile = new InqubusGeneralProfile();
1009             this.ffmpegProfile = new InqubusFfmpegProfile();
1010             this.convert = cbOutputEnable.isSelected();
1011             this.ffmpeg = new File(p.getFfmpegPath());
1012             // TODO コンフィグに設定なし
1013             this.vhookDisabled = false;
1014             this.commentOverlay = p.getOutputCommentOverlay();
1015             this.vhook = new File(p.getFfmpegDllPath());
1016             // TODO コンフィグに設定なし
1017             this.tmpDir = new File(".");
1018             this.font = new File(p.getFontPath());
1019             this.fontIndex = Integer.parseInt(p.getFontIndex());
1020             this.commentOpaque = p.getCommentOpaque();
1021             this.disableFontSizeArrange = p.getFontSizeArrangeDisable();
1022             this.shadowIndex = p.getFontShadow();
1023             // TODO コンフィグに設定なし
1024             this.showConvrting = true;
1025             this.maxNumOfComment = (p.getCommentDisplaySizeDefault()) ? -1 : Integer.parseInt(p.
1026                     getCommentDisplaySizeManual());
1027             this.ngSetting = new InqubusHideCondition();
1028         }
1029
1030         @Override
1031         public OutputProfile getOutputProfile() {
1032             return this.outputProfile;
1033         }
1034
1035         @Override
1036         public GeneralProfile getGeneralProfile() {
1037             return this.generalProfile;
1038         }
1039
1040         @Override
1041         public FfmpegProfile getFfmpegOption() {
1042             return this.ffmpegProfile;
1043         }
1044
1045         @Override
1046         public boolean isConvert() {
1047             return this.convert;
1048         }
1049
1050         @Override
1051         public File getFfmpeg() {
1052             return this.ffmpeg;
1053         }
1054
1055         @Override
1056         public boolean isVhookDisabled() {
1057             return this.vhookDisabled;
1058         }
1059
1060         @Override
1061         public boolean isCommentOverlay() {
1062             return this.commentOverlay;
1063         }
1064
1065         @Override
1066         public File getVhook() {
1067             return this.vhook;
1068         }
1069
1070         @Override
1071         public File getTempDir() {
1072             return this.tmpDir;
1073         }
1074
1075         @Override
1076         public File getFont() {
1077             return this.font;
1078         }
1079
1080         @Override
1081         public int getFontIndex() {
1082             return this.fontIndex;
1083         }
1084
1085         @Override
1086         public boolean isCommentOpaque() {
1087             return this.commentOpaque;
1088         }
1089
1090         @Override
1091         public boolean isDisableFontSizeArrange() {
1092             return this.disableFontSizeArrange;
1093         }
1094
1095         @Override
1096         public int getShadowIndex() {
1097             return this.shadowIndex;
1098         }
1099
1100         @Override
1101         public boolean isShowConverting() {
1102             return this.showConvrting;
1103         }
1104
1105         @Override
1106         public int getMaxNumOfComment() {
1107             return this.maxNumOfComment;
1108         }
1109
1110         @Override
1111         public HideCondition getNgSetting() {
1112             return this.ngSetting;
1113         }
1114
1115         @Override
1116         public String toString(){
1117             return ToStringBuilder.reflectionToString(this);
1118         }
1119     }
1120
1121     private class InqubusOutputProfile implements OutputProfile {
1122         private final File dir;
1123         private final String fileName;
1124         private final String videoId;
1125         private final String title;
1126
1127
1128         private InqubusOutputProfile(){
1129             final Config p = Config.INSTANCE;
1130             this.dir = new File(p.getOutputDir());
1131             this.fileName = fldOutput.getText();
1132             // TODO この時点でのID/Titleはどうするか…
1133             this.videoId = "";
1134             this.title = "";
1135         }
1136
1137         @Override
1138         public File getDir() {
1139             return this.dir;
1140         }
1141
1142         @Override
1143         public String getFileName() {
1144             return this.fileName;
1145         }
1146
1147         @Override
1148         public String getVideoId() {
1149             return this.videoId;
1150         }
1151
1152         @Override
1153         public String getTitile() {
1154             return this.title;
1155         }
1156
1157         @Override
1158         public String toString(){
1159             return ToStringBuilder.reflectionToString(this);
1160         }
1161     }
1162
1163     private class InqubusFfmpegProfile implements FfmpegProfile {
1164         private final String extOption;
1165         private final String inOption;
1166         private final String mainOption;
1167         private final String outOption;
1168         private final String avOption;
1169         private final boolean resize;
1170         private final int resizeWidth;
1171         private final int resizeHeight;
1172         private final boolean adjustRatio;
1173
1174         private InqubusFfmpegProfile() {
1175             // TODO FFMPEGオプションは、後でメイン画面でも設定できるようにするかも
1176             final Config p = Config.INSTANCE;
1177             this.extOption = p.getFfmpegExtension();
1178             this.inOption = p.getFfmpegInOption();
1179             this.mainOption = p.getFfmpegMainOption();
1180             this.outOption = p.getFfmpegOutOption();
1181             this.avOption = p.getFfmpegAvOption();
1182             this.resize = p.getFfmpegResizeEnable();
1183             this.resizeWidth = Integer.parseInt(p.getFfmpegResizeWidth());
1184             this.resizeHeight = Integer.parseInt(p.getFfmpegResizeHeight());
1185             this.adjustRatio = p.getFfmpegKeepAspect();
1186         }
1187
1188         @Override
1189         public String getExtOption() {
1190             return this.extOption;
1191         }
1192
1193         @Override
1194         public String getInOption() {
1195             return this.inOption;
1196         }
1197
1198         @Override
1199         public String getMainOption() {
1200             return this.mainOption;
1201         }
1202
1203         @Override
1204         public String getOutOption() {
1205             return this.outOption;
1206         }
1207
1208         @Override
1209         public String getAvfilterOption() {
1210             return this.avOption;
1211         }
1212
1213         @Override
1214         public boolean isResize() {
1215             return this.resize;
1216         }
1217
1218         @Override
1219         public int getResizeWidth() {
1220             return this.resizeWidth;
1221         }
1222
1223         @Override
1224         public int getResizeHeight() {
1225             return this.resizeHeight;
1226         }
1227
1228         @Override
1229         public boolean isAdjustRatio() {
1230             return this.adjustRatio;
1231         }
1232
1233         @Override
1234         public String toString(){
1235             return ToStringBuilder.reflectionToString(this);
1236         }
1237     }
1238
1239     private class InqubusHideCondition implements ConvertProfile.HideCondition{
1240
1241         @Override
1242         public String getWord() {
1243             // TODO
1244             return "";
1245         }
1246
1247         @Override
1248         public String getId() {
1249             // TODO
1250             return "";
1251         }
1252
1253         @Override
1254         public String toString(){
1255             return ToStringBuilder.reflectionToString(this);
1256         }
1257     }
1258 }