1 19 20 package org.netbeans.modules.enode; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 26 import org.openide.ErrorManager; 27 import org.openide.filesystems.FileObject; 28 import org.openide.nodes.Node; 29 import org.openide.util.Lookup; 30 import org.openide.util.Lookup.Template; 31 32 import org.netbeans.spi.enode.LookupContentFactory; 33 34 38 public class FactoryWrapper implements LookupContentFactory { 39 40 44 private FileObject f; 45 46 49 private Object obj; 50 51 52 public FactoryWrapper(FileObject f) { 53 this.f = f; 54 } 55 56 61 public Object create(Node target) { 62 if (obj == null) { 63 obj = instantiate(); 64 } 65 if (obj instanceof LookupContentFactory) { 66 LookupContentFactory lcf = (LookupContentFactory)obj; 67 return lcf.create(target); 68 } 69 return obj; 70 } 71 72 77 public Lookup createLookup(Node target) { 78 if (obj == null) { 79 obj = instantiate(); 80 } 81 if (obj instanceof LookupContentFactory) { 82 LookupContentFactory lcf = (LookupContentFactory)obj; 83 return lcf.createLookup(target); 84 } 85 return null; 86 } 87 88 93 boolean matches(Template template) { 94 if (template.getType() != null) { 95 if (obj != null) { 96 return template.getType().isAssignableFrom(obj.getClass()); 97 } 98 if (! resultImplements().contains(template.getType().getName())) { 99 return false; 100 } 101 } 102 return true; 103 } 104 105 110 private List resultImplements() { 111 String classAttr = (String )f.getAttribute("implements"); ArrayList res = new ArrayList (); 113 StringTokenizer t = new StringTokenizer (classAttr, ","); 114 while (t.hasMoreElements()) { 115 res.add(t.nextElement()); 116 } 117 return res; 118 } 119 120 127 private Class clazz() { 128 if (obj != null) { 129 return obj.getClass(); 130 } 131 try { 132 String classAttr = (String )f.getAttribute("factoryClass"); ClassLoader cl = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 134 if (classAttr != null) { 135 Class c = Class.forName(classAttr, true, cl); 136 return c; 137 } else { 138 throw new IllegalStateException ("Attribute factoryClass not specified for " + f); } 140 } catch (ClassNotFoundException cnfe) { 141 IllegalStateException ise = new IllegalStateException (); 142 ErrorManager.getDefault().annotate(ise, cnfe); 143 throw ise; 144 } 145 } 146 147 152 private Object instantiate() { 153 try { 154 return clazz().newInstance(); 155 } catch (InstantiationException is) { 156 IllegalStateException ise = new IllegalStateException (); 157 ErrorManager.getDefault().annotate(ise, is); 158 throw ise; 159 } catch (IllegalAccessException iae) { 160 IllegalStateException ise = new IllegalStateException (); 161 ErrorManager.getDefault().annotate(ise, iae); 162 throw ise; 163 } 164 } 165 166 169 public String toString() { 170 if (obj != null) { 171 return "FactoryWrapper[" + clazz().getName() + "]"; } 173 return "FactoryWrapper[" + f.getAttribute("factoryClass") + "]"; } 175 176 } 177 | Popular Tags |