1 19 20 package org.netbeans.modules.looks; 21 22 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import org.netbeans.api.registry.AttributeEvent; 31 import org.netbeans.api.registry.BindingEvent; 32 import org.netbeans.api.registry.ContextListener; 33 import org.netbeans.api.registry.SubcontextEvent; 34 import org.netbeans.spi.registry.SpiUtils; 35 import org.openide.filesystems.FileObject; 36 import org.openide.util.Enumerations; 37 38 43 public abstract class RegistryBridge { 44 45 public static RegistryBridge getDefault( FileObject fo ) { 46 return RAPI.getBridge( fo ); } 49 50 51 53 public static void setDefault( FileObject fo ) { 54 RAPI.setBridge( fo ); } 57 58 59 public abstract Object resolve( String name ); 60 61 public abstract Enumeration getObjects( String name, Class type ); 62 63 public abstract Enumeration getNames( String name ); 64 65 public abstract void addListener( String name, Listener listener ); 66 67 public abstract void removeListener( String name, Listener listener ); 68 69 70 73 public static class RAPI extends RegistryBridge { 74 75 private static final Map cache = new HashMap ( ); 76 77 private org.netbeans.api.registry.Context rootCtx; 78 79 private static RegistryBridge defaultBridge; 81 private RAPI( org.netbeans.api.registry.Context rootCtx ) { 82 this.rootCtx = rootCtx; 83 } 84 85 88 public static RegistryBridge getBridge( FileObject fo ) { 89 90 org.netbeans.api.registry.Context ctx = null; 91 92 if ( fo == null ) { 93 94 if ( defaultBridge != null ) { 95 return defaultBridge; 96 } 97 98 ctx = org.netbeans.api.registry.Context.getDefault(); 100 } 101 else { 102 if ( defaultBridge != null ) { 104 return defaultBridge; 105 } 106 } 107 108 109 RegistryBridge rb = (RegistryBridge)cache.get( ctx ); 110 if ( rb == null ) { 111 rb = new RAPI( ctx ); 112 cache.put( ctx, rb ); 113 } 114 return rb; 115 } 116 117 119 public static void setBridge( FileObject fo ) { 120 org.netbeans.api.registry.Context ctx = SpiUtils.createContext( 121 org.netbeans.api.registry.fs.FileSystemContextFactory.createContext( fo ) ); 122 123 defaultBridge = new RAPI( ctx ); 124 } 126 127 public Object resolve( String name ) { 128 129 int lastSlashIndex = name.lastIndexOf( '/' ); 131 org.netbeans.api.registry.Context ctx = lastSlashIndex == -1 ? 132 rootCtx : 133 rootCtx.getSubcontext( name.substring( 0, lastSlashIndex ) ); 134 135 if ( ctx == null ) { 136 return null; 137 } 138 139 return ctx.getObject( name.substring( lastSlashIndex + 1), null ); 140 } 141 142 public Enumeration getNames( String name ) { 143 org.netbeans.api.registry.Context ctx = rootCtx.getSubcontext( name ); 144 return Collections.enumeration( ctx.getOrderedNames() ); 145 } 146 147 public Enumeration getObjects( String name, final Class type ) { 148 149 final org.netbeans.api.registry.Context subContext = rootCtx.getSubcontext( name ); 150 151 if ( subContext == null ) { 152 Object o = resolve( name ); 154 if ( o != null && ( type == null || type.isInstance( o ) ) ) { 155 return Enumerations.singleton(o); 156 } 157 return Enumerations.empty(); 159 } 160 161 Enumeration en = Collections.enumeration( subContext.getOrderedNames() ); 162 163 return Enumerations.convert(en, new Enumerations.Processor() { 164 public Object process(Object object, Collection ignore) { 165 String bindingName = (String )object; 166 167 if ( bindingName.endsWith( "/" ) ) { return null; } 170 171 Object l = subContext.getObject(bindingName, null); 172 173 if ( type == null ) { 174 return l; 175 } 176 else { 177 return type.isInstance( l ) ? l : null; 178 } 179 } 180 }); 181 } 182 183 public void addListener( String name, Listener listener ) { 184 185 final org.netbeans.api.registry.Context subContext = rootCtx.getSubcontext( name ); 186 187 if ( subContext == null ) { 188 throw new IllegalArgumentException ( "Context " + name + " does not exist" ); } 190 191 listener.setContext( subContext ); 192 subContext.addContextListener( listener ); 193 194 } 195 196 public void removeListener( String name, Listener listener ) { 197 198 if ( listener.context == null ) { 199 throw new IllegalArgumentException ( "Context " + name + " does not exist" ); } 201 202 listener.context.removeContextListener( listener ); 203 204 } 205 206 208 private static org.netbeans.api.registry.Context getRootContext( org.netbeans.api.registry.Context ctx ) { 209 while( ctx.getParentContext() != null ) { 210 ctx = ctx.getParentContext(); 211 } 212 return ctx; 213 } 214 215 } 216 217 218 public static abstract class Listener implements ContextListener { 219 220 private org.netbeans.api.registry.Context context; 221 222 223 public void setContext( org.netbeans.api.registry.Context context ) { 224 this.context = context; 225 } 226 227 public abstract void selectorChanged(); 228 229 public void attributeChanged( AttributeEvent evt ) { 230 selectorChanged(); 231 } 232 233 public void bindingChanged( BindingEvent evt ) { 234 selectorChanged(); 235 } 236 237 public void subcontextChanged( SubcontextEvent evt ) { 238 selectorChanged(); 239 } 240 241 } 242 243 244 246 305 306 307 } 308 | Popular Tags |