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;
65 // ------------------------------------------------------------------------
66 // Constructors -----------------------------------------------------------
67 // ------------------------------------------------------------------------
74 systemClipboard
= Toolkit
.getDefaultToolkit().getSystemClipboard();
75 } catch (java
.awt
.HeadlessException e
) {
80 // ------------------------------------------------------------------------
81 // Clipboard --------------------------------------------------------------
82 // ------------------------------------------------------------------------
85 * Copy an image to the clipboard.
87 * @param image image to copy
89 public void copyImage(final BufferedImage image
) {
91 if (systemClipboard
!= null) {
97 * Copy a text string to the clipboard.
99 * @param text string to copy
101 public void copyText(final String text
) {
103 if (systemClipboard
!= null) {
104 StringSelection stringSelection
= new StringSelection(text
);
105 systemClipboard
.setContents(stringSelection
, null);
110 * Obtain an image from the clipboard.
112 * @return image from the clipboard, or null if no image is available
114 public BufferedImage
pasteImage() {
115 if (systemClipboard
!= null) {
122 * Obtain a text string from the clipboard.
124 * @return text string from the clipboard, or null if no text is
127 public String
pasteText() {
128 if (systemClipboard
!= null) {
135 * Returns true if the clipboard has an image.
137 * @return true if an image is available from the clipboard
139 public boolean isImage() {
143 return (image
!= null);
147 * Returns true if the clipboard has a text string.
149 * @return true if a text string is available from the clipboard
151 public boolean isText() {
155 return (text
!= null);
159 * Returns true if the clipboard is empty.
161 * @return true if the clipboard is empty
163 public boolean isEmpty() {
164 return ((isText() == false) && (isImage() == false));
168 * Copy image from the clipboard to text.
170 private void getClipboardImage() {
171 if (systemClipboard
!= null) {
172 Transferable contents
= systemClipboard
.getContents(null);
173 if (contents
!= null) {
174 if (contents
.isDataFlavorSupported(DataFlavor
.imageFlavor
)) {
176 Image img
= (Image
) contents
.getTransferData(DataFlavor
.imageFlavor
);
177 image
= new BufferedImage(img
.getWidth(null),
178 img
.getHeight(null), BufferedImage
.TYPE_INT_ARGB
);
179 image
.getGraphics().drawImage(img
, 0, 0, null);
180 } catch (IOException e
) {
182 } catch (UnsupportedFlavorException e
) {
191 * Copy text string from the clipboard to text.
193 private void getClipboardText() {
194 if (systemClipboard
!= null) {
195 Transferable contents
= systemClipboard
.getContents(null);
196 if (contents
!= null) {
197 if (contents
.isDataFlavorSupported(DataFlavor
.stringFlavor
)) {
199 text
= (String
) contents
.getTransferData(DataFlavor
.stringFlavor
);
200 } catch (IOException e
) {
202 } catch (UnsupportedFlavorException e
) {
211 * Clear whatever is on the local clipboard. Note that this will not
212 * clear the system clipboard.
214 public void clear() {