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