OSDN Git Service

少コメをコンフィグ化
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / MainFrame.java
1 /*
2  * MainFrame.java
3  *
4  * Created on 2011/05/28, 18:14:51
5  */
6 package yukihane.inqubus.gui;
7
8 import java.awt.Dimension;
9 import java.awt.Image;
10 import java.awt.ItemSelectable;
11 import java.awt.Point;
12 import java.awt.Toolkit;
13 import java.awt.datatransfer.DataFlavor;
14 import java.awt.datatransfer.Transferable;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.event.ItemEvent;
18 import java.awt.event.ItemListener;
19 import java.awt.event.KeyEvent;
20 import java.awt.event.WindowAdapter;
21 import java.awt.event.WindowEvent;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URL;
27 import java.nio.file.FileSystem;
28 import java.nio.file.FileSystems;
29 import java.nio.file.Path;
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.SortedSet;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import java.util.regex.Pattern;
38 import javax.swing.BorderFactory;
39 import javax.swing.DefaultComboBoxModel;
40 import javax.swing.DropMode;
41 import javax.swing.GroupLayout;
42 import javax.swing.GroupLayout.Alignment;
43 import javax.swing.JButton;
44 import javax.swing.JCheckBox;
45 import javax.swing.JFileChooser;
46 import javax.swing.JFrame;
47 import javax.swing.JLabel;
48 import javax.swing.JMenu;
49 import javax.swing.JMenuBar;
50 import javax.swing.JMenuItem;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPanel;
53 import javax.swing.JScrollPane;
54 import javax.swing.JTabbedPane;
55 import javax.swing.JTable;
56 import javax.swing.JTextField;
57 import javax.swing.KeyStroke;
58 import javax.swing.LayoutStyle.ComponentPlacement;
59 import javax.swing.SwingUtilities;
60 import javax.swing.TransferHandler;
61 import javax.swing.WindowConstants;
62 import javax.swing.border.BevelBorder;
63 import org.apache.commons.configuration.ConfigurationException;
64 import org.apache.commons.lang.builder.ToStringBuilder;
65 import saccubus.MainFrame_AboutBox;
66 import saccubus.util.WayBackTimeParser;
67 import saccubus.worker.profile.CommentProfile;
68 import saccubus.worker.profile.ConvertProfile;
69 import saccubus.worker.profile.DownloadProfile;
70 import saccubus.worker.profile.FfmpegProfile;
71 import saccubus.worker.profile.GeneralProfile;
72 import saccubus.worker.profile.LoginProfile;
73 import saccubus.worker.profile.OutputProfile;
74 import saccubus.worker.profile.ProxyProfile;
75 import saccubus.worker.profile.VideoProfile;
76 import yukihane.Util;
77 import yukihane.inqubus.config.Config;
78 import yukihane.inqubus.config.ConfigCommentProfile;
79 import yukihane.inqubus.config.ConfigConvertProfile;
80 import yukihane.inqubus.config.ConfigFfmpegProfile;
81 import yukihane.inqubus.config.ConfigGeneralProfile;
82 import yukihane.inqubus.config.ConfigLoginProfile;
83 import yukihane.inqubus.config.ConfigOutputProfile;
84 import yukihane.inqubus.config.ConfigProxyProfile;
85 import yukihane.inqubus.filewatch.FileWatch;
86 import yukihane.inqubus.filewatch.FileWatchUtil;
87 import yukihane.inqubus.manager.RequestProcess;
88 import yukihane.inqubus.manager.TaskKind;
89 import yukihane.inqubus.manager.TaskManage;
90 import yukihane.inqubus.manager.TaskManageListener;
91 import yukihane.inqubus.manager.TaskStatus;
92 import yukihane.inqubus.model.Target;
93 import yukihane.inqubus.model.TargetsTableModel;
94
95 /**
96  *
97  * @author yuki
98  */
99 public class MainFrame extends JFrame {
100
101     private static final long serialVersionUID = 1L;
102     private static final Logger logger = Logger.getLogger(MainFrame.class.getName());
103     private static final String ID_FIELD_TOOLTIP = "動画のIDまたはURLを入力します。";
104     private static final String FILE_LOCALBUTTON_TOOLTIP
105             = "<html>ダウンロードする場合はチェックを外します。<br/>ローカルファイルを使用する場合はチェックを入れます。</html>";
106     private static final String FILE_INPUTFIELD_TOOLTIP
107             = "<html>ダウンロードする場合はファイル命名規則を入力します。<br/>"
108             + "ローカルファイルを使用する場合はパスを含むファイル名を入力します。</html>";
109     private static final String FILE_OUTPUTFIELD_TOOLTIP
110             = "ファイル命名規則入力します。";
111     private final TargetsTableModel targetModel = new TargetsTableModel();
112     private final TaskManage taskManager;
113     private final Thread videoFileWatcherThread;
114     private final FileWatch videoFileWatcher;
115     private final Thread commentFileWatcherThread;
116     private final FileWatch commentFileWatcher;
117
118
119     /** Creates new form MainFrame */
120     public MainFrame() {
121         super();
122         addWindowListener(new MainFrameWindowListener());
123
124         final Config p = Config.INSTANCE;
125
126         // ワーカスレッド生成
127         final int thDownload = p.getSystemDownloadThread();
128         final int secDownload = p.getSystemDownloadWait();
129         final int thConvert = p.getSystemConvertThread();
130         taskManager = new TaskManage(thDownload, secDownload, thConvert, new GuiTaskManageListener());
131
132         // ディレクトリ監視スレッド生成
133         final FileSystem fs = FileSystems.getDefault();
134
135         final List<String> videoSearchDirs = p.getSearchVideoDirs();
136         videoSearchDirs.add(p.getVideoDir());
137         final Set<Path> videoPaths = new HashSet<>(videoSearchDirs.size());
138         for (String s : videoSearchDirs) {
139             videoPaths.add(fs.getPath(s));
140         }
141         videoFileWatcher = new FileWatch(videoPaths);
142         this.videoFileWatcherThread = new Thread(videoFileWatcher);
143         this.videoFileWatcherThread.setDaemon(true);
144
145         final List<String> commentSearchDirs = p.getSearchCommentDirs();
146         commentSearchDirs.add(p.getCommentDir());
147         final Set<Path> commentPaths = new HashSet<>(commentSearchDirs.size());
148         for(String s : commentSearchDirs) {
149             commentPaths.add(fs.getPath(s));
150         }
151         commentFileWatcher = new FileWatch(commentPaths);
152         this.commentFileWatcherThread = new Thread(commentFileWatcher);
153         this.commentFileWatcherThread.setDaemon(true);
154
155         final URL url = MainFrame_AboutBox.class.getResource("icon.png");
156         final Image icon1 = Toolkit.getDefaultToolkit().createImage(url);
157         final URL url32 = MainFrame_AboutBox.class.getResource("icon32.png");
158         final Image icon2 = Toolkit.getDefaultToolkit().createImage(url32);
159         final List<Image> images = new ArrayList<>(2);
160         images.add(icon1);
161         images.add(icon2);
162         setIconImages(images);
163
164         final JPanel pnlMain = new JPanel();
165         final JScrollPane scrDisplay = new JScrollPane();
166         tblDisplay = new JTable(targetModel, new TargetsColumnModel());
167         tblDisplay.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
168         final JPanel pnlButton = new JPanel();
169         final JPanel pnlInputMain = new JPanel();
170         final JLabel lblId = new JLabel();
171         final JLabel lblVideo = new JLabel();
172         cbVideoLocal = new JCheckBox();
173         cbVideoLocal.setToolTipText(FILE_LOCALBUTTON_TOOLTIP);
174         cmbVideo.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
175         btnVideo.addActionListener(
176                 new FileChooseAction(MainFrame.this, JFileChooser.FILES_ONLY, fldVideo));
177         final JLabel lblComment = new JLabel();
178
179         fldBackLog.setToolTipText("YYYY/MM/DD hh:mm:ss形式、あるいは1970/01/01からの経過秒を入力します。");
180         cbBackLog.addItemListener(new ItemListener() {
181
182             @Override
183             public void itemStateChanged(ItemEvent e) {
184                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
185                 fldBackLog.setEnabled(selected);
186             }
187         });
188         cbBackLog.addPropertyChangeListener("enabled", new PropertyChangeListener() {
189
190             @Override
191             public void propertyChange(PropertyChangeEvent evt) {
192                 final boolean enabled = ((Boolean) evt.getNewValue()).booleanValue();
193                 final boolean fldEnabled = enabled ? cbBackLog.isSelected() : false;
194                 fldBackLog.setEnabled(fldEnabled);
195             }
196         });
197         cbBackLogReduce.setToolTipText("「コメントの量を減らす」場合はチェックを付けます。");
198
199         cbCommentLocal = new JCheckBox();
200         cbCommentLocal.setToolTipText(FILE_LOCALBUTTON_TOOLTIP);
201         cbCommentLocal.addItemListener(new ItemListener() {
202
203             @Override
204             public void itemStateChanged(ItemEvent e) {
205                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
206                 cbBackLogReduce.setEnabled(!selected);
207                 cbBackLog.setEnabled(!selected);
208             }
209         });
210         cmbComment.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
211         btnComment.addActionListener(
212                 new FileChooseAction(MainFrame.this, JFileChooser.FILES_ONLY, fldComment));
213         final JLabel lblOutput = new JLabel();
214         cbOutputEnable = new JCheckBox();
215         fldOutput = new JTextField();
216         fldOutput.setToolTipText(FILE_OUTPUTFIELD_TOOLTIP);
217
218         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
219
220         btnStop.addActionListener(new StopActionListener());
221         final ApplyActionListener applyListener = new ApplyActionListener();
222         btnApply.addActionListener(applyListener);
223         btnClear.addActionListener(new ActionListener() {
224
225             @Override
226             public void actionPerformed(ActionEvent e) {
227                 initInputPanel();
228             }
229         });
230
231         pnlMain.setBorder(BorderFactory.createEtchedBorder());
232
233         tblDisplay.setDropMode(DropMode.INSERT_ROWS);
234         scrDisplay.setViewportView(tblDisplay);
235
236         pnlButton.setBorder(BorderFactory.createEtchedBorder());
237
238         GroupLayout gl_pnlButton = new GroupLayout(pnlButton);
239         pnlButton.setLayout(gl_pnlButton);
240         gl_pnlButton.setHorizontalGroup(
241             gl_pnlButton.createParallelGroup(Alignment.LEADING)
242             .addGroup(gl_pnlButton.createSequentialGroup()
243                 .addContainerGap()
244                 .addPreferredGap(ComponentPlacement.RELATED)
245                 .addComponent(btnStop)
246                 .addPreferredGap(ComponentPlacement.RELATED, 250, Short.MAX_VALUE)
247                 .addContainerGap())
248         );
249         gl_pnlButton.setVerticalGroup(
250             gl_pnlButton.createParallelGroup(Alignment.LEADING)
251             .addGroup(gl_pnlButton.createSequentialGroup()
252                 .addContainerGap()
253                 .addGroup(gl_pnlButton.createParallelGroup(Alignment.BASELINE)
254                     .addComponent(btnStop)
255                 )
256                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
257             )
258         );
259
260         lblId.setText("ID");
261
262
263         cmbId = new IdComboBox(videoFileWatcher);
264         cmbId.setToolTipText(ID_FIELD_TOOLTIP);
265         cmbId.getEditorComponent().addActionListener(applyListener);
266         cmbId.getEditorComponent().addFocusListener(new java.awt.event.FocusAdapter() {
267
268             public void focusLost(java.awt.event.FocusEvent evt) {
269                 idFieldFocusLost(evt);
270             }
271         });
272
273         lblVideo.setText("動画");
274
275         cbVideoLocal.setText("local");
276         cbVideoLocal.addItemListener(new java.awt.event.ItemListener() {
277
278             public void itemStateChanged(java.awt.event.ItemEvent evt) {
279                 useMovieLocalCheckBoxItemStateChanged(evt);
280             }
281         });
282
283         lblComment.setText("コメント");
284
285         cbCommentLocal.setText("local");
286         cbCommentLocal.addItemListener(new java.awt.event.ItemListener() {
287
288             public void itemStateChanged(java.awt.event.ItemEvent evt) {
289                 useMovieLocalCheckBoxItemStateChanged(evt);
290             }
291         });
292
293         lblOutput.setText("出力");
294
295         cbOutputEnable.setText("変換");
296         cbOutputEnable.addItemListener(new java.awt.event.ItemListener() {
297
298             public void itemStateChanged(java.awt.event.ItemEvent evt) {
299                 outputConvertCheckBoxItemStateChanged(evt);
300             }
301         });
302
303
304         final GroupLayout glInputMain = new GroupLayout(pnlInputMain);
305         pnlInputMain.setLayout(glInputMain);
306         glInputMain.setHorizontalGroup(
307             glInputMain.createParallelGroup(Alignment.LEADING)
308             .addGroup(glInputMain.createSequentialGroup()
309                 .addContainerGap()
310                 .addComponent(lblId)
311                 .addPreferredGap(ComponentPlacement.RELATED)
312                 .addComponent(cmbId, GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE)
313                 .addPreferredGap(ComponentPlacement.UNRELATED)
314                 .addComponent(cbBackLogReduce)
315                 .addPreferredGap(ComponentPlacement.UNRELATED)
316                 .addComponent(cbBackLog)
317                 .addPreferredGap(ComponentPlacement.RELATED)
318                 .addComponent(fldBackLog, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
319                 .addContainerGap()
320             )
321             .addGroup(glInputMain.createSequentialGroup()
322                 .addContainerGap()
323                 .addGroup(glInputMain.createParallelGroup(Alignment.LEADING)
324                     .addComponent(lblVideo)
325                     .addComponent(lblComment)
326                     .addComponent(lblOutput)
327                 )
328                 .addPreferredGap(ComponentPlacement.RELATED)
329                 .addGroup(glInputMain.createParallelGroup(Alignment.LEADING)
330                     .addComponent(cbVideoLocal)
331                     .addComponent(cbCommentLocal)
332                     .addComponent(cbOutputEnable)
333                 )
334                 .addPreferredGap(ComponentPlacement.RELATED)
335                 .addGroup(glInputMain.createParallelGroup(Alignment.LEADING)
336                     .addComponent(cmbVideo, 300, 300, Short.MAX_VALUE)
337                     .addComponent(cmbComment, 300, 300, Short.MAX_VALUE)
338                     .addComponent(fldOutput, 300, 300, Short.MAX_VALUE)
339                 )
340                 .addGroup(glInputMain.createParallelGroup()
341                     .addComponent(btnVideo)
342                     .addComponent(btnComment)
343                 )
344                 .addContainerGap()
345             )
346         );
347
348         glInputMain.setVerticalGroup(
349             glInputMain.createParallelGroup(Alignment.LEADING)
350             .addGroup(glInputMain.createSequentialGroup()
351                 .addContainerGap()
352                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
353                     .addComponent(cmbId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
354                     .addComponent(lblId)
355                     .addComponent(cbBackLogReduce)
356                     .addComponent(cbBackLog)
357                     .addComponent(fldBackLog)
358                 )
359                 .addPreferredGap(ComponentPlacement.RELATED)
360                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
361                     .addComponent(lblVideo)
362                     .addComponent(cbVideoLocal)
363                     .addComponent(cmbVideo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
364                     .addComponent(btnVideo)
365                 )
366                 .addPreferredGap(ComponentPlacement.RELATED)
367                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
368                     .addComponent(lblComment)
369                     .addComponent(cbCommentLocal)
370                     .addComponent(cmbComment, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
371                     .addComponent(btnComment)
372                 )
373                 .addPreferredGap(ComponentPlacement.RELATED)
374                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
375                     .addComponent(lblOutput)
376                     .addComponent(fldOutput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
377                     .addComponent(cbOutputEnable)
378                 )
379             )
380         );
381
382         // ffmpeg入力パネル
383         pnlInputFfmpeg.fldFfmpegOptionResizeWidth.setEnabled(false);
384         pnlInputFfmpeg.fldFfmpegOptionResizeHeight.setEnabled(false);
385         pnlInputFfmpeg.cbFfmpegOptionKeepAspect.setEnabled(false);
386         pnlInputFfmpeg.cmbFfmpegOptionFile.addActionListener(new ActionListener() {
387
388             @Override
389             public void actionPerformed(ActionEvent e) {
390                 final boolean notFile = !pnlInputFfmpeg.mdlFfmpegOption.isFile();
391                 pnlInputFfmpeg.fldFfmpegOptionExtension.setEnabled(notFile);
392                 pnlInputFfmpeg.fldFfmpegOptionMain.setEnabled(notFile);
393                 pnlInputFfmpeg.fldFfmpegOptionIn.setEnabled(notFile);
394                 pnlInputFfmpeg.fldFfmpegOptionOut.setEnabled(notFile);
395                 pnlInputFfmpeg.fldFfmpegOptionAv.setEnabled(notFile);
396                 pnlInputFfmpeg.cbFfmpegOptionResize.setEnabled(notFile);
397             }
398         });
399         pnlInputFfmpeg.cbFfmpegOptionResize.addItemListener(new ItemListener() {
400
401             @Override
402             public void itemStateChanged(ItemEvent e) {
403                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
404                 pnlInputFfmpeg.fldFfmpegOptionResizeWidth.setEnabled(selected);
405                 pnlInputFfmpeg.fldFfmpegOptionResizeHeight.setEnabled(selected);
406                 pnlInputFfmpeg.cbFfmpegOptionKeepAspect.setEnabled(selected);
407             }
408         });
409         pnlInputFfmpeg.cbFfmpegOptionResize.addPropertyChangeListener("enabled", new PropertyChangeListener() {
410
411             @Override
412             public void propertyChange(PropertyChangeEvent evt) {
413                 final boolean enabled = ((Boolean) evt.getNewValue()).booleanValue();
414                 final boolean fldEnabled = enabled ? pnlInputFfmpeg.cbFfmpegOptionResize.isSelected() : false;
415                 pnlInputFfmpeg.fldFfmpegOptionResizeWidth.setEnabled(fldEnabled);
416                 pnlInputFfmpeg.fldFfmpegOptionResizeHeight.setEnabled(fldEnabled);
417                 pnlInputFfmpeg.cbFfmpegOptionKeepAspect.setEnabled(fldEnabled);
418             }
419         });
420
421
422         tbpInput.add("メイン", pnlInputMain);
423         tbpInput.add("ffmpeg", pnlInputFfmpeg);
424
425         // 入力部のボタンやメッセージ表示部
426         fldInputMessage.setEditable(false);
427         fldInputMessage.setEnabled(false);
428         fldInputMessage.setBorder(BorderFactory.createEmptyBorder());
429
430         final JPanel pnlInputButton = new JPanel();
431         final GroupLayout glInputButton = new GroupLayout(pnlInputButton);
432         pnlInputButton.setLayout(glInputButton);
433         glInputButton.setHorizontalGroup(glInputButton.createSequentialGroup()
434             .addContainerGap()
435             .addComponent(fldInputMessage, GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
436             .addPreferredGap(ComponentPlacement.UNRELATED)
437             .addComponent(btnClear)
438             .addPreferredGap(ComponentPlacement.UNRELATED)
439             .addComponent(btnApply)
440             .addContainerGap()
441         );
442         glInputButton.setVerticalGroup(glInputButton.createSequentialGroup()
443             .addContainerGap()
444             .addGroup(glInputButton.createParallelGroup(Alignment.BASELINE)
445                 .addComponent(fldInputMessage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
446                 .addComponent(btnClear)
447                 .addComponent(btnApply)
448             )
449             .addContainerGap()
450         );
451
452         // 画面下半分の入力部分
453         final JPanel pnlInputAll = new JPanel();
454         pnlInputAll.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
455         final GroupLayout glInputAll = new GroupLayout(pnlInputAll);
456         pnlInputAll.setLayout(glInputAll);
457         glInputAll.setHorizontalGroup(glInputAll.createParallelGroup()
458             .addComponent(tbpInput)
459             .addComponent(pnlInputButton)
460         );
461         glInputAll.setVerticalGroup(glInputAll.createSequentialGroup()
462             .addComponent(tbpInput)
463             .addComponent(pnlInputButton)
464         );
465
466         GroupLayout gl_pnlMain = new GroupLayout(pnlMain);
467         pnlMain.setLayout(gl_pnlMain);
468         gl_pnlMain.setHorizontalGroup(
469             gl_pnlMain.createParallelGroup(Alignment.LEADING)
470             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
471                 .addContainerGap()
472                 .addGroup(gl_pnlMain.createParallelGroup(Alignment.TRAILING)
473                     .addComponent(scrDisplay, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
474                     .addComponent(pnlButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
475                     .addComponent(pnlInputAll, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
476                 )
477                 .addContainerGap())
478         );
479         gl_pnlMain.setVerticalGroup(
480             gl_pnlMain.createParallelGroup(Alignment.LEADING)
481             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
482                 .addContainerGap()
483                 .addComponent(scrDisplay, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
484                 .addPreferredGap(ComponentPlacement.RELATED)
485                 .addComponent(pnlButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
486                 .addPreferredGap(ComponentPlacement.RELATED)
487                 .addComponent(pnlInputAll, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
488                 .addContainerGap()
489             )
490         );
491
492
493         JMenuBar menuBar = initMenuBar();
494         setJMenuBar(menuBar);
495
496         GroupLayout layout = new GroupLayout(getContentPane());
497         getContentPane().setLayout(layout);
498         layout.setHorizontalGroup(
499             layout.createParallelGroup(Alignment.LEADING)
500             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
501         );
502         layout.setVerticalGroup(
503             layout.createParallelGroup(Alignment.LEADING)
504             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
505         );
506
507         pack();
508         setMinimumSize(getSize());
509
510         /*
511          * 画面のサイズや位置を前回終了時のものに設定する
512          */
513         final int windowWidth = p.getSystemWindowWidth();
514         final int windowHeight = p.getSystemWindowHeight();
515         if (windowWidth > 0 && windowHeight > 0) {
516             setSize(windowWidth, windowHeight);
517         }
518
519         // TODO 最大化した状態で終了した場合の考慮
520         final int windowPosX = p.getSystemWindowPosX();
521         final int windowPosY = p.getSystemWindowPosY();
522         if (windowPosX > 1024 && windowPosY > 1024) {
523             setLocation(windowPosX, windowPosY);
524         } else {
525             setLocationByPlatform(true);
526         }
527
528         final int colId = p.getSystemColumnId();
529         if(colId > 0) {
530             tblDisplay.getColumnModel().getColumn(0).setPreferredWidth(colId);
531         }
532         final int colStatus = p.getSystemColumnStatus();
533         if(colStatus > 0) {
534             tblDisplay.getColumnModel().getColumn(4).setPreferredWidth(colStatus);
535         }
536
537         initInputPanel();
538         pnlMain.setTransferHandler(new DownloadListTransferHandler());
539         tblDisplay.setTransferHandler(new TableTransferHandler());
540     }
541
542     public void startWatcher() {
543         videoFileWatcherThread.start();
544         commentFileWatcherThread.start();
545     }
546
547     private static void createFieldInfo( FileComboBox combo,  boolean useLocal,  String text, String pattern,  Set<Path> allFiles) {
548         if (useLocal) {
549             final SortedSet<String> matchFiles = FileWatchUtil.getFileNamesContain(allFiles, text);
550             DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(matchFiles.toArray(new String[0]));
551             combo.setModel(model);
552         } else {
553             combo.setModel(new DefaultComboBoxModel<String>());
554             combo.getEditorComponent().setText(pattern);
555         }
556     }
557
558     private class GuiTaskManageListener implements TaskManageListener {
559
560         @Override
561         public void process(final int id, final TaskKind kind, final TaskStatus status, final double percentage,
562                 final String message) {
563             SwingUtilities.invokeLater(new Runnable() {
564
565                 @Override
566                 public void run() {
567                     targetModel.setStatus(id, kind, status, percentage, message);
568                 }
569             });
570         }
571     }
572
573     private class StopActionListener implements ActionListener {
574
575         @Override
576         public void actionPerformed(ActionEvent e) {
577             final int row = tblDisplay.getSelectedRow();
578             final Target t = targetModel.getTarget(row);
579             final boolean res = taskManager.cancel(t.getRowId());
580             logger.log(Level.FINE, "停止: {0} {1}", new Object[]{t.getVideoId(), res});
581             if (res) {
582                 targetModel.setStatus(t.getRowId(), null, TaskStatus.CANCELLED, -1.0, "キャンセル");
583             }
584         }
585     }
586
587     private class ApplyActionListener implements ActionListener {
588
589         @Override
590         public void actionPerformed(ActionEvent e) {
591             try {
592                 final DownloadProfile downProf = new InqubusDownloadProfile();
593                 final String id = Util.getVideoId(cmbId.getText());
594                 final InqubusConvertProfile convProf = new InqubusConvertProfile();
595                 logger.log(Level.INFO, downProf.toString());
596                 logger.log(Level.INFO, convProf.toString());
597                 final RequestProcess rp = new RequestProcess(downProf, id, convProf);
598                 taskManager.add(rp);
599                 targetModel.addTarget(new Target(rp));
600                 initInputPanel();
601             } catch (Throwable th) {
602                 logger.log(Level.SEVERE, null, th);
603                 JOptionPane.showMessageDialog(MainFrame.this, th.getMessage(), "中断しました", JOptionPane.ERROR_MESSAGE);
604             }
605         }
606     }
607
608     /**
609      * 動画, コメントの"local"チェックボックス更新時の処理.
610      */
611     private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
612         final Config p = Config.INSTANCE;
613
614         final ItemSelectable source = evt.getItemSelectable();
615
616         JButton button;
617         FileComboBox combo;
618         Set<Path> allFiles;
619         String pattern;
620         if (source == cbVideoLocal) {
621             button = btnVideo;
622             combo = cmbVideo;
623             allFiles = videoFileWatcher.getFiles();
624             pattern = p.getVideoFileNamePattern();
625         } else {
626             button = btnComment;
627             combo = cmbComment;
628             allFiles = commentFileWatcher.getFiles();
629             pattern = p.getCommentFileNamePattern();
630         }
631
632         final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
633
634         button.setEnabled(useLocal);
635         createFieldInfo(combo, useLocal, cmbId.getText(), pattern, allFiles);
636
637     }
638
639     private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
640         final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
641         fldOutput.setEnabled(convert);
642     }//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
643
644     private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
645         final Config p = Config.INSTANCE;
646         final String id = cmbId.getText();
647
648         createFieldInfo(cmbVideo, cbVideoLocal.isSelected(), id, p.getVideoFileNamePattern(), videoFileWatcher.getFiles());
649         createFieldInfo(cmbComment, cbCommentLocal.isSelected(), id, p.getCommentFileNamePattern(), commentFileWatcher.getFiles());
650     }//GEN-LAST:event_idFieldFocusLost
651     // Variables declaration - do not modify//GEN-BEGIN:variables
652     private final JTable tblDisplay;
653     // ボタン領域
654     private final JButton btnStop = new JButton("停止");
655     // 入力領域
656     private final JTabbedPane tbpInput = new JTabbedPane(JTabbedPane.BOTTOM);
657     // 入力領域 - メイン
658     private final IdComboBox cmbId;
659     private final JCheckBox cbBackLogReduce = new JCheckBox("少コメ");
660     private final JCheckBox cbBackLog = new JCheckBox("過去ログ");
661     private final JTextField fldBackLog = new JTextField();
662     private final JCheckBox cbVideoLocal;
663     private final FileComboBox cmbVideo = new FileComboBox();
664     private final JTextField fldVideo = cmbVideo.getEditorComponent();
665     private final JButton btnVideo = new JButton("...");
666     private final JCheckBox cbCommentLocal;
667     private final FileComboBox cmbComment = new FileComboBox();
668     private final JTextField fldComment = cmbComment.getEditorComponent();
669     private final JButton btnComment = new JButton("...");
670     private final JCheckBox cbOutputEnable;
671     private final JTextField fldOutput;
672     // 入力領域 - ffmpeg
673     private final FfmpegParamPanel pnlInputFfmpeg = new FfmpegParamPanel();
674     // 適用
675     private final JTextField fldInputMessage = new JTextField();
676     private final JButton btnClear = new JButton("クリア");
677     private final JButton btnApply = new JButton("適用");
678     // End of variables declaration//GEN-END:variables
679
680     private void initInputPanel() {
681         initMainTab();
682         initFfmpegTab();
683         tbpInput.setSelectedIndex(0);
684         cmbId.requestFocus();
685     }
686
687     private void initMainTab() {
688         final Config p = Config.INSTANCE;
689
690         cmbId.setText("");
691         cbBackLogReduce.setSelected(p.getCommentMinDisabled());
692         cbBackLog.setEnabled(true);
693         fldBackLog.setEnabled(false);
694
695         final boolean videoLocal = p.getVideoUseLocal();
696         cbVideoLocal.setSelected(videoLocal);
697         if (!videoLocal) {
698             fldVideo.setText(p.getVideoFileNamePattern());
699         }
700         btnVideo.setEnabled(videoLocal);
701
702         final boolean commentLocal = p.getCommentUseLocal();
703         cbCommentLocal.setSelected(commentLocal);
704         if (!commentLocal) {
705             fldComment.setText(p.getCommentFileNamePattern());
706         }
707         btnComment.setEnabled(commentLocal);
708
709         final boolean convert = p.getOutputEnable();
710         cbOutputEnable.setSelected(convert);
711         fldOutput.setEnabled(convert);
712         fldOutput.setText(p.getOutputFileNamePattern());
713     }
714
715     private void initFfmpegTab() {
716         pnlInputFfmpeg.init(Config.INSTANCE);
717     }
718
719     private JMenuBar initMenuBar() {
720         final JMenuBar menuBar = new JMenuBar();
721
722         final JMenu mnFile = new JMenu("ファイル(F)");
723         menuBar.add(mnFile);
724
725         final JMenuItem itExit = new JMenuItem("終了(X)", KeyEvent.VK_X);
726         itExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
727         final ActionListener exitActionListener = new ActionListener() {
728
729             @Override
730             public void actionPerformed(ActionEvent e) {
731                 processWindowEvent(new WindowEvent(MainFrame.this, WindowEvent.WINDOW_CLOSING));
732             }
733         };
734         itExit.addActionListener(exitActionListener);
735         mnFile.add(itExit);
736
737         final JMenu mnTool = new JMenu("ツール(T)");
738         menuBar.add(mnTool);
739
740         final JMenuItem itOption = new JMenuItem("オプション(O)...", KeyEvent.VK_O);
741         // TODO ショートカットキー
742         itOption.addActionListener(new ActionListener() {
743
744             @Override
745             public void actionPerformed(ActionEvent e) {
746                 final yukihane.inqubus.gui.ConfigDialog dlg = new yukihane.inqubus.gui.ConfigDialog(MainFrame.this);
747                 dlg.setLocationRelativeTo(MainFrame.this);
748                 dlg.setModal(true);
749                 dlg.setVisible(true);
750             }
751         });
752         mnTool.add(itOption);
753
754         final JMenu mnHelp = new JMenu("ヘルプ(H)");
755         menuBar.add(mnHelp);
756
757         final JMenuItem itAbout = new JMenuItem("このソフトウェアについて(A)...", KeyEvent.VK_A);
758         itAbout.addActionListener(new ActionListener() {
759
760             @Override
761             public void actionPerformed(ActionEvent e) {
762                 MainFrame_AboutBox dlg = new MainFrame_AboutBox(MainFrame.this);
763                 dlg.pack();
764                 dlg.setLocationRelativeTo(MainFrame.this);
765                 dlg.setModal(true);
766                 dlg.setVisible(true);
767             }
768         });
769         mnHelp.add(itAbout);
770
771         return menuBar;
772     }
773
774     private class DownloadListTransferHandler extends TransferHandler {
775
776         private static final long serialVersionUID = 1L;
777         private final Pattern movieIdPattern = Pattern.compile("(\\w\\w\\d+)");
778
779         @Override
780         public boolean canImport(TransferHandler.TransferSupport support) {
781             Transferable transferable = support.getTransferable();
782             if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
783                     || transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
784                 return true;
785             }
786             return false;
787         }
788
789         @Override
790         public boolean importData(TransferHandler.TransferSupport support) {
791 //            try {
792 //                Transferable transferable = support.getTransferable();
793 //                if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
794 //                    @SuppressWarnings("unchecked")
795 //                    final List<File> data = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
796 //                    Collection<Target> targets = Target.from(data);
797 //                    targetModel.addTarget(targets);
798 //                } else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
799 //                    String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
800 //                    Matcher matcher = movieIdPattern.matcher(data);
801 //                    if (matcher.find()) {
802 //                        String movieId = matcher.group(1);
803 //                        Target target = Target.fromId(movieId);
804 //                        targetModel.addTarget(target);
805 //                    } else {
806 //                        return false;
807 //                    }
808 //
809 //                }
810 //                return false;
811 //            } catch (Exception e) {
812 //                logger.log(Level.SEVERE, null, e);
813 //                return false;
814 //            }
815             // TODO 上記実装見直し(Locationは削除された)
816             return false;
817         }
818     }
819
820     private class TableTransferHandler extends DownloadListTransferHandler {
821
822         private static final long serialVersionUID = 1L;
823
824         @Override
825         public boolean canImport(TransferHandler.TransferSupport support) {
826             return super.canImport(support);
827         }
828
829         @Override
830         public boolean importData(TransferHandler.TransferSupport support) {
831             return super.importData(support);
832         }
833     }
834
835     private class MainFrameWindowListener extends WindowAdapter {
836         @Override
837         public void windowClosing(WindowEvent e) {
838             final Config p = Config.INSTANCE;
839
840             final Dimension size = getSize();
841             p.setSystemWindowWidth(size.width);
842             p.setSystemWindowHeight(size.height);
843
844             final Point pos = getLocation();
845             p.setSystemWindowPosX(pos.x);
846             p.setSystemWindowPosY(pos.y);
847
848             p.setSystemColumnId(tblDisplay.getColumnModel().getColumn(0).getWidth());
849             p.setSystemColumnVideo(tblDisplay.getColumnModel().getColumn(1).getWidth());
850             p.setSystemColumnComment(tblDisplay.getColumnModel().getColumn(2).getWidth());
851             p.setSystemColumnConvert(tblDisplay.getColumnModel().getColumn(3).getWidth());
852             p.setSystemColumnStatus(tblDisplay.getColumnModel().getColumn(4).getWidth());
853             try {
854                 p.save();
855             } catch (ConfigurationException ex) {
856                 logger.log(Level.SEVERE, "コンフィグ保存失敗", ex);
857             }
858         }
859     }
860
861     /*
862      * ここからDownloadProfile作成用クラスの定義
863      */
864     private class InqubusDownloadProfile implements DownloadProfile {
865
866         private final LoginProfile loginProfile;
867         private final ProxyProfile proxyProfile;
868         private final VideoProfile videoProfile;
869         private final CommentProfile commentProfile;
870         private final GeneralProfile generalProfile;
871
872         private InqubusDownloadProfile() {
873             this.loginProfile = new ConfigLoginProfile();
874             this.proxyProfile = new ConfigProxyProfile();
875             this.videoProfile = new InqubusVideoProfile();
876             this.commentProfile = new InqubusCommentProfile();
877             this.generalProfile = new ConfigGeneralProfile();
878         }
879
880         @Override
881         public LoginProfile getLoginProfile() {
882             return this.loginProfile;
883         }
884
885         @Override
886         public ProxyProfile getProxyProfile() {
887             return this.proxyProfile;
888         }
889
890         @Override
891         public VideoProfile getVideoProfile() {
892             return this.videoProfile;
893         }
894
895         @Override
896         public CommentProfile getCommentProfile() {
897             return this.commentProfile;
898         }
899
900         @Override
901         public GeneralProfile getGeneralProfile() {
902             return this.generalProfile;
903         }
904
905         @Override
906         public String toString(){
907             return ToStringBuilder.reflectionToString(this);
908         }
909     }
910
911     private class InqubusVideoProfile implements VideoProfile {
912         private final boolean download;
913         private final File dir;
914         private final String fileName;
915         private final File localFile;
916
917         private InqubusVideoProfile(){
918             final Config p = Config.INSTANCE;
919             this.download = !cbVideoLocal.isSelected();
920             if (this.download) {
921                 this.dir = new File(p.getVideoDir());
922                 this.fileName = fldVideo.getText();
923                 this.localFile = null;
924             } else {
925                 this.dir = null;
926                 this.fileName = null;
927                 this.localFile = new File(fldVideo.getText());
928             }
929         }
930
931         @Override
932         public boolean isDownload() {
933             return this.download;
934         }
935
936         @Override
937         public File getDir() {
938             return this.dir;
939         }
940
941         @Override
942         public String getFileName() {
943             return this.fileName;
944         }
945
946         @Override
947         public File getLocalFile() {
948             return this.localFile;
949         }
950
951         @Override
952         public String toString(){
953             return ToStringBuilder.reflectionToString(this);
954         }
955     }
956
957     private class InqubusCommentProfile extends ConfigCommentProfile {
958         private final boolean download;
959         private final File dir;
960         private final String fileName;
961         private final File localFile;
962         private final boolean disablePerMinComment;
963         private final long backLogPoint;
964
965         private InqubusCommentProfile() {
966             super();
967
968             final Config p = Config.INSTANCE;
969             this.download = !cbCommentLocal.isSelected();
970             if (this.download) {
971                 this.dir = new File(p.getCommentDir());
972                 this.fileName = fldComment.getText();
973                 this.localFile = null;
974             } else {
975                 this.dir = null;
976                 this.fileName = null;
977                 this.localFile = new File(fldComment.getText());
978             }
979
980             if(cbBackLog.isSelected()) {
981                 try {
982                     this.backLogPoint = WayBackTimeParser.parse(fldBackLog.getText());
983                 } catch (IOException ex) {
984                     throw new IllegalArgumentException("過去ログ時刻指定が誤っています。", ex);
985                 }
986             } else {
987                 this.backLogPoint = -1L;
988             }
989
990             this.disablePerMinComment = cbBackLogReduce.isSelected();
991         }
992
993         @Override
994         public boolean isDownload() {
995             return this.download;
996         }
997
998         @Override
999         public File getDir() {
1000             return this.dir;
1001         }
1002
1003         @Override
1004         public String getFileName() {
1005             return this.fileName;
1006         }
1007
1008         @Override
1009         public File getLocalFile() {
1010             return this.localFile;
1011         }
1012
1013         @Override
1014         public boolean isDisablePerMinComment() {
1015             return this.disablePerMinComment;
1016         }
1017
1018         @Override
1019         public long getBackLogPoint() {
1020             return this.backLogPoint;
1021         }
1022
1023         @Override
1024         public String toString(){
1025             return ToStringBuilder.reflectionToString(this);
1026         }
1027     }
1028
1029     /*
1030      * ここからConvertProfile作成用クラスの定義
1031      */
1032     private class InqubusConvertProfile extends ConfigConvertProfile {
1033         private final OutputProfile outputProfile;
1034         private final GeneralProfile generalProfile;
1035         private final FfmpegProfile ffmpegProfile;
1036         private final boolean convert;
1037
1038         private InqubusConvertProfile() throws IOException {
1039             final Config p = Config.INSTANCE;
1040             this.outputProfile = new InqubusOutputProfile();
1041             this.generalProfile = new ConfigGeneralProfile();
1042
1043             final File file = pnlInputFfmpeg.mdlFfmpegOption.getSelectedFile();
1044             if (file != null) {
1045                 this.ffmpegProfile = new ConfigFfmpegProfile();
1046             } else {
1047                 this.ffmpegProfile = new InqubusFfmpegProfile();
1048             }
1049
1050             this.convert = cbOutputEnable.isSelected();
1051         }
1052
1053         @Override
1054         public OutputProfile getOutputProfile() {
1055             return this.outputProfile;
1056         }
1057
1058         @Override
1059         public GeneralProfile getGeneralProfile() {
1060             return this.generalProfile;
1061         }
1062
1063         @Override
1064         public FfmpegProfile getFfmpegOption() {
1065             return this.ffmpegProfile;
1066         }
1067
1068         @Override
1069         public boolean isConvert() {
1070             return this.convert;
1071         }
1072
1073         @Override
1074         public String toString(){
1075             return ToStringBuilder.reflectionToString(this);
1076         }
1077     }
1078
1079     private class InqubusOutputProfile extends ConfigOutputProfile {
1080         private final String fileName;
1081         private final String videoId;
1082         private final String title;
1083
1084
1085         private InqubusOutputProfile() {
1086             final Config p = Config.INSTANCE;
1087             this.fileName = fldOutput.getText();
1088             // TODO この時点でのID/Titleはどうするか…
1089             this.videoId = "";
1090             this.title = "";
1091         }
1092
1093         @Override
1094         public String getFileName() {
1095             return this.fileName;
1096         }
1097
1098         @Override
1099         public String getVideoId() {
1100             return this.videoId;
1101         }
1102
1103         @Override
1104         public String getTitile() {
1105             return this.title;
1106         }
1107
1108         @Override
1109         public String toString(){
1110             return ToStringBuilder.reflectionToString(this);
1111         }
1112     }
1113
1114     private class InqubusFfmpegProfile implements FfmpegProfile {
1115         private final String extOption;
1116         private final String inOption;
1117         private final String mainOption;
1118         private final String outOption;
1119         private final String avOption;
1120         private final boolean resize;
1121         private final int resizeWidth;
1122         private final int resizeHeight;
1123         private final boolean adjustRatio;
1124
1125         private InqubusFfmpegProfile() throws IOException {
1126             this.extOption = pnlInputFfmpeg.fldFfmpegOptionExtension.getText();
1127             this.inOption = pnlInputFfmpeg.fldFfmpegOptionIn.getText();
1128             this.mainOption = pnlInputFfmpeg.fldFfmpegOptionMain.getText();
1129             this.outOption = pnlInputFfmpeg.fldFfmpegOptionOut.getText();
1130             this.avOption = pnlInputFfmpeg.fldFfmpegOptionAv.getText();
1131             this.resize = pnlInputFfmpeg.cbFfmpegOptionResize.isSelected();
1132             this.resizeWidth = Integer.parseInt(pnlInputFfmpeg.fldFfmpegOptionResizeWidth.getText());
1133             this.resizeHeight = Integer.parseInt(pnlInputFfmpeg.fldFfmpegOptionResizeHeight.getText());
1134             this.adjustRatio = pnlInputFfmpeg.cbFfmpegOptionKeepAspect.isSelected();
1135         }
1136
1137         @Override
1138         public String getExtOption() {
1139             return this.extOption;
1140         }
1141
1142         @Override
1143         public String getInOption() {
1144             return this.inOption;
1145         }
1146
1147         @Override
1148         public String getMainOption() {
1149             return this.mainOption;
1150         }
1151
1152         @Override
1153         public String getOutOption() {
1154             return this.outOption;
1155         }
1156
1157         @Override
1158         public String getAvfilterOption() {
1159             return this.avOption;
1160         }
1161
1162         @Override
1163         public boolean isResize() {
1164             return this.resize;
1165         }
1166
1167         @Override
1168         public int getResizeWidth() {
1169             return this.resizeWidth;
1170         }
1171
1172         @Override
1173         public int getResizeHeight() {
1174             return this.resizeHeight;
1175         }
1176
1177         @Override
1178         public boolean isAdjustRatio() {
1179             return this.adjustRatio;
1180         }
1181
1182         @Override
1183         public String toString(){
1184             return ToStringBuilder.reflectionToString(this);
1185         }
1186     }
1187 }