From: yukihane Date: Sun, 21 Aug 2011 05:19:13 +0000 (+0900) Subject: サムネイル情報を取得し画面(新規)に表示する機能 X-Git-Tag: rel20110821_ver1.8.0~6 X-Git-Url: http://git.osdn.net/view?p=coroid%2Finqubus.git;a=commitdiff_plain;h=1fe07f2c00096bc266c1dfbe6b9812eb27b8894f サムネイル情報を取得し画面(新規)に表示する機能 --- diff --git a/frontend/src/saccubus/MainFrame.java b/frontend/src/saccubus/MainFrame.java index 4c19291..4ea591f 100644 --- a/frontend/src/saccubus/MainFrame.java +++ b/frontend/src/saccubus/MainFrame.java @@ -1,6 +1,7 @@ package saccubus; import java.io.IOException; +import javax.swing.event.ChangeEvent; import saccubus.converter.Converter; import java.awt.BorderLayout; import java.awt.Dimension; @@ -57,6 +58,7 @@ import saccubus.properties.MovieSetting; import saccubus.properties.OutputVideoSetting; import saccubus.util.FileDropTarget; import saccubus.util.PopupRightClick; +import yukihane.Util; import yukihane.nicovideo.NicoDBFinder; /** @@ -104,6 +106,7 @@ public class MainFrame extends JFrame { private JPanel opPanel = new JPanel(); private JLabel videoIdLabel = new JLabel(); private JTextField videoIdField = new JTextField(); + private JButton infoViewButton = new JButton("INFO"); private JLabel wayBackLabel = new JLabel(); private JTextField wayBackField = new JTextField(); private JCheckBox reduceCommentCheckBox = new JCheckBox(); @@ -123,6 +126,9 @@ public class MainFrame extends JFrame { ButtonGroup CommentSaveButtonGroup = new ButtonGroup(); ButtonGroup ConvSaveButtonGroup = new ButtonGroup(); + // 動画情報表示ダイアログ + private VideoInfoViewer videoInfoViewer; + private class FfmpegOptionCheckBoxListener implements ActionListener{ @Override @@ -176,6 +182,27 @@ public class MainFrame extends JFrame { } } + private class InfoViewButtonListener implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { + try { + showVideoInfoView(Util.getVideoId(videoIdField.getText())); + } catch (Exception ex) { + statusBar.setText(ex.getMessage()); + } + } + + private void showVideoInfoView(String videoId) { + if(videoInfoViewer == null){ + videoInfoViewer = new VideoInfoViewer(MainFrame.this); + } + videoInfoViewer.setBasicSetting(getBasicSetting()); + videoInfoViewer.setVisible(true); + videoInfoViewer.load(videoId); + } + } + /** * メイン画面最上部(ID/URL, 過去ログ, 変換ボタン)のパネルを初期化します. */ @@ -186,6 +213,8 @@ public class MainFrame extends JFrame { videoIdField.setText("http://www.nicovideo.jp/watch/"); reduceCommentCheckBox.setText("旧仕様コメント数"); + infoViewButton.addActionListener(new InfoViewButtonListener()); + final GroupLayout oppLayout = new GroupLayout(opPanel); opPanel.setLayout(oppLayout); @@ -194,7 +223,9 @@ public class MainFrame extends JFrame { .addComponent(videoIdLabel) .addComponent(wayBackLabel)) .addGroup(oppLayout.createParallelGroup() - .addComponent(videoIdField) + .addGroup(oppLayout.createSequentialGroup() + .addComponent(videoIdField) + .addComponent(infoViewButton)) .addGroup(oppLayout.createSequentialGroup() .addComponent(wayBackField) .addComponent(reduceCommentCheckBox))) @@ -203,7 +234,8 @@ public class MainFrame extends JFrame { oppLayout.setVerticalGroup(oppLayout.createSequentialGroup() .addGroup(oppLayout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(videoIdLabel) - .addComponent(videoIdField)) + .addComponent(videoIdField) + .addComponent(infoViewButton)) .addGroup(oppLayout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(wayBackLabel) .addComponent(wayBackField) diff --git a/frontend/src/saccubus/VideoInfoViewer.java b/frontend/src/saccubus/VideoInfoViewer.java new file mode 100644 index 0000000..349013c --- /dev/null +++ b/frontend/src/saccubus/VideoInfoViewer.java @@ -0,0 +1,309 @@ +package saccubus; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.concurrent.ExecutionException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JDialog; +import javax.swing.GroupLayout; +import javax.swing.GroupLayout.Alignment; +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.LayoutStyle.ComponentPlacement; +import javax.swing.JTextField; +import javax.swing.JTextArea; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.SwingWorker; +import nicobrowser.NicoHttpClient; +import nicobrowser.util.RssContent; +import nicobrowser.util.RssContentParser; +import org.apache.commons.lang.StringUtils; +import saccubus.properties.BasicSetting; +import yukihane.Util; + +public class VideoInfoViewer extends JDialog { + + private static final long serialVersionUID = 1L; + private static final Logger logger = Logger.getLogger(VideoInfoViewer.class.getName()); + private BasicSetting setting; + private JTextField fldLength; + private JTextField fldTitle; + private JTextField fldId; + private JTextField fldSizeHigh; + private JTextField fldLoadId; + private JTextField fldSizeLow; + private JTextArea fldDescription; + private JLabel lblImage; + private JTextField fldError; + private JButton btnReload; + + public void setBasicSetting(BasicSetting setting){ + this.setting = setting; + } + + public VideoInfoViewer(JFrame owner) { + super(owner); + + if (owner != null) { + final Dimension ownerSize = owner.getSize(); + final Point ownerLocation = owner.getLocation(); + setLocation(new Point(ownerLocation.x + ownerSize.width, ownerLocation.y)); + } + + setTitle("いんきゅばす動画情報"); + setDefaultCloseOperation(HIDE_ON_CLOSE); + + lblImage = new JLabel(); + + JLabel lblLength = new JLabel("length"); + + JLabel lblSize = new JLabel("size(MB)"); + + fldId = new JTextField(); + fldId.setColumns(10); + fldId.setEditable(false); + + fldTitle = new JTextField(); + fldTitle.setColumns(10); + fldTitle.setEditable(false); + + fldLength = new JTextField(); + fldLength.setColumns(10); + fldLength.setEditable(false); + + fldSizeHigh = new JTextField(); + fldSizeHigh.setColumns(10); + fldSizeHigh.setEditable(false); + + fldSizeLow = new JTextField(); + fldSizeLow.setColumns(10); + fldSizeLow.setEditable(false); + final JScrollPane scrDesc = new JScrollPane(); + + + JLabel lblLoadId = new JLabel("load ID"); + + fldLoadId = new JTextField(); + fldLoadId.setColumns(10); + + btnReload = new JButton("更新"); + btnReload.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + String id; + try { + id = Util.getVideoId(fldLoadId.getText()); + } catch (Exception ex) { + fldError.setText("エラー"); + logger.log(Level.SEVERE, null, ex); + return; + } + + load(id); + } + }); + + final JButton btnClose = new JButton("閉じる"); + btnClose.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + setVisible(false); + } + }); + + fldError = new JTextField(); + fldError.setEditable(false); + fldError.setColumns(10); + fldError.setForeground(Color.RED); + + GroupLayout groupLayout = new GroupLayout(getContentPane()); + groupLayout.setHorizontalGroup( + groupLayout.createParallelGroup(Alignment.TRAILING) + .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup() + .addContainerGap() + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(scrDesc) + .addContainerGap()) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(lblImage, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.UNRELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(lblSize) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(fldSizeHigh, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(fldSizeLow, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(lblLength) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(fldLength, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)) + .addComponent(fldId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(lblLoadId) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(fldLoadId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(btnReload) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(fldError, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.RELATED, 47, Short.MAX_VALUE) + .addComponent(btnClose) + .addContainerGap()) + .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup() + .addComponent(fldTitle, GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE) + .addContainerGap()))) + ); + groupLayout.setVerticalGroup( + groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup() + .addContainerGap() + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addComponent(lblImage, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(fldId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.UNRELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) + .addComponent(lblLength) + .addComponent(fldLength, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(ComponentPlacement.RELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) + .addComponent(lblSize) + .addComponent(fldSizeHigh, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addComponent(fldSizeLow, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(fldTitle, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.RELATED) + .addComponent(scrDesc, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE) + .addPreferredGap(ComponentPlacement.RELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) + .addComponent(lblLoadId) + .addComponent(fldLoadId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addComponent(btnReload) + .addComponent(btnClose) + .addComponent(fldError, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addContainerGap()) + ); + + fldDescription = new JTextArea(); + scrDesc.setViewportView(fldDescription); + fldDescription.setLineWrap(true); + fldDescription.setEditable(false); + getContentPane().setLayout(groupLayout); + pack(); + } + + void load(String videoId) { + fldError.setText(""); + if (StringUtils.isEmpty(videoId)) { + return; + } + btnReload.setEnabled(false); + new ThumbnailWorker(videoId, setting).execute(); + } + + private static class Result { + + private RssContent content; + private ImageIcon image; + + private Result(RssContent content, ImageIcon image) { + this.content = content; + this.image = image; + } + } + + private class ThumbnailWorker extends SwingWorker { + + private final String videoId; + private final BasicSetting setting; + + private ThumbnailWorker(String videoId, BasicSetting setting) { + this.videoId = videoId; + this.setting = setting; + } + + @Override + protected Result doInBackground() throws Exception { + NicoHttpClient client; + if (setting == null || !setting.isProxyUse()) { + client = new NicoHttpClient(); + } else { + client = new NicoHttpClient(setting.getProxyHost(), setting.getProxyPort()); + } + + InputStream is = null; + try { + is = client.getThumbInfo(videoId); + final RssContent cont = new RssContentParser().parse(is); + + final URL thumbUrl = new URL(cont.getThumbnail_url()); + final ImageIcon image = new ImageIcon(thumbUrl); + + return new Result(cont, image); + } catch (IOException ex) { + logger.log(Level.SEVERE, null, ex); + return null; + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ex) { + logger.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + protected void done() { + btnReload.setEnabled(true); + + Result res = null; + try { + res = get(); + } catch (InterruptedException ex) { + Logger.getLogger(VideoInfoViewer.class.getName()).log(Level.SEVERE, null, ex); + } catch (ExecutionException ex) { + Logger.getLogger(VideoInfoViewer.class.getName()).log(Level.SEVERE, null, ex); + } + if(res == null){ + fldError.setText("エラー"); + } + + final RssContent cont = res.content; + final ImageIcon image = res.image; + + fldId.setText(cont.getVideo_id()); + fldTitle.setText(cont.getTitle()); + fldLength.setText(cont.getLength()); + double sizeHigh = getSize(cont.getSize_high()); + fldSizeHigh.setText(String.format("%.2f", sizeHigh)); + double sizeLow = getSize(cont.getSize_low()); + fldSizeLow.setText(String.format("%.2f", sizeLow)); + fldDescription.setText(cont.getDescription()); + lblImage.setIcon(image); + + } + + private double getSize(final String high) throws NumberFormatException { + double sizeHigh = -1.0; + if (StringUtils.isNotEmpty(high)) { + sizeHigh = Long.parseLong(high) / 1024.0 / 1024.0; + } + return sizeHigh; + } + } +}