From f8b30e4b3bf8cb407c4301d5348e1be45fa811da Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sat, 2 Nov 2019 00:36:06 -0500 Subject: [PATCH] TImage copy to clipboard --- src/jexer/TField.java | 1 + src/jexer/TImage.java | 58 +++++++++++++++++++++++++++++++++- src/jexer/bits/Clipboard.java | 59 ++++++++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 29632ca..90dd4e4 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -376,6 +376,7 @@ public class TField extends TWidget implements EditMenuUser { // Pass to parent for the things we don't care about. super.onKeypress(keypress); } + /** * Handle posted command events. * diff --git a/src/jexer/TImage.java b/src/jexer/TImage.java index cd0ce96..1463f10 100644 --- a/src/jexer/TImage.java +++ b/src/jexer/TImage.java @@ -34,15 +34,17 @@ import jexer.backend.ECMA48Terminal; import jexer.backend.MultiScreen; import jexer.backend.SwingTerminal; import jexer.bits.Cell; +import jexer.event.TCommandEvent; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import jexer.event.TResizeEvent; +import static jexer.TCommand.*; import static jexer.TKeypress.*; /** * TImage renders a piece of a bitmap image on screen. */ -public class TImage extends TWidget { +public class TImage extends TWidget implements EditMenuUser { // ------------------------------------------------------------------------ // Constants -------------------------------------------------------------- @@ -325,6 +327,20 @@ public class TImage extends TWidget { resized = true; } + /** + * Handle posted command events. + * + * @param command command event + */ + @Override + public void onCommand(final TCommandEvent command) { + if (command.equals(cmCopy)) { + // Copy image to clipboard. + getClipboard().copyImage(image); + return; + } + } + // ------------------------------------------------------------------------ // TWidget ---------------------------------------------------------------- // ------------------------------------------------------------------------ @@ -762,4 +778,44 @@ public class TImage extends TWidget { return newImage; } + // ------------------------------------------------------------------------ + // EditMenuUser ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Check if the cut menu item should be enabled. + * + * @return true if the cut menu item should be enabled + */ + public boolean isEditMenuCut() { + return false; + } + + /** + * Check if the copy menu item should be enabled. + * + * @return true if the copy menu item should be enabled + */ + public boolean isEditMenuCopy() { + return true; + } + + /** + * Check if the paste menu item should be enabled. + * + * @return true if the paste menu item should be enabled + */ + public boolean isEditMenuPaste() { + return false; + } + + /** + * Check if the clear menu item should be enabled. + * + * @return true if the clear menu item should be enabled + */ + public boolean isEditMenuClear() { + return false; + } + } diff --git a/src/jexer/bits/Clipboard.java b/src/jexer/bits/Clipboard.java index 114a732..5c1ea9a 100644 --- a/src/jexer/bits/Clipboard.java +++ b/src/jexer/bits/Clipboard.java @@ -62,6 +62,62 @@ public class Clipboard { */ private java.awt.datatransfer.Clipboard systemClipboard = null; + /** + * The image selection class. + */ + private ImageSelection imageSelection; + + /** + * ImageSelection is used to hold an image while on the clipboard. + */ + private class ImageSelection implements Transferable { + + /** + * Returns an array of DataFlavor objects indicating the flavors the + * data can be provided in. The array should be ordered according to + * preference for providing the data (from most richly descriptive to + * least descriptive). + * + * @return an array of data flavors in which this data can be + * transferred + */ + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[] { DataFlavor.imageFlavor }; + } + + /** + * Returns whether or not the specified data flavor is supported for + * this object. + * + * @param flavor the requested flavor for the data + * @return boolean indicating whether or not the data flavor is + * supported + */ + public boolean isDataFlavorSupported(DataFlavor flavor) { + return DataFlavor.imageFlavor.equals(flavor); + } + + /** + * Returns an object which represents the data to be transferred. The + * class of the object returned is defined by the representation + * class of the flavor. + * + * @param flavor the requested flavor for the data + * @throws IOException if the data is no longer available in the + * requested flavor. + * @throws UnsupportedFlavorException if the requested data flavor is + * not supported. + */ + public Object getTransferData(DataFlavor flavor) + throws UnsupportedFlavorException, IOException { + + if (!DataFlavor.imageFlavor.equals(flavor)) { + throw new UnsupportedFlavorException(flavor); + } + return image; + } + } + // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -89,7 +145,8 @@ public class Clipboard { public void copyImage(final BufferedImage image) { this.image = image; if (systemClipboard != null) { - // TODO + ImageSelection imageSelection = new ImageSelection(); + systemClipboard.setContents(imageSelection, null); } } -- 2.27.0