KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > highlighting > MemoryMimeDataProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2.highlighting;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
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 /**
33  *
34  * @author vita
35  */

36 public final class MemoryMimeDataProvider implements MimeDataProvider {
37     
38     private static final HashMap JavaDoc<String JavaDoc, Lkp> CACHE = new HashMap JavaDoc<String JavaDoc, Lkp>();
39     
40     /** Creates a new instance of MemoryMimeDataProvider */
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 JavaDoc mimePath, Object JavaDoc... 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 JavaDoc mimePath, Object JavaDoc... 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 JavaDoc 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 JavaDoc 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 JavaDoc<Object JavaDoc> all = new ArrayList JavaDoc<Object JavaDoc>();
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 JavaDoc... instances) {
99             all.addAll(Arrays.asList(instances));
100             contents.set(all, null);
101         }
102
103         public void removeInstances(Object JavaDoc... instances) {
104             ArrayList JavaDoc<Object JavaDoc> newAll = new ArrayList JavaDoc<Object JavaDoc>();
105             
106             loop:
107             for(Object JavaDoc oo : all) {
108                 for(Object JavaDoc 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     } // End of Lkp class
126
}
127
Popular Tags