OSDN Git Service

過去ログの時刻入力欄のenable/disable判定を正しくする
[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         }
415     }
416     /** This method is called from within the constructor to
417      * initialize the form.
418      * WARNING: Do NOT modify this code. The content of this method is
419      * always regenerated by the Form Editor.
420      */
421     @SuppressWarnings("unchecked")
422     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
423     private void initComponents() {
424     }// </editor-fold>//GEN-END:initComponents
425
426     private File searchFileMatchId(final File dir, final String id) {
427         // TODO 候補は複数返すようにして、その後の対処は呼び出しもとで行ってもらった方が良いかも
428         if (id.isEmpty()) {
429             return null;
430         }
431
432         final File[] lists = dir.listFiles(new FilenameFilter() {
433
434             final Pattern pattern = Pattern.compile(id + "\\D");
435
436             @Override
437             public boolean accept(File dir, String name) {
438                 return pattern.matcher(name).find();
439             }
440         });
441
442         if (lists.length == 1) {
443             return lists[0];
444         } else if (lists.length > 1) {
445             throw new UnsupportedOperationException();
446         } else {
447             return null;
448         }
449     }
450
451     /**
452      * 動画, コメントの"local"チェックボックス更新時の処理.
453      */
454     private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
455         final Config p = Config.INSTANCE;
456
457         final ItemSelectable source = evt.getItemSelectable();
458
459         JButton btn;
460         JTextField field;
461         File dir;
462         if (source == cbVideoLocal) {
463             btn = btnVideo;
464             field = fldVideo;
465             dir = new File(p.getVideoDir());
466         } else {
467             btn = btnComment;
468             field = fldComment;
469             dir = new File(p.getCommentDir());
470         }
471
472         final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
473         btn.setEnabled(useLocal);
474
475         String text;
476         if (useLocal) {
477             final File f = searchFileMatchId(dir, fldId.getText());
478             if (f != null) {
479                 text = f.getPath();
480             } else {
481                 text = "";
482             }
483         } else {
484             text = p.getVideoFileNamePattern();
485         }
486         field.setText(text);
487
488     }//GEN-LAST:event_useMovieLocalCheckBoxItemStateChanged
489
490     private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
491         final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
492         fldOutput.setEnabled(convert);
493     }//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
494
495     private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
496         final Config p = Config.INSTANCE;
497         final String id = fldId.getText();
498         if (id.isEmpty()) {
499             return;
500         }
501
502         if (cbVideoLocal.isSelected() && fldVideo.getText().isEmpty()) {
503             final File dir = new File(p.getVideoDir());
504             final File file = searchFileMatchId(dir, id);
505             if (file != null) {
506                 fldVideo.setText(file.getPath());
507             }
508         }
509
510         if (cbCommentLocal.isSelected() && fldComment.getText().isEmpty()) {
511             final File dir = new File(p.getCommentDir());
512             final File file = searchFileMatchId(dir, id);
513             if (file != null) {
514                 fldComment.setText(file.getPath());
515             }
516         }
517
518     }//GEN-LAST:event_idFieldFocusLost
519     // Variables declaration - do not modify//GEN-BEGIN:variables
520     private final JTable tblDisplay;
521     // ボタン領域
522     private final JButton btnStart = new JButton("開始");
523     private final JButton btnStop = new JButton("停止");
524     private final JButton btnDeselect = new JButton("選択解除");
525     //入力領域 - 標準
526     private final JTextField fldId = new JTextField();
527     private final JCheckBox cbBackLogReduce = new JCheckBox("コメ数減少");
528     private final JCheckBox cbBackLog = new JCheckBox("過去ログ");
529     private final JTextField fldBackLog = new JTextField();
530     private final JCheckBox cbVideoLocal;
531     private final JTextField fldVideo;
532     private final JButton btnVideo;
533     private final JCheckBox cbCommentLocal;
534     private final JTextField fldComment;
535     private final JButton btnComment;
536     private final JCheckBox cbOutputEnable;
537     private final JTextField fldOutput;
538     // 適用
539     private final JButton btnApply = new JButton("適用");
540     // End of variables declaration//GEN-END:variables
541
542     private void initInputPanel() {
543         final Config p = Config.INSTANCE;
544
545         fldId.setText("");
546         fldBackLog.setEnabled(false);
547         cbBackLog.setEnabled(true);
548
549         final boolean movieLocal = p.getVideoUseLocal();
550         cbVideoLocal.setSelected(movieLocal);
551         btnVideo.setEnabled(movieLocal);
552         if (!movieLocal) {
553             fldVideo.setText(p.getVideoFileNamePattern());
554         }
555
556         final boolean commentLocal = p.getCommentUseLocal();
557         cbCommentLocal.setSelected(commentLocal);
558         btnComment.setEnabled(commentLocal);
559         if (!commentLocal) {
560             fldComment.setText(p.getCommentFileNamePattern());
561         }
562
563         final boolean convert = p.getOutputEnable();
564         cbOutputEnable.setSelected(convert);
565         fldOutput.setEnabled(convert);
566         fldOutput.setText(p.getOutputFileNamePattern());
567
568     }
569
570     private JMenuBar initMenuBar() {
571         final JMenuBar menuBar = new JMenuBar();
572
573         final JMenu mnFile = new JMenu("ファイル(F)");
574         menuBar.add(mnFile);
575
576         final JMenuItem itExit = new JMenuItem("終了(X)", KeyEvent.VK_X);
577         itExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
578         itExit.addActionListener(new ActionListener() {
579
580             @Override
581             public void actionPerformed(ActionEvent e) {
582                 throw new UnsupportedOperationException("Not supported yet.");
583             }
584         });
585         mnFile.add(itExit);
586
587         final JMenu mnTool = new JMenu("ツール(T)");
588         menuBar.add(mnTool);
589
590         final JMenuItem itOption = new JMenuItem("オプション(O)...", KeyEvent.VK_O);
591         // TODO ショートカットキー
592         itOption.addActionListener(new ActionListener() {
593
594             @Override
595             public void actionPerformed(ActionEvent e) {
596                 final yukihane.inqubus.gui.ConfigDialog dlg = new yukihane.inqubus.gui.ConfigDialog(MainFrame.this);
597                 dlg.setLocationRelativeTo(MainFrame.this);
598                 dlg.setModal(true);
599                 dlg.setVisible(true);
600             }
601         });
602         mnTool.add(itOption);
603
604         final JMenu mnHelp = new JMenu("ヘルプ(H)");
605         menuBar.add(mnHelp);
606
607         final JMenuItem itAbout = new JMenuItem("このソフトウェアについて(A)...", KeyEvent.VK_A);
608         itAbout.addActionListener(new ActionListener() {
609
610             @Override
611             public void actionPerformed(ActionEvent e) {
612                 MainFrame_AboutBox dlg = new MainFrame_AboutBox(MainFrame.this);
613                 dlg.pack();
614                 dlg.setLocationRelativeTo(MainFrame.this);
615                 dlg.setModal(true);
616                 dlg.setVisible(true);
617             }
618         });
619         mnHelp.add(itAbout);
620
621         return menuBar;
622     }
623
624     private class DownloadProgressListener implements WorkerListener<DownloadResult, DownloadProgress> {
625
626         @Override
627         public void process(DownloadProgress progress) {
628             throw new UnsupportedOperationException("Not supported yet.");
629         }
630
631         @Override
632         public void cancelled() {
633             throw new UnsupportedOperationException("Not supported yet.");
634         }
635
636         @Override
637         public void done(DownloadResult result) {
638             throw new UnsupportedOperationException("Not supported yet.");
639         }
640
641         @Override
642         public void error(Throwable th) {
643             throw new UnsupportedOperationException("Not supported yet.");
644         }
645     }
646
647     private class ConvertProgressListener implements WorkerListener<ConvertResult, ConvertProgress> {
648
649         @Override
650         public void process(ConvertProgress progress) {
651             throw new UnsupportedOperationException("Not supported yet.");
652         }
653
654         @Override
655         public void cancelled() {
656             throw new UnsupportedOperationException("Not supported yet.");
657         }
658
659         @Override
660         public void done(ConvertResult result) {
661             throw new UnsupportedOperationException("Not supported yet.");
662         }
663
664         @Override
665         public void error(Throwable th) {
666             throw new UnsupportedOperationException("Not supported yet.");
667         }
668     }
669
670
671
672     private class DownloadListTransferHandler extends TransferHandler {
673
674         private static final long serialVersionUID = 1L;
675         private final Pattern movieIdPattern = Pattern.compile("(\\w\\w\\d+)");
676
677         @Override
678         public boolean canImport(TransferHandler.TransferSupport support) {
679             Transferable transferable = support.getTransferable();
680             if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
681                     || transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
682                 return true;
683             }
684             return false;
685         }
686
687         @Override
688         public boolean importData(TransferHandler.TransferSupport support) {
689 //            try {
690 //                Transferable transferable = support.getTransferable();
691 //                if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
692 //                    @SuppressWarnings("unchecked")
693 //                    final List<File> data = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
694 //                    Collection<Target> targets = Target.from(data);
695 //                    targetModel.addTarget(targets);
696 //                } else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
697 //                    String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
698 //                    Matcher matcher = movieIdPattern.matcher(data);
699 //                    if (matcher.find()) {
700 //                        String movieId = matcher.group(1);
701 //                        Target target = Target.fromId(movieId);
702 //                        targetModel.addTarget(target);
703 //                    } else {
704 //                        return false;
705 //                    }
706 //
707 //                }
708 //                return false;
709 //            } catch (Exception e) {
710 //                logger.log(Level.SEVERE, null, e);
711 //                return false;
712 //            }
713             // TODO 上記実装見直し(Locationは削除された)
714             return false;
715         }
716     }
717
718     private class TableTransferHandler extends DownloadListTransferHandler {
719
720         private static final long serialVersionUID = 1L;
721
722         @Override
723         public boolean canImport(TransferHandler.TransferSupport support) {
724             return super.canImport(support);
725         }
726
727         @Override
728         public boolean importData(TransferHandler.TransferSupport support) {
729             return super.importData(support);
730         }
731     }
732
733     /*
734      * ここからDownloadProfile作成用クラスの定義
735      */
736
737     private class InqubusDownloadProfile implements DownloadProfile {
738
739         private final LoginProfile loginProfile;
740         private final ProxyProfile proxyProfile;
741         private final VideoProfile videoProfile;
742         private final CommentProfile commentProfile;
743         private final GeneralProfile generalProfile;
744
745         private InqubusDownloadProfile() {
746             this.loginProfile = new InqubusLoginProfile();
747             this.proxyProfile = new InqubusProxyProfile();
748             this.videoProfile = new InqubusVideoProfile();
749             this.commentProfile = new InqubusCommentProfile();
750             this.generalProfile = new InqubusGeneralProfile();
751         }
752
753         @Override
754         public LoginProfile getLoginInfo() {
755             return this.loginProfile;
756         }
757
758         @Override
759         public ProxyProfile getProxyProfile() {
760             return this.proxyProfile;
761         }
762
763         @Override
764         public VideoProfile getVideoProfile() {
765             return this.videoProfile;
766         }
767
768         @Override
769         public CommentProfile getCommentProfile() {
770             return this.commentProfile;
771         }
772
773         @Override
774         public GeneralProfile getGeneralProfile() {
775             return this.generalProfile;
776         }
777
778         @Override
779         public String toString(){
780             return ToStringBuilder.reflectionToString(this);
781         }
782     }
783
784     private class InqubusLoginProfile implements LoginProfile {
785         private final String mail;
786         private final String password;
787
788         private InqubusLoginProfile(){
789             final Config p = Config.INSTANCE;
790             this.mail = p.getId();
791             this.password = p.getPassword();
792         }
793
794         @Override
795         public String getMail() {
796             return this.mail;
797         }
798
799         @Override
800         public String getPassword() {
801             return this.password;
802         }
803
804         @Override
805         public String toString(){
806             return ToStringBuilder.reflectionToString(this);
807         }
808     }
809
810     private class InqubusProxyProfile implements ProxyProfile {
811         private final boolean use;
812         private final String host;
813         private final int port;
814
815         private InqubusProxyProfile(){
816             final Config p = Config.INSTANCE;
817             this.use = p.getProxyUse();
818             this.host = p.getProxyHost();
819             final String pp = p.getProxyPort();
820             this.port = StringUtils.isBlank(pp) ? -1 : Integer.parseInt(pp);
821         }
822
823         @Override
824         public boolean use() {
825             return this.use;
826         }
827
828         @Override
829         public String getHost() {
830             return this.host;
831         }
832
833         @Override
834         public int getPort() {
835             return this.port;
836         }
837
838         @Override
839         public String toString(){
840             return ToStringBuilder.reflectionToString(this);
841         }
842     }
843
844     private class InqubusVideoProfile implements VideoProfile {
845         private final boolean download;
846         private final File dir;
847         private final String fileName;
848         private final File localFile;
849
850         private InqubusVideoProfile(){
851             final Config p = Config.INSTANCE;
852             this.download = !cbVideoLocal.isSelected();
853             if (this.download) {
854                 this.dir = new File(p.getVideoDir());
855                 this.fileName = fldVideo.getText();
856                 this.localFile = null;
857             } else {
858                 this.dir = null;
859                 this.fileName = null;
860                 this.localFile = new File(fldVideo.getText());
861             }
862         }
863
864         @Override
865         public boolean isDownload() {
866             return this.download;
867         }
868
869         @Override
870         public File getDir() {
871             return this.dir;
872         }
873
874         @Override
875         public String getFileName() {
876             return this.fileName;
877         }
878
879         @Override
880         public File getLocalFile() {
881             return this.localFile;
882         }
883
884         @Override
885         public String toString(){
886             return ToStringBuilder.reflectionToString(this);
887         }
888     }
889
890     private class InqubusCommentProfile implements CommentProfile {
891         private final boolean download;
892         private final File dir;
893         private final String fileName;
894         private final File localFile;
895         private final int lengthRelatedCommentSize;
896         private final boolean disablePerMinComment;
897         private final int perMinCommentSize;
898         private final long backLogPoint;
899
900         private InqubusCommentProfile() {
901             final Config p = Config.INSTANCE;
902             this.download = !cbCommentLocal.isSelected();
903             if (this.download) {
904                 this.dir = new File(p.getCommentDir());
905                 this.fileName = fldComment.getText();
906                 this.localFile = null;
907             } else {
908                 this.dir = null;
909                 this.fileName = null;
910                 this.localFile = new File(fldComment.getText());
911             }
912
913             if(cbBackLog.isSelected()) {
914                 try {
915                     this.backLogPoint = WayBackTimeParser.parse(fldBackLog.getText());
916                 } catch (IOException ex) {
917                     throw new IllegalArgumentException("過去ログ時刻指定が誤っています。", ex);
918                 }
919             } else {
920                 this.backLogPoint = -1L;
921             }
922
923             this.disablePerMinComment = cbBackLogReduce.isSelected();
924             this.lengthRelatedCommentSize
925                     = (p.getCommentSizeAutosize()) ? -1 : Integer.parseInt(p.getCommentSizeManual());
926             this.perMinCommentSize
927                     = (p.getCommentMinSizeAutosize()) ? -1 : Integer.parseInt(p.getCommentMinSizeManual());
928         }
929
930         @Override
931         public boolean isDownload() {
932             return this.download;
933         }
934
935         @Override
936         public File getDir() {
937             return this.dir;
938         }
939
940         @Override
941         public String getFileName() {
942             return this.fileName;
943         }
944
945         @Override
946         public File getLocalFile() {
947             return this.localFile;
948         }
949
950         @Override
951         public int getLengthRelatedCommentSize() {
952             return this.lengthRelatedCommentSize;
953         }
954
955         @Override
956         public boolean isDisablePerMinComment() {
957             return this.disablePerMinComment;
958         }
959
960         @Override
961         public int getPerMinCommentSize() {
962             return this.perMinCommentSize;
963         }
964
965         @Override
966         public long getBackLogPoint() {
967             return this.backLogPoint;
968         }
969
970         @Override
971         public String toString(){
972             return ToStringBuilder.reflectionToString(this);
973         }
974     }
975
976     private class InqubusGeneralProfile implements GeneralProfile {
977         private final String replaceFrom;
978         private final String replaceTo;
979         private InqubusGeneralProfile() {
980             final Config p = Config.INSTANCE;
981             this.replaceFrom = p.getReplaceFrom();
982             this.replaceTo = p.getReplaceTo();
983         }
984
985         @Override
986         public String getReplaceFrom() {
987             return this.replaceFrom;
988         }
989
990         @Override
991         public String getReplaceTo() {
992             return this.replaceTo;
993         }
994
995         @Override
996         public String toString(){
997             return ToStringBuilder.reflectionToString(this);
998         }
999     }
1000
1001     /*
1002      * ここからConvertProfile作成用クラスの定義
1003      */
1004     private class InqubusConvertProfile implements ConvertProfile {
1005         private final OutputProfile outputProfile;
1006         private final GeneralProfile generalProfile;
1007         private final FfmpegProfile ffmpegProfile;
1008         private final boolean convert;
1009         private final File ffmpeg;
1010         private final boolean vhookDisabled;
1011         private final boolean commentOverlay;
1012         private final File vhook;
1013         private final File tmpDir;
1014         private final File font;
1015         private final int fontIndex;
1016         private final boolean commentOpaque;
1017         private final boolean disableFontSizeArrange;
1018         private final int shadowIndex;
1019         private final boolean showConvrting;
1020         private final int maxNumOfComment;
1021         private final HideCondition ngSetting;
1022
1023         private InqubusConvertProfile() {
1024             final Config p = Config.INSTANCE;
1025             this.outputProfile = new InqubusOutputProfile();
1026             this.generalProfile = new InqubusGeneralProfile();
1027             this.ffmpegProfile = new InqubusFfmpegProfile();
1028             this.convert = cbOutputEnable.isSelected();
1029             this.ffmpeg = new File(p.getFfmpegPath());
1030             // TODO コンフィグに設定なし
1031             this.vhookDisabled = false;
1032             this.commentOverlay = p.getOutputCommentOverlay();
1033             this.vhook = new File(p.getFfmpegDllPath());
1034             // TODO コンフィグに設定なし
1035             this.tmpDir = new File(".");
1036             this.font = new File(p.getFontPath());
1037             this.fontIndex = Integer.parseInt(p.getFontIndex());
1038             this.commentOpaque = p.getCommentOpaque();
1039             this.disableFontSizeArrange = p.getFontSizeArrangeDisable();
1040             this.shadowIndex = p.getFontShadow();
1041             // TODO コンフィグに設定なし
1042             this.showConvrting = true;
1043             this.maxNumOfComment = (p.getCommentDisplaySizeDefault()) ? -1 : Integer.parseInt(p.
1044                     getCommentDisplaySizeManual());
1045             this.ngSetting = new InqubusHideCondition();
1046         }
1047
1048         @Override
1049         public OutputProfile getOutputProfile() {
1050             return this.outputProfile;
1051         }
1052
1053         @Override
1054         public GeneralProfile getGeneralProfile() {
1055             return this.generalProfile;
1056         }
1057
1058         @Override
1059         public FfmpegProfile getFfmpegOption() {
1060             return this.ffmpegProfile;
1061         }
1062
1063         @Override
1064         public boolean isConvert() {
1065             return this.convert;
1066         }
1067
1068         @Override
1069         public File getFfmpeg() {
1070             return this.ffmpeg;
1071         }
1072
1073         @Override
1074         public boolean isVhookDisabled() {
1075             return this.vhookDisabled;
1076         }
1077
1078         @Override
1079         public boolean isCommentOverlay() {
1080             return this.commentOverlay;
1081         }
1082
1083         @Override
1084         public File getVhook() {
1085             return this.vhook;
1086         }
1087
1088         @Override
1089         public File getTempDir() {
1090             return this.tmpDir;
1091         }
1092
1093         @Override
1094         public File getFont() {
1095             return this.font;
1096         }
1097
1098         @Override
1099         public int getFontIndex() {
1100             return this.fontIndex;
1101         }
1102
1103         @Override
1104         public boolean isCommentOpaque() {
1105             return this.commentOpaque;
1106         }
1107
1108         @Override
1109         public boolean isDisableFontSizeArrange() {
1110             return this.disableFontSizeArrange;
1111         }
1112
1113         @Override
1114         public int getShadowIndex() {
1115             return this.shadowIndex;
1116         }
1117
1118         @Override
1119         public boolean isShowConverting() {
1120             return this.showConvrting;
1121         }
1122
1123         @Override
1124         public int getMaxNumOfComment() {
1125             return this.maxNumOfComment;
1126         }
1127
1128         @Override
1129         public HideCondition getNgSetting() {
1130             return this.ngSetting;
1131         }
1132
1133         @Override
1134         public String toString(){
1135             return ToStringBuilder.reflectionToString(this);
1136         }
1137     }
1138
1139     private class InqubusOutputProfile implements OutputProfile {
1140         private final File dir;
1141         private final String fileName;
1142         private final String videoId;
1143         private final String title;
1144
1145
1146         private InqubusOutputProfile(){
1147             final Config p = Config.INSTANCE;
1148             this.dir = new File(p.getOutputDir());
1149             this.fileName = fldOutput.getText();
1150             // TODO この時点でのID/Titleはどうするか…
1151             this.videoId = "";
1152             this.title = "";
1153         }
1154
1155         @Override
1156         public File getDir() {
1157             return this.dir;
1158         }
1159
1160         @Override
1161         public String getFileName() {
1162             return this.fileName;
1163         }
1164
1165         @Override
1166         public String getVideoId() {
1167             return this.videoId;
1168         }
1169
1170         @Override
1171         public String getTitile() {
1172             return this.title;
1173         }
1174
1175         @Override
1176         public String toString(){
1177             return ToStringBuilder.reflectionToString(this);
1178         }
1179     }
1180
1181     private class InqubusFfmpegProfile implements FfmpegProfile {
1182         private final String extOption;
1183         private final String inOption;
1184         private final String mainOption;
1185         private final String outOption;
1186         private final String avOption;
1187         private final boolean resize;
1188         private final int resizeWidth;
1189         private final int resizeHeight;
1190         private final boolean adjustRatio;
1191
1192         private InqubusFfmpegProfile() {
1193             // TODO FFMPEGオプションは、後でメイン画面でも設定できるようにするかも
1194             final Config p = Config.INSTANCE;
1195             this.extOption = p.getFfmpegExtension();
1196             this.inOption = p.getFfmpegInOption();
1197             this.mainOption = p.getFfmpegMainOption();
1198             this.outOption = p.getFfmpegOutOption();
1199             this.avOption = p.getFfmpegAvOption();
1200             this.resize = p.getFfmpegResizeEnable();
1201             this.resizeWidth = Integer.parseInt(p.getFfmpegResizeWidth());
1202             this.resizeHeight = Integer.parseInt(p.getFfmpegResizeHeight());
1203             this.adjustRatio = p.getFfmpegKeepAspect();
1204         }
1205
1206         @Override
1207         public String getExtOption() {
1208             return this.extOption;
1209         }
1210
1211         @Override
1212         public String getInOption() {
1213             return this.inOption;
1214         }
1215
1216         @Override
1217         public String getMainOption() {
1218             return this.mainOption;
1219         }
1220
1221         @Override
1222         public String getOutOption() {
1223             return this.outOption;
1224         }
1225
1226         @Override
1227         public String getAvfilterOption() {
1228             return this.avOption;
1229         }
1230
1231         @Override
1232         public boolean isResize() {
1233             return this.resize;
1234         }
1235
1236         @Override
1237         public int getResizeWidth() {
1238             return this.resizeWidth;
1239         }
1240
1241         @Override
1242         public int getResizeHeight() {
1243             return this.resizeHeight;
1244         }
1245
1246         @Override
1247         public boolean isAdjustRatio() {
1248             return this.adjustRatio;
1249         }
1250
1251         @Override
1252         public String toString(){
1253             return ToStringBuilder.reflectionToString(this);
1254         }
1255     }
1256
1257     private class InqubusHideCondition implements ConvertProfile.HideCondition{
1258
1259         @Override
1260         public String getWord() {
1261             // TODO
1262             return "";
1263         }
1264
1265         @Override
1266         public String getId() {
1267             // TODO
1268             return "";
1269         }
1270
1271         @Override
1272         public String toString(){
1273             return ToStringBuilder.reflectionToString(this);
1274         }
1275     }
1276 }