OSDN Git Service

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