OSDN Git Service

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