OSDN Git Service

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