GUI: automatically select URLs from clipboard
authorNiki Roo <niki@nikiroo.be>
Mon, 27 Feb 2017 14:36:55 +0000 (15:36 +0100)
committerNiki Roo <niki@nikiroo.be>
Mon, 27 Feb 2017 14:36:55 +0000 (15:36 +0100)
- When Importing an URL, automatically set the default content to the
  clipboard content if it looks like a URL (if it starts with "http")

src/be/nikiroo/fanfix/reader/LocalReaderFrame.java

index e15ee8d17bb4889ac31061fc8313ad1bab6cb618..4e8d9ace0f801b45f3089152c534a45dc6ef3aaa 100644 (file)
@@ -3,6 +3,8 @@ package be.nikiroo.fanfix.reader;
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Frame;
+import java.awt.Toolkit;
+import java.awt.datatransfer.DataFlavor;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
@@ -486,19 +488,32 @@ class LocalReaderFrame extends JFrame {
        private void imprt(boolean askUrl) {
                JFileChooser fc = new JFileChooser();
 
-               String url;
+               Object url;
                if (askUrl) {
+                       String clipboard = "";
+                       try {
+                               clipboard = ("" + Toolkit.getDefaultToolkit()
+                                               .getSystemClipboard().getData(DataFlavor.stringFlavor))
+                                               .trim();
+                       } catch (Exception e) {
+                               // No data will be handled
+                       }
+
+                       if (clipboard == null || !clipboard.startsWith("http")) {
+                               clipboard = "";
+                       }
+
                        url = JOptionPane.showInputDialog(LocalReaderFrame.this,
                                        "url of the story to import?", "Importing from URL",
-                                       JOptionPane.QUESTION_MESSAGE);
+                                       JOptionPane.QUESTION_MESSAGE, null, null, clipboard);
                } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) {
                        url = fc.getSelectedFile().getAbsolutePath();
                } else {
                        url = null;
                }
 
-               if (url != null && !url.isEmpty()) {
-                       imprt(url, null);
+               if (url != null && !url.toString().isEmpty()) {
+                       imprt(url.toString(), null);
                }
        }