]> source.dussan.org Git - tigervnc.git/commitdiff
Fix for issue 1215
authorBrian P. Hinz <bphinz@users.sf.net>
Fri, 12 Mar 2021 00:31:11 +0000 (19:31 -0500)
committerBrian P. Hinz <bphinz@users.sf.net>
Fri, 12 Mar 2021 00:31:57 +0000 (19:31 -0500)
java/com/tigervnc/vncviewer/ClipboardDialog.java

index 4a5d9f27e0d248c38384e7e5fb5a822421a37a41..23923895355ef47bbb81d81301c926d24f850cde 100644 (file)
@@ -41,22 +41,24 @@ class ClipboardDialog extends Dialog {
            // Custom TransferHandler designed to limit the size of outbound
            // clipboard transfers to VncViewer.maxCutText.getValue() bytes.
            private LogWriter vlog = new LogWriter("VncTransferHandler");
+      private long start;
 
-           public void exportToClipboard(JComponent c, Clipboard clip, int a)
+           public void exportToClipboard(JComponent comp, Clipboard clip, int action)
                throws IllegalStateException {
-             if (!(c instanceof JTextComponent)) return;
-        String text = ((JTextComponent)c).getText();
+        if (action != TransferHandler.COPY) return;
+        if (start == 0)
+          start = System.currentTimeMillis();
+             StringSelection selection = new StringSelection(((JTextArea)comp).getText());
         try {
-          if (text.equals((String)clip.getData(DataFlavor.stringFlavor))) return;
-        } catch (IOException e) {
-          // worst case we set the clipboard contents unnecessarily
-               vlog.info(e.toString());
-        } catch (UnsupportedFlavorException e) {
-          // worst case we set the clipboard contents unnecessarily
-               vlog.info(e.toString());
+               clip.setContents(selection, selection);
+        } catch(IllegalStateException e) {
+          // something else has the clipboard, keep trying for at most 50ms
+          if ((System.currentTimeMillis() - start) < 50)
+            exportToClipboard(comp, clip, action);
+          else
+            vlog.info("Couldn't access system clipboard for > 50ms");
         }
-             StringSelection selection = new StringSelection(text);
-             clip.setContents(selection, null);
+        start = 0;
            }
 
            public boolean importData(JComponent c, Transferable t) {
@@ -217,6 +219,5 @@ class ClipboardDialog extends Dialog {
   private static boolean listen = true;
   static ClipboardDialog dialog;
   static MyJTextArea textArea = new MyJTextArea();
-  static Toolkit tk = Toolkit.getDefaultToolkit();
   static LogWriter vlog = new LogWriter("ClipboardDialog");
 }