Search in sources :

Example 1 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project jna by java-native-access.

the class AlphaMaskDemo2 method run.

public void run() {
    // Must find a graphics configuration with a depth of 32 bits
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    frame = new JFrame("Alpha Mask Demo");
    alphaWindow = new JWindow(frame, gconfig);
    icon = new JLabel();
    icon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    alphaWindow.getContentPane().add(icon);
    JButton quit = new JButton("Quit");
    JLabel label = new JLabel("Drag this window by its image");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    alphaWindow.getContentPane().add(label, BorderLayout.NORTH);
    alphaWindow.getContentPane().add(quit, BorderLayout.SOUTH);
    quit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e))
                offset = e.getPoint();
        }

        public void mouseReleased(MouseEvent e) {
            offset = null;
        }

        public void mouseDragged(MouseEvent e) {
            if (offset != null) {
                Window w = (Window) e.getSource();
                Point where = e.getPoint();
                where.translate(-offset.x, -offset.y);
                Point loc = w.getLocationOnScreen();
                loc.translate(where.x, where.y);
                w.setLocation(loc.x, loc.y);
            }
        }
    };
    alphaWindow.addMouseListener(handler);
    alphaWindow.addMouseMotionListener(handler);
    JPanel p = new JPanel(new BorderLayout(8, 8));
    p.setBorder(new EmptyBorder(8, 8, 8, 8));
    p.setTransferHandler(new TransferHandler() {

        private static final long serialVersionUID = 1L;

        public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
            List<DataFlavor> list = Arrays.asList(transferFlavors);
            if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
                return true;
            }
            if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
                return true;
            }
            System.err.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
            return false;
        }

        public boolean importData(JComponent comp, Transferable t) {
            try {
                if (t.isDataFlavorSupported(URL_FLAVOR)) {
                    URL url = (URL) t.getTransferData(URL_FLAVOR);
                    setImage(Toolkit.getDefaultToolkit().getImage(url));
                    return true;
                }
                if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
                    String s = (String) t.getTransferData(URI_LIST_FLAVOR);
                    String[] uris = s.split("[\r\n]");
                    if (uris.length > 0) {
                        URL url = new URL(uris[0]);
                        setImage(Toolkit.getDefaultToolkit().getImage(url));
                        return true;
                    }
                    return false;
                }
                if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
                    setImage(image);
                    return true;
                }
                if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
                    File f = files.get(0);
                    URL url = new URL("file://" + f.toURI().toURL().getPath());
                    Image image = Toolkit.getDefaultToolkit().getImage(url);
                    setImage(image);
                    return true;
                }
                DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
                if (flavor != null) {
                    Reader reader = flavor.getReaderForText(t);
                    char[] buf = new char[512];
                    StringBuilder b = new StringBuilder();
                    int count;
                    // encoding wrong
                    while ((count = reader.read(buf)) > 0) {
                        for (int i = 0; i < count; i++) {
                            if (buf[i] != 0)
                                b.append(buf, i, 1);
                        }
                    }
                    String html = b.toString();
                    Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
                    Matcher m = p.matcher(html);
                    if (m.find()) {
                        URL url = new URL(m.group(1));
                        System.out.println("Load image from " + url);
                        Image image = Toolkit.getDefaultToolkit().getImage(url);
                        setImage(image);
                        return true;
                    }
                    System.err.println("Can't parse text: " + html);
                    return false;
                }
                System.err.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return false;
        }
    });
    p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
    p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    final JSlider slider = new JSlider(0, 255, 255);
    slider.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            WindowUtils.setWindowAlpha(alphaWindow, value / 255f);
        }
    });
    p.add(slider, BorderLayout.SOUTH);
    frame.getContentPane().add(p);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    centerOnScreen(frame);
    frame.setVisible(true);
    WindowUtils.setWindowTransparent(alphaWindow, true);
    alphaWindow.setLocation(frame.getX() + frame.getWidth() + 4, frame.getY());
    try {
        URL url = getClass().getResource("tardis.png");
        if (url != null) {
            setImage(Toolkit.getDefaultToolkit().getImage(url));
        }
    } catch (Exception e) {
    }
}
Also used : JPanel(javax.swing.JPanel) Matcher(java.util.regex.Matcher) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Reader(java.io.Reader) Image(java.awt.Image) URL(java.net.URL) GraphicsConfiguration(java.awt.GraphicsConfiguration) DataFlavor(java.awt.datatransfer.DataFlavor) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) JSlider(javax.swing.JSlider) List(java.util.List) ChangeListener(javax.swing.event.ChangeListener) EmptyBorder(javax.swing.border.EmptyBorder) Window(java.awt.Window) JWindow(javax.swing.JWindow) Pattern(java.util.regex.Pattern) MouseEvent(java.awt.event.MouseEvent) JWindow(javax.swing.JWindow) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) JLabel(javax.swing.JLabel) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) TransferHandler(javax.swing.TransferHandler) File(java.io.File) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 2 with DataFlavor

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

the class bug8059739 method runTest.

private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df : dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) && charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Clipboard(java.awt.datatransfer.Clipboard) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 3 with DataFlavor

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

the class MappingGenerationTest method test5.

/**
     * Verifies that a flavor doesn't have any flavor-to-native mappings after
     * a call to setNativesForFlavor() with this flavor and an empty native
     * array as arguments.
     */
public static void test5() {
    final DataFlavor flavor = new DataFlavor("text/plain-TEST5; charset=Unicode", null);
    fm.getNativesForFlavor(flavor);
    fm.setNativesForFlavor(flavor, new String[0]);
    List<String> natives = fm.getNativesForFlavor(flavor);
    if (!natives.isEmpty()) {
        System.err.println("natives=" + natives);
        throw new RuntimeException("Test failed");
    }
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Example 4 with DataFlavor

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

the class MappingGenerationTest method test1.

/**
     * Verifies that Lists returned from getNativesForFlavor() and
     * getFlavorsForNative() are not modified with a subsequent call
     * to addUnencodedNativeForFlavor() and addFlavorForUnencodedNative()
     * respectively.
     */
public static void test1() {
    DataFlavor df = new DataFlavor("text/plain-test1", null);
    String nat = "native1";
    List<String> natives = fm.getNativesForFlavor(df);
    fm.addUnencodedNativeForFlavor(df, nat);
    List<String> nativesNew = fm.getNativesForFlavor(df);
    if (natives.equals(nativesNew)) {
        System.err.println("orig=" + natives);
        System.err.println("new=" + nativesNew);
        throw new RuntimeException("Test failed");
    }
    List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
    fm.addFlavorForUnencodedNative(nat, df);
    List<DataFlavor> flavorsNew = fm.getFlavorsForNative(nat);
    if (flavors.equals(flavorsNew)) {
        System.err.println("orig=" + flavors);
        System.err.println("new=" + flavorsNew);
        throw new RuntimeException("Test failed");
    }
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Example 5 with DataFlavor

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

the class MappingGenerationTest method test2.

/**
     * Verifies that SystemFlavorMap is not affected by modification of
     * the Lists returned from getNativesForFlavor() and
     * getFlavorsForNative().
     */
public static void test2() {
    DataFlavor df = new DataFlavor("text/plain-test2", null);
    String nat = "native2";
    DataFlavor extraDf = new DataFlavor("text/test", null);
    List<String> natives = fm.getNativesForFlavor(df);
    natives.add("Should not be here");
    java.util.List nativesNew = fm.getNativesForFlavor(df);
    if (natives.equals(nativesNew)) {
        System.err.println("orig=" + natives);
        System.err.println("new=" + nativesNew);
        throw new RuntimeException("Test failed");
    }
    List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
    flavors.add(extraDf);
    java.util.List flavorsNew = fm.getFlavorsForNative(nat);
    if (flavors.equals(flavorsNew)) {
        System.err.println("orig=" + flavors);
        System.err.println("new=" + flavorsNew);
        throw new RuntimeException("Test failed");
    }
}
Also used : List(java.util.List) DataFlavor(java.awt.datatransfer.DataFlavor)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)50 IOException (java.io.IOException)12 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 JPanel (javax.swing.JPanel)11 Transferable (java.awt.datatransfer.Transferable)10 JLabel (javax.swing.JLabel)9 List (java.util.List)8 Point (java.awt.Point)7 ArrayList (java.util.ArrayList)7 Clipboard (java.awt.datatransfer.Clipboard)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 File (java.io.File)3 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Image (java.awt.Image)2 Insets (java.awt.Insets)2