1 19 package org.netbeans.modules.gsf; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.netbeans.modules.gsf.Language; 25 import org.netbeans.spi.editor.mimelookup.MimeLookupInitializer; 26 import org.openide.util.Lookup; 27 import org.openide.util.lookup.InstanceContent; 28 import org.openide.util.lookup.Lookups; 29 30 31 41 public class MimeLookupInitializerImpl implements MimeLookupInitializer { 42 private String [] mimeTypes; 43 private Map <String , Lookup.Result> children = new HashMap (); private Lookup lookup; 45 46 public MimeLookupInitializerImpl() { 47 this(new String [0]); 48 } 49 50 public MimeLookupInitializerImpl(String [] mimeTypes) { 51 this.mimeTypes = mimeTypes; 52 } 53 54 66 public Lookup.Result child(String mimeType) { 67 synchronized (children) { 68 String [] newMimeType = new String [mimeTypes.length + 1]; 69 System.arraycopy(mimeTypes, 0, newMimeType, 0, mimeTypes.length); 70 newMimeType[mimeTypes.length] = mimeType; 71 72 Lookup.Result child = (Lookup.Result)children.get(mimeType); 73 74 if (child == null) { 75 child = Lookups.fixed(new Object [] { new MimeLookupInitializerImpl(newMimeType) }) 76 .lookup(new Lookup.Template(MimeLookupInitializerImpl.class)); 77 children.put(mimeType, child); 78 } 79 80 return child; 81 } 82 } 83 84 90 public Lookup lookup() { 91 if (lookup == null) { 92 if (mimeTypes.length != 1) { 93 lookup = Lookup.EMPTY; 94 95 return lookup; 96 } 97 98 if (LanguageRegistry.getInstance().isSupported(mimeTypes[0])) { 99 final Language language = 100 LanguageRegistry.getInstance().getLanguageByMimeType(mimeTypes[0]); 101 assert language != null; 102 103 LanguageRegistry.getInstance().initializeLanguageForEditor(language); 106 107 lookup = 108 Lookups.fixed(new Integer [] { Integer.valueOf(1) }, 109 new InstanceContent.Convertor() { 110 public Object convert(Object obj) { 111 switch (((Integer )obj).intValue()) { 112 case 1: 113 114 GsfEditorKitFactory outer = 115 new GsfEditorKitFactory(language); 116 117 return outer.kit(); 118 } 119 120 return null; 121 } 122 123 public Class type(Object obj) { 124 switch (((Integer )obj).intValue()) { 125 case 1: 126 return GsfEditorKitFactory.GsfEditorKit.class; 127 } 128 129 return null; 130 } 131 132 public String id(Object obj) { 133 return obj.toString(); 134 } 135 136 public String displayName(Object obj) { 137 return obj.toString(); 138 } 139 }); 140 } 141 } 142 143 return lookup; 144 } 145 } 146 | Popular Tags |