OSDN Git Service

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