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