Search in sources :

Example 36 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class DuplicatedNativesTest method main.

public static void main(String[] args) throws Exception {
    // 1. Check that returned natives do not contain duplicates.
    SystemFlavorMap flavorMap = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
    for (Map.Entry<DataFlavor, String> entry : flavorMap.getNativesForFlavors(null).entrySet()) {
        List<String> natives = flavorMap.getNativesForFlavor(entry.getKey());
        if (new HashSet<>(natives).size() != natives.size()) {
            throw new RuntimeException("FAILED: returned natives contain duplicates: " + Arrays.toString(natives.toArray()));
        }
    }
    // 2. Check that even if we set a duplicate it would be ignored.
    flavorMap.setNativesForFlavor(DataFlavor.stringFlavor, new String[] { "test", "test", "test" });
    List<String> natives = flavorMap.getNativesForFlavor(DataFlavor.stringFlavor);
    if (new HashSet<>(natives).size() != natives.size()) {
        throw new RuntimeException("FAILED: duplicates were not ignored: " + Arrays.toString(natives.toArray()));
    }
}
Also used : SystemFlavorMap(java.awt.datatransfer.SystemFlavorMap) Map(java.util.Map) SystemFlavorMap(java.awt.datatransfer.SystemFlavorMap) DataFlavor(java.awt.datatransfer.DataFlavor) HashSet(java.util.HashSet)

Example 37 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class MappingGenerationTest method test3.

/**
     * Verifies that addUnencodedNativeForFlavor() for a particular text flavor
     * doesn't affect mappings for other flavors.
     */
public static void test3() {
    DataFlavor df1 = new DataFlavor("text/plain-test3", null);
    DataFlavor df2 = new DataFlavor("text/plain-test3; charset=Unicode; class=java.io.Reader", null);
    String nat = "native3";
    List<String> natives = fm.getNativesForFlavor(df2);
    fm.addUnencodedNativeForFlavor(df1, nat);
    List<String> nativesNew = fm.getNativesForFlavor(df2);
    if (!natives.equals(nativesNew)) {
        System.err.println("orig=" + natives);
        System.err.println("new=" + nativesNew);
        throw new RuntimeException("Test failed");
    }
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Example 38 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class MappingGenerationTest method test4.

/**
     * Verifies that addUnencodedNativeForFlavor() really adds the specified
     * flavor-to-native mapping to the existing mappings.
     */
public static void test4() {
    DataFlavor df = new DataFlavor("text/plain-test4; charset=Unicode; class=java.io.Reader", null);
    String nat = "native4";
    List<String> natives = fm.getNativesForFlavor(df);
    if (!natives.contains(nat)) {
        fm.addUnencodedNativeForFlavor(df, nat);
        List<String> nativesNew = fm.getNativesForFlavor(df);
        natives.add(nat);
        if (!natives.equals(nativesNew)) {
            System.err.println("orig=" + natives);
            System.err.println("new=" + nativesNew);
            throw new RuntimeException("Test failed");
        }
    }
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Example 39 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jabref by JabRef.

the class EntryTableTransferHandler method importData.

/**
     * This method is called when stuff is drag to the component.
     *
     * Imports the dropped URL or plain text as a new entry in the current library.
     *
     */
@Override
public boolean importData(JComponent comp, Transferable t) {
    // If the drop target is the main table, we want to record which
    // row the item was dropped on, to identify the entry if needed:
    int dropRow = -1;
    if (comp instanceof JTable) {
        dropRow = ((JTable) comp).getSelectedRow();
    }
    try {
        // This flavor is used for dragged file links in Windows:
        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            // JOptionPane.showMessageDialog(null, "Received
            // javaFileListFlavor");
            @SuppressWarnings("unchecked") List<Path> files = ((List<File>) t.getTransferData(DataFlavor.javaFileListFlavor)).stream().map(File::toPath).collect(Collectors.toList());
            return handleDraggedFiles(files, dropRow);
        } else if (t.isDataFlavorSupported(urlFlavor)) {
            URL dropLink = (URL) t.getTransferData(urlFlavor);
            return handleDropTransfer(dropLink);
        } else if (t.isDataFlavorSupported(stringFlavor)) {
            String dropStr = (String) t.getTransferData(stringFlavor);
            LOGGER.debug("Received stringFlavor: " + dropStr);
            return handleDropTransfer(dropStr, dropRow);
        }
    } catch (IOException ioe) {
        LOGGER.error("Failed to read dropped data", ioe);
    } catch (UnsupportedFlavorException | ClassCastException ufe) {
        LOGGER.error("Drop type error", ufe);
    }
    // all supported flavors failed
    LOGGER.info("Can't transfer input: ");
    DataFlavor[] inflavs = t.getTransferDataFlavors();
    for (DataFlavor inflav : inflavs) {
        LOGGER.info("  " + inflav);
    }
    return false;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) URL(java.net.URL) DataFlavor(java.awt.datatransfer.DataFlavor) JTable(javax.swing.JTable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 40 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.

the class DragSourceContext method readObject.

/**
     * Deserializes this <code>DragSourceContext</code>. This method first
     * performs default deserialization for all non-<code>transient</code>
     * fields. This object's <code>Transferable</code> and
     * <code>DragSourceListener</code> are then deserialized as well by using
     * the next two objects in the stream. If the resulting
     * <code>Transferable</code> is <code>null</code>, this object's
     * <code>Transferable</code> is set to a dummy <code>Transferable</code>
     * which supports no <code>DataFlavor</code>s.
     *
     * @since 1.4
     */
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
    ObjectInputStream.GetField f = s.readFields();
    DragGestureEvent newTrigger = (DragGestureEvent) f.get("trigger", null);
    if (newTrigger == null) {
        throw new InvalidObjectException("Null trigger");
    }
    if (newTrigger.getDragSource() == null) {
        throw new InvalidObjectException("Null DragSource");
    }
    if (newTrigger.getComponent() == null) {
        throw new InvalidObjectException("Null trigger component");
    }
    int newSourceActions = f.get("sourceActions", 0) & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
    if (newSourceActions == DnDConstants.ACTION_NONE) {
        throw new InvalidObjectException("Invalid source actions");
    }
    int triggerActions = newTrigger.getDragAction();
    if (triggerActions != DnDConstants.ACTION_COPY && triggerActions != DnDConstants.ACTION_MOVE && triggerActions != DnDConstants.ACTION_LINK) {
        throw new InvalidObjectException("No drag action");
    }
    trigger = newTrigger;
    cursor = (Cursor) f.get("cursor", null);
    useCustomCursor = f.get("useCustomCursor", false);
    sourceActions = newSourceActions;
    transferable = (Transferable) s.readObject();
    listener = (DragSourceListener) s.readObject();
    // Implementation assumes 'transferable' is never null.
    if (transferable == null) {
        if (emptyTransferable == null) {
            emptyTransferable = new Transferable() {

                public DataFlavor[] getTransferDataFlavors() {
                    return new DataFlavor[0];
                }

                public boolean isDataFlavorSupported(DataFlavor flavor) {
                    return false;
                }

                public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                    throw new UnsupportedFlavorException(flavor);
                }
            };
        }
        transferable = emptyTransferable;
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) InvalidObjectException(java.io.InvalidObjectException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) ObjectInputStream(java.io.ObjectInputStream) DataFlavor(java.awt.datatransfer.DataFlavor)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)53 Transferable (java.awt.datatransfer.Transferable)13 IOException (java.io.IOException)13 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 List (java.util.List)11 JPanel (javax.swing.JPanel)11 JLabel (javax.swing.JLabel)9 ArrayList (java.util.ArrayList)8 Point (java.awt.Point)7 Clipboard (java.awt.datatransfer.Clipboard)6 File (java.io.File)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 InputStream (java.io.InputStream)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2