1 19 20 package org.netbeans.modules.editor.mimelookup; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import org.netbeans.api.editor.mimelookup.MimePath; 25 import org.netbeans.spi.editor.mimelookup.MimeDataProvider; 26 import org.netbeans.spi.editor.mimelookup.MimeLookupInitializer; 27 import org.openide.util.Lookup; 28 import org.openide.util.LookupEvent; 29 import org.openide.util.LookupListener; 30 import org.openide.util.WeakListeners; 31 import org.openide.util.lookup.ProxyLookup; 32 33 37 public final class MimePathLookup extends ProxyLookup implements LookupListener { 38 39 private MimePath mimePath; 40 private Lookup.Result<MimeDataProvider> dataProviders; 41 private Lookup.Result<MimeLookupInitializer> mimeInitializers; 43 44 public MimePathLookup(MimePath mimePath) { 45 super(); 46 47 if (mimePath == null) { 48 throw new NullPointerException ("Mime path can't be null."); } 50 51 this.mimePath = mimePath; 52 53 dataProviders = Lookup.getDefault().lookup(new Lookup.Template<MimeDataProvider>(MimeDataProvider.class)); 54 dataProviders.addLookupListener((LookupListener) WeakListeners.create(LookupListener.class, this, dataProviders)); 55 56 mimeInitializers = Lookup.getDefault().lookup(new Lookup.Template<MimeLookupInitializer>(MimeLookupInitializer.class)); 57 mimeInitializers.addLookupListener((LookupListener) WeakListeners.create(LookupListener.class, this, mimeInitializers)); 58 59 rebuild(); 60 } 61 62 public MimePath getMimePath() { 63 return mimePath; 64 } 65 66 private void rebuild() { 67 ArrayList <Lookup> lookups = new ArrayList <Lookup>(); 68 69 for (MimeDataProvider provider : dataProviders.allInstances()) { 70 Lookup mimePathLookup = provider.getLookup(mimePath); 71 if (mimePathLookup != null) { 72 lookups.add(mimePathLookup); 73 } 74 } 75 76 for (MimeLookupInitializer initializer : mimeInitializers.allInstances()) { 77 for (int j = 0; j < mimePath.size(); j++) { 78 Lookup.Result children = initializer.child(mimePath.getMimeType(j)); 79 for (Iterator k = children.allInstances().iterator(); k.hasNext(); ) { 80 MimeLookupInitializer mli = (MimeLookupInitializer) k.next(); 81 Lookup mimePathLookup = mli.lookup(); 82 if (mimePathLookup != null) { 83 lookups.add(mimePathLookup); 84 } 85 } 86 } 87 } 88 89 setLookups((Lookup [])lookups.toArray(new Lookup[lookups.size()])); 90 } 91 92 96 public void resultChanged(LookupEvent ev) { 97 rebuild(); 98 } 99 100 } 101 | Popular Tags |