Inserts the clipboard contents into the text.
()
| 1935 | * Inserts the clipboard contents into the text. |
| 1936 | */ |
| 1937 | public void paste() { |
| 1938 | // System.out.println("focus owner is: " + isFocusOwner()); |
| 1939 | if (editable) { |
| 1940 | Clipboard clipboard = getToolkit().getSystemClipboard(); |
| 1941 | try { |
| 1942 | String selection = |
| 1943 | ((String) clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor)); |
| 1944 | |
| 1945 | if (selection.contains("\r\n")) { |
| 1946 | selection = selection.replaceAll("\r\n", "\n"); |
| 1947 | |
| 1948 | } else if (selection.contains("\r")) { |
| 1949 | // The Mac OS MRJ doesn't convert \r to \n, so do it here |
| 1950 | selection = selection.replace('\r','\n'); |
| 1951 | } |
| 1952 | |
| 1953 | // Remove tabs and replace with spaces |
| 1954 | // https://github.com/processing/processing/issues/108 |
| 1955 | if (selection.contains("\t")) { |
| 1956 | int tabSize = Preferences.getInteger("editor.tabs.size"); |
| 1957 | char[] c = new char[tabSize]; |
| 1958 | Arrays.fill(c, ' '); |
| 1959 | String tabString = new String(c); |
| 1960 | selection = selection.replaceAll("\t", tabString); |
| 1961 | } |
| 1962 | |
| 1963 | // Replace unicode x00A0 (non-breaking space) with just a plain space. |
| 1964 | // Seen often on Mac OS X when pasting from Safari. [fry 030929] |
| 1965 | selection = selection.replace('\u00A0', ' '); |
| 1966 | |
| 1967 | // Remove ASCII NUL characters. Reported when pasting from |
| 1968 | // Acrobat Reader and PDF documents. [fry 130719] |
| 1969 | // https://github.com/processing/processing/issues/1973 |
| 1970 | if (selection.indexOf('\0') != -1) { |
| 1971 | //System.out.println("found NUL charaacters"); |
| 1972 | //int before = selection.length(); |
| 1973 | selection = selection.replaceAll("\0", ""); |
| 1974 | //int after = selection.length(); |
| 1975 | //System.out.println(before + " " + after); |
| 1976 | } |
| 1977 | |
| 1978 | int repeatCount = inputHandler.getRepeatCount(); |
| 1979 | selection = selection.repeat(Math.max(0, repeatCount)); |
| 1980 | setSelectedText(selection); |
| 1981 | |
| 1982 | } catch (Exception e) { |
| 1983 | getToolkit().beep(); |
| 1984 | System.err.println("Clipboard does not contain a string"); |
| 1985 | DataFlavor[] flavors = clipboard.getAvailableDataFlavors(); |
| 1986 | for (DataFlavor f : flavors) { |
| 1987 | try { |
| 1988 | Object o = clipboard.getContents(this).getTransferData(f); |
| 1989 | System.out.println(f + " = " + o); |
| 1990 | } catch (Exception ex) { |
| 1991 | ex.printStackTrace(); |
| 1992 | } |
| 1993 | } |
| 1994 |
no test coverage detected