Java Code Examples for java.awt.datatransfer.SystemFlavorMap

The following are top voted examples for showing how to use java.awt.datatransfer.SystemFlavorMap. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: openjdk-jdk10   File: GetNativesForFlavorTest.java   Source Code and License 6 votes vote down vote up
/**
 * Verifies that the encoded native is not added if there are other
 * natives mapped to this DataFlavor.
 */
public static void test1() throws ClassNotFoundException {
    final DataFlavor flavor =
            new DataFlavor("text/plain-TEST; charset=Unicode");

    final java.util.List natives = fm.getNativesForFlavor(flavor);

    if (natives.size() > 1) {
        for (final Iterator i = natives.iterator(); i.hasNext(); ) {
            String element = (String) i.next();
            if (SystemFlavorMap.isJavaMIMEType(element)) {
                throw new RuntimeException("getFlavorsForNative() returns: "
                        + natives);
            }
        }
    }
}
 
Example 2
Project: openjdk-jdk10   File: GetNativesForFlavorTest.java   Source Code and License 6 votes vote down vote up
/**
 * Verifies that the encoded native is added only for DataFlavors
 * that has no mappings and that DataFlavor is properly encoded.
 */
public static void test4() throws ClassNotFoundException {
    final DataFlavor flavor =
            new DataFlavor("unknown/unknown");

    final java.util.List natives = fm.getNativesForFlavor(flavor);

    if (natives.size() == 1) {
        String element = (String) natives.get(0);
        if (SystemFlavorMap.isJavaMIMEType(element)) {
            final DataFlavor decodedFlavor =
                    SystemFlavorMap.decodeDataFlavor(element);
            if (!flavor.equals(decodedFlavor)) {
                System.err.println("DataFlavor is not properly incoded:");
                System.err.println("    encoded flavor: " + flavor);
                System.err.println("    decoded flavor: " + decodedFlavor);
                throw new RuntimeException("getFlavorsForNative() returns:"
                        + natives);
            }
        }
    } else {
        throw new RuntimeException("getFlavorsForNative() returns:"
                + natives);
    }
}
 
Example 3
Project: openjdk-jdk10   File: InvalidMapArgumentsTest.java   Source Code and License 6 votes vote down vote up
public void initMappings() {
    //initialize mapping variables used in this test
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    //create a DataFlavor with valid parameters (mimeType, humanPresentableName)
    test_flav = new DataFlavor("text/plain; charset=ascii","ASCII Flavor");

    //create a String native
    test_nat = "TEXT_TEST";

    //create a DataFlavor array
    test_flavors = new DataFlavor[] {test_flav};

    //create a String native array
    test_natives = new String[] {test_nat};
}
 
Example 4
Project: openjdk-jdk10   File: GetNativesForNewFlavorTest.java   Source Code and License 6 votes vote down vote up
public void doTest() throws Exception {
    // Initialize DataFlavors and arrays used for test data
    initMappings();

    boolean passed = true;
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Get all the native strings and preferred DataFlavor mappings
    hash = new Hashtable(flavorMap.getFlavorsForNatives(null));
    hashSize = hash.size();

    // GetNativesForFlavor using unknown DataFlavor (verify 2-way mapping)
    //
    // If a new DataFlavor is specified, the method should establish a mapping
    // in both directions between specified DataFlavor and an encoded
    // version of its MIME type as its native.
    System.out.println("GetNativesForFlavor using new DataFlavor");

    comp1 = new Vector(Arrays.asList(test_natives_set));
    comp2 = new Vector(flavorMap.getNativesForFlavor(test_flavor1));

    comp3 = new Vector(Arrays.asList(test_flavors_set));
    comp4 = new Vector(flavorMap.getFlavorsForNative(test_encoded));

    if ( !comp1.equals(comp2) || !comp3.equals(comp4) ) {
        throw new RuntimeException("\n*** After passing a new DataFlavor" +
            "\nwith getNativesForFlavor(DataFlavor flav)" +
            "\nthe mapping in both directions was not established.");
    }
    else
       System.out.println("GetNativesForFlavor using new DataFlavor: Test Passes");
}
 
Example 5
Project: openjdk-jdk10   File: GetNativesForNewFlavorTest.java   Source Code and License 6 votes vote down vote up
public void initMappings() throws Exception {
    //initialize mapping variables used in this test
    //create a DataFlavor from Button class
    test_flavor1 = new DataFlavor(Class.forName("java.awt.Button"), "Button");

    //create an Encoded String native using Button class MIME Type
    String buttonMIME = test_flavor1.getMimeType();
    test_encoded = SystemFlavorMap.encodeJavaMIMEType(buttonMIME);

    //create a DataFlavor from the Encoded String native
    test_flavor2 = SystemFlavorMap.decodeDataFlavor(test_encoded);

    //create and initialize DataFlavor arrays
    test_flavors_set = new DataFlavor[] {test_flavor1};

    //create and initialize String native arrays
    test_natives_set = new String[] {test_encoded};
}
 
Example 6
Project: openjdk-jdk10   File: GetFlavorsForNewNativeTest.java   Source Code and License 6 votes vote down vote up
public void initMappings() throws Exception {
   //initialize mapping variables used in this test
  //create an Unencoded String native
  test_native1 = "TEST1";

  //create a DataFlavor from Button class
  test_flavor1 = new DataFlavor(Class.forName("java.awt.Button"), "Button");

  //create an Encoded String native using Button class MIME Type
  String buttonMIME = test_flavor1.getMimeType();
  test_encoded = SystemFlavorMap.encodeJavaMIMEType(buttonMIME);

  //create a DataFlavor from the Encoded String native
  test_flavor2 = SystemFlavorMap.decodeDataFlavor(test_encoded);

  //create and initialize DataFlavor arrays
  test_flavors_set = new DataFlavor[] {test_flavor2};

  //create and initialize String native arrays
  test_natives_set = new String[] {test_encoded};
}
 
Example 7
Project: openjdk9   File: ConstructFlavoredObjectTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
        SystemFlavorMap systemFlavorMap = (SystemFlavorMap) SystemFlavorMap.
                getDefaultFlavorMap();
        systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
        systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);

        TransferHandler transferHandler = new TransferHandler("Test Handler");

        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        transferHandler.exportToClipboard(new JLabel("Test"), clipboard,
                TransferHandler.COPY);

        Object clipboardData = clipboard.getData(dataFlavor);

        if (!(clipboardData instanceof MyStringReader)) {
            throw new RuntimeException("Wrong clipboard data!");
        }
    }
 
Example 8
Project: openjdk9   File: GetNativesForFlavorTest.java   Source Code and License 6 votes vote down vote up
/**
 * Verifies that the encoded native is not added if there are other
 * natives mapped to this DataFlavor.
 */
public static void test1() throws ClassNotFoundException {
    final DataFlavor flavor =
            new DataFlavor("text/plain-TEST; charset=Unicode");

    final java.util.List natives = fm.getNativesForFlavor(flavor);

    if (natives.size() > 1) {
        for (final Iterator i = natives.iterator(); i.hasNext(); ) {
            String element = (String) i.next();
            if (SystemFlavorMap.isJavaMIMEType(element)) {
                throw new RuntimeException("getFlavorsForNative() returns: "
                        + natives);
            }
        }
    }
}
 
Example 9
Project: openjdk9   File: GetNativesForFlavorTest.java   Source Code and License 6 votes vote down vote up
/**
 * Verifies that the encoded native is added only for DataFlavors
 * that has no mappings and that DataFlavor is properly encoded.
 */
public static void test4() throws ClassNotFoundException {
    final DataFlavor flavor =
            new DataFlavor("unknown/unknown");

    final java.util.List natives = fm.getNativesForFlavor(flavor);

    if (natives.size() == 1) {
        String element = (String) natives.get(0);
        if (SystemFlavorMap.isJavaMIMEType(element)) {
            final DataFlavor decodedFlavor =
                    SystemFlavorMap.decodeDataFlavor(element);
            if (!flavor.equals(decodedFlavor)) {
                System.err.println("DataFlavor is not properly incoded:");
                System.err.println("    encoded flavor: " + flavor);
                System.err.println("    decoded flavor: " + decodedFlavor);
                throw new RuntimeException("getFlavorsForNative() returns:"
                        + natives);
            }
        }
    } else {
        throw new RuntimeException("getFlavorsForNative() returns:"
                + natives);
    }
}
 
Example 10
Project: openjdk9   File: InvalidMapArgumentsTest.java   Source Code and License 6 votes vote down vote up
public void initMappings() {
    //initialize mapping variables used in this test
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    //create a DataFlavor with valid parameters (mimeType, humanPresentableName)
    test_flav = new DataFlavor("text/plain; charset=ascii","ASCII Flavor");

    //create a String native
    test_nat = "TEXT_TEST";

    //create a DataFlavor array
    test_flavors = new DataFlavor[] {test_flav};

    //create a String native array
    test_natives = new String[] {test_nat};
}
 
Example 11
Project: openjdk9   File: GetNativesForNewFlavorTest.java   Source Code and License 6 votes vote down vote up
public void initMappings() throws Exception {
    //initialize mapping variables used in this test
    //create a DataFlavor from Button class
    test_flavor1 = new DataFlavor(Class.forName("java.awt.Button"), "Button");

    //create an Encoded String native using Button class MIME Type
    String buttonMIME = test_flavor1.getMimeType();
    test_encoded = SystemFlavorMap.encodeJavaMIMEType(buttonMIME);

    //create a DataFlavor from the Encoded String native
    test_flavor2 = SystemFlavorMap.decodeDataFlavor(test_encoded);

    //create and initialize DataFlavor arrays
    test_flavors_set = new DataFlavor[] {test_flavor1};

    //create and initialize String native arrays
    test_natives_set = new String[] {test_encoded};
}
 
Example 12
Project: openjdk9   File: GetFlavorsForNewNativeTest.java   Source Code and License 6 votes vote down vote up
public void initMappings() throws Exception {
   //initialize mapping variables used in this test
  //create an Unencoded String native
  test_native1 = "TEST1";

  //create a DataFlavor from Button class
  test_flavor1 = new DataFlavor(Class.forName("java.awt.Button"), "Button");

  //create an Encoded String native using Button class MIME Type
  String buttonMIME = test_flavor1.getMimeType();
  test_encoded = SystemFlavorMap.encodeJavaMIMEType(buttonMIME);

  //create a DataFlavor from the Encoded String native
  test_flavor2 = SystemFlavorMap.decodeDataFlavor(test_encoded);

  //create and initialize DataFlavor arrays
  test_flavors_set = new DataFlavor[] {test_flavor2};

  //create and initialize String native arrays
  test_natives_set = new String[] {test_encoded};
}
 
Example 13
Project: javify   File: DropTarget.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a <code>DropTarget</code> object.
 *
 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 * returns true.
 */
public DropTarget (Component c, int i, DropTargetListener dtl, boolean b,
                   FlavorMap fm)
{
  if (GraphicsEnvironment.isHeadless ())
    throw new HeadlessException ();

  setComponent(c);
  setDefaultActions(i);
  dropTargetListener = dtl;

  if (fm == null)
    flavorMap = SystemFlavorMap.getDefaultFlavorMap();
  else
    flavorMap = fm;

  setActive (b);

  if (c != null)
    c.setDropTarget(this);
}
 
Example 14
Project: intellij-ce-playground   File: FileCopyPasteUtil.java   Source Code and License 6 votes vote down vote up
public static DataFlavor createDataFlavor(@NotNull final String mimeType, @Nullable final Class<?> klass, final boolean register) {
  try {
    final DataFlavor flavor =
      klass != null ? new DataFlavor(mimeType + ";class=" + klass.getName(), null, klass.getClassLoader()) : new DataFlavor(mimeType);

    if (register) {
      final FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
      if (map instanceof SystemFlavorMap) {
        ((SystemFlavorMap)map).addUnencodedNativeForFlavor(flavor, mimeType);
      }
    }

    return flavor;
  }
  catch (ClassNotFoundException e) {
    LOG.error(e);
    //noinspection ConstantConditions
    return null;
  }
}
 
Example 15
Project: jvm-stm   File: DropTarget.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a <code>DropTarget</code> object.
 *
 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 * returns true.
 */
public DropTarget (Component c, int i, DropTargetListener dtl, boolean b,
                   FlavorMap fm)
{
  if (GraphicsEnvironment.isHeadless ())
    throw new HeadlessException ();

  setComponent(c);
  setDefaultActions(i);
  dropTargetListener = dtl;
  
  if (fm == null)
    flavorMap = SystemFlavorMap.getDefaultFlavorMap();
  else
    flavorMap = fm;
  
  setActive (b);
  
  if (c != null)
    c.setDropTarget(this);
}
 
Example 16
Project: jo-widgets   File: SwingClipboard.java   Source Code and License 6 votes vote down vote up
public SwingClipboard() {
    super(SwingOptions.getClipbaordPollingMillis());
    if (!SwingUtilities.isEventDispatchThread()) {
        throw new IllegalStateException("The clipboard must be created in the event dispatcher thread");
    }

    this.systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    final FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
    if (map instanceof SystemFlavorMap) {
        final SystemFlavorMap systemMap = (SystemFlavorMap) map;
        systemMap.addFlavorForUnencodedNative(TransferContainer.MIME_TYPE, TRANSFER_CONTAINER_FLAVOR);
        systemMap.addUnencodedNativeForFlavor(TRANSFER_CONTAINER_FLAVOR, TransferContainer.MIME_TYPE);
    }

    checkContentChanged();
}
 
Example 17
Project: cn1   File: DataSource.java   Source Code and License 6 votes vote down vote up
private static List<String> getNativesForFlavors(DataFlavor[] flavors) {
    ArrayList<String> natives = new ArrayList<String>();
    
    SystemFlavorMap flavorMap = 
        (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
    
    for (int i = 0; i < flavors.length; i++) {
        List<String> list = flavorMap.getNativesForFlavor(flavors[i]);
        for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            String nativeFormat = it.next();
            if (!natives.contains(nativeFormat)) {
                natives.add(nativeFormat);
            }
        }
    }
    return natives;
}
 
Example 18
Project: cn1   File: DataSnapshot.java   Source Code and License 6 votes vote down vote up
/**
 * @param dataObject
 */
public DataSnapshot(DataProvider data) {
    nativeFormats = data.getNativeFormats();
    text = data.getText();
    fileList = data.getFileList();
    url = data.getURL();
    html = data.getHTML();
    rawBitmap = data.getRawBitmap();
    
    serializedObjects = Collections.synchronizedMap(new HashMap<Class<?>, byte[]>());
    
    for (int i = 0; i < nativeFormats.length; i++) {
        DataFlavor df = null;
        try {
            df = SystemFlavorMap.decodeDataFlavor(nativeFormats[i]);
        } catch (ClassNotFoundException e) {}
        if (df != null) {
            Class<?> clazz = df.getRepresentationClass();
            byte[] bytes = data.getSerializedObject(clazz);
            if (bytes != null) {
                serializedObjects.put(clazz, bytes);
            }
        }
    }
    // TODO: refine the list of native formats
}
 
Example 19
Project: cn1   File: DataSnapshot.java   Source Code and License 6 votes vote down vote up
public boolean isNativeFormatAvailable(String nativeFormat) {
    if (nativeFormat == null) {
        return false;
    }
    if (nativeFormat.equals(FORMAT_TEXT)) {
        return (text != null);
    }
    if (nativeFormat.equals(FORMAT_FILE_LIST)) {
        return (fileList != null);
    }
    if (nativeFormat.equals(FORMAT_URL)) {
        return (url != null);
    }
    if (nativeFormat.equals(FORMAT_HTML)) {
        return (html != null);
    }
    if (nativeFormat.equals(FORMAT_IMAGE)) {
        return (rawBitmap != null);
    }
    try {
        DataFlavor df = SystemFlavorMap.decodeDataFlavor(nativeFormat);
        return serializedObjects.containsKey(df.getRepresentationClass());
    } catch (Exception e) {
        return false;
    }
}
 
Example 20
Project: cn1   File: DataProxy.java   Source Code and License 6 votes vote down vote up
private Object getSerializedObject(DataFlavor f)
        throws IOException, UnsupportedFlavorException {
    String nativeFormat = SystemFlavorMap.encodeDataFlavor(f);
    if ((nativeFormat == null) || 
            !data.isNativeFormatAvailable(nativeFormat)) {
        throw new UnsupportedFlavorException(f);
    }
    byte bytes[] = data.getSerializedObject(f.getRepresentationClass());
    if (bytes == null) {
        // awt.4F=Data is not available
        throw new IOException(Messages.getString("awt.4F")); //$NON-NLS-1$
    }
    ByteArrayInputStream str = new ByteArrayInputStream(bytes);
    try {
        return new ObjectInputStream(str).readObject();
    } catch (ClassNotFoundException ex) {
        throw new IOException(ex.getMessage());
    }
}
 
Example 21
Project: JamVM-PH   File: DropTarget.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a <code>DropTarget</code> object.
 *
 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 * returns true.
 */
public DropTarget (Component c, int i, DropTargetListener dtl, boolean b,
                   FlavorMap fm)
{
  if (GraphicsEnvironment.isHeadless ())
    throw new HeadlessException ();

  setComponent(c);
  setDefaultActions(i);
  dropTargetListener = dtl;
  
  if (fm == null)
    flavorMap = SystemFlavorMap.getDefaultFlavorMap();
  else
    flavorMap = fm;
  
  setActive (b);
  
  if (c != null)
    c.setDropTarget(this);
}
 
Example 22
Project: r2cat   File: VectorGraphicsTransferable.java   Source Code and License 6 votes vote down vote up
private static void addType(Map result, String atom, String mimeType, String description, String className)
{
   try
   {
      DataFlavor df = new DataFlavor(mimeType, description);
      SystemFlavorMap map =  (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
      map.addUnencodedNativeForFlavor(df, atom);

      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Class cls = loader == null ? Class.forName(className) : loader.loadClass(className);
      ExportFileType type = (ExportFileType) cls.newInstance();

      result.put(df,type);
   }
   catch (Throwable x)
   {
      System.err.println("Unable to install flavor for mime type '"+mimeType+"' (this is expected if not using JDK 1.4)");
   }
}
 
Example 23
Project: tools-idea   File: FileCopyPasteUtil.java   Source Code and License 6 votes vote down vote up
public static DataFlavor createDataFlavor(@NotNull final String mimeType, @Nullable final Class<?> klass, final boolean register) {
  try {
    final String typeString = klass != null ? mimeType + ";class=" + klass.getName() : mimeType;
    final DataFlavor flavor = new DataFlavor(typeString);

    if (register) {
      final FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
      if (map instanceof SystemFlavorMap) {
        ((SystemFlavorMap)map).addUnencodedNativeForFlavor(flavor, mimeType);
      }
    }

    return flavor;
  }
  catch (ClassNotFoundException e) {
    LOG.error(e);
    //noinspection ConstantConditions
    return null;
  }
}
 
Example 24
Project: classpath   File: DropTarget.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a <code>DropTarget</code> object.
 *
 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 * returns true.
 */
public DropTarget (Component c, int i, DropTargetListener dtl, boolean b,
                   FlavorMap fm)
{
  if (GraphicsEnvironment.isHeadless ())
    throw new HeadlessException ();

  setComponent(c);
  setDefaultActions(i);
  dropTargetListener = dtl;

  if (fm == null)
    flavorMap = SystemFlavorMap.getDefaultFlavorMap();
  else
    flavorMap = fm;

  setActive (b);

  if (c != null)
    c.setDropTarget(this);
}
 
Example 25
Project: freeVM   File: DataSource.java   Source Code and License 6 votes vote down vote up
private static List<String> getNativesForFlavors(DataFlavor[] flavors) {
    ArrayList<String> natives = new ArrayList<String>();
    
    SystemFlavorMap flavorMap = 
        (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
    
    for (int i = 0; i < flavors.length; i++) {
        List<String> list = flavorMap.getNativesForFlavor(flavors[i]);
        for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            String nativeFormat = it.next();
            if (!natives.contains(nativeFormat)) {
                natives.add(nativeFormat);
            }
        }
    }
    return natives;
}
 
Example 26
Project: freeVM   File: DataSnapshot.java   Source Code and License 6 votes vote down vote up
/**
 * @param dataObject
 */
public DataSnapshot(DataProvider data) {
    nativeFormats = data.getNativeFormats();
    text = data.getText();
    fileList = data.getFileList();
    url = data.getURL();
    html = data.getHTML();
    rawBitmap = data.getRawBitmap();
    
    serializedObjects = Collections.synchronizedMap(new HashMap<Class<?>, byte[]>());
    
    for (int i = 0; i < nativeFormats.length; i++) {
        DataFlavor df = null;
        try {
            df = SystemFlavorMap.decodeDataFlavor(nativeFormats[i]);
        } catch (ClassNotFoundException e) {}
        if (df != null) {
            Class<?> clazz = df.getRepresentationClass();
            byte[] bytes = data.getSerializedObject(clazz);
            if (bytes != null) {
                serializedObjects.put(clazz, bytes);
            }
        }
    }
    // TODO: refine the list of native formats
}
 
Example 27
Project: freeVM   File: DataSnapshot.java   Source Code and License 6 votes vote down vote up
public boolean isNativeFormatAtailable(String nativeFormat) {
    if (nativeFormat == null) {
        return false;
    }
    if (nativeFormat.equals(FORMAT_TEXT)) {
        return (text != null);
    }
    if (nativeFormat.equals(FORMAT_FILE_LIST)) {
        return (fileList != null);
    }
    if (nativeFormat.equals(FORMAT_URL)) {
        return (url != null);
    }
    if (nativeFormat.equals(FORMAT_HTML)) {
        return (html != null);
    }
    if (nativeFormat.equals(FORMAT_IMAGE)) {
        return (rawBitmap != null);
    }
    try {
        DataFlavor df = SystemFlavorMap.decodeDataFlavor(nativeFormat);
        return serializedObjects.containsKey(df.getRepresentationClass());
    } catch (Exception e) {
        return false;
    }
}
 
Example 28
Project: freeVM   File: DataProxy.java   Source Code and License 6 votes vote down vote up
private Object getSerializedObject(DataFlavor f)
        throws IOException, UnsupportedFlavorException {
    String nativeFormat = SystemFlavorMap.encodeDataFlavor(f);
    if ((nativeFormat == null) || 
            !data.isNativeFormatAtailable(nativeFormat)) {
        throw new UnsupportedFlavorException(f);
    }
    byte bytes[] = data.getSerializedObject(f.getRepresentationClass());
    if (bytes == null) {
        // awt.4F=Data is not available
        throw new IOException(Messages.getString("awt.4F")); //$NON-NLS-1$
    }
    ByteArrayInputStream str = new ByteArrayInputStream(bytes);
    try {
        return new ObjectInputStream(str).readObject();
    } catch (ClassNotFoundException ex) {
        throw new IOException(ex.getMessage());
    }
}
 
Example 29
Project: freeVM   File: DataSource.java   Source Code and License 6 votes vote down vote up
private static List<String> getNativesForFlavors(DataFlavor[] flavors) {
    ArrayList<String> natives = new ArrayList<String>();
    
    SystemFlavorMap flavorMap = 
        (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
    
    for (int i = 0; i < flavors.length; i++) {
        List<String> list = flavorMap.getNativesForFlavor(flavors[i]);
        for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            String nativeFormat = it.next();
            if (!natives.contains(nativeFormat)) {
                natives.add(nativeFormat);
            }
        }
    }
    return natives;
}
 
Example 30
Project: freeVM   File: DataSnapshot.java   Source Code and License 6 votes vote down vote up
/**
 * @param dataObject
 */
public DataSnapshot(DataProvider data) {
    nativeFormats = data.getNativeFormats();
    text = data.getText();
    fileList = data.getFileList();
    url = data.getURL();
    html = data.getHTML();
    rawBitmap = data.getRawBitmap();
    
    serializedObjects = Collections.synchronizedMap(new HashMap<Class<?>, byte[]>());
    
    for (int i = 0; i < nativeFormats.length; i++) {
        DataFlavor df = null;
        try {
            df = SystemFlavorMap.decodeDataFlavor(nativeFormats[i]);
        } catch (ClassNotFoundException e) {}
        if (df != null) {
            Class<?> clazz = df.getRepresentationClass();
            byte[] bytes = data.getSerializedObject(clazz);
            if (bytes != null) {
                serializedObjects.put(clazz, bytes);
            }
        }
    }
    // TODO: refine the list of native formats
}
 
Example 31
Project: freeVM   File: DataSnapshot.java   Source Code and License 6 votes vote down vote up
public boolean isNativeFormatAvailable(String nativeFormat) {
    if (nativeFormat == null) {
        return false;
    }
    if (nativeFormat.equals(FORMAT_TEXT)) {
        return (text != null);
    }
    if (nativeFormat.equals(FORMAT_FILE_LIST)) {
        return (fileList != null);
    }
    if (nativeFormat.equals(FORMAT_URL)) {
        return (url != null);
    }
    if (nativeFormat.equals(FORMAT_HTML)) {
        return (html != null);
    }
    if (nativeFormat.equals(FORMAT_IMAGE)) {
        return (rawBitmap != null);
    }
    try {
        DataFlavor df = SystemFlavorMap.decodeDataFlavor(nativeFormat);
        return serializedObjects.containsKey(df.getRepresentationClass());
    } catch (Exception e) {
        return false;
    }
}
 
Example 32
Project: freeVM   File: DataProxy.java   Source Code and License 6 votes vote down vote up
private Object getSerializedObject(DataFlavor f)
        throws IOException, UnsupportedFlavorException {
    String nativeFormat = SystemFlavorMap.encodeDataFlavor(f);
    if ((nativeFormat == null) || 
            !data.isNativeFormatAvailable(nativeFormat)) {
        throw new UnsupportedFlavorException(f);
    }
    byte bytes[] = data.getSerializedObject(f.getRepresentationClass());
    if (bytes == null) {
        // awt.4F=Data is not available
        throw new IOException(Messages.getString("awt.4F")); //$NON-NLS-1$
    }
    ByteArrayInputStream str = new ByteArrayInputStream(bytes);
    try {
        return new ObjectInputStream(str).readObject();
    } catch (ClassNotFoundException ex) {
        throw new IOException(ex.getMessage());
    }
}
 
Example 33
Project: consulo   File: FileCopyPasteUtil.java   Source Code and License 6 votes vote down vote up
public static DataFlavor createDataFlavor(@Nonnull final String mimeType, @Nullable final Class<?> klass, final boolean register) {
  try {
    final DataFlavor flavor =
            klass != null ? new DataFlavor(mimeType + ";class=" + klass.getName(), null, klass.getClassLoader()) : new DataFlavor(mimeType);

    if (register) {
      final FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
      if (map instanceof SystemFlavorMap) {
        ((SystemFlavorMap)map).addUnencodedNativeForFlavor(flavor, mimeType);
      }
    }

    return flavor;
  }
  catch (ClassNotFoundException e) {
    LOG.error(e);
    //noinspection ConstantConditions
    return null;
  }
}
 
Example 34
Project: OpenJSharp   File: DropTarget.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a new DropTarget given the <code>Component</code>
 * to associate itself with, an <code>int</code> representing
 * the default acceptable action(s) to
 * support, a <code>DropTargetListener</code>
 * to handle event processing, a <code>boolean</code> indicating
 * if the <code>DropTarget</code> is currently accepting drops, and
 * a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
 * <P>
 * The Component will receive drops only if it is enabled.
 * @param c         The <code>Component</code> with which this <code>DropTarget</code> is associated
 * @param ops       The default acceptable actions for this <code>DropTarget</code>
 * @param dtl       The <code>DropTargetListener</code> for this <code>DropTarget</code>
 * @param act       Is the <code>DropTarget</code> accepting drops.
 * @param fm        The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 *            returns true
 * @see java.awt.GraphicsEnvironment#isHeadless
 */
public DropTarget(Component c, int ops, DropTargetListener dtl,
                  boolean act, FlavorMap fm)
    throws HeadlessException
{
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }

    component = c;

    setDefaultActions(ops);

    if (dtl != null) try {
        addDropTargetListener(dtl);
    } catch (TooManyListenersException tmle) {
        // do nothing!
    }

    if (c != null) {
        c.setDropTarget(this);
        setActive(act);
    }

    if (fm != null) {
        flavorMap = fm;
    } else {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }
}
 
Example 35
Project: OpenJSharp   File: DragSource.java   Source Code and License 5 votes vote down vote up
/**
 * Deserializes this <code>DragSource</code>. This method first performs
 * default deserialization. Next, this object's <code>FlavorMap</code> is
 * deserialized by using the next object in the stream.
 * If the resulting <code>FlavorMap</code> is <code>null</code>, this
 * object's <code>FlavorMap</code> is set to the default FlavorMap for
 * this thread's <code>ClassLoader</code>.
 * Next, this object's listeners are deserialized by reading a
 * <code>null</code>-terminated sequence of 0 or more key/value pairs
 * from the stream:
 * <ul>
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceListenerK</code>, a <code>DragSourceListener</code> is
 * deserialized using the corresponding value object and added to this
 * <code>DragSource</code>.
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceMotionListenerK</code>, a
 * <code>DragSourceMotionListener</code> is deserialized using the
 * corresponding value object and added to this <code>DragSource</code>.
 * <li>Otherwise, the key/value pair is skipped.
 * </ul>
 *
 * @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
 * @since 1.4
 */
private void readObject(ObjectInputStream s)
  throws ClassNotFoundException, IOException {
    s.defaultReadObject();

    // 'flavorMap' was written explicitly
    flavorMap = (FlavorMap)s.readObject();

    // Implementation assumes 'flavorMap' is never null.
    if (flavorMap == null) {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
        String key = ((String)keyOrNull).intern();

        if (dragSourceListenerK == key) {
            addDragSourceListener((DragSourceListener)(s.readObject()));
        } else if (dragSourceMotionListenerK == key) {
            addDragSourceMotionListener(
                (DragSourceMotionListener)(s.readObject()));
        } else {
            // skip value for unrecognized key
            s.readObject();
        }
    }
}
 
Example 36
Project: jdk8u-jdk   File: DropTarget.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a new DropTarget given the <code>Component</code>
 * to associate itself with, an <code>int</code> representing
 * the default acceptable action(s) to
 * support, a <code>DropTargetListener</code>
 * to handle event processing, a <code>boolean</code> indicating
 * if the <code>DropTarget</code> is currently accepting drops, and
 * a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
 * <P>
 * The Component will receive drops only if it is enabled.
 * @param c         The <code>Component</code> with which this <code>DropTarget</code> is associated
 * @param ops       The default acceptable actions for this <code>DropTarget</code>
 * @param dtl       The <code>DropTargetListener</code> for this <code>DropTarget</code>
 * @param act       Is the <code>DropTarget</code> accepting drops.
 * @param fm        The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 *            returns true
 * @see java.awt.GraphicsEnvironment#isHeadless
 */
public DropTarget(Component c, int ops, DropTargetListener dtl,
                  boolean act, FlavorMap fm)
    throws HeadlessException
{
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }

    component = c;

    setDefaultActions(ops);

    if (dtl != null) try {
        addDropTargetListener(dtl);
    } catch (TooManyListenersException tmle) {
        // do nothing!
    }

    if (c != null) {
        c.setDropTarget(this);
        setActive(act);
    }

    if (fm != null) {
        flavorMap = fm;
    } else {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }
}
 
Example 37
Project: jdk8u-jdk   File: DragSource.java   Source Code and License 5 votes vote down vote up
/**
 * Deserializes this <code>DragSource</code>. This method first performs
 * default deserialization. Next, this object's <code>FlavorMap</code> is
 * deserialized by using the next object in the stream.
 * If the resulting <code>FlavorMap</code> is <code>null</code>, this
 * object's <code>FlavorMap</code> is set to the default FlavorMap for
 * this thread's <code>ClassLoader</code>.
 * Next, this object's listeners are deserialized by reading a
 * <code>null</code>-terminated sequence of 0 or more key/value pairs
 * from the stream:
 * <ul>
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceListenerK</code>, a <code>DragSourceListener</code> is
 * deserialized using the corresponding value object and added to this
 * <code>DragSource</code>.
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceMotionListenerK</code>, a
 * <code>DragSourceMotionListener</code> is deserialized using the
 * corresponding value object and added to this <code>DragSource</code>.
 * <li>Otherwise, the key/value pair is skipped.
 * </ul>
 *
 * @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
 * @since 1.4
 */
private void readObject(ObjectInputStream s)
  throws ClassNotFoundException, IOException {
    s.defaultReadObject();

    // 'flavorMap' was written explicitly
    flavorMap = (FlavorMap)s.readObject();

    // Implementation assumes 'flavorMap' is never null.
    if (flavorMap == null) {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
        String key = ((String)keyOrNull).intern();

        if (dragSourceListenerK == key) {
            addDragSourceListener((DragSourceListener)(s.readObject()));
        } else if (dragSourceMotionListenerK == key) {
            addDragSourceMotionListener(
                (DragSourceMotionListener)(s.readObject()));
        } else {
            // skip value for unrecognized key
            s.readObject();
        }
    }
}
 
Example 38
Project: jdk8u-jdk   File: ImageTransferTest.java   Source Code and License 5 votes vote down vote up
static String[] retrieveFormatsToTest() {
    SystemFlavorMap sfm = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
    java.util.List<String> ln = sfm.getNativesForFlavor(DataFlavor.imageFlavor);
    if (OSInfo.OSType.WINDOWS.equals(OSInfo.getOSType()) && !ln.contains("METAFILEPICT")) {
        // for test failing on JDK without this fix
        ln.add("METAFILEPICT");
    }
    return ln.toArray(new String[ln.size()]);
}
 
Example 39
Project: jdk8u-jdk   File: DataFlavorSearcher.java   Source Code and License 5 votes vote down vote up
static public DataFlavor getByteDataFlavorForNative(String[] nats) {
    FlavorTable flavorTable = (FlavorTable) SystemFlavorMap.getDefaultFlavorMap();

    for (String nat : nats) {
        java.util.List<DataFlavor> flavors = flavorTable.getFlavorsForNative(nat);
        for (DataFlavor flavor : flavors) {
            if (flavor != null
                    && flavor.getRepresentationClass().equals(byte[].class)) {
                return flavor;
            }
        }
    }
    throw new RuntimeException("No data flavor was found for natives: " + Arrays.toString(nats));
}
 
Example 40
Project: openjdk-jdk10   File: DropTarget.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a new DropTarget given the {@code Component}
 * to associate itself with, an {@code int} representing
 * the default acceptable action(s) to
 * support, a {@code DropTargetListener}
 * to handle event processing, a {@code boolean} indicating
 * if the {@code DropTarget} is currently accepting drops, and
 * a {@code FlavorMap} to use (or null for the default {@code FlavorMap}).
 * <P>
 * The Component will receive drops only if it is enabled.
 * @param c         The {@code Component} with which this {@code DropTarget} is associated
 * @param ops       The default acceptable actions for this {@code DropTarget}
 * @param dtl       The {@code DropTargetListener} for this {@code DropTarget}
 * @param act       Is the {@code DropTarget} accepting drops.
 * @param fm        The {@code FlavorMap} to use, or null for the default {@code FlavorMap}
 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 *            returns true
 * @see java.awt.GraphicsEnvironment#isHeadless
 */
public DropTarget(Component c, int ops, DropTargetListener dtl,
                  boolean act, FlavorMap fm)
    throws HeadlessException
{
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }

    component = c;

    setDefaultActions(ops);

    if (dtl != null) try {
        addDropTargetListener(dtl);
    } catch (TooManyListenersException tmle) {
        // do nothing!
    }

    if (c != null) {
        c.setDropTarget(this);
        setActive(act);
    }

    if (fm != null) {
        flavorMap = fm;
    } else {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }
}
 
Example 41
Project: openjdk-jdk10   File: DragSource.java   Source Code and License 5 votes vote down vote up
/**
 * Deserializes this {@code DragSource}. This method first performs
 * default deserialization. Next, this object's {@code FlavorMap} is
 * deserialized by using the next object in the stream.
 * If the resulting {@code FlavorMap} is {@code null}, this
 * object's {@code FlavorMap} is set to the default FlavorMap for
 * this thread's {@code ClassLoader}.
 * Next, this object's listeners are deserialized by reading a
 * {@code null}-terminated sequence of 0 or more key/value pairs
 * from the stream:
 * <ul>
 * <li>If a key object is a {@code String} equal to
 * {@code dragSourceListenerK}, a {@code DragSourceListener} is
 * deserialized using the corresponding value object and added to this
 * {@code DragSource}.
 * <li>If a key object is a {@code String} equal to
 * {@code dragSourceMotionListenerK}, a
 * {@code DragSourceMotionListener} is deserialized using the
 * corresponding value object and added to this {@code DragSource}.
 * <li>Otherwise, the key/value pair is skipped.
 * </ul>
 *
 * @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
 * @since 1.4
 */
private void readObject(ObjectInputStream s)
  throws ClassNotFoundException, IOException {
    s.defaultReadObject();

    // 'flavorMap' was written explicitly
    flavorMap = (FlavorMap)s.readObject();

    // Implementation assumes 'flavorMap' is never null.
    if (flavorMap == null) {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
        String key = ((String)keyOrNull).intern();

        if (dragSourceListenerK == key) {
            addDragSourceListener((DragSourceListener)(s.readObject()));
        } else if (dragSourceMotionListenerK == key) {
            addDragSourceMotionListener(
                (DragSourceMotionListener)(s.readObject()));
        } else {
            // skip value for unrecognized key
            s.readObject();
        }
    }
}
 
Example 42
Project: openjdk-jdk10   File: DataFlavorSearcher.java   Source Code and License 5 votes vote down vote up
static public DataFlavor getByteDataFlavorForNative(String[] nats) {
    FlavorTable flavorTable = (FlavorTable) SystemFlavorMap.getDefaultFlavorMap();

    for (String nat : nats) {
        java.util.List<DataFlavor> flavors = flavorTable.getFlavorsForNative(nat);
        for (DataFlavor flavor : flavors) {
            if (flavor != null
                    && flavor.getRepresentationClass().equals(byte[].class)) {
                return flavor;
            }
        }
    }
    throw new RuntimeException("No data flavor was found for natives: " + Arrays.toString(nats));
}
 
Example 43
Project: openjdk-jdk10   File: SetNativesForFlavorTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
        final String nativeString = "NATIVE";

        final SystemFlavorMap fm =
            (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

        fm.setNativesForFlavor(DataFlavor.plainTextFlavor,
                               new String[] { nativeString });

        final java.util.List natives =
            fm.getNativesForFlavor(DataFlavor.plainTextFlavor);

        if (natives.size() != 1 || !natives.contains(nativeString)) {
            throw new RuntimeException("getNativesForFlavor() returns:" +
                                       natives);
        }

        final DataFlavor dataFlavor =
            new DataFlavor("text/unknown; class=java.lang.String");

        fm.setFlavorsForNative(nativeString, new DataFlavor[] { dataFlavor });

        final java.util.List flavors = fm.getFlavorsForNative(nativeString);

        if (flavors.size() != 1 || !flavors.contains(dataFlavor)) {
            throw new RuntimeException("getFlavorsForNative() returns:" +
                                       flavors);
        }
}
 
Example 44
Project: openjdk-jdk10   File: AddFlavorTest.java   Source Code and License 5 votes vote down vote up
void doTest() throws Exception {
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Test addFlavorForUnencodedNative(String nat, DataFlavor flav);
    //
    // Enumerate through all the system defined String natives,
    // and for each String native, define it again to the SystemFlavorMap
    // with a slightly modified String native (name).
    //
    // As a list of DataFlavors will be returned for each String native,
    // the method addFlavorForUnencodedNative will be called for each
    // DataFlavor in the list.
    hashVerify = new Hashtable();

    for (String key : flavorMap.getFlavorsForNatives(null).keySet()) {
        Set<DataFlavor> flavorsSet = new HashSet<>(flavorMap.getFlavorsForNative(key));
        // construct a unique String native
        key = key.concat("TEST");

        for (DataFlavor element : flavorsSet) {
            flavorMap.addFlavorForUnencodedNative(key, element);
        }
        hashVerify.put(key, new Vector(flavorsSet));
    }

    // Assertions: After establishing "new" mappings, verify that the defined
    //             DataFlavors can be retrieved and that the List is preserved.
    verifyNewMappings();
}
 
Example 45
Project: openjdk-jdk10   File: ManyFlavorMapTest.java   Source Code and License 5 votes vote down vote up
public void doTest() {
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Get SystemFlavorMap Maps of String Natives and DataFlavors
    mapFlavors = flavorMap.getNativesForFlavors(null);
    mapNatives = flavorMap.getFlavorsForNatives(null);

    hashFlavors = new Hashtable(mapFlavors);
    hashNatives = new Hashtable(mapNatives);

    // Assertion: Verify getNativesForFlavors(null) is returning the full list
    //            of String Native entries
    List listNatives = flavorMap.getNativesForFlavor(null);
    verifyListAllNativeEntries(listNatives);

    // Assertion: Verify getFlavorsForNatives(null) is returning the full list
    //            of DataFlavor entries
    List listFlavors = flavorMap.getFlavorsForNative(null);
    verifyListAllDataFlavorEntries(listFlavors);

    // Assertion: Verify getNativesForFlavor(DataFlavor flav) is returning the list
    //            of Native Strings (for all supported DataFlavors)
    //
    //            Verify that the first list item is the most preferred Native
    verifyListNativeEntries();

    // Assertion: Verify getFlavorsForNative(String nat) is returning the list
    //            of DataFlavors(for all supported natives)
    //
    //            Verify that the first list item is the most preferred DataFlavor
    verifyListDataFlavorEntries();
}
 
Example 46
Project: openjdk-jdk10   File: SetNativesTest.java   Source Code and License 5 votes vote down vote up
public void doTest() {
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Get SystemFlavorMap Maps of String Natives and DataFlavors
    mapFlavors = flavorMap.getNativesForFlavors(null);
    mapNatives = flavorMap.getFlavorsForNatives(null);

    hashFlavors = new Hashtable(mapFlavors);
    hashNatives = new Hashtable(mapNatives);


    // Test setFlavorsForNative(String nat, DataFlavors[] flavors);
    //
    // Enumerate through all the system defined String natives,
    // and for each String native, define it again to the SystemFlavorMap
    // with a slightly modified String native (name).
    //
    // For verification, a seperate Hashtable will be maintained of the additions.
    String key;
    hashVerify = new Hashtable();

    for (Enumeration e = hashNatives.keys() ; e.hasMoreElements() ;) {
        key = (String)e.nextElement();

        java.util.List listFlavors = flavorMap.getFlavorsForNative(key);
        Vector vectorFlavors = new Vector(listFlavors);
        DataFlavor[] arrayFlavors = (DataFlavor[])vectorFlavors.toArray(new DataFlavor[0]);

        key = key.concat("TEST");   // construct a unique String native
                                    // define the new String native entry
        flavorMap.setFlavorsForNative(key, arrayFlavors);
                                    // keep track of this new native entry
        hashVerify.put(key, vectorFlavors);
    }

    // After establishing "new" mappings, verify that the defined
    // DataFlavors can be retrieved and that the List (order) is preserved.
    verifyNewMappings();
}
 
Example 47
Project: openjdk-jdk10   File: ImageTransferTest.java   Source Code and License 5 votes vote down vote up
static String[] retrieveFormatsToTest() {
    SystemFlavorMap sfm = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
    java.util.List<String> ln = sfm.getNativesForFlavor(DataFlavor.imageFlavor);
    if (OSInfo.OSType.WINDOWS.equals(OSInfo.getOSType()) && !ln.contains("METAFILEPICT")) {
        // for test failing on JDK without this fix
        ln.add("METAFILEPICT");
    }
    return ln.toArray(new String[ln.size()]);
}
 
Example 48
Project: openjdk9   File: DropTarget.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a new DropTarget given the {@code Component}
 * to associate itself with, an {@code int} representing
 * the default acceptable action(s) to
 * support, a {@code DropTargetListener}
 * to handle event processing, a {@code boolean} indicating
 * if the {@code DropTarget} is currently accepting drops, and
 * a {@code FlavorMap} to use (or null for the default {@code FlavorMap}).
 * <P>
 * The Component will receive drops only if it is enabled.
 * @param c         The {@code Component} with which this {@code DropTarget} is associated
 * @param ops       The default acceptable actions for this {@code DropTarget}
 * @param dtl       The {@code DropTargetListener} for this {@code DropTarget}
 * @param act       Is the {@code DropTarget} accepting drops.
 * @param fm        The {@code FlavorMap} to use, or null for the default {@code FlavorMap}
 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 *            returns true
 * @see java.awt.GraphicsEnvironment#isHeadless
 */
public DropTarget(Component c, int ops, DropTargetListener dtl,
                  boolean act, FlavorMap fm)
    throws HeadlessException
{
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }

    component = c;

    setDefaultActions(ops);

    if (dtl != null) try {
        addDropTargetListener(dtl);
    } catch (TooManyListenersException tmle) {
        // do nothing!
    }

    if (c != null) {
        c.setDropTarget(this);
        setActive(act);
    }

    if (fm != null) {
        flavorMap = fm;
    } else {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }
}
 
Example 49
Project: openjdk9   File: DragSource.java   Source Code and License 5 votes vote down vote up
/**
 * Deserializes this {@code DragSource}. This method first performs
 * default deserialization. Next, this object's {@code FlavorMap} is
 * deserialized by using the next object in the stream.
 * If the resulting {@code FlavorMap} is {@code null}, this
 * object's {@code FlavorMap} is set to the default FlavorMap for
 * this thread's {@code ClassLoader}.
 * Next, this object's listeners are deserialized by reading a
 * {@code null}-terminated sequence of 0 or more key/value pairs
 * from the stream:
 * <ul>
 * <li>If a key object is a {@code String} equal to
 * {@code dragSourceListenerK}, a {@code DragSourceListener} is
 * deserialized using the corresponding value object and added to this
 * {@code DragSource}.
 * <li>If a key object is a {@code String} equal to
 * {@code dragSourceMotionListenerK}, a
 * {@code DragSourceMotionListener} is deserialized using the
 * corresponding value object and added to this {@code DragSource}.
 * <li>Otherwise, the key/value pair is skipped.
 * </ul>
 *
 * @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
 * @since 1.4
 */
private void readObject(ObjectInputStream s)
  throws ClassNotFoundException, IOException {
    s.defaultReadObject();

    // 'flavorMap' was written explicitly
    flavorMap = (FlavorMap)s.readObject();

    // Implementation assumes 'flavorMap' is never null.
    if (flavorMap == null) {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
        String key = ((String)keyOrNull).intern();

        if (dragSourceListenerK == key) {
            addDragSourceListener((DragSourceListener)(s.readObject()));
        } else if (dragSourceMotionListenerK == key) {
            addDragSourceMotionListener(
                (DragSourceMotionListener)(s.readObject()));
        } else {
            // skip value for unrecognized key
            s.readObject();
        }
    }
}
 
Example 50
Project: openjdk9   File: DataFlavorSearcher.java   Source Code and License 5 votes vote down vote up
static public DataFlavor getByteDataFlavorForNative(String[] nats) {
    FlavorTable flavorTable = (FlavorTable) SystemFlavorMap.getDefaultFlavorMap();

    for (String nat : nats) {
        java.util.List<DataFlavor> flavors = flavorTable.getFlavorsForNative(nat);
        for (DataFlavor flavor : flavors) {
            if (flavor != null
                    && flavor.getRepresentationClass().equals(byte[].class)) {
                return flavor;
            }
        }
    }
    throw new RuntimeException("No data flavor was found for natives: " + Arrays.toString(nats));
}
 
Example 51
Project: openjdk9   File: GetNativesForNewFlavorTest.java   Source Code and License 5 votes vote down vote up
public void doTest() throws Exception {
    // Initialize DataFlavors and arrays used for test data
    initMappings();

    boolean passed = true;
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Get all the native strings and preferred DataFlavor mappings
    hash = new Hashtable(flavorMap.getFlavorsForNatives(null));
    hashSize = hash.size();

    // GetNativesForFlavor using unknown DataFlavor (verify 2-way mapping)
    //
    // If a new DataFlavor is specified, the method should establish a mapping
    // in both directions between specified DataFlavor and an encoded
    // version of its MIME type as its native.
    System.out.println("GetNativesForFlavor using new DataFlavor");

    comp1 = new Vector(Arrays.asList(test_natives_set));
    comp2 = new Vector(flavorMap.getNativesForFlavor(test_flavor1));

    comp3 = new Vector(Arrays.asList(test_flavors_set));
    comp4 = new Vector(flavorMap.getFlavorsForNative(test_encoded));

    if ( !comp1.equals(comp2) || !comp3.equals(comp4) ) {
        throw new RuntimeException("\n*** After passing a new DataFlavor" +
            "\nwith getNativesForFlavor(DataFlavor flav)" +
            "\nthe mapping in both directions was not established.");
    }
    else
       System.out.println("GetNativesForFlavor using new DataFlavor: Test Passes");
}
 
Example 52
Project: openjdk9   File: SetNativesForFlavorTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
        final String nativeString = "NATIVE";

        final SystemFlavorMap fm =
            (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

        fm.setNativesForFlavor(DataFlavor.plainTextFlavor,
                               new String[] { nativeString });

        final java.util.List natives =
            fm.getNativesForFlavor(DataFlavor.plainTextFlavor);

        if (natives.size() != 1 || !natives.contains(nativeString)) {
            throw new RuntimeException("getNativesForFlavor() returns:" +
                                       natives);
        }

        final DataFlavor dataFlavor =
            new DataFlavor("text/unknown; class=java.lang.String");

        fm.setFlavorsForNative(nativeString, new DataFlavor[] { dataFlavor });

        final java.util.List flavors = fm.getFlavorsForNative(nativeString);

        if (flavors.size() != 1 || !flavors.contains(dataFlavor)) {
            throw new RuntimeException("getFlavorsForNative() returns:" +
                                       flavors);
        }
}
 
Example 53
Project: openjdk9   File: AddFlavorTest.java   Source Code and License 5 votes vote down vote up
void doTest() throws Exception {
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Test addFlavorForUnencodedNative(String nat, DataFlavor flav);
    //
    // Enumerate through all the system defined String natives,
    // and for each String native, define it again to the SystemFlavorMap
    // with a slightly modified String native (name).
    //
    // As a list of DataFlavors will be returned for each String native,
    // the method addFlavorForUnencodedNative will be called for each
    // DataFlavor in the list.
    hashVerify = new Hashtable();

    for (String key : flavorMap.getFlavorsForNatives(null).keySet()) {
        Set<DataFlavor> flavorsSet = new HashSet<>(flavorMap.getFlavorsForNative(key));
        // construct a unique String native
        key = key.concat("TEST");

        for (DataFlavor element : flavorsSet) {
            flavorMap.addFlavorForUnencodedNative(key, element);
        }
        hashVerify.put(key, new Vector(flavorsSet));
    }

    // Assertions: After establishing "new" mappings, verify that the defined
    //             DataFlavors can be retrieved and that the List is preserved.
    verifyNewMappings();
}
 
Example 54
Project: openjdk9   File: ManyFlavorMapTest.java   Source Code and License 5 votes vote down vote up
public void doTest() {
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Get SystemFlavorMap Maps of String Natives and DataFlavors
    mapFlavors = flavorMap.getNativesForFlavors(null);
    mapNatives = flavorMap.getFlavorsForNatives(null);

    hashFlavors = new Hashtable(mapFlavors);
    hashNatives = new Hashtable(mapNatives);

    // Assertion: Verify getNativesForFlavors(null) is returning the full list
    //            of String Native entries
    List listNatives = flavorMap.getNativesForFlavor(null);
    verifyListAllNativeEntries(listNatives);

    // Assertion: Verify getFlavorsForNatives(null) is returning the full list
    //            of DataFlavor entries
    List listFlavors = flavorMap.getFlavorsForNative(null);
    verifyListAllDataFlavorEntries(listFlavors);

    // Assertion: Verify getNativesForFlavor(DataFlavor flav) is returning the list
    //            of Native Strings (for all supported DataFlavors)
    //
    //            Verify that the first list item is the most preferred Native
    verifyListNativeEntries();

    // Assertion: Verify getFlavorsForNative(String nat) is returning the list
    //            of DataFlavors(for all supported natives)
    //
    //            Verify that the first list item is the most preferred DataFlavor
    verifyListDataFlavorEntries();
}
 
Example 55
Project: openjdk9   File: SetNativesTest.java   Source Code and License 5 votes vote down vote up
public void doTest() {
    flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();

    // Get SystemFlavorMap Maps of String Natives and DataFlavors
    mapFlavors = flavorMap.getNativesForFlavors(null);
    mapNatives = flavorMap.getFlavorsForNatives(null);

    hashFlavors = new Hashtable(mapFlavors);
    hashNatives = new Hashtable(mapNatives);


    // Test setFlavorsForNative(String nat, DataFlavors[] flavors);
    //
    // Enumerate through all the system defined String natives,
    // and for each String native, define it again to the SystemFlavorMap
    // with a slightly modified String native (name).
    //
    // For verification, a seperate Hashtable will be maintained of the additions.
    String key;
    hashVerify = new Hashtable();

    for (Enumeration e = hashNatives.keys() ; e.hasMoreElements() ;) {
        key = (String)e.nextElement();

        java.util.List listFlavors = flavorMap.getFlavorsForNative(key);
        Vector vectorFlavors = new Vector(listFlavors);
        DataFlavor[] arrayFlavors = (DataFlavor[])vectorFlavors.toArray(new DataFlavor[0]);

        key = key.concat("TEST");   // construct a unique String native
                                    // define the new String native entry
        flavorMap.setFlavorsForNative(key, arrayFlavors);
                                    // keep track of this new native entry
        hashVerify.put(key, vectorFlavors);
    }

    // After establishing "new" mappings, verify that the defined
    // DataFlavors can be retrieved and that the List (order) is preserved.
    verifyNewMappings();
}
 
Example 56
Project: openjdk9   File: ImageTransferTest.java   Source Code and License 5 votes vote down vote up
static String[] retrieveFormatsToTest() {
    SystemFlavorMap sfm = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
    java.util.List<String> ln = sfm.getNativesForFlavor(DataFlavor.imageFlavor);
    if (OSInfo.OSType.WINDOWS.equals(OSInfo.getOSType()) && !ln.contains("METAFILEPICT")) {
        // for test failing on JDK without this fix
        ln.add("METAFILEPICT");
    }
    return ln.toArray(new String[ln.size()]);
}
 
Example 57
Project: Java8CN   File: DropTarget.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a new DropTarget given the <code>Component</code>
 * to associate itself with, an <code>int</code> representing
 * the default acceptable action(s) to
 * support, a <code>DropTargetListener</code>
 * to handle event processing, a <code>boolean</code> indicating
 * if the <code>DropTarget</code> is currently accepting drops, and
 * a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
 * <P>
 * The Component will receive drops only if it is enabled.
 * @param c         The <code>Component</code> with which this <code>DropTarget</code> is associated
 * @param ops       The default acceptable actions for this <code>DropTarget</code>
 * @param dtl       The <code>DropTargetListener</code> for this <code>DropTarget</code>
 * @param act       Is the <code>DropTarget</code> accepting drops.
 * @param fm        The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 *            returns true
 * @see java.awt.GraphicsEnvironment#isHeadless
 */
public DropTarget(Component c, int ops, DropTargetListener dtl,
                  boolean act, FlavorMap fm)
    throws HeadlessException
{
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }

    component = c;

    setDefaultActions(ops);

    if (dtl != null) try {
        addDropTargetListener(dtl);
    } catch (TooManyListenersException tmle) {
        // do nothing!
    }

    if (c != null) {
        c.setDropTarget(this);
        setActive(act);
    }

    if (fm != null) {
        flavorMap = fm;
    } else {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }
}
 
Example 58
Project: Java8CN   File: DragSource.java   Source Code and License 5 votes vote down vote up
/**
 * Deserializes this <code>DragSource</code>. This method first performs
 * default deserialization. Next, this object's <code>FlavorMap</code> is
 * deserialized by using the next object in the stream.
 * If the resulting <code>FlavorMap</code> is <code>null</code>, this
 * object's <code>FlavorMap</code> is set to the default FlavorMap for
 * this thread's <code>ClassLoader</code>.
 * Next, this object's listeners are deserialized by reading a
 * <code>null</code>-terminated sequence of 0 or more key/value pairs
 * from the stream:
 * <ul>
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceListenerK</code>, a <code>DragSourceListener</code> is
 * deserialized using the corresponding value object and added to this
 * <code>DragSource</code>.
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceMotionListenerK</code>, a
 * <code>DragSourceMotionListener</code> is deserialized using the
 * corresponding value object and added to this <code>DragSource</code>.
 * <li>Otherwise, the key/value pair is skipped.
 * </ul>
 *
 * @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
 * @since 1.4
 */
private void readObject(ObjectInputStream s)
  throws ClassNotFoundException, IOException {
    s.defaultReadObject();

    // 'flavorMap' was written explicitly
    flavorMap = (FlavorMap)s.readObject();

    // Implementation assumes 'flavorMap' is never null.
    if (flavorMap == null) {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
        String key = ((String)keyOrNull).intern();

        if (dragSourceListenerK == key) {
            addDragSourceListener((DragSourceListener)(s.readObject()));
        } else if (dragSourceMotionListenerK == key) {
            addDragSourceMotionListener(
                (DragSourceMotionListener)(s.readObject()));
        } else {
            // skip value for unrecognized key
            s.readObject();
        }
    }
}
 
Example 59
Project: jdk8u_jdk   File: DropTarget.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a new DropTarget given the <code>Component</code>
 * to associate itself with, an <code>int</code> representing
 * the default acceptable action(s) to
 * support, a <code>DropTargetListener</code>
 * to handle event processing, a <code>boolean</code> indicating
 * if the <code>DropTarget</code> is currently accepting drops, and
 * a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
 * <P>
 * The Component will receive drops only if it is enabled.
 * @param c         The <code>Component</code> with which this <code>DropTarget</code> is associated
 * @param ops       The default acceptable actions for this <code>DropTarget</code>
 * @param dtl       The <code>DropTargetListener</code> for this <code>DropTarget</code>
 * @param act       Is the <code>DropTarget</code> accepting drops.
 * @param fm        The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 *            returns true
 * @see java.awt.GraphicsEnvironment#isHeadless
 */
public DropTarget(Component c, int ops, DropTargetListener dtl,
                  boolean act, FlavorMap fm)
    throws HeadlessException
{
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }

    component = c;

    setDefaultActions(ops);

    if (dtl != null) try {
        addDropTargetListener(dtl);
    } catch (TooManyListenersException tmle) {
        // do nothing!
    }

    if (c != null) {
        c.setDropTarget(this);
        setActive(act);
    }

    if (fm != null) {
        flavorMap = fm;
    } else {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }
}
 
Example 60
Project: jdk8u_jdk   File: DragSource.java   Source Code and License 5 votes vote down vote up
/**
 * Deserializes this <code>DragSource</code>. This method first performs
 * default deserialization. Next, this object's <code>FlavorMap</code> is
 * deserialized by using the next object in the stream.
 * If the resulting <code>FlavorMap</code> is <code>null</code>, this
 * object's <code>FlavorMap</code> is set to the default FlavorMap for
 * this thread's <code>ClassLoader</code>.
 * Next, this object's listeners are deserialized by reading a
 * <code>null</code>-terminated sequence of 0 or more key/value pairs
 * from the stream:
 * <ul>
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceListenerK</code>, a <code>DragSourceListener</code> is
 * deserialized using the corresponding value object and added to this
 * <code>DragSource</code>.
 * <li>If a key object is a <code>String</code> equal to
 * <code>dragSourceMotionListenerK</code>, a
 * <code>DragSourceMotionListener</code> is deserialized using the
 * corresponding value object and added to this <code>DragSource</code>.
 * <li>Otherwise, the key/value pair is skipped.
 * </ul>
 *
 * @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
 * @since 1.4
 */
private void readObject(ObjectInputStream s)
  throws ClassNotFoundException, IOException {
    s.defaultReadObject();

    // 'flavorMap' was written explicitly
    flavorMap = (FlavorMap)s.readObject();

    // Implementation assumes 'flavorMap' is never null.
    if (flavorMap == null) {
        flavorMap = SystemFlavorMap.getDefaultFlavorMap();
    }

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
        String key = ((String)keyOrNull).intern();

        if (dragSourceListenerK == key) {
            addDragSourceListener((DragSourceListener)(s.readObject()));
        } else if (dragSourceMotionListenerK == key) {
            addDragSourceMotionListener(
                (DragSourceMotionListener)(s.readObject()));
        } else {
            // skip value for unrecognized key
            s.readObject();
        }
    }
}