Hi,
this patch adds some code to an internal FocusListener implementation in
BasicTextUI that copies the current selection into the system clipboard and
fixes PR 26736[0].

Together with my focus change patch this completes the integration of the system
clipboard in Free Swing.

The code contains security related stuff which I am unfamiliar with. Can
somebody review that part? Gary?

You still cannot middle-click paste stuff into other (non-java) applications
because the problem that FocusEvents are not send to top-level windows[1].

The ChangeLog:
2006-03-21  Robert Schuster  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicTextUI.java:
        (FocusListener.focusLost): Put current selection into the system
        clipboard.

cya
Robert

[0] - http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26736
[1] - http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26174
Index: javax/swing/plaf/basic/BasicTextUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTextUI.java,v
retrieving revision 1.74
diff -u -r1.74 BasicTextUI.java
--- javax/swing/plaf/basic/BasicTextUI.java	13 Mar 2006 22:06:47 -0000	1.74
+++ javax/swing/plaf/basic/BasicTextUI.java	21 Mar 2006 19:02:29 -0000
@@ -42,10 +42,14 @@
 import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Graphics;
+import java.awt.HeadlessException;
 import java.awt.Insets;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.Shape;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.StringSelection;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.beans.PropertyChangeEvent;
@@ -607,6 +611,44 @@
       public void focusLost(FocusEvent e)
       {
         textComponent.repaint();
+        
+        // Integrates Swing text components with the system clipboard:
+        // The idea is that if one wants to copy text around X11-style
+        // (select text and middle-click in the target component) the focus
+        // will move to the new component which gives the old focus owner the
+        // possibility to paste its selection into the clipboard.
+        if (!e.isTemporary()
+            && textComponent.getSelectionStart()
+               != textComponent.getSelectionEnd())
+          {
+            SecurityManager sm = System.getSecurityManager();
+            try
+              {
+                if (sm != null)
+                  sm.checkSystemClipboardAccess();
+                
+                Clipboard cb = Toolkit.getDefaultToolkit().getSystemSelection();
+                if (cb != null)
+                  {
+                    StringSelection selection = new StringSelection(textComponent.getSelectedText());
+                    cb.setContents(selection, selection);
+                  }
+              }
+            catch (SecurityException se)
+              {
+                // Not allowed to access the clipboard: Ignore and
+                // do not access it.
+              }
+            catch (HeadlessException he)
+              {
+                // There is no AWT: Ignore and do not access the
+                // clipboard.
+              }
+            catch (IllegalStateException ise)
+            {
+                // Clipboard is currently unavaible.
+            }
+          }
       }
     };
 

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to