OSDN Git Service

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