add paste right-click option to the Add Contact form. GitHub issue #85

auto-update
Zlatin Balevsky 2021-10-14 17:48:19 +01:00
parent 0efe12f247
commit 8df6bc3b8c
No known key found for this signature in database
GPG Key ID: A72832072D525E41
3 changed files with 61 additions and 0 deletions

View File

@ -634,6 +634,7 @@ ADD_CONTACT_TITLE=Add a new contact
ADD_CONTACT_BODY=Copy-paste the full MuWire ID of your new contact below.
ADD_CONTACT_INVALID_ID_TITLE=Invalid full MuWire ID
ADD_CONTACT_INVALID_ID_BODY=You have entered an invalid full MuWire ID.
PASTE=Paste
## My Feed dialog
MY_FEED=My Feed

View File

@ -2,7 +2,11 @@ package com.muwire.gui
import griffon.core.artifact.GriffonView
import javax.swing.JMenuItem
import javax.swing.JPopupMenu
import java.awt.event.ActionListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import static com.muwire.gui.Translator.trans
@ -64,6 +68,39 @@ class AddContactView {
}
}
}
idArea.addMouseListener(new MouseAdapter() {
@Override
void mousePressed(MouseEvent e) {
if (!CopyPasteSupport.canPasteString())
return
if (e.isPopupTrigger() || e.button == MouseEvent.BUTTON3)
showPopupMenu(e)
}
@Override
void mouseReleased(MouseEvent e) {
if (!CopyPasteSupport.canPasteString())
return
if (e.isPopupTrigger() || e.button == MouseEvent.BUTTON3)
showPopupMenu(e)
}
})
}
private void showPopupMenu(MouseEvent event) {
JPopupMenu menu = new JPopupMenu()
JMenuItem paste = new JMenuItem(trans("PASTE"))
paste.addActionListener({doPaste()})
menu.add(paste)
menu.show(event.getComponent(), event.getX(), event.getY())
}
private void doPaste() {
String contents = CopyPasteSupport.pasteFromClipboard()
if (contents == null)
return
idArea.setText(contents)
}
void mvcGroupInit(Map<String,String> args) {

View File

@ -3,6 +3,9 @@ package com.muwire.gui;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.InputStream;
public class CopyPasteSupport {
@ -21,4 +24,24 @@ public class CopyPasteSupport {
StringSelection selection = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
}
/**
* @return what's in the clipboard if it can be represented as a string. This does not handle multi-byte characters.
*/
public static String pasteFromClipboard() {
try {
InputStream is = (InputStream)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.getTextPlainUnicodeFlavor());
StringBuilder sb = new StringBuilder();
int b;
while ((b = is.read()) >= 0)
sb.append((char)b);
return sb.toString();
} catch (UnsupportedFlavorException| IOException e) {
return null;
}
}
public static boolean canPasteString() {
return Toolkit.getDefaultToolkit().getSystemClipboard().isDataFlavorAvailable(DataFlavor.getTextPlainUnicodeFlavor());
}
}