KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > beanbrowser > ClipboardKids


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.beanbrowser;
21
22 import java.awt.datatransfer.Clipboard JavaDoc;
23 import java.awt.datatransfer.DataFlavor JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
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 /** Children list of a Clipboard.
36  * Each key is a DataFlavor.
37  */

38 public class ClipboardKids extends Children.Keys {
39     
40     private Clipboard JavaDoc clip;
41     private ClipboardListener list;
42     
43     public ClipboardKids(Clipboard JavaDoc 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 JavaDoc t = clip.getContents(null);
69         if (t == null) {
70             setKeys(Collections.EMPTY_SET);
71         } else {
72             DataFlavor JavaDoc[] flavors = t.getTransferDataFlavors();
73             if (flavors == null) {
74                 // Should not happen, but sometimes does.
75
setKeys(Collections.EMPTY_SET);
76             } else {
77                 setKeys(flavors);
78             }
79         }
80     }
81     
82     protected Node[] createNodes(Object JavaDoc key) {
83         DataFlavor JavaDoc flav = (DataFlavor JavaDoc) key;
84         try {
85             Object JavaDoc obj = clip.getContents(null).getTransferData(flav);
86             if (obj instanceof MultiTransferObject) {
87                 MultiTransferObject mto = (MultiTransferObject) obj;
88                 List JavaDoc nue = new LinkedList JavaDoc();
89                 int count = mto.getCount();
90                 for (int i = 0; i < count; i++) {
91                     nue.add(PropSetKids.makePlainNode("MultiTransferObject [" + i + "]"));
92                     DataFlavor JavaDoc[] 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 JavaDoc 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 JavaDoc e) {
107             return new Node[] { PropSetKids.makeErrorNode(e) };
108         }
109     }
110     
111     private static Node makeFlavorNode(DataFlavor JavaDoc flav, Object JavaDoc obj) {
112         Node n = PropSetKids.makeObjectNode(obj);
113         n.setDisplayName(flav.getHumanPresentableName() + " = " + n.getDisplayName());
114         return n;
115     }
116     
117 }
118
Popular Tags