Copy screen text to clipboard
[nikiroo-utils.git] / src / jexer / bits / Clipboard.java
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 // Constructors -----------------------------------------------------------
67 // ------------------------------------------------------------------------
68
69 /**
70 * Public constructor.
71 */
72 public Clipboard() {
73 try {
74 systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
75 } catch (java.awt.HeadlessException e) {
76 // SQUASH
77 }
78 }
79
80 // ------------------------------------------------------------------------
81 // Clipboard --------------------------------------------------------------
82 // ------------------------------------------------------------------------
83
84 /**
85 * Copy an image to the clipboard.
86 *
87 * @param image image to copy
88 */
89 public void copyImage(final BufferedImage image) {
90 this.image = image;
91 if (systemClipboard != null) {
92 // TODO
93 }
94 }
95
96 /**
97 * Copy a text string to the clipboard.
98 *
99 * @param text string to copy
100 */
101 public void copyText(final String text) {
102 this.text = text;
103 if (systemClipboard != null) {
104 StringSelection stringSelection = new StringSelection(text);
105 systemClipboard.setContents(stringSelection, null);
106 }
107 }
108
109 /**
110 * Obtain an image from the clipboard.
111 *
112 * @return image from the clipboard, or null if no image is available
113 */
114 public BufferedImage pasteImage() {
115 if (systemClipboard != null) {
116 getClipboardImage();
117 }
118 return image;
119 }
120
121 /**
122 * Obtain a text string from the clipboard.
123 *
124 * @return text string from the clipboard, or null if no text is
125 * available
126 */
127 public String pasteText() {
128 if (systemClipboard != null) {
129 getClipboardText();
130 }
131 return text;
132 }
133
134 /**
135 * Returns true if the clipboard has an image.
136 *
137 * @return true if an image is available from the clipboard
138 */
139 public boolean isImage() {
140 if (image == null) {
141 getClipboardImage();
142 }
143 return (image != null);
144 }
145
146 /**
147 * Returns true if the clipboard has a text string.
148 *
149 * @return true if a text string is available from the clipboard
150 */
151 public boolean isText() {
152 if (text == null) {
153 getClipboardText();
154 }
155 return (text != null);
156 }
157
158 /**
159 * Returns true if the clipboard is empty.
160 *
161 * @return true if the clipboard is empty
162 */
163 public boolean isEmpty() {
164 return ((isText() == false) && (isImage() == false));
165 }
166
167 /**
168 * Copy image from the clipboard to text.
169 */
170 private void getClipboardImage() {
171 if (systemClipboard != null) {
172 Transferable contents = systemClipboard.getContents(null);
173 if (contents != null) {
174 if (contents.isDataFlavorSupported(DataFlavor.imageFlavor)) {
175 try {
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) {
181 // SQUASH
182 } catch (UnsupportedFlavorException e) {
183 // SQUASH
184 }
185 }
186 }
187 }
188 }
189
190 /**
191 * Copy text string from the clipboard to text.
192 */
193 private void getClipboardText() {
194 if (systemClipboard != null) {
195 Transferable contents = systemClipboard.getContents(null);
196 if (contents != null) {
197 if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
198 try {
199 text = (String) contents.getTransferData(DataFlavor.stringFlavor);
200 } catch (IOException e) {
201 // SQUASH
202 } catch (UnsupportedFlavorException e) {
203 // SQUASH
204 }
205 }
206 }
207 }
208 }
209
210 /**
211 * Clear whatever is on the local clipboard. Note that this will not
212 * clear the system clipboard.
213 */
214 public void clear() {
215 image = null;
216 text = null;
217 }
218
219 }