OSDN Git Service

bedeb76ea41f5ec8c1841ce7126d64a0345f74b3
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / MainFrame.java
1 /*
2  * MainFrame.java
3  *
4  * Created on 2011/05/28, 18:14:51
5  */
6 package yukihane.inqubus.gui;
7
8 import java.awt.Image;
9 import java.awt.ItemSelectable;
10 import java.awt.Toolkit;
11 import java.awt.datatransfer.DataFlavor;
12 import java.awt.datatransfer.Transferable;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.event.ItemEvent;
16 import java.awt.event.ItemListener;
17 import java.awt.event.KeyEvent;
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import javax.swing.BorderFactory;
30 import javax.swing.DropMode;
31 import javax.swing.GroupLayout;
32 import javax.swing.GroupLayout.Alignment;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBox;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuBar;
39 import javax.swing.JMenuItem;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTable;
43 import javax.swing.JTextField;
44 import javax.swing.KeyStroke;
45 import javax.swing.LayoutStyle.ComponentPlacement;
46 import javax.swing.TransferHandler;
47 import javax.swing.WindowConstants;
48 import org.apache.commons.lang.StringUtils;
49 import org.apache.commons.lang.builder.ToStringBuilder;
50 import saccubus.MainFrame_AboutBox;
51 import saccubus.util.WayBackTimeParser;
52 import saccubus.worker.convert.ConvertProgress;
53 import saccubus.worker.download.DownloadProgress;
54 import saccubus.worker.WorkerListener;
55 import saccubus.worker.profile.CommentProfile;
56 import saccubus.worker.profile.DownloadProfile;
57 import saccubus.worker.profile.GeneralProfile;
58 import saccubus.worker.profile.LoginProfile;
59 import saccubus.worker.profile.ProxyProfile;
60 import saccubus.worker.profile.VideoProfile;
61 import yukihane.Util;
62 import yukihane.inqubus.Config;
63 import yukihane.inqubus.manager.TaskManage;
64 import yukihane.inqubus.model.Target;
65 import yukihane.inqubus.model.TargetsTableModel;
66
67 /**
68  *
69  * @author yuki
70  */
71 public class MainFrame extends JFrame {
72
73     private static final long serialVersionUID = 1L;
74     private static final Logger logger = Logger.getLogger(MainFrame.class.getName());
75     private static final String ID_FIELD_TOOLTIP = "動画のIDまたはURLを入力します。";
76     private static final String FILE_LOCALBUTTON_TOOLTIP
77             = "ダウンロードする場合はチェックを外します。ローカルファイルを使用する場合はチェックを入れます。";
78     private static final String FILE_INPUTFIELD_TOOLTIP
79             = "ダウンロードする場合はファイル命名規則を入力します。"
80             + "ローカルファイルを使用する場合はパスを含むファイル名を入力します。";
81     private static final String FILE_OUTPUTFIELD_TOOLTIP
82             = "ファイル命名規則入力します。";
83     private final TargetsTableModel targetModel = new TargetsTableModel();
84     private final TaskManage taskManager;
85     private final DownloadProgressListener downloadProgressListener = new DownloadProgressListener();
86     private final ConvertProgressListener convertProgressListener = new ConvertProgressListener();
87
88     /** Creates new form MainFrame */
89     public MainFrame() {
90         final URL url = MainFrame_AboutBox.class.getResource("icon.png");
91         final Image icon1 = Toolkit.getDefaultToolkit().createImage(url);
92         final URL url32 = MainFrame_AboutBox.class.getResource("icon32.png");
93         final Image icon2 = Toolkit.getDefaultToolkit().createImage(url32);
94         final List<Image> images = new ArrayList<Image>(2);
95         images.add(icon1);
96         images.add(icon2);
97         setIconImages(images);
98
99         final JPanel pnlMain = new JPanel();
100         final JScrollPane scrDisplay = new JScrollPane();
101         tblDisplay = new JTable();
102         final JPanel pnlButton = new JPanel();
103         btnStart = new JButton();
104         btnStop = new JButton();
105         btnDeselect = new JButton();
106         final JPanel pnlInputMain = new JPanel();
107         final JLabel lblId = new JLabel();
108         fldId = new JTextField();
109         fldId.setToolTipText(ID_FIELD_TOOLTIP);
110         final JLabel lblVideo = new JLabel();
111         cbVideoLocal = new JCheckBox();
112         cbVideoLocal.setToolTipText(FILE_LOCALBUTTON_TOOLTIP);
113         fldVideo = new JTextField();
114         fldVideo.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
115         btnVideo = new JButton();
116         final JLabel lblComment = new JLabel();
117         cbCommentLocal = new JCheckBox();
118         cbCommentLocal.setToolTipText(FILE_LOCALBUTTON_TOOLTIP);
119         cbCommentLocal.addItemListener(new ItemListener() {
120
121             @Override
122             public void itemStateChanged(ItemEvent e) {
123                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
124                 cbBackLogReduce.setEnabled(!selected);
125                 cbBackLog.setEnabled(!selected);
126                 if(selected) {
127                     cbBackLog.setSelected(false);
128                 }
129             }
130         });
131         fldComment = new JTextField();
132         fldComment.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
133         btnComment = new JButton();
134         final JLabel lblOutput = new JLabel();
135         cbOutputEnable = new JCheckBox();
136         fldOutput = new JTextField();
137         fldOutput.setToolTipText(FILE_OUTPUTFIELD_TOOLTIP);
138
139         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
140
141         btnApply.addActionListener(new ApplyActionListener());
142
143         pnlMain.setBorder(BorderFactory.createEtchedBorder());
144
145         tblDisplay.setModel(targetModel);
146         tblDisplay.setDropMode(DropMode.INSERT_ROWS);
147         scrDisplay.setViewportView(tblDisplay);
148
149         pnlButton.setBorder(BorderFactory.createEtchedBorder());
150
151         btnStart.setText("開始");
152
153         btnStop.setText("停止");
154
155         btnDeselect.setText("選択解除");
156
157         GroupLayout gl_pnlButton = new GroupLayout(pnlButton);
158         pnlButton.setLayout(gl_pnlButton);
159         gl_pnlButton.setHorizontalGroup(
160             gl_pnlButton.createParallelGroup(Alignment.LEADING)
161             .addGroup(gl_pnlButton.createSequentialGroup()
162                 .addContainerGap()
163                 .addComponent(btnStart)
164                 .addPreferredGap(ComponentPlacement.RELATED)
165                 .addComponent(btnStop)
166                 .addPreferredGap(ComponentPlacement.RELATED, 250, Short.MAX_VALUE)
167                 .addComponent(btnDeselect)
168                 .addContainerGap())
169         );
170         gl_pnlButton.setVerticalGroup(
171             gl_pnlButton.createParallelGroup(Alignment.LEADING)
172             .addGroup(gl_pnlButton.createSequentialGroup()
173                 .addContainerGap()
174                 .addGroup(gl_pnlButton.createParallelGroup(Alignment.BASELINE)
175                     .addComponent(btnStart)
176                     .addComponent(btnStop)
177                     .addComponent(btnDeselect))
178                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
179         );
180
181         pnlInputMain.setBorder(BorderFactory.createEtchedBorder());
182
183         lblId.setText("ID");
184
185         fldId.addFocusListener(new java.awt.event.FocusAdapter() {
186
187             public void focusLost(java.awt.event.FocusEvent evt) {
188                 idFieldFocusLost(evt);
189             }
190         });
191
192         lblVideo.setText("動画");
193
194         cbVideoLocal.setText("local");
195         cbVideoLocal.addItemListener(new java.awt.event.ItemListener() {
196
197             public void itemStateChanged(java.awt.event.ItemEvent evt) {
198                 useMovieLocalCheckBoxItemStateChanged(evt);
199             }
200         });
201
202         btnVideo.setText("...");
203
204         lblComment.setText("コメント");
205
206         cbCommentLocal.setText("local");
207         cbCommentLocal.addItemListener(new java.awt.event.ItemListener() {
208
209             public void itemStateChanged(java.awt.event.ItemEvent evt) {
210                 useMovieLocalCheckBoxItemStateChanged(evt);
211             }
212         });
213
214         btnComment.setText("...");
215
216         lblOutput.setText("出力");
217
218         cbOutputEnable.setText("変換");
219         cbOutputEnable.addItemListener(new java.awt.event.ItemListener() {
220
221             public void itemStateChanged(java.awt.event.ItemEvent evt) {
222                 outputConvertCheckBoxItemStateChanged(evt);
223             }
224         });
225
226
227         GroupLayout gl_pnlInputMain = new GroupLayout(pnlInputMain);
228         pnlInputMain.setLayout(gl_pnlInputMain);
229         gl_pnlInputMain.setHorizontalGroup(
230             gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
231             .addGroup(gl_pnlInputMain.createSequentialGroup()
232                 .addContainerGap()
233                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
234                     .addGroup(gl_pnlInputMain.createSequentialGroup()
235                         .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
236                             .addComponent(lblComment)
237                             .addComponent(lblVideo)
238                             .addComponent(lblId)
239                             .addComponent(lblOutput))
240                         .addPreferredGap(ComponentPlacement.RELATED)
241                         .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
242                             .addGroup(gl_pnlInputMain.createSequentialGroup()
243                                 .addComponent(cbVideoLocal)
244                                 .addPreferredGap(ComponentPlacement.RELATED)
245                                 .addComponent(fldVideo, GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
246                                 .addPreferredGap(ComponentPlacement.RELATED)
247                                 .addComponent(btnVideo))
248                             .addGroup(gl_pnlInputMain.createSequentialGroup()
249                                 .addComponent(fldId, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
250                                 .addPreferredGap(ComponentPlacement.UNRELATED)
251                                 .addComponent(cbBackLogReduce)
252                                 .addComponent(cbBackLog)
253                                 .addComponent(fldBackLog, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
254                             )
255                             .addGroup(Alignment.TRAILING, gl_pnlInputMain.createSequentialGroup()
256                                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.TRAILING)
257                                     .addGroup(Alignment.LEADING, gl_pnlInputMain.createSequentialGroup()
258                                         .addComponent(cbOutputEnable)
259                                         .addPreferredGap(ComponentPlacement.RELATED)
260                                         .addComponent(fldOutput, GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
261                                     .addGroup(gl_pnlInputMain.createSequentialGroup()
262                                         .addComponent(cbCommentLocal)
263                                         .addPreferredGap(ComponentPlacement.RELATED)
264                                         .addComponent(fldComment, GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)))
265                                 .addPreferredGap(ComponentPlacement.RELATED)
266                                 .addComponent(btnComment))))
267                     .addComponent(btnApply, Alignment.TRAILING))
268                 .addContainerGap())
269         );
270         gl_pnlInputMain.setVerticalGroup(
271             gl_pnlInputMain.createParallelGroup(Alignment.LEADING)
272             .addGroup(gl_pnlInputMain.createSequentialGroup()
273                 .addContainerGap()
274                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
275                     .addComponent(fldId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
276                     .addComponent(lblId)
277                     .addComponent(cbBackLogReduce)
278                     .addComponent(cbBackLog)
279                     .addComponent(fldBackLog)
280                 )
281                 .addPreferredGap(ComponentPlacement.RELATED)
282                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
283                     .addComponent(lblVideo)
284                     .addComponent(fldVideo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
285                     .addComponent(btnVideo)
286                     .addComponent(cbVideoLocal))
287                 .addPreferredGap(ComponentPlacement.RELATED)
288                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
289                     .addComponent(lblComment)
290                     .addComponent(fldComment, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
291                     .addComponent(btnComment)
292                     .addComponent(cbCommentLocal))
293                 .addPreferredGap(ComponentPlacement.RELATED)
294                 .addGroup(gl_pnlInputMain.createParallelGroup(Alignment.BASELINE)
295                     .addComponent(lblOutput)
296                     .addComponent(fldOutput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
297                     .addComponent(cbOutputEnable))
298                 .addPreferredGap(ComponentPlacement.UNRELATED)
299                 .addComponent(btnApply)
300                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
301         );
302
303         GroupLayout gl_pnlMain = new GroupLayout(pnlMain);
304         pnlMain.setLayout(gl_pnlMain);
305         gl_pnlMain.setHorizontalGroup(
306             gl_pnlMain.createParallelGroup(Alignment.LEADING)
307             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
308                 .addContainerGap()
309                 .addGroup(gl_pnlMain.createParallelGroup(Alignment.TRAILING)
310                     .addComponent(pnlInputMain, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
311                     .addComponent(scrDisplay, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
312                     .addComponent(pnlButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
313                 .addContainerGap())
314         );
315         gl_pnlMain.setVerticalGroup(
316             gl_pnlMain.createParallelGroup(Alignment.LEADING)
317             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
318                 .addContainerGap()
319                 .addComponent(scrDisplay, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
320                 .addPreferredGap(ComponentPlacement.RELATED)
321                 .addComponent(pnlButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
322                 .addPreferredGap(ComponentPlacement.RELATED)
323                 .addComponent(pnlInputMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
324                 .addGap(24, 24, 24))
325         );
326
327
328         JMenuBar menuBar = initMenuBar();
329         setJMenuBar(menuBar);
330
331         GroupLayout layout = new GroupLayout(getContentPane());
332         getContentPane().setLayout(layout);
333         layout.setHorizontalGroup(
334             layout.createParallelGroup(Alignment.LEADING)
335             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
336         );
337         layout.setVerticalGroup(
338             layout.createParallelGroup(Alignment.LEADING)
339             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
340         );
341
342         pack();
343         initInputPanel();
344         pnlMain.setTransferHandler(new DownloadListTransferHandler());
345         tblDisplay.setTransferHandler(new TableTransferHandler());
346
347         final Config p = Config.INSTANCE;
348         // TODO コンフィグからスレッド数
349         taskManager = new TaskManage(1, 1);
350
351     }
352
353     private class ApplyActionListener implements ActionListener {
354
355         @Override
356         public void actionPerformed(ActionEvent e) {
357             final DownloadProfile prof = new InqubusDownloadProfile();
358             final String id = Util.getVideoId(fldId.getText());
359             logger.log(Level.INFO, prof.toString());
360             // TODO 処理開始
361 //            new Download(prof, id).execute();
362         }
363     }
364     /** This method is called from within the constructor to
365      * initialize the form.
366      * WARNING: Do NOT modify this code. The content of this method is
367      * always regenerated by the Form Editor.
368      */
369     @SuppressWarnings("unchecked")
370     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
371     private void initComponents() {
372     }// </editor-fold>//GEN-END:initComponents
373
374     private File searchFileMatchId(final File dir, final String id) {
375         // TODO 候補は複数返すようにして、その後の対処は呼び出しもとで行ってもらった方が良いかも
376         if (id.isEmpty()) {
377             return null;
378         }
379
380         final File[] lists = dir.listFiles(new FilenameFilter() {
381
382             final Pattern pattern = Pattern.compile(id + "\\D");
383
384             @Override
385             public boolean accept(File dir, String name) {
386                 return pattern.matcher(name).find();
387             }
388         });
389
390         if (lists.length == 1) {
391             return lists[0];
392         } else if (lists.length > 1) {
393             throw new UnsupportedOperationException();
394         } else {
395             return null;
396         }
397     }
398
399     private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
400         final Config p = Config.INSTANCE;
401
402         final ItemSelectable source = evt.getItemSelectable();
403
404         JButton btn;
405         JTextField field;
406         File dir;
407         if (source == cbVideoLocal) {
408             btn = btnVideo;
409             field = fldVideo;
410             dir = new File(p.getVideoDir());
411         } else {
412             btn = btnComment;
413             field = fldComment;
414             dir = new File(p.getCommentDir());
415         }
416
417         final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
418         btn.setEnabled(useLocal);
419
420         String text;
421         if (useLocal) {
422             final File f = searchFileMatchId(dir, fldId.getText());
423             if (f != null) {
424                 text = f.getPath();
425             } else {
426                 text = "";
427             }
428         } else {
429             text = p.getVideoFileNamePattern();
430         }
431         field.setText(text);
432
433         fldId.setEnabled(!(cbVideoLocal.isSelected() && cbCommentLocal.isSelected()));
434
435     }//GEN-LAST:event_useMovieLocalCheckBoxItemStateChanged
436
437     private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
438         final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
439         fldOutput.setEnabled(convert);
440     }//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
441
442     private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
443         final Config p = Config.INSTANCE;
444         final String id = fldId.getText();
445         if (id.isEmpty()) {
446             return;
447         }
448
449         if (cbVideoLocal.isSelected() && fldVideo.getText().isEmpty()) {
450             final File dir = new File(p.getVideoDir());
451             final File file = searchFileMatchId(dir, id);
452             if (file != null) {
453                 fldVideo.setText(file.getPath());
454             }
455         }
456
457         if (cbCommentLocal.isSelected() && fldComment.getText().isEmpty()) {
458             final File dir = new File(p.getCommentDir());
459             final File file = searchFileMatchId(dir, id);
460             if (file != null) {
461                 fldComment.setText(file.getPath());
462             }
463         }
464
465     }//GEN-LAST:event_idFieldFocusLost
466     // Variables declaration - do not modify//GEN-BEGIN:variables
467     private final JTable tblDisplay;
468     // ボタン領域
469     private final JButton btnStart;
470     private final JButton btnStop;
471     private final JButton btnDeselect;
472     //入力領域 - 標準
473     private final JTextField fldId;
474     private final JCheckBox cbBackLogReduce = new JCheckBox("コメ数減少");
475     private final JCheckBox cbBackLog = new JCheckBox("過去ログ");
476     private final JTextField fldBackLog = new JTextField();
477     private final JCheckBox cbVideoLocal;
478     private final JTextField fldVideo;
479     private final JButton btnVideo;
480     private final JCheckBox cbCommentLocal;
481     private final JTextField fldComment;
482     private final JButton btnComment;
483     private final JCheckBox cbOutputEnable;
484     private final JTextField fldOutput;
485     // 適用
486     private final JButton btnApply = new JButton("適用");
487     // End of variables declaration//GEN-END:variables
488
489     private void initInputPanel() {
490         final Config p = Config.INSTANCE;
491
492         fldId.setText("");
493         fldBackLog.setEnabled(false);
494         fldBackLog.setToolTipText("YYYY/MM/DD hh:mm:ss形式、あるいは1970/01/01からの経過秒を入力します。");
495         cbBackLog.setEnabled(true);
496         cbBackLog.addItemListener(new ItemListener() {
497
498             @Override
499             public void itemStateChanged(ItemEvent e) {
500                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
501                 fldBackLog.setEnabled(selected);
502             }
503         });
504         cbBackLogReduce.setToolTipText("「コメントの量を減らす」場合はチェックを付けます。");
505
506         final boolean movieLocal = p.getVideoUseLocal();
507         cbVideoLocal.setSelected(movieLocal);
508         btnVideo.setEnabled(movieLocal);
509         if (!movieLocal) {
510             fldVideo.setText(p.getVideoFileNamePattern());
511         }
512
513         final boolean commentLocal = p.getCommentUseLocal();
514         cbCommentLocal.setSelected(commentLocal);
515         btnComment.setEnabled(commentLocal);
516         if (!commentLocal) {
517             fldComment.setText(p.getCommentFileNamePattern());
518         }
519
520         final boolean convert = p.getOutputEnable();
521         cbOutputEnable.setSelected(convert);
522         fldOutput.setEnabled(convert);
523         fldOutput.setText(p.getOutputFileNamePattern());
524
525     }
526
527     private JMenuBar initMenuBar() {
528         final JMenuBar menuBar = new JMenuBar();
529
530         final JMenu mnFile = new JMenu("ファイル(F)");
531         menuBar.add(mnFile);
532
533         final JMenuItem itExit = new JMenuItem("終了(X)", KeyEvent.VK_X);
534         itExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
535         itExit.addActionListener(new ActionListener() {
536
537             @Override
538             public void actionPerformed(ActionEvent e) {
539                 throw new UnsupportedOperationException("Not supported yet.");
540             }
541         });
542         mnFile.add(itExit);
543
544         final JMenu mnTool = new JMenu("ツール(T)");
545         menuBar.add(mnTool);
546
547         final JMenuItem itOption = new JMenuItem("オプション(O)...", KeyEvent.VK_O);
548         // TODO ショートカットキー
549         itOption.addActionListener(new ActionListener() {
550
551             @Override
552             public void actionPerformed(ActionEvent e) {
553                 final yukihane.inqubus.gui.ConfigDialog dlg = new yukihane.inqubus.gui.ConfigDialog(MainFrame.this);
554                 dlg.setLocationRelativeTo(MainFrame.this);
555                 dlg.setModal(true);
556                 dlg.setVisible(true);
557             }
558         });
559         mnTool.add(itOption);
560
561         final JMenu mnHelp = new JMenu("ヘルプ(H)");
562         menuBar.add(mnHelp);
563
564         final JMenuItem itAbout = new JMenuItem("このソフトウェアについて(A)...", KeyEvent.VK_A);
565         itAbout.addActionListener(new ActionListener() {
566
567             @Override
568             public void actionPerformed(ActionEvent e) {
569                 MainFrame_AboutBox dlg = new MainFrame_AboutBox(MainFrame.this);
570                 dlg.pack();
571                 dlg.setLocationRelativeTo(MainFrame.this);
572                 dlg.setModal(true);
573                 dlg.setVisible(true);
574             }
575         });
576         mnHelp.add(itAbout);
577
578         return menuBar;
579     }
580
581     private class DownloadProgressListener implements WorkerListener<DownloadProgress> {
582
583         @Override
584         public void process(DownloadProgress progress) {
585             throw new UnsupportedOperationException("Not supported yet.");
586         }
587     }
588
589     private class ConvertProgressListener implements WorkerListener<ConvertProgress> {
590
591         @Override
592         public void process(ConvertProgress progress) {
593             throw new UnsupportedOperationException("Not supported yet.");
594         }
595     }
596
597
598
599     private class DownloadListTransferHandler extends TransferHandler {
600
601         private static final long serialVersionUID = 1L;
602         private final Pattern movieIdPattern = Pattern.compile("(\\w\\w\\d+)");
603
604         @Override
605         public boolean canImport(TransferHandler.TransferSupport support) {
606             Transferable transferable = support.getTransferable();
607             if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
608                     || transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
609                 return true;
610             }
611             return false;
612         }
613
614         @Override
615         public boolean importData(TransferHandler.TransferSupport support) {
616             try {
617                 Transferable transferable = support.getTransferable();
618                 if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
619                     @SuppressWarnings("unchecked")
620                     final List<File> data = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
621                     Collection<Target> targets = Target.from(data);
622                     targetModel.addTarget(targets);
623                 } else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
624                     String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
625                     Matcher matcher = movieIdPattern.matcher(data);
626                     if (matcher.find()) {
627                         String movieId = matcher.group(1);
628                         Target target = Target.fromId(movieId);
629                         targetModel.addTarget(target);
630                     } else {
631                         return false;
632                     }
633
634                 }
635                 return false;
636             } catch (Exception e) {
637                 logger.log(Level.SEVERE, null, e);
638                 return false;
639             }
640         }
641     }
642
643     private class TableTransferHandler extends DownloadListTransferHandler {
644
645         private static final long serialVersionUID = 1L;
646
647         @Override
648         public boolean canImport(TransferHandler.TransferSupport support) {
649             return super.canImport(support);
650         }
651
652         @Override
653         public boolean importData(TransferHandler.TransferSupport support) {
654             return super.importData(support);
655         }
656     }
657
658
659     private class InqubusDownloadProfile implements DownloadProfile {
660
661         private final LoginProfile loginProfile;
662         private final ProxyProfile proxyProfile;
663         private final VideoProfile videoProfile;
664         private final CommentProfile commentProfile;
665         private final GeneralProfile generalProfile;
666
667         private InqubusDownloadProfile() {
668             this.loginProfile = new InqubusLoginProfile();
669             this.proxyProfile = new InqubusProxyProfile();
670             this.videoProfile = new InqubusVideoProfile();
671             this.commentProfile = new InqubusCommentProfile();
672             this.generalProfile = new InqubusGeneralProfile();
673         }
674
675         @Override
676         public LoginProfile getLoginInfo() {
677             return this.loginProfile;
678         }
679
680         @Override
681         public ProxyProfile getProxyProfile() {
682             return this.proxyProfile;
683         }
684
685         @Override
686         public VideoProfile getVideoProfile() {
687             return this.videoProfile;
688         }
689
690         @Override
691         public CommentProfile getCommentProfile() {
692             return this.commentProfile;
693         }
694
695         @Override
696         public GeneralProfile getGeneralProfile() {
697             return this.generalProfile;
698         }
699
700         @Override
701         public String toString(){
702             return ToStringBuilder.reflectionToString(this);
703         }
704     }
705
706     private class InqubusLoginProfile implements LoginProfile {
707         private final String mail;
708         private final String password;
709
710         private InqubusLoginProfile(){
711             final Config p = Config.INSTANCE;
712             this.mail = p.getId();
713             this.password = p.getPassword();
714         }
715
716         @Override
717         public String getMail() {
718             return this.mail;
719         }
720
721         @Override
722         public String getPassword() {
723             return this.password;
724         }
725
726         @Override
727         public String toString(){
728             return ToStringBuilder.reflectionToString(this);
729         }
730     }
731
732     private class InqubusProxyProfile implements ProxyProfile {
733         private final boolean use;
734         private final String host;
735         private final int port;
736
737         private InqubusProxyProfile(){
738             final Config p = Config.INSTANCE;
739             this.use = p.getProxyUse();
740             this.host = p.getProxyHost();
741             final String pp = p.getProxyPort();
742             this.port = StringUtils.isBlank(pp) ? -1 : Integer.parseInt(pp);
743         }
744
745         @Override
746         public boolean use() {
747             return this.use;
748         }
749
750         @Override
751         public String getHost() {
752             return this.host;
753         }
754
755         @Override
756         public int getPort() {
757             return this.port;
758         }
759
760         @Override
761         public String toString(){
762             return ToStringBuilder.reflectionToString(this);
763         }
764     }
765
766     private class InqubusVideoProfile implements VideoProfile {
767         private final boolean download;
768         private final File dir;
769         private final String fileName;
770         private final File localFile;
771
772         private InqubusVideoProfile(){
773             final Config p = Config.INSTANCE;
774             this.download = !cbVideoLocal.isSelected();
775             if (this.download) {
776                 this.dir = new File(p.getVideoDir());
777                 this.fileName = fldVideo.getText();
778                 this.localFile = null;
779             } else {
780                 this.dir = null;
781                 this.fileName = null;
782                 this.localFile = new File(fldVideo.getText());
783             }
784         }
785
786         @Override
787         public boolean isDownload() {
788             return this.download;
789         }
790
791         @Override
792         public File getDir() {
793             return this.dir;
794         }
795
796         @Override
797         public String getFileName() {
798             return this.fileName;
799         }
800
801         @Override
802         public File getLocalFile() {
803             return this.localFile;
804         }
805
806         @Override
807         public String toString(){
808             return ToStringBuilder.reflectionToString(this);
809         }
810     }
811
812     private class InqubusCommentProfile implements CommentProfile {
813         private final boolean download;
814         private final File dir;
815         private final String fileName;
816         private final File localFile;
817         private final int lengthRelatedCommentSize;
818         private final boolean disablePerMinComment;
819         private final int perMinCommentSize;
820         private final long backLogPoint;
821
822         private InqubusCommentProfile() {
823             final Config p = Config.INSTANCE;
824             this.download = !cbVideoLocal.isSelected();
825             if (this.download) {
826                 this.dir = new File(p.getVideoDir());
827                 this.fileName = fldVideo.getText();
828                 this.localFile = null;
829             } else {
830                 this.dir = null;
831                 this.fileName = null;
832                 this.localFile = new File(fldVideo.getText());
833             }
834
835             if(cbBackLog.isSelected()) {
836                 try {
837                     this.backLogPoint = WayBackTimeParser.parse(fldBackLog.getText());
838                 } catch (IOException ex) {
839                     throw new IllegalArgumentException("過去ログ時刻指定が誤っています。", ex);
840                 }
841             } else {
842                 this.backLogPoint = -1L;
843             }
844
845             this.disablePerMinComment = cbBackLogReduce.isSelected();
846             this.lengthRelatedCommentSize
847                     = (p.getCommentSizeAutosize()) ? -1 : Integer.parseInt(p.getCommentSizeManual());
848             this.perMinCommentSize
849                     = (p.getCommentMinSizeAutosize()) ? -1 : Integer.parseInt(p.getCommentMinSizeManual());
850         }
851
852         @Override
853         public boolean isDownload() {
854             return this.download;
855         }
856
857         @Override
858         public File getDir() {
859             return this.dir;
860         }
861
862         @Override
863         public String getFileName() {
864             return this.fileName;
865         }
866
867         @Override
868         public File getLocalFile() {
869             return this.localFile;
870         }
871
872         @Override
873         public int getLengthRelatedCommentSize() {
874             return this.lengthRelatedCommentSize;
875         }
876
877         @Override
878         public boolean isDisablePerMinComment() {
879             return this.disablePerMinComment;
880         }
881
882         @Override
883         public int getPerMinCommentSize() {
884             return this.perMinCommentSize;
885         }
886
887         @Override
888         public long getBackLogPoint() {
889             return this.backLogPoint;
890         }
891
892         @Override
893         public String toString(){
894             return ToStringBuilder.reflectionToString(this);
895         }
896     }
897
898     private class InqubusGeneralProfile implements GeneralProfile {
899         private final String replaceFrom;
900         private final String replaceTo;
901         private InqubusGeneralProfile() {
902             final Config p = Config.INSTANCE;
903             this.replaceFrom = p.getReplaceFrom();
904             this.replaceTo = p.getReplaceTo();
905         }
906
907         @Override
908         public String getReplaceFrom() {
909             return this.replaceFrom;
910         }
911
912         @Override
913         public String getReplaceTo() {
914             return this.replaceTo;
915         }
916
917         @Override
918         public String toString(){
919             return ToStringBuilder.reflectionToString(this);
920         }
921     }
922 }