OSDN Git Service

動画/コメント入力フィールドにDnD実装
authoryukihane <yukihane.feather@gmail.com>
Wed, 7 Sep 2011 10:36:03 +0000 (19:36 +0900)
committeryukihane <yukihane.feather@gmail.com>
Fri, 9 Sep 2011 11:45:04 +0000 (20:45 +0900)
frontend/src/yukihane/inqubus/gui/ContentTransferHandler.java [new file with mode: 0644]
frontend/src/yukihane/inqubus/gui/MainFrame.java

diff --git a/frontend/src/yukihane/inqubus/gui/ContentTransferHandler.java b/frontend/src/yukihane/inqubus/gui/ContentTransferHandler.java
new file mode 100644 (file)
index 0000000..a4f64e7
--- /dev/null
@@ -0,0 +1,82 @@
+package yukihane.inqubus.gui;
+
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.JCheckBox;
+import javax.swing.TransferHandler;
+import javax.swing.text.JTextComponent;
+
+/**
+ * メイン画面の動画/コメント入力フィールド用TransferHandler
+ * @author user
+ */
+class ContentTransferHandler extends TransferHandler {
+
+    private static final Logger logger = Logger.getLogger(ContentTransferHandler.class.getName());
+    private final JCheckBox cbLocal;
+    private final TransferHandler defaultTransferHandler;
+
+    /**
+     *
+     * @param defaultTransferHandler 対象JTextFieldインスタンスのデフォルトtransfer handler.
+     * @param cbLocal 対応するローカルチェックボックス.
+     */
+    ContentTransferHandler(TransferHandler defaultTransferHandler, JCheckBox cbLocal) {
+        this.defaultTransferHandler = defaultTransferHandler;
+        this.cbLocal = cbLocal;
+    }
+
+    @Override
+    public boolean canImport(TransferHandler.TransferSupport support) {
+        if (!cbLocal.isSelected()) {
+            return defaultTransferHandler.canImport(support);
+        }
+
+        if (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
+                && !support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
+            return false;
+        }
+
+        if (support.isDrop()) {
+            support.setDropAction(LINK);
+        }
+        return true;
+    }
+
+    @Override
+    public boolean importData(TransferHandler.TransferSupport support) {
+        if (!cbLocal.isSelected() || !support.isDrop()) {
+            return defaultTransferHandler.importData(support);
+        }
+
+        final Transferable tr = support.getTransferable();
+        try {
+            String text;
+            if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
+                List<File> files = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
+                if (files.size() > 0) {
+                    File file = files.get(0);
+                    text = file.getPath();
+                } else {
+                    text = "";
+                }
+            } else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
+                text = (String) tr.getTransferData(DataFlavor.stringFlavor);
+            } else {
+                return false;
+            }
+            final JTextComponent comp = (JTextComponent) support.getComponent();
+            comp.setText(text);
+            return true;
+        } catch (UnsupportedFlavorException | IOException ex) {
+            logger.log(Level.SEVERE, null, ex);
+        }
+        return false;
+    }
+}
index 51e19c2..5133499 100644 (file)
@@ -10,8 +10,6 @@ import java.awt.Image;
 import java.awt.ItemSelectable;
 import java.awt.Point;
 import java.awt.Toolkit;
-import java.awt.datatransfer.DataFlavor;
-import java.awt.datatransfer.Transferable;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
@@ -34,7 +32,6 @@ import java.util.Set;
 import java.util.SortedSet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import java.util.regex.Pattern;
 import javax.swing.BorderFactory;
 import javax.swing.DefaultComboBoxModel;
 import javax.swing.DropMode;
@@ -57,7 +54,6 @@ import javax.swing.JTextField;
 import javax.swing.KeyStroke;
 import javax.swing.LayoutStyle.ComponentPlacement;
 import javax.swing.SwingUtilities;
-import javax.swing.TransferHandler;
 import javax.swing.WindowConstants;
 import javax.swing.border.BevelBorder;
 import org.apache.commons.configuration.ConfigurationException;
@@ -65,7 +61,6 @@ import org.apache.commons.lang.builder.ToStringBuilder;
 import saccubus.MainFrame_AboutBox;
 import saccubus.util.WayBackTimeParser;
 import saccubus.worker.profile.CommentProfile;
-import saccubus.worker.profile.ConvertProfile;
 import saccubus.worker.profile.DownloadProfile;
 import saccubus.worker.profile.FfmpegProfile;
 import saccubus.worker.profile.GeneralProfile;
@@ -174,6 +169,7 @@ public class MainFrame extends JFrame {
         cmbVideo.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
         btnVideo.addActionListener(
                 new FileChooseActionListener(MainFrame.this, JFileChooser.FILES_ONLY, fldVideo));
+        fldVideo.setTransferHandler(new ContentTransferHandler(fldVideo.getTransferHandler(), cbVideoLocal));
         final JLabel lblComment = new JLabel();
 
         fldBackLog.setToolTipText("YYYY/MM/DD hh:mm:ss形式、あるいは1970/01/01からの経過秒を入力します。");
@@ -210,6 +206,7 @@ public class MainFrame extends JFrame {
         cmbComment.setToolTipText(FILE_INPUTFIELD_TOOLTIP);
         btnComment.addActionListener(
                 new FileChooseActionListener(MainFrame.this, JFileChooser.FILES_ONLY, fldComment));
+        fldComment.setTransferHandler(new ContentTransferHandler(fldComment.getTransferHandler(), cbCommentLocal));
         final JLabel lblOutput = new JLabel();
         cbOutputEnable = new JCheckBox();
         fldOutput = new JTextField();