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