changeset 79:2a6795772ed4

TabEdit: Added 'copy value' option to tag's context menu
author Fox
date Sat, 07 Jan 2023 12:00:40 +0100
parents f3fe8eebdcfd
children 0d4e996a5d99
files src/junotu/Main.java src/junotu/TabEdit.java
diffstat 2 files changed, 36 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Main.java	Sat Jan 07 02:12:43 2023 +0100
+++ b/src/junotu/Main.java	Sat Jan 07 12:00:40 2023 +0100
@@ -5,6 +5,10 @@
 
 import javax.swing.SwingUtilities;
 import java.awt.Desktop;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.StringSelection;
+import java.awt.datatransfer.DataFlavor;
 import java.net.URI;
 
 import junotu.Database;
@@ -156,5 +160,23 @@
 	    throw new RuntimeException(e);
 	}
     }
+
+    public static String clipboardGet()
+    {
+	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+	try {
+	    return (String)clipboard.getData( DataFlavor.stringFlavor );
+	} catch( Exception e ) {
+	    return "";
+	}
+    }
+
+    public static void clipboardSet( String string )
+    {
+	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+	StringSelection transferable = new StringSelection(string);
+	System.out.print("Set system clipboard to '"+string+"'.\n");
+	clipboard.setContents( transferable, transferable );
+    }
     
 }
--- a/src/junotu/TabEdit.java	Sat Jan 07 02:12:43 2023 +0100
+++ b/src/junotu/TabEdit.java	Sat Jan 07 12:00:40 2023 +0100
@@ -13,6 +13,7 @@
 
 import javax.swing.KeyStroke;
 import javax.swing.SwingUtilities;
+import javax.swing.MenuElement;
 import javax.swing.event.DocumentListener;
 import javax.swing.event.DocumentEvent;
 
@@ -86,6 +87,7 @@
 
     private JPopupMenu tagMenu;
     private JMenuItem tagMenu_OpenUri;
+    private JMenuItem tagMenu_CopyValue;
 
     
     public TabEdit()
@@ -108,6 +110,7 @@
 
 	tagMenu = new JPopupMenu("Tag menu");
 	tagMenu_OpenUri = tagMenu.add("Open as URI");
+	tagMenu_CopyValue = tagMenu.add("Copy value");
 
 	tags.setLayout( new FlowLayout() );
 
@@ -145,7 +148,12 @@
 	save.addActionListener(this);
 	addTag.addActionListener(this);
 
-	tagMenu_OpenUri.addActionListener(this);
+	MenuElement tagMenuElements[] = tagMenu.getSubElements();
+	for( int i = 0; i < tagMenuElements.length; i++ ) {
+	    if( tagMenuElements[i] instanceof JMenuItem ) {
+		((JMenuItem)tagMenuElements[i]).addActionListener(this);
+	    }
+	}
 
 	editedTagField.addFocusListener(
 					new FocusListener()
@@ -196,6 +204,7 @@
 	addTag.setToolTipText("Add new tag.");
 
 	tagMenu_OpenUri.setToolTipText("Open tag's value as URI; most commonly a file path ('file://') or URL ('http://').");
+	tagMenu_CopyValue.setToolTipText("Copy tag's value to clipboard.");
 	
     }
 
@@ -523,6 +532,7 @@
 	if( e.isPopupTrigger() ) {
 	    TagWidget tagWidget = (TagWidget)e.getSource();
 	    tagMenu_OpenUri.setEnabled( tagWidget.value instanceof String );
+	    tagMenu_CopyValue.setEnabled( tagWidget.value != null );
 	    tagMenu.show( tagWidget, e.getX(), e.getY() );
 	    return true;
 	} else {
@@ -564,6 +574,9 @@
 	} else if( source == tagMenu_OpenUri ) {
 	    TagWidget tagWidget = (TagWidget)tagMenu.getInvoker();
 	    Main.actionOpenURI( (String)tagWidget.value );
+	} else if( source == tagMenu_CopyValue ) {
+	    TagWidget tagWidget = (TagWidget)tagMenu.getInvoker();
+	    Main.clipboardSet( String.valueOf(tagWidget.value) );
 	}
     }