| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 Kevin Lamonte |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer.bits; |
| 30 | |
| 31 | import java.awt.Image; |
| 32 | import java.awt.Toolkit; |
| 33 | import java.awt.datatransfer.DataFlavor; |
| 34 | import java.awt.datatransfer.StringSelection; |
| 35 | import java.awt.datatransfer.Transferable; |
| 36 | import java.awt.datatransfer.UnsupportedFlavorException; |
| 37 | import java.awt.image.BufferedImage; |
| 38 | import java.io.IOException; |
| 39 | |
| 40 | /** |
| 41 | * Clipboard provides convenience methods to copy text and images to and from |
| 42 | * a shared clipboard. When the system clipboard is available it is used. |
| 43 | */ |
| 44 | public class Clipboard { |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Variables -------------------------------------------------------------- |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | /** |
| 51 | * The image last copied to the clipboard. |
| 52 | */ |
| 53 | private BufferedImage image = null; |
| 54 | |
| 55 | /** |
| 56 | * The text string last copied to the clipboard. |
| 57 | */ |
| 58 | private String text = null; |
| 59 | |
| 60 | /** |
| 61 | * The system clipboard, or null if it is not available. |
| 62 | */ |
| 63 | private java.awt.datatransfer.Clipboard systemClipboard = null; |
| 64 | |
| 65 | /** |
| 66 | * The image selection class. |
| 67 | */ |
| 68 | private ImageSelection imageSelection; |
| 69 | |
| 70 | /** |
| 71 | * ImageSelection is used to hold an image while on the clipboard. |
| 72 | */ |
| 73 | private class ImageSelection implements Transferable { |
| 74 | |
| 75 | /** |
| 76 | * Returns an array of DataFlavor objects indicating the flavors the |
| 77 | * data can be provided in. The array should be ordered according to |
| 78 | * preference for providing the data (from most richly descriptive to |
| 79 | * least descriptive). |
| 80 | * |
| 81 | * @return an array of data flavors in which this data can be |
| 82 | * transferred |
| 83 | */ |
| 84 | public DataFlavor[] getTransferDataFlavors() { |
| 85 | return new DataFlavor[] { DataFlavor.imageFlavor }; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns whether or not the specified data flavor is supported for |
| 90 | * this object. |
| 91 | * |
| 92 | * @param flavor the requested flavor for the data |
| 93 | * @return boolean indicating whether or not the data flavor is |
| 94 | * supported |
| 95 | */ |
| 96 | public boolean isDataFlavorSupported(DataFlavor flavor) { |
| 97 | return DataFlavor.imageFlavor.equals(flavor); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns an object which represents the data to be transferred. The |
| 102 | * class of the object returned is defined by the representation |
| 103 | * class of the flavor. |
| 104 | * |
| 105 | * @param flavor the requested flavor for the data |
| 106 | * @throws IOException if the data is no longer available in the |
| 107 | * requested flavor. |
| 108 | * @throws UnsupportedFlavorException if the requested data flavor is |
| 109 | * not supported. |
| 110 | */ |
| 111 | public Object getTransferData(DataFlavor flavor) |
| 112 | throws UnsupportedFlavorException, IOException { |
| 113 | |
| 114 | if (!DataFlavor.imageFlavor.equals(flavor)) { |
| 115 | throw new UnsupportedFlavorException(flavor); |
| 116 | } |
| 117 | return image; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // ------------------------------------------------------------------------ |
| 122 | // Constructors ----------------------------------------------------------- |
| 123 | // ------------------------------------------------------------------------ |
| 124 | |
| 125 | /** |
| 126 | * Public constructor. |
| 127 | */ |
| 128 | public Clipboard() { |
| 129 | try { |
| 130 | systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 131 | } catch (java.awt.HeadlessException e) { |
| 132 | // SQUASH |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // ------------------------------------------------------------------------ |
| 137 | // Clipboard -------------------------------------------------------------- |
| 138 | // ------------------------------------------------------------------------ |
| 139 | |
| 140 | /** |
| 141 | * Copy an image to the clipboard. |
| 142 | * |
| 143 | * @param image image to copy |
| 144 | */ |
| 145 | public void copyImage(final BufferedImage image) { |
| 146 | this.image = image; |
| 147 | if (systemClipboard != null) { |
| 148 | ImageSelection imageSelection = new ImageSelection(); |
| 149 | systemClipboard.setContents(imageSelection, null); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Copy a text string to the clipboard. |
| 155 | * |
| 156 | * @param text string to copy |
| 157 | */ |
| 158 | public void copyText(final String text) { |
| 159 | this.text = text; |
| 160 | if (systemClipboard != null) { |
| 161 | StringSelection stringSelection = new StringSelection(text); |
| 162 | systemClipboard.setContents(stringSelection, null); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Obtain an image from the clipboard. |
| 168 | * |
| 169 | * @return image from the clipboard, or null if no image is available |
| 170 | */ |
| 171 | public BufferedImage pasteImage() { |
| 172 | if (systemClipboard != null) { |
| 173 | getClipboardImage(); |
| 174 | } |
| 175 | return image; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Obtain a text string from the clipboard. |
| 180 | * |
| 181 | * @return text string from the clipboard, or null if no text is |
| 182 | * available |
| 183 | */ |
| 184 | public String pasteText() { |
| 185 | if (systemClipboard != null) { |
| 186 | getClipboardText(); |
| 187 | } |
| 188 | return text; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns true if the clipboard has an image. |
| 193 | * |
| 194 | * @return true if an image is available from the clipboard |
| 195 | */ |
| 196 | public boolean isImage() { |
| 197 | if (image == null) { |
| 198 | getClipboardImage(); |
| 199 | } |
| 200 | return (image != null); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns true if the clipboard has a text string. |
| 205 | * |
| 206 | * @return true if a text string is available from the clipboard |
| 207 | */ |
| 208 | public boolean isText() { |
| 209 | if (text == null) { |
| 210 | getClipboardText(); |
| 211 | } |
| 212 | return (text != null); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Returns true if the clipboard is empty. |
| 217 | * |
| 218 | * @return true if the clipboard is empty |
| 219 | */ |
| 220 | public boolean isEmpty() { |
| 221 | return ((isText() == false) && (isImage() == false)); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Copy image from the clipboard to text. |
| 226 | */ |
| 227 | private void getClipboardImage() { |
| 228 | if (systemClipboard != null) { |
| 229 | Transferable contents = systemClipboard.getContents(null); |
| 230 | if (contents != null) { |
| 231 | if (contents.isDataFlavorSupported(DataFlavor.imageFlavor)) { |
| 232 | try { |
| 233 | Image img = (Image) contents.getTransferData(DataFlavor.imageFlavor); |
| 234 | image = new BufferedImage(img.getWidth(null), |
| 235 | img.getHeight(null), BufferedImage.TYPE_INT_ARGB); |
| 236 | image.getGraphics().drawImage(img, 0, 0, null); |
| 237 | } catch (IOException e) { |
| 238 | // SQUASH |
| 239 | } catch (UnsupportedFlavorException e) { |
| 240 | // SQUASH |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Copy text string from the clipboard to text. |
| 249 | */ |
| 250 | private void getClipboardText() { |
| 251 | if (systemClipboard != null) { |
| 252 | Transferable contents = systemClipboard.getContents(null); |
| 253 | if (contents != null) { |
| 254 | if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) { |
| 255 | try { |
| 256 | text = (String) contents.getTransferData(DataFlavor.stringFlavor); |
| 257 | } catch (IOException e) { |
| 258 | // SQUASH |
| 259 | } catch (UnsupportedFlavorException e) { |
| 260 | // SQUASH |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Clear whatever is on the local clipboard. Note that this will not |
| 269 | * clear the system clipboard. |
| 270 | */ |
| 271 | public void clear() { |
| 272 | image = null; |
| 273 | text = null; |
| 274 | } |
| 275 | |
| 276 | } |