1 19 20 package org.openide.nodes; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.datatransfer.UnsupportedFlavorException ; 25 import java.awt.dnd.DnDConstants ; 26 import java.io.IOException ; 27 import java.text.MessageFormat ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import org.openide.util.datatransfer.ExTransferable; 31 import org.openide.util.datatransfer.MultiTransferObject; 32 import org.openide.util.datatransfer.PasteType; 33 34 41 public abstract class NodeTransfer extends Object { 42 46 public static final int DND_NONE = DnDConstants.ACTION_NONE; 47 public static final int DND_COPY = DnDConstants.ACTION_COPY; 48 public static final int DND_MOVE = DnDConstants.ACTION_MOVE; 49 public static final int DND_COPY_OR_MOVE = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE; 50 public static final int DND_LINK = DnDConstants.ACTION_LINK; 51 public static final int DND_REFERENCE = DnDConstants.ACTION_LINK; 52 53 57 public static final int CLIPBOARD_COPY = DND_COPY; 58 59 61 public static final int CLIPBOARD_CUT = 0x04; 62 63 66 public static final int COPY = CLIPBOARD_COPY | DND_COPY; 67 68 71 public static final int MOVE = CLIPBOARD_CUT | DND_MOVE; 72 73 77 private static final DataFlavor nodePasteFlavor; 78 static { 79 try { 80 nodePasteFlavor = new DataFlavor ( 81 "application/x-java-openide-nodepaste;class=org.openide.nodes.Node", Node.getString("LBL_nodePasteFlavor"), 83 Node.class.getClassLoader()); 84 } catch (ClassNotFoundException e) { 85 throw new AssertionError (e); 86 } 87 } 88 89 91 private static MessageFormat dndMimeType = new MessageFormat ( 92 "application/x-java-openide-nodednd;class=org.openide.nodes.Node;mask={0}" ); 94 95 private NodeTransfer() { 96 } 97 98 101 private static DataFlavor createDndFlavor(int actions) { 102 try { 103 return new DataFlavor (dndMimeType.format(new Object [] { new Integer (actions) }), 104 null, Node.class.getClassLoader()); 105 } catch (ClassNotFoundException cnfE) { 106 throw (IllegalStateException ) new IllegalStateException ().initCause(cnfE); 107 } 108 } 109 110 117 public static ExTransferable.Single transferable(final Node n, int actions) { 118 return new ExTransferable.Single(createDndFlavor(actions)) { 119 public Object getData() { 120 return n; 121 } 122 }; 123 } 124 125 133 public static Node node(Transferable t, int action) { 134 DataFlavor [] flavors = t.getTransferDataFlavors(); 135 136 if (flavors == null) { 137 return null; 138 } 139 140 int len = flavors.length; 141 142 String subtype = "x-java-openide-nodednd"; String primary = "application"; String mask = "mask"; 146 for (int i = 0; i < len; i++) { 147 DataFlavor df = flavors[i]; 148 149 if (df.getSubType().equals(subtype) && df.getPrimaryType().equals(primary)) { 150 try { 151 int m = Integer.valueOf(df.getParameter(mask)).intValue(); 152 153 if ((m & action) != 0) { 154 return (Node) t.getTransferData(df); 156 } 157 } catch (NumberFormatException nfe) { 158 maybeReportException(nfe); 159 } catch (ClassCastException cce) { 160 maybeReportException(cce); 161 } catch (IOException ioe) { 162 maybeReportException(ioe); 163 } catch (UnsupportedFlavorException ufe) { 164 maybeReportException(ufe); 165 } 166 } 167 } 168 169 return null; 170 } 171 172 190 public static Node[] nodes(Transferable t, int action) { 191 try { 192 if (t.isDataFlavorSupported(ExTransferable.multiFlavor)) { 193 MultiTransferObject mto = (MultiTransferObject) t.getTransferData(ExTransferable.multiFlavor); 194 int count = mto.getCount(); 195 Node[] ns = new Node[count]; 196 boolean ok = true; 197 198 for (int i = 0; i < count; i++) { 199 Node n = node(mto.getTransferableAt(i), action); 200 201 if (n == null) { 202 ok = false; 203 204 break; 205 } else { 206 ns[i] = n; 207 } 208 } 209 210 if (ok && (count > 0)) { 211 return ns; 212 } 213 } else { 214 Node n = node(t, action); 215 216 if (n != null) { 217 return new Node[] { n }; 218 } 219 } 220 } catch (ClassCastException cce) { 221 maybeReportException(cce); 222 } catch (IOException ioe) { 223 maybeReportException(ioe); 224 } catch (UnsupportedFlavorException ufe) { 225 maybeReportException(ufe); 226 } 227 228 return null; 229 } 230 231 244 public static <T extends Node.Cookie> T cookie(Transferable t, int action, Class <T> cookie) { 245 Node n = node(t, action); 246 247 return (n == null) ? null : n.getCookie(cookie); 248 } 249 250 256 public static ExTransferable.Single createPaste(final Paste paste) { 257 return new ExTransferable.Single(nodePasteFlavor) { 258 public Object getData() { 259 return paste; 260 } 261 }; 262 } 263 264 271 public static Paste findPaste(Transferable t) { 272 try { 273 if (t.isDataFlavorSupported(nodePasteFlavor)) { 274 return (Paste) t.getTransferData(nodePasteFlavor); 275 } 276 } catch (ClassCastException cce) { 277 maybeReportException(cce); 278 } catch (IOException ioe) { 279 maybeReportException(ioe); 280 } catch (UnsupportedFlavorException ufe) { 281 maybeReportException(ufe); 282 } 283 284 return null; 285 } 286 287 293 private static void maybeReportException(Exception e) { 294 Logger.getLogger(NodeTransfer.class.getName()).log(Level.WARNING, null, e); 295 296 } 298 299 316 public interface Paste { 317 323 public PasteType[] types(Node target); 324 } 325 } 326 | Popular Tags |