1 19 20 package org.openide.loaders; 21 22 23 import java.awt.datatransfer.*; 24 import java.awt.dnd.DnDConstants ; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import org.openide.util.datatransfer.*; 30 31 40 public abstract class LoaderTransfer { 41 42 43 private LoaderTransfer () {} 44 45 49 public static final int DND_NONE = DnDConstants.ACTION_NONE; 50 public static final int DND_COPY = DnDConstants.ACTION_COPY; 51 public static final int DND_MOVE = DnDConstants.ACTION_MOVE; 52 public static final int DND_COPY_OR_MOVE = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE; 53 public static final int DND_LINK = DnDConstants.ACTION_LINK; 54 public static final int DND_REFERENCE = DnDConstants.ACTION_LINK; 55 56 60 public static final int CLIPBOARD_COPY = DND_COPY; 61 62 64 public static final int CLIPBOARD_CUT = 0x04; 65 66 69 public static final int COPY = CLIPBOARD_COPY | DND_COPY; 70 71 74 public static final int MOVE = CLIPBOARD_CUT | DND_MOVE; 75 76 77 79 private static MessageFormat dndMimeType = new MessageFormat ( 80 "application/x-java-openide-dataobjectdnd;class=org.openide.loaders.DataObject;mask={0}" ); 82 83 90 public static ExTransferable.Single transferable (final DataObject d, int actions) { 91 return new ExTransferable.Single (createDndFlavor (actions)) { 92 public Object getData () { 93 return d; 94 } 95 }; 96 } 97 98 106 public static DataObject getDataObject (Transferable t, int action) { 107 DataFlavor[] flavors = t.getTransferDataFlavors (); 108 if (flavors == null) { 109 return null; 110 } 111 int len = flavors.length; 112 113 String subtype = "x-java-openide-dataobjectdnd"; String primary = "application"; String mask = "mask"; 117 for (int i = 0; i < len; i++) { 118 DataFlavor df = flavors[i]; 119 120 if ( 121 df.getSubType ().equals (subtype) && 122 df.getPrimaryType ().equals (primary) 123 ) { 124 try { 125 int m = Integer.valueOf (df.getParameter (mask)).intValue (); 126 if ((m & action) != 0) { 127 DataObject o = (DataObject)t.getTransferData(df); 129 if (o.isValid()) { 130 return o; 131 } else { 132 return null; 134 } 135 } 136 } catch (NumberFormatException nfe) { 137 maybeReportException (nfe); 138 } catch (ClassCastException cce) { 139 maybeReportException (cce); 140 } catch (IOException ioe) { 141 DataObject.LOG.fine( 151 "Object in clipboard refers to a non existing file. "+ ioe.toString()); } catch (UnsupportedFlavorException ufe) { 153 maybeReportException (ufe); 154 } 155 } 156 } 157 158 return null; 159 } 160 161 174 public static DataObject[] getDataObjects (Transferable t, int action) { 175 try { 176 if (t.isDataFlavorSupported (ExTransferable.multiFlavor)) { 177 MultiTransferObject mto = (MultiTransferObject) t.getTransferData (ExTransferable.multiFlavor); 178 int count = mto.getCount (); 179 DataObject[] datas = new DataObject[count]; 180 boolean ok = true; 181 for (int i = 0; i < count; i++) { 182 DataObject d = getDataObject (mto.getTransferableAt (i), action); 183 if (d == null) { 184 ok = false; 185 break; 186 } else { 187 datas[i] = d; 188 } 189 } 190 if (ok && count > 0) return datas; 191 } else { 192 DataObject d = getDataObject (t, action); 193 if (d != null) return new DataObject[] { d }; 194 } 195 } catch (ClassCastException cce) { 196 maybeReportException (cce); 197 } catch (IOException ioe) { 198 maybeReportException (ioe); 199 } catch (UnsupportedFlavorException ufe) { 200 maybeReportException (ufe); 201 } 202 return null; 203 } 204 205 208 private static DataFlavor createDndFlavor (int actions) { 209 try { 210 return new DataFlavor(dndMimeType.format(new Object [] {new Integer (actions)}), null, DataObject.class.getClassLoader()); 211 } catch (ClassNotFoundException ex) { 212 throw new AssertionError (ex); 213 } 214 } 215 216 222 private static void maybeReportException (Exception e) { 223 Logger.getLogger(LoaderTransfer.class.getName()).log(Level.WARNING, null, e); 224 } 225 } 226 | Popular Tags |