OSDN Git Service

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