/*
* MainFrame.java
*
* Created on 2011/05/28, 18:14:51
*/
package yukihane.inqubus.gui;
import java.awt.ItemSelectable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ItemEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import yukihane.inqubus.config.Properties;
import yukihane.inqubus.model.Target;
import yukihane.inqubus.model.TargetsTableModel;
/**
*
* @author yuki
*/
public class MainFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(MainFrame.class.getName());
private final TargetsTableModel targetModel = new TargetsTableModel();
/** Creates new form MainFrame */
public MainFrame() {
initComponents();
initInputPanel();
jPanel1.setTransferHandler(new DownloadListTransferHandler());
jTable1.setTransferHandler(new TableTransferHandler());
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
idLabel = new javax.swing.JLabel();
idField = new javax.swing.JTextField();
movieLabel = new javax.swing.JLabel();
useMovieLocalCheckBox = new javax.swing.JCheckBox();
movieFileField = new javax.swing.JTextField();
movieFileSelectButton = new javax.swing.JButton();
commentLabel = new javax.swing.JLabel();
useCommentLocalCheckBox = new javax.swing.JCheckBox();
commentFileField = new javax.swing.JTextField();
commentFileSelectButton = new javax.swing.JButton();
outputLabel = new javax.swing.JLabel();
outputConvertCheckBox = new javax.swing.JCheckBox();
outputFileField = new javax.swing.JTextField();
applyButton = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jTable1.setModel(targetModel);
jTable1.setDropMode(javax.swing.DropMode.INSERT_ROWS);
jScrollPane1.setViewportView(jTable1);
jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jButton2.setText("開始");
jButton3.setText("停止");
jButton4.setText("選択解除");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 250, Short.MAX_VALUE)
.addComponent(jButton4)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
idLabel.setText("ID");
idField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
idFieldFocusLost(evt);
}
});
movieLabel.setText("動画");
useMovieLocalCheckBox.setText("local");
useMovieLocalCheckBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
useMovieLocalCheckBoxItemStateChanged(evt);
}
});
movieFileSelectButton.setText("...");
commentLabel.setText("コメント");
useCommentLocalCheckBox.setText("local");
useCommentLocalCheckBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
useMovieLocalCheckBoxItemStateChanged(evt);
}
});
commentFileSelectButton.setText("...");
outputLabel.setText("出力");
outputConvertCheckBox.setText("変換");
outputConvertCheckBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
outputConvertCheckBoxItemStateChanged(evt);
}
});
applyButton.setText("適用");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(commentLabel)
.addComponent(movieLabel)
.addComponent(idLabel)
.addComponent(outputLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(useMovieLocalCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(movieFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(movieFileSelectButton))
.addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
.addComponent(outputConvertCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(outputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(useCommentLocalCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(commentFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(commentFileSelectButton))))
.addComponent(applyButton, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(idLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(movieLabel)
.addComponent(movieFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(movieFileSelectButton)
.addComponent(useMovieLocalCheckBox))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(commentLabel)
.addComponent(commentFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(commentFileSelectButton)
.addComponent(useCommentLocalCheckBox))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(outputLabel)
.addComponent(outputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(outputConvertCheckBox))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(applyButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))
);
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// //GEN-END:initComponents
private File searchFileMatchId(final File dir, final String id) throws UnsupportedOperationException {
// TODO 候補は複数返すようにして、その後の対処は呼び出しもとで行ってもらった方が良いかも
if (id.isEmpty()) {
return null;
}
final File[] lists = dir.listFiles(new FilenameFilter() {
final Pattern pattern = Pattern.compile(id + "\\D");
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).find();
}
});
if (lists.length == 1) {
return lists[0];
} else if (lists.length > 1) {
throw new UnsupportedOperationException();
} else {
return null;
}
}
private void useMovieLocalCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useMovieLocalCheckBoxItemStateChanged
final Properties p = Properties.INSTANCE;
final ItemSelectable source = evt.getItemSelectable();
JButton btn;
JTextField field;
File dir;
if (source == useMovieLocalCheckBox) {
btn = movieFileSelectButton;
field = movieFileField;
dir = new File(p.getVideoDir());
} else {
btn = commentFileSelectButton;
field = commentFileField;
dir = new File(p.getCommentDir());
}
final boolean useLocal = (evt.getStateChange() == ItemEvent.SELECTED);
btn.setEnabled(useLocal);
String text;
if (useLocal) {
final File f = searchFileMatchId(dir, idField.getText());
if (f != null) {
text = f.getPath();
} else {
text = "";
}
} else {
text = p.getVideoFileNamePattern();
}
field.setText(text);
}//GEN-LAST:event_useMovieLocalCheckBoxItemStateChanged
private void outputConvertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_outputConvertCheckBoxItemStateChanged
final boolean convert = (evt.getStateChange() == ItemEvent.SELECTED);
outputFileField.setEnabled(convert);
}//GEN-LAST:event_outputConvertCheckBoxItemStateChanged
private void idFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_idFieldFocusLost
final Properties p = Properties.INSTANCE;
final String id = idField.getText();
if (id.isEmpty()) {
return;
}
if (useMovieLocalCheckBox.isSelected() && movieFileField.getText().isEmpty()) {
final File dir = new File(p.getVideoDir());
final File file = searchFileMatchId(dir, id);
if (file != null) {
movieFileField.setText(file.getPath());
}
}
if (useCommentLocalCheckBox.isSelected() && commentFileField.getText().isEmpty()) {
final File dir = new File(p.getCommentDir());
final File file = searchFileMatchId(dir, id);
if (file != null) {
commentFileField.setText(file.getPath());
}
}
}//GEN-LAST:event_idFieldFocusLost
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton applyButton;
private javax.swing.JTextField commentFileField;
private javax.swing.JButton commentFileSelectButton;
private javax.swing.JLabel commentLabel;
private javax.swing.JTextField idField;
private javax.swing.JLabel idLabel;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField movieFileField;
private javax.swing.JButton movieFileSelectButton;
private javax.swing.JLabel movieLabel;
private javax.swing.JCheckBox outputConvertCheckBox;
private javax.swing.JTextField outputFileField;
private javax.swing.JLabel outputLabel;
private javax.swing.JCheckBox useCommentLocalCheckBox;
private javax.swing.JCheckBox useMovieLocalCheckBox;
// End of variables declaration//GEN-END:variables
private void initInputPanel() {
idField.setText("");
final Properties p = Properties.INSTANCE;
final boolean movieLocal = p.getVideoUseLocal();
useMovieLocalCheckBox.setSelected(movieLocal);
movieFileSelectButton.setEnabled(movieLocal);
if (!movieLocal) {
movieFileField.setText(p.getVideoFileNamePattern());
}
final boolean commentLocal = p.getCommentUseLocal();
useCommentLocalCheckBox.setSelected(commentLocal);
commentFileSelectButton.setEnabled(commentLocal);
if (!commentLocal) {
commentFileField.setText(p.getCommentFileNamePattern());
}
final boolean convert = p.getOutputEnable();
outputConvertCheckBox.setSelected(convert);
outputFileField.setEnabled(convert);
outputFileField.setText(p.getOutputFileNamePattern());
}
private class DownloadListTransferHandler extends TransferHandler {
private static final long serialVersionUID = 1L;
private final Pattern movieIdPattern = Pattern.compile("(\\w\\w\\d+)");
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
Transferable transferable = support.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
|| transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return true;
}
return false;
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
try {
Transferable transferable = support.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
@SuppressWarnings("unchecked")
final List data = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
Collection targets = Target.from(data);
targetModel.addTarget(targets);
} else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
Matcher matcher = movieIdPattern.matcher(data);
if (matcher.find()) {
String movieId = matcher.group(1);
Target target = Target.fromId(movieId);
targetModel.addTarget(target);
} else {
return false;
}
}
return false;
} catch (Exception e) {
logger.log(Level.SEVERE, null, e);
return false;
}
}
}
private class TableTransferHandler extends DownloadListTransferHandler {
private static final long serialVersionUID = 1L;
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return super.canImport(support);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
return super.importData(support);
}
}
}