--- old/src/macosx/lib/flavormap.properties 2013-02-05 16:36:48.146292100 +0400 +++ new/src/macosx/lib/flavormap.properties 2013-02-05 16:36:47.613724500 +0400 @@ -76,3 +76,5 @@ text/uri-list=application/x-java-file-list;class=java.util.List PNG=image/x-java-image;class=java.awt.Image JFIF=image/x-java-image;class=java.awt.Image +HTML=text/html;charset=utf-8;eoln="\r\n";terminators=1 +RICH_TEXT=text/rtf --- /dev/null 2013-02-05 16:36:50.000000000 +0400 +++ new/test/java/awt/DataFlavor/MissedHtmlAndRtfBug.java 2013-02-05 16:36:50.259060400 +0400 @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import sun.awt.SunToolkit; + +import java.awt.*; +import java.awt.datatransfer.*; +import java.awt.dnd.*; +import java.awt.event.InputEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.*; +import java.util.List; + +/* + @test + @bug 8005932 + @summary Java 7 on mac os x only provides text clipboard formats + @author mikhail.cherkasov@oracle.com + @run main MissedHtmlAndRtfBug +*/ +public class MissedHtmlAndRtfBug { + static DataFlavor htmlDataFlavor = getByteDataFlavorForNative("HTML", "HTML Format"); + static DataFlavor rtfDataFlavor = getByteDataFlavorForNative("RICH_TEXT", "Rich Text Format"); + private static Robot robot; + private static SunToolkit toolkit; + + private DnDPanel panel1; + private DnDPanel panel2; + private final Frame frame; + + static DataFlavor getByteDataFlavorForNative(String... nats) { + FlavorTable flavorTable = (FlavorTable) SystemFlavorMap.getDefaultFlavorMap(); + + for (String nat : nats) { + List 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)); + } + + + MissedHtmlAndRtfBug() { + frame = new Frame("MissedHtmlAndRtfBug"); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + + Dimension d = new Dimension(100, 100); + + panel1 = new DnDPanel(Color.yellow, htmlDataFlavor); + panel2 = new DnDPanel(Color.pink, rtfDataFlavor); + panel1.setPreferredSize(d); + panel2.setPreferredSize(d); + frame.setLayout(new GridLayout(1, 2, 0, 0)); + frame.add(panel1); + frame.add(panel2); + + frame.pack(); + DropObject.getInstance().setDataFlavors(new DataFlavor[]{htmlDataFlavor, rtfDataFlavor}); + DropObject.getInstance().place(panel1, new Point(10, 10)); + frame.setVisible(true); + + } + + public static void main(String[] args) throws AWTException, InvocationTargetException, InterruptedException { + String os = System.getProperty("os.name").toLowerCase(); + if (os.indexOf("win") == -1 && os.indexOf("mac") == -1) { + return; //unsupported os + } + final MissedHtmlAndRtfBug dndTest = new MissedHtmlAndRtfBug(); + robot = new Robot(); + robot.setAutoDelay(200); + toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); + toolkit.realSync(); + Frame frame = dndTest.frame; + Point point = frame.getLocationOnScreen(); + Point here = new Point(point.x + 35, point.y + 45); + Point there = new Point(point.x + 150, point.y + 45); + dragAndDrop(here, there); + dragAndDrop(there, here); + + } + + private static void dragAndDrop(Point from, Point to) { + toolkit.realSync(); + robot.mouseMove(from.x + 15, from.y + 15); + robot.mousePress(InputEvent.BUTTON1_MASK); + robot.mouseMove(to.x, to.y); + robot.mouseRelease(InputEvent.BUTTON1_MASK); + } + +} + +class DnDPanel extends Panel { + DropObject dropObject; + DragSource dragSource; + DropTarget dropTarget; + DragGestureListener dgListener; + DragSourceListener dsListener; + DropTargetListener dtListener; + + DnDPanel(Color color, final DataFlavor df) { + this.setBackground(color); + this.dragSource = DragSource.getDefaultDragSource(); + dgListener = new DragGestureListener() { + @Override + public void dragGestureRecognized(DragGestureEvent dge) { + Point location = dge.getDragOrigin(); + if (dropObject != null && dropObject.contains(location.x, location.y)) { + dragSource.startDrag(dge, DragSource.DefaultCopyNoDrop, dropObject, dsListener); + } + } + }; + + dsListener = new DragSourceListener() { + @Override + public void dragEnter(DragSourceDragEvent dsde) { + } + + @Override + public void dragOver(DragSourceDragEvent dsde) { + } + + @Override + public void dropActionChanged(DragSourceDragEvent dsde) { + } + + @Override + public void dragExit(DragSourceEvent dse) { + } + + @Override + public void dragDropEnd(DragSourceDropEvent dsde) { + } + }; + + dtListener = new DropTargetListener() { + @Override + public void dragEnter(DropTargetDragEvent dtde) { + if (dropObject != null) { + dtde.rejectDrag(); + return; + } + dtde.acceptDrag(DnDConstants.ACTION_MOVE); + Transferable t = dtde.getTransferable(); + try { + Object data = t.getTransferData(df); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void dragOver(DropTargetDragEvent dtde) { + if (dropObject != null) { + dtde.rejectDrag(); + return; + } + dtde.acceptDrag(DnDConstants.ACTION_MOVE); + } + + @Override + public void dropActionChanged(DropTargetDragEvent dtde) { + } + + @Override + public void dragExit(DropTargetEvent dte) { + } + + @Override + public void drop(DropTargetDropEvent dtde) { + try { + dtde.acceptDrop(DnDConstants.ACTION_MOVE); + Transferable t = dtde.getTransferable(); + byte[] data = (byte[]) t.getTransferData(df); + System.out.println("We get data: " + new String(data, "UTF-16")); + Point location = dtde.getLocation(); + DropObject dropObject = DropObject.getInstance(); + dropObject.place(DnDPanel.this, location); + dtde.dropComplete(true); + } catch (Exception e) { + e.printStackTrace(); + } + + } + }; + + dragSource.createDefaultDragGestureRecognizer(this, + DnDConstants.ACTION_MOVE, dgListener); + + dropTarget = new DropTarget(this, DnDConstants.ACTION_MOVE, dtListener, true); + + } + + public void paint(Graphics g) { + super.paintComponents(g); + if (dropObject != null) { + dropObject.draw((Graphics2D) g); + } + } + + void setDropObject(DropObject dropObject) { + this.dropObject = dropObject; + } + + DropObject findDropObject(int x, int y) { + if (dropObject != null && dropObject.contains(x, y)) { + return dropObject; + } + return null; + } +} + +class DropObject implements Transferable { + private DropObject() { + } + + static private DropObject instance = new DropObject(); + + public static DropObject getInstance() { + return instance; + } + + DnDPanel panel; + Color color = Color.cyan; + int width = 50; + int height = 50; + int x; + int y; + + public DataFlavor[] getDataFlavors() { + return dataFlavors; + } + + public void setDataFlavors(DataFlavor[] dataFlavors) { + this.dataFlavors = dataFlavors; + } + + DataFlavor[] dataFlavors; + + + void draw(Graphics2D g) { + Color savedColor = g.getColor(); + g.setColor(color); + g.fillRect(x, y, width, height); + g.setColor(Color.lightGray); + g.drawRect(x, y, width, height); + g.setColor(savedColor); + } + + boolean contains(int x, int y) { + return (x > this.x && x < this.x + width) + && (y > this.y && y < this.y + height); + } + + @Override + public DataFlavor[] getTransferDataFlavors() { + return getDataFlavors(); + } + + void place(DnDPanel panel, Point location) { + if (panel != this.panel) { + x = location.x; + y = location.y; + if (this.panel != null) { + this.panel.setDropObject(null); + this.panel.repaint(); + } + this.panel = panel; + this.panel.setDropObject(this); + this.panel.repaint(); + } + } + + @Override + public boolean isDataFlavorSupported(DataFlavor flavor) { + for (DataFlavor f : getDataFlavors()) { + if (f.equals(flavor)) { + return true; + } + } + return false; + } + + @Override + public Object getTransferData(DataFlavor flavor) + throws UnsupportedFlavorException, IOException { + if (isDataFlavorSupported(flavor)) { + return "Test".getBytes("UTF-16"); + } else { + throw new UnsupportedFlavorException(flavor); + } + } +} \ No newline at end of file