Logo Search packages:      
Sourcecode: jline version File versions  Download package

boolean jline::ConsoleReader::paste (  )  throws IOException [inline]

Paste the contents of the clipboard into the console buffer

Returns:
true if clipboard contents pasted

Definition at line 675 of file ConsoleReader.java.

References putString().

Referenced by readLine().

      {
            java.awt.datatransfer.Clipboard clipboard
                  = java.awt.Toolkit.getDefaultToolkit ().getSystemClipboard ();
            if (clipboard == null)
                  return false;

            java.awt.datatransfer.Transferable transferable
                  = clipboard.getContents (null);

            if (transferable == null)
                  return false;

            try
            {
                  Object content = transferable.getTransferData (
                        java.awt.datatransfer.DataFlavor.plainTextFlavor);

                  /*
                   * This fix was suggested in bug #1060649 at
                   * http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056
                   * to get around the deprecated DataFlavor.plainTextFlavor, but
                   * it raises a UnsupportedFlavorException on Mac OS X
                  Object content = new java.awt.datatransfer.DataFlavor ().
                        getReaderForText (transferable);
                  */

                  if (content == null)
                        return false;

                  String value;

                  if (content instanceof Reader)
                  {
                        // TODO: we might want instead connect to the input stream
                        // so we can interpret individual lines
                        value = "";
                        String line = null;
                        for (BufferedReader read = new BufferedReader ((Reader)content);
                              (line = read.readLine ()) != null; )
                        {
                              if (value.length () > 0)
                                    value += "\n";

                              value += line;
                        }
                  }
                  else
                  {
                        value = content.toString ();
                  }


                  if (value == null)
                        return true;

                  putString (value);

                  return true;
            }
            catch (java.awt.datatransfer.UnsupportedFlavorException ufe)
            {
                  ufe.printStackTrace ();
                  return false;
            }
      }


Generated by  Doxygen 1.6.0   Back to index