1 19 20 package org.netbeans.modules.apisupport.beanbrowser; 21 22 import java.awt.datatransfer.Clipboard ; 23 import java.awt.datatransfer.DataFlavor ; 24 import java.awt.datatransfer.Transferable ; 25 import java.util.Collections ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import org.openide.nodes.Children; 29 import org.openide.nodes.Node; 30 import org.openide.util.datatransfer.ClipboardEvent; 31 import org.openide.util.datatransfer.ClipboardListener; 32 import org.openide.util.datatransfer.ExClipboard; 33 import org.openide.util.datatransfer.MultiTransferObject; 34 35 38 public class ClipboardKids extends Children.Keys { 39 40 private Clipboard clip; 41 private ClipboardListener list; 42 43 public ClipboardKids(Clipboard clip) { 44 this.clip = clip; 45 } 46 47 protected void addNotify() { 48 updateKeys(); 49 if (list == null && (clip instanceof ExClipboard)) { 50 list = new ClipboardListener() { 51 public void clipboardChanged(ClipboardEvent ev) { 52 updateKeys(); 53 } 54 }; 55 ((ExClipboard) clip).addClipboardListener(list); 56 } 57 } 58 59 protected void removeNotify() { 60 if (list != null) { 61 ((ExClipboard) clip).removeClipboardListener(list); 62 list = null; 63 } 64 setKeys(Collections.EMPTY_SET); 65 } 66 67 private void updateKeys() { 68 Transferable t = clip.getContents(null); 69 if (t == null) { 70 setKeys(Collections.EMPTY_SET); 71 } else { 72 DataFlavor [] flavors = t.getTransferDataFlavors(); 73 if (flavors == null) { 74 setKeys(Collections.EMPTY_SET); 76 } else { 77 setKeys(flavors); 78 } 79 } 80 } 81 82 protected Node[] createNodes(Object key) { 83 DataFlavor flav = (DataFlavor ) key; 84 try { 85 Object obj = clip.getContents(null).getTransferData(flav); 86 if (obj instanceof MultiTransferObject) { 87 MultiTransferObject mto = (MultiTransferObject) obj; 88 List nue = new LinkedList (); 89 int count = mto.getCount(); 90 for (int i = 0; i < count; i++) { 91 nue.add(PropSetKids.makePlainNode("MultiTransferObject [" + i + "]")); 92 DataFlavor [] flavs = mto.getTransferDataFlavors(i); 93 for (int j = 0; j < flavs.length; j++) { 94 try { 95 nue.add(makeFlavorNode(flavs[j], mto.getTransferData(i, flavs[j]))); 96 } catch (Exception e) { 97 nue.add(PropSetKids.makeErrorNode(e)); 98 } 99 } 100 } 101 nue.add(PropSetKids.makePlainNode("MultiTransferObject [end]")); 102 return (Node[]) nue.toArray(new Node[nue.size()]); 103 } else { 104 return new Node[] { makeFlavorNode(flav, obj) }; 105 } 106 } catch (Exception e) { 107 return new Node[] { PropSetKids.makeErrorNode(e) }; 108 } 109 } 110 111 private static Node makeFlavorNode(DataFlavor flav, Object obj) { 112 Node n = PropSetKids.makeObjectNode(obj); 113 n.setDisplayName(flav.getHumanPresentableName() + " = " + n.getDisplayName()); 114 return n; 115 } 116 117 } 118 | Popular Tags |