OSDN Git Service

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