OSDN Git Service

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