OSDN Git Service

add check box "owner comment only"
[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                     .addComponent(cbVideoLocal)
343                     .addComponent(cbCommentLocal)
344                     .addComponent(cbOutputEnable)
345                 )
346                 .addPreferredGap(ComponentPlacement.RELATED)
347                 .addGroup(glInputMain.createParallelGroup(Alignment.LEADING)
348                     .addComponent(cmbVideo, 300, 300, Short.MAX_VALUE)
349                     .addComponent(cmbComment, 300, 300, Short.MAX_VALUE)
350                     .addComponent(fldOutput, 300, 300, Short.MAX_VALUE)
351                 )
352                 .addGroup(glInputMain.createParallelGroup()
353                     .addComponent(btnVideo)
354                     .addComponent(btnComment)
355                 )
356                 .addContainerGap()
357             )
358             .addGroup(glInputMain.createSequentialGroup()
359                 .addGap(100)
360                 .addComponent(cbOwnerComment)
361                 .addPreferredGap(ComponentPlacement.UNRELATED)
362                 .addComponent(cbBackLogReduce)
363                 .addPreferredGap(ComponentPlacement.UNRELATED)
364                 .addComponent(cbBackLog)
365                 .addPreferredGap(ComponentPlacement.RELATED)
366                 .addComponent(fldBackLog, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
367             )
368         );
369
370         glInputMain.setVerticalGroup(
371             glInputMain.createParallelGroup(Alignment.LEADING)
372             .addGroup(glInputMain.createSequentialGroup()
373                 .addContainerGap()
374                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
375                     .addComponent(cmbId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
376                     .addComponent(lblId)
377                 )
378                 .addPreferredGap(ComponentPlacement.RELATED)
379                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
380                     .addComponent(lblVideo)
381                     .addComponent(cbVideoLocal)
382                     .addComponent(cmbVideo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
383                     .addComponent(btnVideo)
384                 )
385                 .addPreferredGap(ComponentPlacement.RELATED)
386                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
387                     .addComponent(lblComment)
388                     .addComponent(cbCommentLocal)
389                     .addComponent(cmbComment, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
390                     .addComponent(btnComment)
391                 )
392                 .addPreferredGap(ComponentPlacement.RELATED)
393                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
394                     .addComponent(cbOwnerComment)
395                     .addComponent(cbBackLogReduce)
396                     .addComponent(cbBackLog)
397                     .addComponent(fldBackLog)
398                 )
399                 .addPreferredGap(ComponentPlacement.RELATED)
400                 .addGroup(glInputMain.createParallelGroup(Alignment.BASELINE)
401                     .addComponent(lblOutput)
402                     .addComponent(fldOutput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
403                     .addComponent(cbOutputEnable)
404                 )
405             )
406         );
407
408         // ffmpeg入力パネル
409         pnlInputFfmpeg.fldFfmpegOptionResizeWidth.setEnabled(false);
410         pnlInputFfmpeg.fldFfmpegOptionResizeHeight.setEnabled(false);
411         pnlInputFfmpeg.cbFfmpegOptionKeepAspect.setEnabled(false);
412         pnlInputFfmpeg.cmbFfmpegOptionFile.addActionListener(new ActionListener() {
413
414             @Override
415             public void actionPerformed(ActionEvent e) {
416                 final boolean notFile = !pnlInputFfmpeg.mdlFfmpegOption.isFile();
417                 pnlInputFfmpeg.fldFfmpegOptionExtension.setEnabled(notFile);
418                 pnlInputFfmpeg.fldFfmpegOptionMain.setEnabled(notFile);
419                 pnlInputFfmpeg.fldFfmpegOptionIn.setEnabled(notFile);
420                 pnlInputFfmpeg.fldFfmpegOptionOut.setEnabled(notFile);
421                 pnlInputFfmpeg.fldFfmpegOptionAv.setEnabled(notFile);
422                 pnlInputFfmpeg.cbFfmpegOptionResize.setEnabled(notFile);
423             }
424         });
425         pnlInputFfmpeg.cbFfmpegOptionResize.addItemListener(new ItemListener() {
426
427             @Override
428             public void itemStateChanged(ItemEvent e) {
429                 final boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
430                 pnlInputFfmpeg.fldFfmpegOptionResizeWidth.setEnabled(selected);
431                 pnlInputFfmpeg.fldFfmpegOptionResizeHeight.setEnabled(selected);
432                 pnlInputFfmpeg.cbFfmpegOptionKeepAspect.setEnabled(selected);
433             }
434         });
435         pnlInputFfmpeg.cbFfmpegOptionResize.addPropertyChangeListener("enabled", new PropertyChangeListener() {
436
437             @Override
438             public void propertyChange(PropertyChangeEvent evt) {
439                 final boolean enabled = ((Boolean) evt.getNewValue()).booleanValue();
440                 final boolean fldEnabled = enabled ? pnlInputFfmpeg.cbFfmpegOptionResize.isSelected() : false;
441                 pnlInputFfmpeg.fldFfmpegOptionResizeWidth.setEnabled(fldEnabled);
442                 pnlInputFfmpeg.fldFfmpegOptionResizeHeight.setEnabled(fldEnabled);
443                 pnlInputFfmpeg.cbFfmpegOptionKeepAspect.setEnabled(fldEnabled);
444             }
445         });
446
447
448         tbpInput.add("メイン", pnlInputMain);
449         tbpInput.add("ffmpeg", pnlInputFfmpeg);
450
451         // 入力部のボタンやメッセージ表示部
452         fldInputMessage.setEditable(false);
453         fldInputMessage.setEnabled(false);
454         fldInputMessage.setBorder(BorderFactory.createEmptyBorder());
455
456         final JPanel pnlInputButton = new JPanel();
457         final GroupLayout glInputButton = new GroupLayout(pnlInputButton);
458         pnlInputButton.setLayout(glInputButton);
459         glInputButton.setHorizontalGroup(glInputButton.createSequentialGroup()
460             .addContainerGap()
461             .addComponent(fldInputMessage, GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
462             .addPreferredGap(ComponentPlacement.UNRELATED)
463             .addComponent(btnClear)
464             .addPreferredGap(ComponentPlacement.UNRELATED)
465             .addComponent(btnApply)
466             .addContainerGap()
467         );
468         glInputButton.setVerticalGroup(glInputButton.createSequentialGroup()
469             .addContainerGap()
470             .addGroup(glInputButton.createParallelGroup(Alignment.BASELINE)
471                 .addComponent(fldInputMessage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
472                 .addComponent(btnClear)
473                 .addComponent(btnApply)
474             )
475             .addContainerGap()
476         );
477
478         // 画面下半分の入力部分
479         final JPanel pnlInputAll = new JPanel();
480         pnlInputAll.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
481         final GroupLayout glInputAll = new GroupLayout(pnlInputAll);
482         pnlInputAll.setLayout(glInputAll);
483         glInputAll.setHorizontalGroup(glInputAll.createParallelGroup()
484             .addComponent(tbpInput)
485             .addComponent(pnlInputButton)
486         );
487         glInputAll.setVerticalGroup(glInputAll.createSequentialGroup()
488             .addComponent(tbpInput)
489             .addComponent(pnlInputButton)
490         );
491
492         GroupLayout gl_pnlMain = new GroupLayout(pnlMain);
493         pnlMain.setLayout(gl_pnlMain);
494         gl_pnlMain.setHorizontalGroup(
495             gl_pnlMain.createParallelGroup(Alignment.LEADING)
496             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
497                 .addContainerGap()
498                 .addGroup(gl_pnlMain.createParallelGroup(Alignment.TRAILING)
499                     .addComponent(scrDisplay, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
500                     .addComponent(pnlButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
501                     .addComponent(pnlInputAll, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
502                 )
503                 .addContainerGap())
504         );
505         gl_pnlMain.setVerticalGroup(
506             gl_pnlMain.createParallelGroup(Alignment.LEADING)
507             .addGroup(Alignment.TRAILING, gl_pnlMain.createSequentialGroup()
508                 .addContainerGap()
509                 .addComponent(scrDisplay, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
510                 .addPreferredGap(ComponentPlacement.RELATED)
511                 .addComponent(pnlButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
512                 .addPreferredGap(ComponentPlacement.RELATED)
513                 .addComponent(pnlInputAll, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
514                 .addContainerGap()
515             )
516         );
517
518
519         JMenuBar menuBar = initMenuBar();
520         setJMenuBar(menuBar);
521
522         GroupLayout layout = new GroupLayout(getContentPane());
523         getContentPane().setLayout(layout);
524         layout.setHorizontalGroup(
525             layout.createParallelGroup(Alignment.LEADING)
526             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
527         );
528         layout.setVerticalGroup(
529             layout.createParallelGroup(Alignment.LEADING)
530             .addComponent(pnlMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
531         );
532
533         pack();
534         setMinimumSize(getSize());
535
536         /*
537          * 画面のサイズや位置を前回終了時のものに設定する
538          */
539         final int windowWidth = p.getSystemWindowWidth();
540         final int windowHeight = p.getSystemWindowHeight();
541         if (windowWidth > 0 && windowHeight > 0) {
542             setSize(windowWidth, windowHeight);
543         }
544
545         final int windowPosX = p.getSystemWindowPosX();
546         final int windowPosY = p.getSystemWindowPosY();
547         final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
548         if (windowPosX + windowWidth > 0 && windowPosX < screenSize.width
549                 && windowPosY + windowHeight > 0 && windowPosY < screenSize.height) {
550             setLocation(windowPosX, windowPosY);
551         } else {
552             setLocationByPlatform(true);
553         }
554
555         final int colId = p.getSystemColumnId();
556         if(colId > 0) {
557             tblDisplay.getColumnModel().getColumn(0).setPreferredWidth(colId);
558         }
559         final int colStatus = p.getSystemColumnStatus();
560         if(colStatus > 0) {
561             tblDisplay.getColumnModel().getColumn(4).setPreferredWidth(colStatus);
562         }
563
564         initInputPanel();
565     }
566
567     public void startWatcher() {
568         videoFileWatcherThread.start();
569         commentFileWatcherThread.start();
570     }
571
572     private static void createFieldInfo( FileComboBox combo,  boolean useLocal,  String text, String pattern,  Set<Path> allFiles) {
573         if (useLocal) {
574             final SortedSet<String> matchFiles = FileWatchUtil.getFileNamesContain(allFiles, text);
575             DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(matchFiles.toArray(new String[0]));
576             combo.setModel(model);
577         } else {
578             combo.setModel(new DefaultComboBoxModel<String>());
579             combo.getEditorComponent().setText(pattern);
580         }
581     }
582
583     private class GuiTaskManageListener implements TaskManageListener {
584
585         @Override
586         public void process(final int id, final TaskKind kind, final TaskStatus status, final double percentage,
587                 final String message) {
588             SwingUtilities.invokeLater(new Runnable() {
589
590                 @Override
591                 public void run() {
592                     targetModel.setStatus(id, kind, status, percentage, message);
593                 }
594             });
595         }
596     }
597
598     private class StopActionListener implements ActionListener {
599
600         @Override
601         public void actionPerformed(ActionEvent e) {
602             final int row = tblDisplay.getSelectedRow();
603             final Target t = targetModel.getTarget(row);
604             final boolean res = taskManager.cancel(t.getRowId());
605             logger.debug("停止: {} {}", t.getVideoId(), res);
606             if (res) {
607                 targetModel.setStatus(t.getRowId(), null, TaskStatus.CANCELLED, -1.0, "キャンセル");
608             }
609         }
610     }
611
612     private class ApplyActionListener implements ActionListener {
613
614         @Override
615         public void actionPerformed(ActionEvent e) {
616             try {
617                 final DownloadProfile downProf = new InqubusDownloadProfile();
618                 final String id = Util.getVideoId(cmbId.getText());
619                 final InqubusConvertProfile convProf = new InqubusConvertProfile();
620                 logger.debug(downProf.toString());
621                 logger.debug(convProf.toString());
622
623                 final File tempDir = new File(Config.INSTANCE.getSystemTempDir());
624                 thumbRepository.request(downProf.getProxyProfile(), tempDir, id);
625
626                 final RequestProcess rp = new RequestProcess(downProf, id, convProf);
627                 taskManager.add(rp);
628                 targetModel.addTarget(new Target(rp));
629                 initInputPanel();
630             } catch (Throwable th) {
631                 logger.error(null, th);
632                 JOptionPane.showMessageDialog(MainFrame.this, th.getMessage(), "中断しました", JOptionPane.ERROR_MESSAGE);
633             }
634         }
635     }
636
637     /**
638      * 動画, コメントの"local"チェックボックス更新時の処理.
639      */
640     private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
641         final Config p = Config.INSTANCE;
642
643         final ItemSelectable source = evt.getItemSelectable();
644
645         JButton button;
646         FileComboBox combo;
647         Set<Path> allFiles;
648         String pattern;
649         if (source == cbVideoLocal) {
650             button = btnVideo;
651             combo = cmbVideo;
652             allFiles = videoFileWatcher.getFiles();
653             pattern = p.getVideoFileNamePattern();
654         } else {
655             button = btnComment;
656             combo = cmbComment;
657             allFiles = commentFileWatcher.getFiles();
658             pattern = p.getCommentFileNamePattern();
659         }
660
661         final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
662
663         button.setEnabled(useLocal);
664         createFieldInfo(combo, useLocal, cmbId.getText(), pattern, allFiles);
665
666     }
667
668     private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
669         final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
670         fldOutput.setEnabled(convert);
671     }//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
672
673     private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
674         final Config p = Config.INSTANCE;
675         final String id = cmbId.getText();
676
677         createFieldInfo(cmbVideo, cbVideoLocal.isSelected(), id, p.getVideoFileNamePattern(), videoFileWatcher.getFiles());
678         createFieldInfo(cmbComment, cbCommentLocal.isSelected(), id, p.getCommentFileNamePattern(), commentFileWatcher.getFiles());
679     }//GEN-LAST:event_idFieldFocusLost
680     // Variables declaration - do not modify//GEN-BEGIN:variables
681     private final JTable tblDisplay;
682     // ボタン領域
683     private final JButton btnStop = new JButton("停止");
684     // 入力領域
685     private final JTabbedPane tbpInput = new JTabbedPane(JTabbedPane.BOTTOM);
686     // 入力領域 - メイン
687     private final IdComboBox cmbId;
688     private final JCheckBox cbBackLogReduce = new JCheckBox("少コメ");
689     private final JCheckBox cbBackLog = new JCheckBox("過去ログ");
690     private final JTextField fldBackLog = new JTextField();
691     private final JCheckBox cbVideoLocal;
692     private final FileComboBox cmbVideo = new FileComboBox();
693     private final JTextField fldVideo = cmbVideo.getEditorComponent();
694     private final JButton btnVideo = new JButton("...");
695     private final JCheckBox cbCommentLocal;
696     private final FileComboBox cmbComment = new FileComboBox();
697     private final JTextField fldComment = cmbComment.getEditorComponent();
698     private final JButton btnComment = new JButton("...");
699     private final JCheckBox cbOwnerComment;
700     private final JCheckBox cbOutputEnable;
701     private final JTextField fldOutput;
702     // 入力領域 - ffmpeg
703     private final FfmpegParamPanel pnlInputFfmpeg = new FfmpegParamPanel();
704     // 適用
705     private final JTextField fldInputMessage = new JTextField();
706     private final JButton btnClear = new JButton("クリア");
707     private final JButton btnApply = new JButton("適用");
708     // End of variables declaration//GEN-END:variables
709
710     private void initInputPanel() {
711         initMainTab();
712         initFfmpegTab();
713         tbpInput.setSelectedIndex(0);
714         cmbId.requestFocus();
715     }
716
717     private void initMainTab() {
718         final Config p = Config.INSTANCE;
719
720         cmbId.setText("");
721         cbBackLogReduce.setSelected(p.getCommentMinDisabled());
722         cbBackLog.setEnabled(true);
723         cbBackLog.setSelected(false);
724         fldBackLog.setEnabled(false);
725         fldBackLog.setText("");
726
727         final boolean videoLocal = p.getVideoUseLocal();
728         cbVideoLocal.setSelected(videoLocal);
729         if (!videoLocal) {
730             fldVideo.setText(p.getVideoFileNamePattern());
731         }
732         btnVideo.setEnabled(videoLocal);
733
734         final boolean commentLocal = p.getCommentUseLocal();
735         cbCommentLocal.setSelected(commentLocal);
736         if (!commentLocal) {
737             fldComment.setText(p.getCommentFileNamePattern());
738         }
739         btnComment.setEnabled(commentLocal);
740
741         final boolean convert = p.getOutputEnable();
742         cbOutputEnable.setSelected(convert);
743         fldOutput.setEnabled(convert);
744         fldOutput.setText(p.getOutputFileNamePattern());
745     }
746
747     private void initFfmpegTab() {
748         pnlInputFfmpeg.init(Config.INSTANCE);
749     }
750
751     private JMenuBar initMenuBar() {
752         final JMenuBar menuBar = new JMenuBar();
753
754         final JMenu mnFile = new JMenu("ファイル(F)");
755         menuBar.add(mnFile);
756
757         final JMenuItem itExit = new JMenuItem("終了(X)", KeyEvent.VK_X);
758         itExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
759         final ActionListener exitActionListener = new ActionListener() {
760
761             @Override
762             public void actionPerformed(ActionEvent e) {
763                 processWindowEvent(new WindowEvent(MainFrame.this, WindowEvent.WINDOW_CLOSING));
764             }
765         };
766         itExit.addActionListener(exitActionListener);
767         mnFile.add(itExit);
768
769         final JMenu mnTool = new JMenu("ツール(T)");
770         menuBar.add(mnTool);
771
772         final JMenuItem itOption = new JMenuItem("オプション(O)...", KeyEvent.VK_O);
773         itOption.addActionListener(new ActionListener() {
774
775             @Override
776             public void actionPerformed(ActionEvent e) {
777                 final yukihane.inqubus.gui.ConfigDialog dlg = new yukihane.inqubus.gui.ConfigDialog(MainFrame.this);
778                 dlg.setLocationRelativeTo(MainFrame.this);
779                 dlg.setModal(true);
780                 dlg.setVisible(true);
781             }
782         });
783         mnTool.add(itOption);
784
785         final JMenu mnHelp = new JMenu("ヘルプ(H)");
786         menuBar.add(mnHelp);
787
788         final JMenuItem itAbout = new JMenuItem("このソフトウェアについて(A)...", KeyEvent.VK_A);
789         itAbout.addActionListener(new ActionListener() {
790
791             @Override
792             public void actionPerformed(ActionEvent e) {
793                 MainFrame_AboutBox dlg = new MainFrame_AboutBox(MainFrame.this);
794                 dlg.pack();
795                 dlg.setLocationRelativeTo(MainFrame.this);
796                 dlg.setModal(true);
797                 dlg.setVisible(true);
798             }
799         });
800         mnHelp.add(itAbout);
801
802         return menuBar;
803     }
804
805     private class MainFrameWindowListener extends WindowAdapter {
806         @Override
807         public void windowClosing(WindowEvent e) {
808             final Config p = Config.INSTANCE;
809
810             // 保存するのは最大化していない場合だけ
811             if (JFrame.NORMAL == getExtendedState()) {
812                 final Dimension size = getSize();
813                 p.setSystemWindowWidth(size.width);
814                 p.setSystemWindowHeight(size.height);
815
816                 final Point pos = getLocation();
817                 p.setSystemWindowPosX(pos.x);
818                 p.setSystemWindowPosY(pos.y);
819             }
820
821             p.setSystemColumnId(tblDisplay.getColumnModel().getColumn(0).getWidth());
822             p.setSystemColumnVideo(tblDisplay.getColumnModel().getColumn(1).getWidth());
823             p.setSystemColumnComment(tblDisplay.getColumnModel().getColumn(2).getWidth());
824             p.setSystemColumnConvert(tblDisplay.getColumnModel().getColumn(3).getWidth());
825             p.setSystemColumnStatus(tblDisplay.getColumnModel().getColumn(4).getWidth());
826             try {
827                 p.save();
828             } catch (ConfigurationException ex) {
829                 logger.error("コンフィグ保存失敗", ex);
830             }
831         }
832     }
833
834     /*
835      * ここからDownloadProfile作成用クラスの定義
836      */
837     private class InqubusDownloadProfile implements DownloadProfile {
838
839         private final LoginProfile loginProfile;
840         private final ProxyProfile proxyProfile;
841         private final VideoProfile videoProfile;
842         private final CommentProfile commentProfile;
843         private final GeneralProfile generalProfile;
844
845         private InqubusDownloadProfile() {
846             this.loginProfile = new ConfigLoginProfile();
847             this.proxyProfile = new ConfigProxyProfile();
848             this.videoProfile = new InqubusVideoProfile();
849             this.commentProfile = new InqubusCommentProfile();
850             this.generalProfile = new ConfigGeneralProfile();
851         }
852
853         @Override
854         public LoginProfile getLoginProfile() {
855             return this.loginProfile;
856         }
857
858         @Override
859         public ProxyProfile getProxyProfile() {
860             return this.proxyProfile;
861         }
862
863         @Override
864         public VideoProfile getVideoProfile() {
865             return this.videoProfile;
866         }
867
868         @Override
869         public CommentProfile getCommentProfile() {
870             return this.commentProfile;
871         }
872
873         @Override
874         public GeneralProfile getGeneralProfile() {
875             return this.generalProfile;
876         }
877
878         @Override
879         public String toString(){
880             return ToStringBuilder.reflectionToString(this);
881         }
882     }
883
884     private class InqubusVideoProfile implements VideoProfile {
885         private final boolean download;
886         private final File dir;
887         private final String fileName;
888         private final File localFile;
889
890         private InqubusVideoProfile(){
891             final Config p = Config.INSTANCE;
892             this.download = !cbVideoLocal.isSelected();
893             if (this.download) {
894                 this.dir = new File(p.getVideoDir());
895                 this.fileName = fldVideo.getText();
896                 this.localFile = null;
897             } else {
898                 this.dir = null;
899                 this.fileName = null;
900                 this.localFile = new File(fldVideo.getText());
901             }
902         }
903
904         @Override
905         public boolean isDownload() {
906             return this.download;
907         }
908
909         @Override
910         public File getDir() {
911             return this.dir;
912         }
913
914         @Override
915         public String getFileName() {
916             return this.fileName;
917         }
918
919         @Override
920         public File getLocalFile() {
921             return this.localFile;
922         }
923
924         @Override
925         public String toString(){
926             return ToStringBuilder.reflectionToString(this);
927         }
928     }
929
930     private class InqubusCommentProfile extends ConfigCommentProfile {
931         private final boolean download;
932         private final File dir;
933         private final String fileName;
934         private final File localFile;
935         private final boolean disablePerMinComment;
936         private final long backLogPoint;
937
938         private InqubusCommentProfile() {
939             super();
940
941             final Config p = Config.INSTANCE;
942             this.download = !cbCommentLocal.isSelected();
943             if (this.download) {
944                 this.dir = new File(p.getCommentDir());
945                 this.fileName = fldComment.getText();
946                 this.localFile = null;
947             } else {
948                 this.dir = null;
949                 this.fileName = null;
950                 this.localFile = new File(fldComment.getText());
951             }
952
953             if(cbBackLog.isSelected()) {
954                 try {
955                     this.backLogPoint = WayBackTimeParser.parse(fldBackLog.getText());
956                 } catch (IOException ex) {
957                     throw new IllegalArgumentException("過去ログ時刻指定が誤っています。", ex);
958                 }
959             } else {
960                 this.backLogPoint = -1L;
961             }
962
963             this.disablePerMinComment = cbBackLogReduce.isSelected();
964         }
965
966         @Override
967         public boolean isDownload() {
968             return this.download;
969         }
970
971         @Override
972         public File getDir() {
973             return this.dir;
974         }
975
976         @Override
977         public String getFileName() {
978             return this.fileName;
979         }
980
981         @Override
982         public File getLocalFile() {
983             return this.localFile;
984         }
985
986         @Override
987         public boolean isDisablePerMinComment() {
988             return this.disablePerMinComment;
989         }
990
991         @Override
992         public long getBackLogPoint() {
993             return this.backLogPoint;
994         }
995
996         @Override
997         public String toString(){
998             return ToStringBuilder.reflectionToString(this);
999         }
1000     }
1001
1002     /*
1003      * ここからConvertProfile作成用クラスの定義
1004      */
1005     private class InqubusConvertProfile extends ConfigConvertProfile {
1006         private final OutputProfile outputProfile;
1007         private final GeneralProfile generalProfile;
1008         private final FfmpegProfile ffmpegProfile;
1009         private final boolean convert;
1010
1011         private InqubusConvertProfile() throws IOException {
1012             this.outputProfile = new InqubusOutputProfile();
1013             this.generalProfile = new ConfigGeneralProfile();
1014
1015             final File file = pnlInputFfmpeg.mdlFfmpegOption.getSelectedFile();
1016             if (file != null) {
1017                 this.ffmpegProfile = new ConfigFfmpegProfile();
1018             } else {
1019                 this.ffmpegProfile = new InqubusFfmpegProfile();
1020             }
1021
1022             this.convert = cbOutputEnable.isSelected();
1023         }
1024
1025         @Override
1026         public OutputProfile getOutputProfile() {
1027             return this.outputProfile;
1028         }
1029
1030         @Override
1031         public GeneralProfile getGeneralProfile() {
1032             return this.generalProfile;
1033         }
1034
1035         @Override
1036         public FfmpegProfile getFfmpegOption() {
1037             return this.ffmpegProfile;
1038         }
1039
1040         @Override
1041         public boolean isConvert() {
1042             return this.convert;
1043         }
1044
1045         @Override
1046         public String toString(){
1047             return ToStringBuilder.reflectionToString(this);
1048         }
1049     }
1050
1051     private class InqubusOutputProfile extends ConfigOutputProfile {
1052         private final String fileName;
1053         private final String videoId;
1054         private final String title;
1055
1056
1057         private InqubusOutputProfile() {
1058             this.fileName = fldOutput.getText();
1059             // TODO この時点でのID/Titleはどうするか…
1060             this.videoId = "";
1061             this.title = "";
1062         }
1063
1064         @Override
1065         public String getFileName() {
1066             return this.fileName;
1067         }
1068
1069         @Override
1070         public String getVideoId() {
1071             return this.videoId;
1072         }
1073
1074         @Override
1075         public String getTitile() {
1076             return this.title;
1077         }
1078
1079         @Override
1080         public String toString(){
1081             return ToStringBuilder.reflectionToString(this);
1082         }
1083     }
1084
1085     private class InqubusFfmpegProfile implements FfmpegProfile {
1086         private final String extOption;
1087         private final String inOption;
1088         private final String mainOption;
1089         private final String outOption;
1090         private final String avOption;
1091         private final boolean resize;
1092         private final int resizeWidth;
1093         private final int resizeHeight;
1094         private final boolean adjustRatio;
1095
1096         private InqubusFfmpegProfile() throws IOException {
1097             String ext = pnlInputFfmpeg.fldFfmpegOptionExtension.getText();
1098             if (!ext.startsWith(".")) {
1099                 ext = "." + ext;
1100             }
1101             this.extOption = ext;
1102             this.inOption = pnlInputFfmpeg.fldFfmpegOptionIn.getText();
1103             this.mainOption = pnlInputFfmpeg.fldFfmpegOptionMain.getText();
1104             this.outOption = pnlInputFfmpeg.fldFfmpegOptionOut.getText();
1105             this.avOption = pnlInputFfmpeg.fldFfmpegOptionAv.getText();
1106             this.resize = pnlInputFfmpeg.cbFfmpegOptionResize.isSelected();
1107             this.resizeWidth = Integer.parseInt(pnlInputFfmpeg.fldFfmpegOptionResizeWidth.getText());
1108             this.resizeHeight = Integer.parseInt(pnlInputFfmpeg.fldFfmpegOptionResizeHeight.getText());
1109             this.adjustRatio = pnlInputFfmpeg.cbFfmpegOptionKeepAspect.isSelected();
1110         }
1111
1112         @Override
1113         public String getExtOption() {
1114             return this.extOption;
1115         }
1116
1117         @Override
1118         public String getInOption() {
1119             return this.inOption;
1120         }
1121
1122         @Override
1123         public String getMainOption() {
1124             return this.mainOption;
1125         }
1126
1127         @Override
1128         public String getOutOption() {
1129             return this.outOption;
1130         }
1131
1132         @Override
1133         public String getAvfilterOption() {
1134             return this.avOption;
1135         }
1136
1137         @Override
1138         public boolean isResize() {
1139             return this.resize;
1140         }
1141
1142         @Override
1143         public int getResizeWidth() {
1144             return this.resizeWidth;
1145         }
1146
1147         @Override
1148         public int getResizeHeight() {
1149             return this.resizeHeight;
1150         }
1151
1152         @Override
1153         public boolean isAdjustRatio() {
1154             return this.adjustRatio;
1155         }
1156
1157         @Override
1158         public String toString(){
1159             return ToStringBuilder.reflectionToString(this);
1160         }
1161     }
1162 }