1/* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 test 26 @bug 7075105 27 @summary WIN: Provide a way to format HTML on drop 28 @author Denis Fokin: area=datatransfer 29 @run applet/manual=yesno ManualHTMLDataFlavorTest 30*/ 31 32import java.applet.Applet; 33import java.awt.*; 34import java.awt.datatransfer.DataFlavor; 35import java.awt.datatransfer.Transferable; 36import java.awt.datatransfer.UnsupportedFlavorException; 37import java.awt.dnd.*; 38import java.io.IOException; 39 40public class ManualHTMLDataFlavorTest extends Applet { 41 42 class DropPane extends Panel implements DropTargetListener { 43 44 DropPane() { 45 requestFocus(); 46 setBackground(Color.red); 47 setDropTarget(new DropTarget(this, DnDConstants.ACTION_COPY, this)); 48 } 49 50 @Override 51 public Dimension getPreferredSize() { 52 return new Dimension(200,200); 53 } 54 55 @Override 56 public void dragEnter(DropTargetDragEvent dtde) { 57 dtde.acceptDrag(DnDConstants.ACTION_COPY); 58 } 59 60 @Override 61 public void dragOver(DropTargetDragEvent dtde) { 62 dtde.acceptDrag(DnDConstants.ACTION_COPY); 63 } 64 65 @Override 66 public void dropActionChanged(DropTargetDragEvent dtde) { 67 dtde.acceptDrag(DnDConstants.ACTION_COPY); 68 } 69 70 @Override 71 public void dragExit(DropTargetEvent dte) {} 72 73 @Override 74 public void drop(DropTargetDropEvent dtde) { 75 if (!dtde.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) { 76 Sysout.println("DataFlavor.allHtmlFlavor is not present in the system clipboard"); 77 dtde.rejectDrop(); 78 return; 79 } else if (!dtde.isDataFlavorSupported(DataFlavor.fragmentHtmlFlavor)) { 80 Sysout.println("DataFlavor.fragmentHtmlFlavor is not present in the system clipboard"); 81 dtde.rejectDrop(); 82 return; 83 } else if (!dtde.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) { 84 Sysout.println("DataFlavor.selectionHtmlFlavor is not present in the system clipboard"); 85 dtde.rejectDrop(); 86 return; 87 } 88 89 dtde.acceptDrop(DnDConstants.ACTION_COPY); 90 91 Transferable t = dtde.getTransferable(); 92 try { 93 Sysout.println("ALL:"); 94 Sysout.println(t.getTransferData(DataFlavor.allHtmlFlavor).toString()); 95 Sysout.println("FRAGMENT:"); 96 Sysout.println(t.getTransferData(DataFlavor.fragmentHtmlFlavor).toString()); 97 Sysout.println("SELECTION:"); 98 Sysout.println(t.getTransferData(DataFlavor.selectionHtmlFlavor).toString()); 99 } catch (UnsupportedFlavorException | IOException e) { 100 e.printStackTrace(); 101 } 102 103 } 104 } 105 106 public void init() { 107 108 String[] instructions = 109 { 110 "1) The test contains a drop-aware panel with a red background", 111 "2) Open some page in a browser, select some text", 112 " Drag and drop it on the red panel", 113 " IMPORTANT NOTE: the page should be stored locally.", 114 " otherwise for instance iexplore can prohibit drag and drop from", 115 " the browser to other applications because of", 116 " the protected mode restrictions.", 117 " On Mac OS X do NOT use Safari, it does not provide the needed DataFlavor", 118 "3) Check the data in the output area of this dialog", 119 "5) The output should not contain information that any of", 120 " flavors is not present in the system clipboard", 121 "6) The output should contain data in three different formats", 122 " provided by the system clipboard", 123 " - Data after the \"ALL:\" marker should include the data", 124 " from the the \"SELECTION:\" marker", 125 " - Data after the \"FRAGMENT\" marker should include the data", 126 " from the \"SELECTION:\" marker and may be some closing", 127 " tags could be added to the mark-up", 128 " - Data after the \"SELECTION:\" marker should correspond", 129 " to the data selected in the browser", 130 "7) If the above requirements are met, the test is passed" 131 }; 132 133 add(new DropPane()); 134 Sysout.createDialogWithInstructions( instructions ); 135 136 new ManualHTMLDataFlavorTest(); 137 } 138 139 public void start () 140 { 141 setSize (200,200); 142 setVisible(true); 143 validate(); 144 145 }// start() 146 147} 148 149 150/* Place other classes related to the test after this line */ 151 152 153 154 155 156/**************************************************** 157 Standard Test Machinery 158 DO NOT modify anything below -- it's a standard 159 chunk of code whose purpose is to make user 160 interaction uniform, and thereby make it simpler 161 to read and understand someone else's test. 162 ****************************************************/ 163 164/** 165 This is part of the standard test machinery. 166 It creates a dialog (with the instructions), and is the interface 167 for sending text messages to the user. 168 To print the instructions, send an array of strings to Sysout.createDialog 169 WithInstructions method. Put one line of instructions per array entry. 170 To display a message for the tester to see, simply call Sysout.println 171 with the string to be displayed. 172 This mimics System.out.println but works within the test harness as well 173 as standalone. 174 */ 175 176class Sysout 177{ 178 private static TestDialog dialog; 179 180 public static void createDialogWithInstructions( String[] instructions ) 181 { 182 dialog = new TestDialog( new Frame(), "Instructions" ); 183 dialog.printInstructions( instructions ); 184 dialog.setVisible(true); 185 println( "Any messages for the tester will display here." ); 186 } 187 188 public static void createDialog( ) 189 { 190 dialog = new TestDialog( new Frame(), "Instructions" ); 191 String[] defInstr = { "Instructions will appear here. ", "" } ; 192 dialog.printInstructions( defInstr ); 193 dialog.setVisible(true); 194 println( "Any messages for the tester will display here." ); 195 } 196 197 198 public static void printInstructions( String[] instructions ) 199 { 200 dialog.printInstructions( instructions ); 201 } 202 203 204 public static void println( String messageIn ) 205 { 206 dialog.displayMessage( messageIn ); 207 } 208 209}// Sysout class 210 211/** 212 This is part of the standard test machinery. It provides a place for the 213 test instructions to be displayed, and a place for interactive messages 214 to the user to be displayed. 215 To have the test instructions displayed, see Sysout. 216 To have a message to the user be displayed, see Sysout. 217 Do not call anything in this dialog directly. 218 */ 219class TestDialog extends Dialog 220{ 221 222 TextArea instructionsText; 223 TextArea messageText; 224 int maxStringLength = 80; 225 226 //DO NOT call this directly, go through Sysout 227 public TestDialog( Frame frame, String name ) 228 { 229 super( frame, name ); 230 int scrollBoth = TextArea.SCROLLBARS_BOTH; 231 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 232 add( "North", instructionsText ); 233 234 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 235 add("Center", messageText); 236 237 pack(); 238 239 setVisible(true); 240 }// TestDialog() 241 242 //DO NOT call this directly, go through Sysout 243 public void printInstructions( String[] instructions ) 244 { 245 //Clear out any current instructions 246 instructionsText.setText( "" ); 247 248 //Go down array of instruction strings 249 250 String printStr, remainingStr; 251 for( int i=0; i < instructions.length; i++ ) 252 { 253 //chop up each into pieces maxSringLength long 254 remainingStr = instructions[ i ]; 255 while( remainingStr.length() > 0 ) 256 { 257 //if longer than max then chop off first max chars to print 258 if( remainingStr.length() >= maxStringLength ) 259 { 260 //Try to chop on a word boundary 261 int posOfSpace = remainingStr. 262 lastIndexOf( ' ', maxStringLength - 1 ); 263 264 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 265 266 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 267 remainingStr = remainingStr.substring( posOfSpace + 1 ); 268 } 269 //else just print 270 else 271 { 272 printStr = remainingStr; 273 remainingStr = ""; 274 } 275 276 instructionsText.append( printStr + "\n" ); 277 278 }// while 279 280 }// for 281 282 }//printInstructions() 283 284 //DO NOT call this directly, go through Sysout 285 public void displayMessage( String messageIn ) 286 { 287 messageText.append( messageIn + "\n" ); 288 System.out.println(messageIn); 289 } 290 291}// TestDialog class 292