1 7 8 package java.awt.datatransfer; 9 10 import java.io.*; 11 12 26 public class StringSelection implements Transferable , ClipboardOwner { 27 28 private static final int STRING = 0; 29 private static final int PLAIN_TEXT = 1; 30 31 private static final DataFlavor [] flavors = { 32 DataFlavor.stringFlavor, 33 DataFlavor.plainTextFlavor }; 35 36 private String data; 37 38 42 public StringSelection(String data) { 43 this.data = data; 44 } 45 46 56 public DataFlavor [] getTransferDataFlavors() { 57 return (DataFlavor [])flavors.clone(); 60 } 61 62 73 public boolean isDataFlavorSupported(DataFlavor flavor) { 74 for (int i = 0; i < flavors.length; i++) { 76 if (flavor.equals(flavors[i])) { 77 return true; 78 } 79 } 80 return false; 81 } 82 83 107 public Object getTransferData(DataFlavor flavor) 108 throws UnsupportedFlavorException , IOException 109 { 110 if (flavor.equals(flavors[STRING])) { 112 return (Object )data; 113 } else if (flavor.equals(flavors[PLAIN_TEXT])) { 114 return new StringReader(data == null ? "" : data); 115 } else { 116 throw new UnsupportedFlavorException (flavor); 117 } 118 } 119 120 public void lostOwnership(Clipboard clipboard, Transferable contents) { 121 } 122 } 123 | Popular Tags |