OSDN Git Service

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