+++ /dev/null
-/*
- * Jexer - Java Text User Interface
- *
- * The MIT License (MIT)
- *
- * Copyright (C) 2019 Kevin Lamonte
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- *
- * @author Kevin Lamonte [kevin.lamonte@gmail.com]
- * @version 1
- */
-package jexer.bits;
-
-import java.awt.Image;
-import java.awt.Toolkit;
-import java.awt.datatransfer.DataFlavor;
-import java.awt.datatransfer.StringSelection;
-import java.awt.datatransfer.Transferable;
-import java.awt.datatransfer.UnsupportedFlavorException;
-import java.awt.image.BufferedImage;
-import java.io.IOException;
-
-/**
- * Clipboard provides convenience methods to copy text and images to and from
- * a shared clipboard. When the system clipboard is available it is used.
- */
-public class Clipboard {
-
- // ------------------------------------------------------------------------
- // Variables --------------------------------------------------------------
- // ------------------------------------------------------------------------
-
- /**
- * The image last copied to the clipboard.
- */
- private BufferedImage image = null;
-
- /**
- * The text string last copied to the clipboard.
- */
- private String text = null;
-
- /**
- * The system clipboard, or null if it is not available.
- */
- private java.awt.datatransfer.Clipboard systemClipboard = null;
-
- // ------------------------------------------------------------------------
- // Constructors -----------------------------------------------------------
- // ------------------------------------------------------------------------
-
- /**
- * Public constructor.
- */
- public Clipboard() {
- try {
- systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
- } catch (java.awt.HeadlessException e) {
- // SQUASH
- }
- }
-
- // ------------------------------------------------------------------------
- // Clipboard --------------------------------------------------------------
- // ------------------------------------------------------------------------
-
- /**
- * Copy an image to the clipboard.
- *
- * @param image image to copy
- */
- public void copyImage(final BufferedImage image) {
- this.image = image;
- if (systemClipboard != null) {
- // TODO
- }
- }
-
- /**
- * Copy a text string to the clipboard.
- *
- * @param text string to copy
- */
- public void copyText(final String text) {
- this.text = text;
- if (systemClipboard != null) {
- StringSelection stringSelection = new StringSelection(text);
- systemClipboard.setContents(stringSelection, null);
- }
- }
-
- /**
- * Obtain an image from the clipboard.
- *
- * @return image from the clipboard, or null if no image is available
- */
- public BufferedImage pasteImage() {
- if (systemClipboard != null) {
- getClipboardImage();
- }
- return image;
- }
-
- /**
- * Obtain a text string from the clipboard.
- *
- * @return text string from the clipboard, or null if no text is
- * available
- */
- public String pasteText() {
- if (systemClipboard != null) {
- getClipboardText();
- }
- return text;
- }
-
- /**
- * Returns true if the clipboard has an image.
- *
- * @return true if an image is available from the clipboard
- */
- public boolean isImage() {
- if (image == null) {
- getClipboardImage();
- }
- return (image != null);
- }
-
- /**
- * Returns true if the clipboard has a text string.
- *
- * @return true if a text string is available from the clipboard
- */
- public boolean isText() {
- if (text == null) {
- getClipboardText();
- }
- return (text != null);
- }
-
- /**
- * Returns true if the clipboard is empty.
- *
- * @return true if the clipboard is empty
- */
- public boolean isEmpty() {
- return ((isText() == false) && (isImage() == false));
- }
-
- /**
- * Copy image from the clipboard to text.
- */
- private void getClipboardImage() {
- if (systemClipboard != null) {
- Transferable contents = systemClipboard.getContents(null);
- if (contents != null) {
- if (contents.isDataFlavorSupported(DataFlavor.imageFlavor)) {
- try {
- Image img = (Image) contents.getTransferData(DataFlavor.imageFlavor);
- image = new BufferedImage(img.getWidth(null),
- img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
- image.getGraphics().drawImage(img, 0, 0, null);
- } catch (IOException e) {
- // SQUASH
- } catch (UnsupportedFlavorException e) {
- // SQUASH
- }
- }
- }
- }
- }
-
- /**
- * Copy text string from the clipboard to text.
- */
- private void getClipboardText() {
- if (systemClipboard != null) {
- Transferable contents = systemClipboard.getContents(null);
- if (contents != null) {
- if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
- try {
- text = (String) contents.getTransferData(DataFlavor.stringFlavor);
- } catch (IOException e) {
- // SQUASH
- } catch (UnsupportedFlavorException e) {
- // SQUASH
- }
- }
- }
- }
- }
-
- /**
- * Clear whatever is on the local clipboard. Note that this will not
- * clear the system clipboard.
- */
- public void clear() {
- image = null;
- text = null;
- }
-
-}