Class ContentProviders

java.lang.Object
org.eclipse.swt.dnd.ContentProviders

class ContentProviders extends Object
Manages GdkContentProviders and the (de)serializers they rely on As the process only has a single collection of serializers/deserializers/content providers the life-cycle of this class is that of the process, which means this class is a singleton. The challenge of handling content providers in GTK4 is that it is all based around serializing/deserializing GValue and registering GTypes as being transferable. However we want to keep the Java objects in the Java space until we are ready to transfer them. Therefore, we register GTypes associated with each Transfer, those GTypes are boxed GTypes which box a simple long value. That long value we use as an index to connect back to Java object.
  • Field Details

    • instance

      private static ContentProviders instance
      The life-time of the serializers in GTK4 (Gdk) are the life-time of the process so we use a singleton to store the mapping data that is needed.
    • ID_TO_CONTENTTYPE

      private final Map<Integer,String> ID_TO_CONTENTTYPE
      The types registered with registerType(String) on GTK4. Using TreeMap to make the maps sorted to ease debugging. Map of Transfer.id type -> Transfer's type name.
    • CONTENTTYPE_TO_ID

      private final Map<String,Integer> CONTENTTYPE_TO_ID
      The types registered with registerType(String) on GTK4. Using TreeMap to make the maps sorted to ease debugging. Map of Transfer's type name -> Transfer.id type
    • typeIndex

      private int typeIndex
      Next index to use for registerType(String)
    • deserializedData

      private final Map<Long,Object> deserializedData
      This the data that has been deserialized and associates source id (which is a number greater than those in ContentProviders.CLIPBOARD_DATA so they can be differentied) It is expected that the map is normally empty and only has data while a gdk_clipboard_read_value_async (or similar) is active. Map of sourceId -> associated data
    • nextDeserializedDataId

      private long nextDeserializedDataId
    • gdkContentSerializeFunc

      private Callback gdkContentSerializeFunc
    • gdkContentDeserializeFunc

      private Callback gdkContentDeserializeFunc
    • registeredTransfers

      private Map<String,Transfer> registeredTransfers
      All transfers registered with the GTK serializers Key is the transfer obtained with transferKey(Transfer)
    • registeredTransferGtypes

      private Map<String,Long> registeredTransferGtypes
      All transfer IDs registered with the GTK serializers Key is the transfer obtained with transferKey(Transfer) Value is the Gtype of of the transfer
  • Constructor Details

    • ContentProviders

      private ContentProviders()
  • Method Details

    • getInstance

      public static ContentProviders getInstance()
    • registerType

      public int registerType(String formatName)
    • createContentProviders

      public long createContentProviders(Object[] data, Transfer[] transfers, ContentProviders.CLIPBOARD_DATA id)
      Create a set of content providers (GdkContentProviders) for the given SWT Transfer Types and data, assigned to the given clipboard.
      Returns:
      the content providers. The data is owned by the called function.
    • gdkContentSerializeFunc

      private void gdkContentSerializeFunc(long pSerializer)
      Convert the Java object to data with transfer.javaToNative and then write all the data to the output stream. Use the gvalue of the serializer to identify which Transfer and object to complete the transfer with. Referenced by Callback gdkContentSerializeFunc
    • gdkContentDeserializeFunc

      private void gdkContentDeserializeFunc(long pDeserializer)
      Use the gtype + mime_type of the deserializer to identify which Transfer to complete the transfer with. Read all the data from the source (clipboard or drag source) into this memory stream and then, on success, convert the data to Java object with transfer.nativeToJava. Then place the converted object deserializedData and associate the gvalue with the deserializedData by using nextDeserializedDataId Referenced by Callback gdkContentSerializeFunc
    • getTypeId

      private int getTypeId(String mime_type)
      Return the type for the given mime type Returns 0 on error (unknown mime_type). Valid type ids are >= 1. See typeIndex
    • getTransfer

      private Transfer getTransfer(long gtype)
      Return the transfer associated with the given gtype Return null on error (unknown gtype)
    • transferKey

      private String transferKey(Transfer transfer)
      Create a string key for the transfer that can be used as a key for Map and as a GType
    • registerTransfer

      private void registerTransfer(Transfer transfer)
      Register the transfer in the GTK land if not registered already. See class comment for overall design and connection between different parts.
    • getGType

      public long getGType(Transfer transfer)
      Get the GType for the given transfer, and registerTransfer(Transfer) if needed The gtype can then be used to request data from GTK with methods such as gdk_clipboard_read_value_async
    • getObject

      public Object getObject(long gvalue)
      Return the Java object associated with the given GValue
    • clone

      private Object clone(Transfer transfer, Object object)
      When calling gdk_clipboard_read_value_async and related functions, if the clipboard is owned locally it returns the instance that was placed on the clipboard and avoid the serialization and deserialization. This presents a problem for SWT because SWT has always returned a copy (javaToNative/nativeToJava) of the object between the setContents and the getContents. For some objects, like String it may be ok to return the same instance since String is immutable, but for others, such as ImageData or custom types a copy definitely needs to be made to preserve the pre-existing behavior. However, to maintain compatibility with the pre-existing implementations for even String type Transfers that have returned a copy, we copy String types too in this implementation.