1 7 package javax.swing.plaf.basic; 8 9 import java.io.*; 10 import java.awt.datatransfer.*; 11 import javax.swing.plaf.UIResource ; 12 13 20 class BasicTransferable implements Transferable, UIResource { 21 22 protected String plainData; 23 protected String htmlData; 24 25 private static DataFlavor[] htmlFlavors; 26 private static DataFlavor[] stringFlavors; 27 private static DataFlavor[] plainFlavors; 28 29 static { 30 try { 31 htmlFlavors = new DataFlavor[3]; 32 htmlFlavors[0] = new DataFlavor("text/html;class=java.lang.String"); 33 htmlFlavors[1] = new DataFlavor("text/html;class=java.io.Reader"); 34 htmlFlavors[2] = new DataFlavor("text/html;charset=unicode;class=java.io.InputStream"); 35 36 plainFlavors = new DataFlavor[3]; 37 plainFlavors[0] = new DataFlavor("text/plain;class=java.lang.String"); 38 plainFlavors[1] = new DataFlavor("text/plain;class=java.io.Reader"); 39 plainFlavors[2] = new DataFlavor("text/plain;charset=unicode;class=java.io.InputStream"); 40 41 stringFlavors = new DataFlavor[2]; 42 stringFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=java.lang.String"); 43 stringFlavors[1] = DataFlavor.stringFlavor; 44 45 } catch (ClassNotFoundException cle) { 46 System.err.println("error initializing javax.swing.plaf.basic.BasicTranserable"); 47 } 48 } 49 50 public BasicTransferable(String plainData, String htmlData) { 51 this.plainData = plainData; 52 this.htmlData = htmlData; 53 } 54 55 56 62 public DataFlavor[] getTransferDataFlavors() { 63 DataFlavor[] richerFlavors = getRicherFlavors(); 64 int nRicher = (richerFlavors != null) ? richerFlavors.length : 0; 65 int nHTML = (isHTMLSupported()) ? htmlFlavors.length : 0; 66 int nPlain = (isPlainSupported()) ? plainFlavors.length: 0; 67 int nString = (isPlainSupported()) ? stringFlavors.length : 0; 68 int nFlavors = nRicher + nHTML + nPlain + nString; 69 DataFlavor[] flavors = new DataFlavor[nFlavors]; 70 71 int nDone = 0; 73 if (nRicher > 0) { 74 System.arraycopy(richerFlavors, 0, flavors, nDone, nRicher); 75 nDone += nRicher; 76 } 77 if (nHTML > 0) { 78 System.arraycopy(htmlFlavors, 0, flavors, nDone, nHTML); 79 nDone += nHTML; 80 } 81 if (nPlain > 0) { 82 System.arraycopy(plainFlavors, 0, flavors, nDone, nPlain); 83 nDone += nPlain; 84 } 85 if (nString > 0) { 86 System.arraycopy(stringFlavors, 0, flavors, nDone, nString); 87 nDone += nString; 88 } 89 return flavors; 90 } 91 92 98 public boolean isDataFlavorSupported(DataFlavor flavor) { 99 DataFlavor[] flavors = getTransferDataFlavors(); 100 for (int i = 0; i < flavors.length; i++) { 101 if (flavors[i].equals(flavor)) { 102 return true; 103 } 104 } 105 return false; 106 } 107 108 119 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 120 DataFlavor[] richerFlavors = getRicherFlavors(); 121 if (isRicherFlavor(flavor)) { 122 return getRicherData(flavor); 123 } else if (isHTMLFlavor(flavor)) { 124 String data = getHTMLData(); 125 data = (data == null) ? "" : data; 126 if (String .class.equals(flavor.getRepresentationClass())) { 127 return data; 128 } else if (Reader.class.equals(flavor.getRepresentationClass())) { 129 return new StringReader(data); 130 } else if (InputStream.class.equals(flavor.getRepresentationClass())) { 131 return new StringBufferInputStream(data); 132 } 133 } else if (isPlainFlavor(flavor)) { 135 String data = getPlainData(); 136 data = (data == null) ? "" : data; 137 if (String .class.equals(flavor.getRepresentationClass())) { 138 return data; 139 } else if (Reader.class.equals(flavor.getRepresentationClass())) { 140 return new StringReader(data); 141 } else if (InputStream.class.equals(flavor.getRepresentationClass())) { 142 return new StringBufferInputStream(data); 143 } 144 146 } else if (isStringFlavor(flavor)) { 147 String data = getPlainData(); 148 data = (data == null) ? "" : data; 149 return data; 150 } 151 throw new UnsupportedFlavorException(flavor); 152 } 153 154 156 protected boolean isRicherFlavor(DataFlavor flavor) { 157 DataFlavor[] richerFlavors = getRicherFlavors(); 158 int nFlavors = (richerFlavors != null) ? richerFlavors.length : 0; 159 for (int i = 0; i < nFlavors; i++) { 160 if (richerFlavors[i].equals(flavor)) { 161 return true; 162 } 163 } 164 return false; 165 } 166 167 172 protected DataFlavor[] getRicherFlavors() { 173 return null; 174 } 175 176 protected Object getRicherData(DataFlavor flavor) throws UnsupportedFlavorException { 177 return null; 178 } 179 180 182 188 protected boolean isHTMLFlavor(DataFlavor flavor) { 189 DataFlavor[] flavors = htmlFlavors; 190 for (int i = 0; i < flavors.length; i++) { 191 if (flavors[i].equals(flavor)) { 192 return true; 193 } 194 } 195 return false; 196 } 197 198 202 protected boolean isHTMLSupported() { 203 return htmlData != null; 204 } 205 206 209 protected String getHTMLData() { 210 return htmlData; 211 } 212 213 215 221 protected boolean isPlainFlavor(DataFlavor flavor) { 222 DataFlavor[] flavors = plainFlavors; 223 for (int i = 0; i < flavors.length; i++) { 224 if (flavors[i].equals(flavor)) { 225 return true; 226 } 227 } 228 return false; 229 } 230 231 235 protected boolean isPlainSupported() { 236 return plainData != null; 237 } 238 239 242 protected String getPlainData() { 243 return plainData; 244 } 245 246 248 254 protected boolean isStringFlavor(DataFlavor flavor) { 255 DataFlavor[] flavors = stringFlavors; 256 for (int i = 0; i < flavors.length; i++) { 257 if (flavors[i].equals(flavor)) { 258 return true; 259 } 260 } 261 return false; 262 } 263 264 265 } 266
| Popular Tags
|