2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
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
;
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.
44 public class Clipboard
{
46 // ------------------------------------------------------------------------
47 // Variables --------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * The image last copied to the clipboard.
53 private BufferedImage image
= null;
56 * The text string last copied to the clipboard.
58 private String text
= null;
61 * The system clipboard, or null if it is not available.
63 private java
.awt
.datatransfer
.Clipboard systemClipboard
= null;
66 * The image selection class.
68 private ImageSelection imageSelection
;
71 * ImageSelection is used to hold an image while on the clipboard.
73 private class ImageSelection
implements Transferable
{
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
81 * @return an array of data flavors in which this data can be
84 public DataFlavor
[] getTransferDataFlavors() {
85 return new DataFlavor
[] { DataFlavor
.imageFlavor
};
89 * Returns whether or not the specified data flavor is supported for
92 * @param flavor the requested flavor for the data
93 * @return boolean indicating whether or not the data flavor is
96 public boolean isDataFlavorSupported(DataFlavor flavor
) {
97 return DataFlavor
.imageFlavor
.equals(flavor
);
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.
105 * @param flavor the requested flavor for the data
106 * @throws IOException if the data is no longer available in the
108 * @throws UnsupportedFlavorException if the requested data flavor is
111 public Object
getTransferData(DataFlavor flavor
)
112 throws UnsupportedFlavorException
, IOException
{
114 if (!DataFlavor
.imageFlavor
.equals(flavor
)) {
115 throw new UnsupportedFlavorException(flavor
);
121 // ------------------------------------------------------------------------
122 // Constructors -----------------------------------------------------------
123 // ------------------------------------------------------------------------
126 * Public constructor.
130 systemClipboard
= Toolkit
.getDefaultToolkit().getSystemClipboard();
131 } catch (java
.awt
.HeadlessException e
) {
136 // ------------------------------------------------------------------------
137 // Clipboard --------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 * Copy an image to the clipboard.
143 * @param image image to copy
145 public void copyImage(final BufferedImage image
) {
147 if (systemClipboard
!= null) {
148 ImageSelection imageSelection
= new ImageSelection();
149 systemClipboard
.setContents(imageSelection
, null);
154 * Copy a text string to the clipboard.
156 * @param text string to copy
158 public void copyText(final String text
) {
160 if (systemClipboard
!= null) {
161 StringSelection stringSelection
= new StringSelection(text
);
162 systemClipboard
.setContents(stringSelection
, null);
167 * Obtain an image from the clipboard.
169 * @return image from the clipboard, or null if no image is available
171 public BufferedImage
pasteImage() {
172 if (systemClipboard
!= null) {
179 * Obtain a text string from the clipboard.
181 * @return text string from the clipboard, or null if no text is
184 public String
pasteText() {
185 if (systemClipboard
!= null) {
192 * Returns true if the clipboard has an image.
194 * @return true if an image is available from the clipboard
196 public boolean isImage() {
200 return (image
!= null);
204 * Returns true if the clipboard has a text string.
206 * @return true if a text string is available from the clipboard
208 public boolean isText() {
212 return (text
!= null);
216 * Returns true if the clipboard is empty.
218 * @return true if the clipboard is empty
220 public boolean isEmpty() {
221 return ((isText() == false) && (isImage() == false));
225 * Copy image from the clipboard to text.
227 private void getClipboardImage() {
228 if (systemClipboard
!= null) {
229 Transferable contents
= systemClipboard
.getContents(null);
230 if (contents
!= null) {
231 if (contents
.isDataFlavorSupported(DataFlavor
.imageFlavor
)) {
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
) {
239 } catch (UnsupportedFlavorException e
) {
248 * Copy text string from the clipboard to text.
250 private void getClipboardText() {
251 if (systemClipboard
!= null) {
252 Transferable contents
= systemClipboard
.getContents(null);
253 if (contents
!= null) {
254 if (contents
.isDataFlavorSupported(DataFlavor
.stringFlavor
)) {
256 text
= (String
) contents
.getTransferData(DataFlavor
.stringFlavor
);
257 } catch (IOException e
) {
259 } catch (UnsupportedFlavorException e
) {
268 * Clear whatever is on the local clipboard. Note that this will not
269 * clear the system clipboard.
271 public void clear() {