1 19 20 package org.netbeans.modules.languages.dataobject; 21 22 import org.netbeans.modules.languages.dataobject.LanguagesEditorKit; 23 import org.netbeans.api.languages.LanguagesManager; 24 import org.netbeans.modules.languages.features.CompletionProviderImpl; 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 import java.util.HashMap ; 30 import java.util.Map ; 31 32 33 37 public class MimeLookupInitializerImpl implements MimeLookupInitializer { 38 39 private String [] mimeTypes; 40 private Map <String [],Lookup.Result> children = new HashMap <String [],Lookup.Result> (); 41 42 public MimeLookupInitializerImpl () { 43 this (new String [0]); 44 } 45 46 public MimeLookupInitializerImpl (String [] mimeTypes) { 47 this.mimeTypes = mimeTypes; 48 } 56 57 63 public Lookup lookup () { 64 return _lookup (); 65 } 66 67 79 public Lookup.Result child (String mimeType) { 80 synchronized (children){ 81 String [] newMimeType = new String [mimeTypes.length + 1]; 82 System.arraycopy (mimeTypes, 0, newMimeType, 0, mimeTypes.length); 83 newMimeType [mimeTypes.length] = mimeType; 84 Lookup.Result child = children.get (newMimeType); 85 if (child == null){ 86 child = Lookups.fixed ( 87 new Object [] { 88 new MimeLookupInitializerImpl (newMimeType) 89 }).<MimeLookupInitializerImpl>lookup ( 90 new Lookup.Template<MimeLookupInitializerImpl> (MimeLookupInitializerImpl.class) 91 ); 92 children.put (newMimeType, child); 93 } 94 return child; 95 } 96 } 97 98 private Lookup lookup; 99 private Lookup _lookup () { 100 if (lookup == null) { 101 if (mimeTypes.length != 1){ 102 lookup = Lookup.EMPTY; 103 return lookup; 104 } 105 if (LanguagesManager.getDefault ().getSupportedMimeTypes (). 106 contains (mimeTypes [0]) 107 ) { 108 lookup = Lookups.fixed ( 109 new Integer [] { 110 new Integer (1), 111 new Integer (2), 112 }, 115 new InstanceContent.Convertor<Integer ,Object > () { 116 public Object convert (Integer i) { 117 switch (i.intValue ()) { 118 case 1: 119 return new LanguagesEditorKit (mimeTypes [0]); 129 case 2: 130 return new CompletionProviderImpl (); 131 } 142 return null; 143 } 144 public Class <? extends Object > type (Integer i) { 145 switch (i.intValue ()) { 146 case 1: 147 return LanguagesEditorKit.class; 148 case 2: 149 return CompletionProviderImpl.class; 150 } 157 return null; 158 } 159 public String id (Integer i) { 160 return i.toString (); 161 } 162 public String displayName (Integer i) { 163 return i.toString (); 164 } 165 } 166 ); 167 } 168 } 169 return lookup; 170 } 171 172 } 211 | Popular Tags |