OSDN Git Service

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