OSDN Git Service

4f2f3173939f83b3fa82709aa791f847e9253e88
[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                 taskManager.add(rp);
631                 targetModel.addTarget(new Target(rp));
632                 initInputPanel();
633             } catch (Throwable th) {
634                 logger.error(null, th);
635                 JOptionPane.showMessageDialog(MainFrame.this, th.getMessage(), "中断しました", JOptionPane.ERROR_MESSAGE);
636             }
637         }
638     }
639
640     /**
641      * 動画, コメントの"local"チェックボックス更新時の処理.
642      */
643     private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
644         final Config p = Config.INSTANCE;
645
646         final ItemSelectable source = evt.getItemSelectable();
647
648         JButton button;
649         FileComboBox combo;
650         Set<Path> allFiles;
651         String pattern;
652         if (source == cbVideoLocal) {
653             button = btnVideo;
654             combo = cmbVideo;
655             allFiles = videoFileWatcher.getFiles();
656             pattern = p.getVideoFileNamePattern();
657         } else {
658             button = btnComment;
659             combo = cmbComment;
660             allFiles = commentFileWatcher.getFiles();
661             pattern = p.getCommentFileNamePattern();
662         }
663
664         final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
665
666         button.setEnabled(useLocal);
667         createFieldInfo(combo, useLocal, cmbId.getText(), pattern, allFiles);
668
669     }
670
671     private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
672         final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
673         fldOutput.setEnabled(convert);
674     }//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
675
676     private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
677         final Config p = Config.INSTANCE;
678         final String id = cmbId.getText();
679
680         createFieldInfo(cmbVideo, cbVideoLocal.isSelected(), id, p.getVideoFileNamePattern(), videoFileWatcher.getFiles());
681         createFieldInfo(cmbComment, cbCommentLocal.isSelected(), id, p.getCommentFileNamePattern(), commentFileWatcher.getFiles());
682     }//GEN-LAST:event_idFieldFocusLost
683     // Variables declaration - do not modify//GEN-BEGIN:variables
684     private final JTable tblDisplay;
685     // ボタン領域
686     private final JButton btnStop = new JButton("停止");
687     // 入力領域
688     private final JTabbedPane tbpInput = new JTabbedPane(JTabbedPane.BOTTOM);
689     // 入力領域 - メイン
690     private final IdComboBox cmbId;
691     private final JCheckBox cbBackLogReduce = new JCheckBox("少コメ");
692     private final JCheckBox cbBackLog = new JCheckBox("過去ログ");
693     private final JTextField fldBackLog = new JTextField();
694     private final JCheckBox cbVideoLocal;
695     private final FileComboBox cmbVideo = new FileComboBox();
696     private final JTextField fldVideo = cmbVideo.getEditorComponent();
697     private final JButton btnVideo = new JButton("...");
698     private final JCheckBox cbCommentLocal;
699     private final FileComboBox cmbComment = new FileComboBox();
700     private final JTextField fldComment = cmbComment.getEditorComponent();
701     private final JButton btnComment = new JButton("...");
702     private final JCheckBox cbOwnerComment;
703     private final JCheckBox cbOutputEnable;
704     private final JTextField fldOutput;
705     // 入力領域 - ffmpeg
706     private final FfmpegParamPanel pnlInputFfmpeg = new FfmpegParamPanel();
707     // 適用
708     private final JTextField fldInputMessage = new JTextField();
709     private final JButton btnClear = new JButton("クリア");
710     private final JButton btnApply = new JButton("適用");
711     // End of variables declaration//GEN-END:variables
712
713     private void initInputPanel() {
714         initMainTab();
715         initFfmpegTab();
716         tbpInput.setSelectedIndex(0);
717         cmbId.requestFocus();
718     }
719
720     private void initMainTab() {
721         final Config p = Config.INSTANCE;
722
723         cmbId.setText("");
724         cbBackLogReduce.setSelected(p.getCommentMinDisabled());
725         cbBackLog.setEnabled(true);
726         cbBackLog.setSelected(false);
727         fldBackLog.setEnabled(false);
728         fldBackLog.setText("");
729
730         final boolean videoLocal = p.getVideoUseLocal();
731         cbVideoLocal.setSelected(videoLocal);
732         if (!videoLocal) {
733             fldVideo.setText(p.getVideoFileNamePattern());
734         }
735         btnVideo.setEnabled(videoLocal);
736
737         final boolean commentLocal = p.getCommentUseLocal();
738         cbCommentLocal.setSelected(commentLocal);
739         if (!commentLocal) {
740             fldComment.setText(p.getCommentFileNamePattern());
741         }
742         btnComment.setEnabled(commentLocal);
743
744         final boolean convert = p.getOutputEnable();
745         cbOutputEnable.setSelected(convert);
746         fldOutput.setEnabled(convert);
747         fldOutput.setText(p.getOutputFileNamePattern());
748     }
749
750     private void initFfmpegTab() {
751         pnlInputFfmpeg.init(Config.INSTANCE);
752     }
753
754     private JMenuBar initMenuBar() {
755         final JMenuBar menuBar = new JMenuBar();
756
757         final JMenu mnFile = new JMenu("ファイル(F)");
758         menuBar.add(mnFile);
759
760         final JMenuItem itExit = new JMenuItem("終了(X)", KeyEvent.VK_X);
761         itExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
762         final ActionListener exitActionListener = new ActionListener() {
763
764             @Override
765             public void actionPerformed(ActionEvent e) {
766                 processWindowEvent(new WindowEvent(MainFrame.this, WindowEvent.WINDOW_CLOSING));
767             }
768         };
769         itExit.addActionListener(exitActionListener);
770         mnFile.add(itExit);
771
772         final JMenu mnTool = new JMenu("ツール(T)");
773         menuBar.add(mnTool);
774
775         final JMenuItem itOption = new JMenuItem("オプション(O)...", KeyEvent.VK_O);
776         itOption.addActionListener(new ActionListener() {
777
778             @Override
779             public void actionPerformed(ActionEvent e) {
780                 final yukihane.inqubus.gui.ConfigDialog dlg = new yukihane.inqubus.gui.ConfigDialog(MainFrame.this);
781                 dlg.setLocationRelativeTo(MainFrame.this);
782                 dlg.setModal(true);
783                 dlg.setVisible(true);
784             }
785         });
786         mnTool.add(itOption);
787
788         final JMenu mnHelp = new JMenu("ヘルプ(H)");
789         menuBar.add(mnHelp);
790
791         final JMenuItem itAbout = new JMenuItem("このソフトウェアについて(A)...", KeyEvent.VK_A);
792         itAbout.addActionListener(new ActionListener() {
793
794             @Override
795             public void actionPerformed(ActionEvent e) {
796                 MainFrame_AboutBox dlg = new MainFrame_AboutBox(MainFrame.this);
797                 dlg.pack();
798                 dlg.setLocationRelativeTo(MainFrame.this);
799                 dlg.setModal(true);
800                 dlg.setVisible(true);
801             }
802         });
803         mnHelp.add(itAbout);
804
805         return menuBar;
806     }
807
808     private class MainFrameWindowListener extends WindowAdapter {
809         @Override
810         public void windowClosing(WindowEvent e) {
811             final Config p = Config.INSTANCE;
812
813             // 保存するのは最大化していない場合だけ
814             if (JFrame.NORMAL == getExtendedState()) {
815                 final Dimension size = getSize();
816                 p.setSystemWindowWidth(size.width);
817                 p.setSystemWindowHeight(size.height);
818
819                 final Point pos = getLocation();
820                 p.setSystemWindowPosX(pos.x);
821                 p.setSystemWindowPosY(pos.y);
822             }
823
824             p.setSystemColumnId(tblDisplay.getColumnModel().getColumn(0).getWidth());
825             p.setSystemColumnVideo(tblDisplay.getColumnModel().getColumn(1).getWidth());
826             p.setSystemColumnComment(tblDisplay.getColumnModel().getColumn(2).getWidth());
827             p.setSystemColumnConvert(tblDisplay.getColumnModel().getColumn(3).getWidth());
828             p.setSystemColumnStatus(tblDisplay.getColumnModel().getColumn(4).getWidth());
829             try {
830                 p.save();
831             } catch (ConfigurationException ex) {
832                 logger.error("コンフィグ保存失敗", ex);
833             }
834         }
835     }
836
837     /*
838      * ここからDownloadProfile作成用クラスの定義
839      */
840     private class InqubusDownloadProfile implements DownloadProfile {
841
842         private final LoginProfile loginProfile;
843         private final ProxyProfile proxyProfile;
844         private final VideoProfile videoProfile;
845         private final CommentProfile commentProfile;
846         private final GeneralProfile generalProfile;
847
848         private InqubusDownloadProfile() {
849             this.loginProfile = new ConfigLoginProfile();
850             this.proxyProfile = new ConfigProxyProfile();
851             this.videoProfile = new InqubusVideoProfile();
852             this.commentProfile = new InqubusCommentProfile();
853             this.generalProfile = new ConfigGeneralProfile();
854         }
855
856         @Override
857         public LoginProfile getLoginProfile() {
858             return this.loginProfile;
859         }
860
861         @Override
862         public ProxyProfile getProxyProfile() {
863             return this.proxyProfile;
864         }
865
866         @Override
867         public VideoProfile getVideoProfile() {
868             return this.videoProfile;
869         }
870
871         @Override
872         public CommentProfile getCommentProfile() {
873             return this.commentProfile;
874         }
875
876         @Override
877         public GeneralProfile getGeneralProfile() {
878             return this.generalProfile;
879         }
880
881         @Override
882         public String toString(){
883             return ToStringBuilder.reflectionToString(this);
884         }
885     }
886
887     private class InqubusVideoProfile implements VideoProfile {
888         private final boolean download;
889         private final File dir;
890         private final String fileName;
891         private final File localFile;
892
893         private InqubusVideoProfile(){
894             final Config p = Config.INSTANCE;
895             this.download = !cbVideoLocal.isSelected();
896             if (this.download) {
897                 this.dir = new File(p.getVideoDir());
898                 this.fileName = fldVideo.getText();
899                 this.localFile = null;
900             } else {
901                 this.dir = null;
902                 this.fileName = null;
903                 this.localFile = new File(fldVideo.getText());
904             }
905         }
906
907         @Override
908         public boolean isDownload() {
909             return this.download;
910         }
911
912         @Override
913         public File getDir() {
914             return this.dir;
915         }
916
917         @Override
918         public String getFileName() {
919             return this.fileName;
920         }
921
922         @Override
923         public File getLocalFile() {
924             return this.localFile;
925         }
926
927         @Override
928         public String toString(){
929             return ToStringBuilder.reflectionToString(this);
930         }
931     }
932
933     private class InqubusCommentProfile extends ConfigCommentProfile {
934         private final boolean download;
935         private final boolean ownerCommentOnly;
936         private final File dir;
937         private final String fileName;
938         private final File localFile;
939         private final boolean disablePerMinComment;
940         private final long backLogPoint;
941
942         private InqubusCommentProfile() {
943             super();
944
945             final Config p = Config.INSTANCE;
946             this.download = !cbCommentLocal.isSelected();
947             if (this.download) {
948                 this.dir = new File(p.getCommentDir());
949                 this.fileName = fldComment.getText();
950                 this.localFile = null;
951                 this.ownerCommentOnly = cbOwnerComment.isSelected();
952             } else {
953                 this.dir = null;
954                 this.fileName = null;
955                 this.localFile = new File(fldComment.getText());
956                 this.ownerCommentOnly = false;
957             }
958
959             if(cbBackLog.isSelected()) {
960                 try {
961                     this.backLogPoint = WayBackTimeParser.parse(fldBackLog.getText());
962                 } catch (IOException ex) {
963                     throw new IllegalArgumentException("過去ログ時刻指定が誤っています。", ex);
964                 }
965             } else {
966                 this.backLogPoint = -1L;
967             }
968
969             this.disablePerMinComment = cbBackLogReduce.isSelected();
970         }
971
972         @Override
973         public boolean isDownload() {
974             return this.download;
975         }
976
977         @Override
978         public boolean isOwnerCommentOnly(){
979             return this.ownerCommentOnly;
980         }
981
982         @Override
983         public File getDir() {
984             return this.dir;
985         }
986
987         @Override
988         public String getFileName() {
989             return this.fileName;
990         }
991
992         @Override
993         public File getLocalFile() {
994             return this.localFile;
995         }
996
997         @Override
998         public boolean isDisablePerMinComment() {
999             return this.disablePerMinComment;
1000         }
1001
1002         @Override
1003         public long getBackLogPoint() {
1004             return this.backLogPoint;
1005         }
1006
1007         @Override
1008         public String toString(){
1009             return ToStringBuilder.reflectionToString(this);
1010         }
1011     }
1012
1013     /*
1014      * ここからConvertProfile作成用クラスの定義
1015      */
1016     private class InqubusConvertProfile extends ConfigConvertProfile {
1017         private final OutputProfile outputProfile;
1018         private final GeneralProfile generalProfile;
1019         private final FfmpegProfile ffmpegProfile;
1020         private final boolean convert;
1021
1022         private InqubusConvertProfile() throws IOException {
1023             this.outputProfile = new InqubusOutputProfile();
1024             this.generalProfile = new ConfigGeneralProfile();
1025
1026             final File file = pnlInputFfmpeg.mdlFfmpegOption.getSelectedFile();
1027             if (file != null) {
1028                 this.ffmpegProfile = new ConfigFfmpegProfile();
1029             } else {
1030                 this.ffmpegProfile = new InqubusFfmpegProfile();
1031             }
1032
1033             this.convert = cbOutputEnable.isSelected();
1034         }
1035
1036         @Override
1037         public OutputProfile getOutputProfile() {
1038             return this.outputProfile;
1039         }
1040
1041         @Override
1042         public GeneralProfile getGeneralProfile() {
1043             return this.generalProfile;
1044         }
1045
1046         @Override
1047         public FfmpegProfile getFfmpegOption() {
1048             return this.ffmpegProfile;
1049         }
1050
1051         @Override
1052         public boolean isConvert() {
1053             return this.convert;
1054         }
1055
1056         @Override
1057         public String toString(){
1058             return ToStringBuilder.reflectionToString(this);
1059         }
1060     }
1061
1062     private class InqubusOutputProfile extends ConfigOutputProfile {
1063         private final String fileName;
1064         private final String videoId;
1065         private final String title;
1066
1067
1068         private InqubusOutputProfile() {
1069             this.fileName = fldOutput.getText();
1070             // TODO この時点でのID/Titleはどうするか…
1071             this.videoId = "";
1072             this.title = "";
1073         }
1074
1075         @Override
1076         public String getFileName() {
1077             return this.fileName;
1078         }
1079
1080         @Override
1081         public String getVideoId() {
1082             return this.videoId;
1083         }
1084
1085         @Override
1086         public String getTitile() {
1087             return this.title;
1088         }
1089
1090         @Override
1091         public String toString(){
1092             return ToStringBuilder.reflectionToString(this);
1093         }
1094     }
1095
1096     private class InqubusFfmpegProfile implements FfmpegProfile {
1097         private final String extOption;
1098         private final String inOption;
1099         private final String mainOption;
1100         private final String outOption;
1101         private final String avOption;
1102         private final boolean resize;
1103         private final int resizeWidth;
1104         private final int resizeHeight;
1105         private final boolean adjustRatio;
1106
1107         private InqubusFfmpegProfile() throws IOException {
1108             String ext = pnlInputFfmpeg.fldFfmpegOptionExtension.getText();
1109             if (!ext.startsWith(".")) {
1110                 ext = "." + ext;
1111             }
1112             this.extOption = ext;
1113             this.inOption = pnlInputFfmpeg.fldFfmpegOptionIn.getText();
1114             this.mainOption = pnlInputFfmpeg.fldFfmpegOptionMain.getText();
1115             this.outOption = pnlInputFfmpeg.fldFfmpegOptionOut.getText();
1116             this.avOption = pnlInputFfmpeg.fldFfmpegOptionAv.getText();
1117             this.resize = pnlInputFfmpeg.cbFfmpegOptionResize.isSelected();
1118             this.resizeWidth = Integer.parseInt(pnlInputFfmpeg.fldFfmpegOptionResizeWidth.getText());
1119             this.resizeHeight = Integer.parseInt(pnlInputFfmpeg.fldFfmpegOptionResizeHeight.getText());
1120             this.adjustRatio = pnlInputFfmpeg.cbFfmpegOptionKeepAspect.isSelected();
1121         }
1122
1123         @Override
1124         public String getExtOption() {
1125             return this.extOption;
1126         }
1127
1128         @Override
1129         public String getInOption() {
1130             return this.inOption;
1131         }
1132
1133         @Override
1134         public String getMainOption() {
1135             return this.mainOption;
1136         }
1137
1138         @Override
1139         public String getOutOption() {
1140             return this.outOption;
1141         }
1142
1143         @Override
1144         public String getAvfilterOption() {
1145             return this.avOption;
1146         }
1147
1148         @Override
1149         public boolean isResize() {
1150             return this.resize;
1151         }
1152
1153         @Override
1154         public int getResizeWidth() {
1155             return this.resizeWidth;
1156         }
1157
1158         @Override
1159         public int getResizeHeight() {
1160             return this.resizeHeight;
1161         }
1162
1163         @Override
1164         public boolean isAdjustRatio() {
1165             return this.adjustRatio;
1166         }
1167
1168         @Override
1169         public String toString(){
1170             return ToStringBuilder.reflectionToString(this);
1171         }
1172     }
1173 }