1 19 20 package org.netbeans.modules.apisupport.beanbrowser; 21 22 import java.beans.IntrospectionException ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.Node; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.Lookup; 38 import org.openide.util.Utilities; 39 40 43 public class PropSetKids extends Children.Keys { 44 45 private Node original; 46 private Node.PropertySet ps; 47 private PropertyChangeListener pcListener = null; 48 49 public PropSetKids(Node original, Node.PropertySet ps) { 50 this.ps = ps; 51 this.original = original; 52 } 53 54 57 private void updateKeys() { 58 Collection newKeys = new ArrayList (); 59 Node.Property[] props = ps.getProperties(); 60 for (int j = 0; j < props.length; j++) { 61 Node.Property prop = props[j]; 62 if (prop.canRead()) { 63 Class type = prop.getValueType(); 64 if (! (type.isPrimitive() || type == String .class || type == Class .class)) { 65 newKeys.add(prop); 66 } 67 } 68 } 69 setKeys(newKeys); 70 } 71 72 77 protected void addNotify() { 78 updateKeys(); 79 if (pcListener == null) { 80 pcListener = new PropertyChangeListener () { 81 public void propertyChange(PropertyChangeEvent ev) { 82 String prop = ev.getPropertyName(); 83 Node.Property[] props = ps.getProperties(); 84 for (int j = 0; j < props.length; j++) { 85 if (props[j].getName().equals(prop)) { 86 refreshKey(props[j]); 87 break; 88 } 89 } 90 } 91 }; 92 original.addPropertyChangeListener(pcListener); 93 } 94 } 95 96 protected void removeNotify() { 97 if (pcListener != null) { 98 original.removePropertyChangeListener(pcListener); 99 pcListener = null; 100 } 101 setKeys(Collections.EMPTY_SET); 102 } 103 104 108 protected Node[] createNodes(Object key) { 109 return new Node[] { makePropertyNode((Node.Property) key) }; 110 } 111 112 116 private static Node makePropertyNode(Node.Property prop) { 117 Class type = prop.getValueType(); 118 Node node; 119 try { 120 node = makeObjectNode(prop.getValue()); 121 } catch (Throwable t) { 122 node = makeErrorNode(t); 123 } 124 node.setDisplayName(Utilities.getClassName(type) + " " + prop.getDisplayName() + " = " + node.getDisplayName()); 125 return node; 126 } 127 128 135 public static Node makeObjectNode(Object val) { 136 if (val == null) { 137 return makePlainNode("null"); 138 } else if (val instanceof Object []) { 139 return makeCollectionNode(Collections.enumeration(Arrays.asList((Object []) val))); 140 } else if (val.getClass().isArray()) { 141 return makeCollectionNode(Collections.enumeration(Arrays.asList(Utilities.toObjectArray(val)))); 142 } else if (val instanceof Lookup) { 143 Node n = LookupNode.localLookupNode((Lookup) val); 144 n.setShortDescription("String value: `" + val + "'"); 145 n.setDisplayName(n.getDisplayName() + " (class " + val.getClass().getName() + ")"); 146 return n; 147 } else if (val instanceof Enumeration ) { 148 return makeCollectionNode((Enumeration ) val); 149 } else if (val instanceof Collection ) { 150 return makeCollectionNode(Collections.enumeration((Collection ) val)); 151 } else if (val instanceof String ) { 152 return makePlainNode("\"" + (String ) val + "\""); 153 } else if (val instanceof Class ) { 154 return makePlainNode("class " + ((Class ) val).getName()); 155 } else if ((val instanceof Boolean ) || (val instanceof Number )) { 156 return makePlainNode(val.toString()); 157 } else if (val instanceof Character ) { 158 return makePlainNode("(char) '" + val.toString() + "'"); 159 } else { 160 Node objnode; 161 try { 162 objnode = new RefinedBeanNode(val); 163 } catch (IntrospectionException e) { 164 objnode = makeErrorNode(e); 165 } 166 String stringval; 167 try { 168 stringval = "\"" + val + "\""; 169 } catch (RuntimeException e) { 170 stringval = e.toString(); 171 } 172 objnode.setShortDescription(stringval + ": " + objnode.getShortDescription()); 173 objnode.setDisplayName(objnode.getDisplayName() + " (class " + val.getClass().getName() + ")"); 174 return Wrapper.make(objnode); 175 } 176 } 177 178 182 static Node makePlainNode(String name) { 183 AbstractNode toret = new AbstractNode(Children.LEAF) { 184 public HelpCtx getHelpCtx() { 185 return new HelpCtx("org.netbeans.modules.apisupport.beanbrowser"); 186 } 187 }; 188 toret.setName(name); 189 toret.setIconBaseWithExtension("org/netbeans/modules/apisupport/beanbrowser/BeanBrowserIcon.gif"); 190 return toret; 191 } 192 193 197 static Node makeErrorNode(Throwable t) { 198 Node node = makeObjectNode(t); 199 node.setDisplayName("[thrown] " + node.getDisplayName()); 200 return node; 201 } 202 203 208 private static Node makeCollectionNode(final Enumeration val) { 209 final Node[] _base = new Node[] { null }; 210 final String defaultName = "<list of objects>"; 211 Children kids = new Children.Array() { 212 protected void addNotify() { 213 new Thread (new Runnable () { 214 public void run() { 215 int count = 0; 216 while (val.hasMoreElements()) { 217 Node n = makeObjectNode(val.nextElement()); 218 n.setDisplayName("[" + count + "] " + n.getDisplayName()); 219 add(new Node[] { n }); 220 if (count++ == 50) { 221 if (! NotifyDescriptor.OK_OPTION.equals( 222 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation(new String [] { 223 "There were over 50 elements in this array.", 224 "Actually show all of them?" 225 })))) { 226 break; 227 } 228 } 229 } 230 if (defaultName.equals(_base[0].getDisplayName())) { 231 _base[0].setDisplayName("A list of " + count + " children..."); 232 _base[0].setShortDescription(_base[0].getDisplayName()); 233 } else { 234 _base[0].setShortDescription("[" + count + " children] " + _base[0].getShortDescription()); 235 } 236 } 237 }, "making collection node").start(); 238 } 239 }; 240 AbstractNode base = new AbstractNode(kids) { 241 public HelpCtx getHelpCtx() { 242 return new HelpCtx("org.netbeans.modules.apisupport.beanbrowser"); 243 } 244 }; 245 _base[0] = base; 246 base.setName("collection"); 247 base.setDisplayName(defaultName); 248 base.setIconBaseWithExtension("org/netbeans/modules/apisupport/beanbrowser/BeanBrowserIcon.gif"); 249 return base; 250 } 251 } 252 | Popular Tags |