classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Some datatransfer fixes and additions


From: Mark Wielaard
Subject: [cp-patches] FYI: Some datatransfer fixes and additions
Date: Fri, 05 Aug 2005 19:31:19 +0200

Hi,

I was writing some mauve tests for awt/datatransfer. We were missing
Flavor events and listeners and there were various bugs in ClipBoard and
DataFlavor.


2005-08-05  Mark Wielaard  <address@hidden>

        * java/awt/datatransfer/FlavorEvent.java: New class.
        * java/awt/datatransfer/FlavorListener.java: Likewise.
        * java/awt/datatransfer/Clipboard.java (name): Made final.
        (listeners): New final ArrayList field.
        (setContents): Reimplemented.
        (getAvailableDataFlavors): New method.
        (isDataFlavorAvailable): Likewise.
        (getData): Likewise.
        (addFlavorListener): Likewise.
        (removeFlavorListener): Likewise.
        (getFlavorListeners): Likewise.
        * java/awt/datatransfer/DataFlavor.java (javaFileListFlavor):
        Construct with mime media-type application/x-java-file-list.
        (DataFlavor(String mimeType, String humanPresentableName)): Call
        constructor that uses given mimeType.
        (getSubType): Reimplemented.
        (getParameter): Handle 'vitual' humanPresentableName parameter.
        (isMimeTypeEqual): Reimplement.
        (isRepresentationClassRemote): Implement.
        (toString): Add formatting.

With this patch all new mauve tests pass. But especially DataFlavor
could use a rewrite. So expect some more changes here, plus some extra
tests and documentation in the future.

Committed,

Mark
-- 
Escape the Java Trap with GNU Classpath!
http://www.gnu.org/philosophy/java-trap.html

Join the community at http://planet.classpath.org/
Index: java/awt/datatransfer/Clipboard.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/datatransfer/Clipboard.java,v
retrieving revision 1.8
diff -u -r1.8 Clipboard.java
--- java/awt/datatransfer/Clipboard.java        2 Jul 2005 20:32:26 -0000       
1.8
+++ java/awt/datatransfer/Clipboard.java        5 Aug 2005 17:23:24 -0000
@@ -1,5 +1,5 @@
 /* Clipboard.java -- Class for transferring data via cut and paste.
-   Copyright (C) 1999, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2001, 2005 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -38,16 +38,21 @@
 
 package java.awt.datatransfer;
 
+import java.io.IOException;
+import java.util.ArrayList;
+
 /**
  * This class allows data to be transferred using a cut and paste type
  * mechanism.
  *
  * @author Aaron M. Renn (address@hidden)
+ * @author Mark J. Wielaard (address@hidden)
  */
 public class Clipboard
 {
   /**
-   * The data being transferred.
+   * The data currently on this clipboard.  For use by
+   * subclasses. Also returned by the public method getContents().
    */
   protected Transferable contents;
 
@@ -57,7 +62,10 @@
   protected ClipboardOwner owner;
 
   // The clipboard name
-  private String name;
+  private final String name;
+
+  // The flavor listeners (most likely small).
+  private final ArrayList listeners = new ArrayList(3);
 
   /**
    * Initializes a new instance of <code>Clipboard</code> with the
@@ -81,7 +89,8 @@
   /**
    * Returns the contents of the clipboard.
    *
-   * @param requestor The object requesting the contents.
+   * @param requestor The object requesting the contents. This
+   * implementation ignores this parameter.
    *
    * @exception IllegalStateException If the clipboard is currently unavailable
    */
@@ -91,24 +100,108 @@
   }
 
   /**
-   * Sets the content and owner of this clipboard.
-   * If the given owner is different from the current owner
-   * then lostOwnership is called on the current owner.
-   * XXX - is this called with the old or new contents.
+   * Sets the content and owner of this clipboard.  If the given owner
+   * is different from the current owner then <code>lostOwnership()</code>
+   * is called on the current owner with the old contents of the given
+   * clipboard.
    *
    * @param contents The new clipboard contents.
    * @param owner The new clipboard owner
    *
    * @exception IllegalStateException If the clipboard is currently unavailable
    */
-  public synchronized void setContents(Transferable contents, ClipboardOwner 
owner)
+  public synchronized void setContents(Transferable contents,
+                                      ClipboardOwner owner)
   {
-    if (this.owner != owner)
-      if (this.owner != null)
-        this.owner.lostOwnership(this, contents);
- 
-    this.owner = owner;
+    Transferable oldContents = getContents(null);
     this.contents = contents;
+    if (this.owner != owner)
+      {
+       ClipboardOwner oldOwner = this.owner;
+       this.owner = owner;
+       if (oldOwner != null)
+         oldOwner.lostOwnership(this, oldContents);
+      }
+
+    FlavorListener[] fs = getFlavorListeners();
+    if (fs.length > 0)
+      {
+       // We are a bit optimistic here. We assume DataFlavors will be
+       // given in the same order. If the number of flavors is
+       // different or the order of the DataFlavors in the list then
+       // fire a change event.
+       boolean newFlavors = ((contents != null && oldContents == null)
+                             || (contents == null && oldContents != null));
+       if (!newFlavors && contents != null && oldContents != null)
+         {
+           DataFlavor[] df1 = contents.getTransferDataFlavors();
+           DataFlavor[] df2 = oldContents.getTransferDataFlavors();
+           newFlavors = df1.length != df2.length;
+           
+           for (int i = 0; !newFlavors && i < df1.length; i++)
+             newFlavors = !df1[i].equals(df2[i]);
+         }
+
+       if (newFlavors)
+         {
+           FlavorEvent e = new FlavorEvent(this);
+           for (int i = 0; i < fs.length; i++)
+             fs[i].flavorsChanged(e);
+         }
+      }
+  }
+
+  public DataFlavor[] getAvailableDataFlavors()
+  {
+    Transferable c = getContents(null);
+    if (c == null)
+      return new DataFlavor[0];
+    else
+      return c.getTransferDataFlavors();
+  }
+
+  public boolean isDataFlavorAvailable(DataFlavor flavor)
+  {
+    DataFlavor[] fs = getAvailableDataFlavors();
+    for (int i = 0; i < fs.length; i++)
+      if (flavor.equals(fs[i]))
+       return true;
+
+    return false;
   }
-}
 
+  public Object getData(DataFlavor flavor)
+    throws UnsupportedFlavorException, IOException
+  {
+    Transferable c = getContents(null);
+    if (c == null)
+      throw new UnsupportedFlavorException(flavor);
+    else
+      return c.getTransferData(flavor);
+  }
+
+  public void addFlavorListener(FlavorListener listener)
+  {
+    synchronized(listeners)
+      {
+       listeners.add(listener);
+      }
+  }
+
+  public void removeFlavorListener(FlavorListener listener)
+  {
+    synchronized(listeners)
+      {
+       listeners.remove(listener);
+      }
+  }
+
+  public FlavorListener[] getFlavorListeners()
+  {
+    synchronized(listeners)
+      {
+       return (FlavorListener[])
+         listeners.toArray(new FlavorListener[listeners.size()]);
+      }
+  }
+}
Index: java/awt/datatransfer/DataFlavor.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/datatransfer/DataFlavor.java,v
retrieving revision 1.24
diff -u -r1.24 DataFlavor.java
--- java/awt/datatransfer/DataFlavor.java       2 Jul 2005 20:32:26 -0000       
1.24
+++ java/awt/datatransfer/DataFlavor.java       5 Aug 2005 17:23:25 -0000
@@ -49,6 +49,7 @@
 import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
+import java.rmi.Remote;
 
 /**
  * This class represents a particular data format used for transferring
@@ -127,10 +128,9 @@
 
   javaFileListFlavor
       = new DataFlavor(java.util.List.class,
+                      "application/x-java-file-list; class=java.util.List",
                       "Java File List");
 
-  // javaFileListFlavor.mimeType = "application/x-java-file-list";
-
   imageFlavor
       = new DataFlavor(java.awt.Image.class,
                        "Java Image");
@@ -335,7 +335,8 @@
 public
 DataFlavor(String mimeType, String humanPresentableName)
 {
-  this (getRepresentationClassFromMime (mimeType, null), humanPresentableName);
+  this (getRepresentationClassFromMime (mimeType, null),
+       mimeType, humanPresentableName);
 }
 
 /*************************************************************************/
@@ -426,17 +427,15 @@
 public String
 getSubType()
 {
-  int idx = mimeType.indexOf("/");
-  if (idx == -1)
-    return("");
-
-  String subtype = mimeType.substring(idx + 1);
-
-  idx = subtype.indexOf(" ");
-  if (idx == -1)
-    return(subtype);
+  int start = mimeType.indexOf("/");
+  if (start == -1)
+    return "";
+
+  int end = mimeType.indexOf(";", start + 1);
+  if (end == -1)
+    return mimeType.substring(start + 1);
   else
-    return(subtype.substring(0, idx));
+    return mimeType.substring(start + 1, end);
 }
 
 /*************************************************************************/
@@ -480,6 +479,9 @@
 public String
 getParameter(String paramName)
 {
+  if ("humanPresentableName".equals(paramName))
+    return getHumanPresentableName();
+
   return getParameter(paramName, mimeType);
 }
 
@@ -500,21 +502,28 @@
 
 /**
  * Tests the MIME type of this object for equality against the specified
- * MIME type.
+ * MIME type. Ignores parameters.
  *
  * @param mimeType The MIME type to test against.
  *
  * @return <code>true</code> if the MIME type is equal to this object's
- * MIME type, <code>false</code> otherwise.
+ * MIME type (ignoring parameters), <code>false</code> otherwise.
  *
  * @exception NullPointerException If mimeType is null.
  */
 public boolean
 isMimeTypeEqual(String mimeType)
 {
-  // FIXME: Need to handle default attributes and parameters
+  String mime = getMimeType();
+  int i = mime.indexOf(";");
+  if (i != -1)
+    mime = mime.substring(0, i);
+
+  i = mimeType.indexOf(";");
+  if (i != -1)
+    mimeType = mimeType.substring(0, i);
 
-  return(this.mimeType.equals(mimeType));
+  return mime.equals(mimeType);
 }
 
 /*************************************************************************/
@@ -599,8 +608,7 @@
 public boolean
 isRepresentationClassRemote()
 {
-  // FIXME: Implement
-  throw new RuntimeException("Not implemented");
+  return Remote.class.isAssignableFrom (representationClass);
 }
 
 /*************************************************************************/
@@ -852,12 +860,11 @@
 public String
 toString()
 {
-  return("DataFlavor[representationClass="
-         + representationClass.getName()
-         + ",mimeType="
-         + mimeType
-         + "humanPresentableName="
-         + humanPresentableName);
+  return(getClass().getName()
+        + "[representationClass=" + getRepresentationClass().getName()
+         + ",mimeType=" + getMimeType()
+         + ",humanPresentableName=" + getHumanPresentableName()
+        + "]");
 }
 
 /*************************************************************************/
Index: java/awt/datatransfer/FlavorEvent.java
===================================================================
RCS file: java/awt/datatransfer/FlavorEvent.java
diff -N java/awt/datatransfer/FlavorEvent.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ java/awt/datatransfer/FlavorEvent.java      5 Aug 2005 17:23:25 -0000
@@ -0,0 +1,55 @@
+/* FlavorEvent -- Event indicating a ClipBoard has different flavors available.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package java.awt.datatransfer;
+
+import java.util.EventObject;
+
+/**
+ * Event indicating a Clipboard has different flavors available.
+ * Fired by a ClipBoard for registered FlavorListeners.
+ *
+ * @author Mark J. Wielaard (address@hidden)
+ */
+public class FlavorEvent extends EventObject
+{
+  public FlavorEvent(Clipboard board)
+  {
+    super(board);
+  }
+}
Index: java/awt/datatransfer/FlavorListener.java
===================================================================
RCS file: java/awt/datatransfer/FlavorListener.java
diff -N java/awt/datatransfer/FlavorListener.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ java/awt/datatransfer/FlavorListener.java   5 Aug 2005 17:23:25 -0000
@@ -0,0 +1,54 @@
+/* FlavorListener -- Interface for tagging an interest in FlavorEvents. 
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package java.awt.datatransfer;
+
+import java.util.EventListener;
+
+/**
+ * Interface for tagging an interest in FlavorEvents by a class.  The
+ * flavorsChanged() method will be called with a FlavorEvent pointing
+ * to the Clipboard which has content in different Flavors available.
+ *
+ * @author Mark J. Wielaard (address@hidden)
+ */
+public interface FlavorListener
+  extends EventListener
+{
+  void flavorsChanged(FlavorEvent event);
+}

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]