1 19 20 package org.netbeans.modules.editor.lib2.highlighting; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import org.netbeans.api.editor.mimelookup.MimePath; 27 import org.netbeans.spi.editor.mimelookup.MimeDataProvider; 28 import org.openide.util.Lookup; 29 import org.openide.util.lookup.AbstractLookup; 30 import org.openide.util.lookup.InstanceContent; 31 32 36 public final class MemoryMimeDataProvider implements MimeDataProvider { 37 38 private static final HashMap <String , Lkp> CACHE = new HashMap <String , Lkp>(); 39 40 41 public MemoryMimeDataProvider() { 42 } 43 44 public Lookup getLookup(MimePath mimePath) { 45 return getLookup(mimePath.getPath(), true); 46 } 47 48 public static void addInstances(String mimePath, Object ... instances) { 49 assert mimePath != null : "Mime path can't be null"; 50 getLookup(mimePath, true).addInstances(instances); 51 } 52 53 public static void removeInstances(String mimePath, Object ... instances) { 54 assert mimePath != null : "Mime path can't be null"; 55 getLookup(mimePath, true).removeInstances(instances); 56 } 57 58 public static void reset(String mimePath) { 59 if (mimePath == null) { 60 synchronized (CACHE) { 61 for(Lkp lookup : CACHE.values()) { 62 lookup.reset(); 63 } 64 } 65 } else { 66 Lkp lookup = getLookup(mimePath, false); 67 if (lookup != null) { 68 lookup.reset(); 69 } 70 } 71 } 72 73 private static Lkp getLookup(String mimePath, boolean create) { 74 synchronized (CACHE) { 75 Lkp lookup = CACHE.get(mimePath); 76 if (lookup == null && create) { 77 lookup = new Lkp(); 78 CACHE.put(mimePath, lookup); 79 } 80 return lookup; 81 } 82 } 83 84 private static final class Lkp extends AbstractLookup { 85 86 private ArrayList <Object > all = new ArrayList <Object >(); 87 private InstanceContent contents; 88 89 public Lkp() { 90 this(new InstanceContent()); 91 } 92 93 private Lkp(InstanceContent ic) { 94 super(ic); 95 this.contents = ic; 96 } 97 98 public void addInstances(Object ... instances) { 99 all.addAll(Arrays.asList(instances)); 100 contents.set(all, null); 101 } 102 103 public void removeInstances(Object ... instances) { 104 ArrayList <Object > newAll = new ArrayList <Object >(); 105 106 loop: 107 for(Object oo : all) { 108 for(Object o : instances) { 109 if (o == oo) { 110 continue loop; 111 } 112 } 113 114 newAll.add(oo); 115 } 116 117 all = newAll; 118 contents.set(all, null); 119 } 120 121 public void reset() { 122 all.clear(); 123 contents.set(all, null); 124 } 125 } } 127 | Popular Tags |