Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Samuel Halliday |
38 |
* |
39 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
40 |
*/ |
41 |
package org.netbeans.modules.editor.java; |
42 |
|
43 |
import java.util.ArrayList; |
44 |
import java.util.Collection; |
45 |
import java.util.prefs.PreferenceChangeEvent; |
46 |
import java.util.prefs.PreferenceChangeListener; |
47 |
import java.util.prefs.Preferences; |
48 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
49 |
|
50 |
/** |
51 |
* A whitelist/blacklist of excluded classes and packages for the Java completer. |
52 |
* Requested in RFE #125060. |
53 |
* |
54 |
* @author Samuel Halliday |
55 |
*/ |
56 |
public final class ExcludeCompletion implements PreferenceChangeListener { |
57 |
|
58 |
// TODO: move to CodeCompletionPanel |
59 |
public static final String JAVA_COMPLETION_WHITELIST = "javaCompletionWhitelist"; //NOI18N |
60 |
public static final String JAVA_COMPLETION_BLACKLIST = "javaCompletionBlacklist"; //NOI18N |
61 |
private final Collection<String> exclude = new ArrayList<String>(); |
62 |
private final Collection<String> include = new ArrayList<String>(); |
63 |
private static volatile ExcludeCompletion singleton = null; |
64 |
|
65 |
/** |
66 |
* Cheap operation to return an instance |
67 |
* |
68 |
* @return |
69 |
*/ |
70 |
public static ExcludeCompletion getInstance() { |
71 |
// lazy check without lock |
72 |
if (singleton == null) |
73 |
synchronized (ExcludeCompletion.class) { |
74 |
if (singleton == null) { |
75 |
singleton = new ExcludeCompletion(); |
76 |
// is there not a way to just register to listen to Java Completion changes? |
77 |
// the preferences tree does not appear to be very deep |
78 |
Preferences preferences = |
79 |
MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE).lookup(Preferences.class); |
80 |
// ?? why is this commented "weak" code used in Utilities? |
81 |
// WeakListeners.create(PreferenceChangeListener.class, this, preferences); |
82 |
// preferences.addPreferenceChangeListener(null); |
83 |
preferences.addPreferenceChangeListener(singleton); |
84 |
// initial population |
85 |
preferences.put(JAVA_COMPLETION_BLACKLIST, "sun,apple"); |
86 |
preferences.put(JAVA_COMPLETION_WHITELIST, "sun.security"); |
87 |
} |
88 |
} |
89 |
assert singleton != null; |
90 |
return singleton; |
91 |
} |
92 |
|
93 |
private ExcludeCompletion() { |
94 |
} |
95 |
|
96 |
/** |
97 |
* @param fqn Fully Qualified Name |
98 |
* @return |
99 |
*/ |
100 |
public boolean isExcluded(String fqn) { |
101 |
//#122334: do not propose imports from the default package |
102 |
if (fqn == null || fqn.length() == 0) |
103 |
return true; |
104 |
|
105 |
if (include.size() > 0) |
106 |
for (String entry : include) { |
107 |
if (entry.length() > fqn.length()) |
108 |
continue; |
109 |
if (fqn.startsWith(entry)) |
110 |
return false; |
111 |
} |
112 |
|
113 |
if (exclude.size() > 0) |
114 |
for (String entry : exclude) { |
115 |
if (entry.length() > fqn.length()) |
116 |
continue; |
117 |
if (fqn.startsWith(entry)) |
118 |
return true; |
119 |
} |
120 |
|
121 |
return false; |
122 |
} |
123 |
|
124 |
public void preferenceChange(PreferenceChangeEvent evt) { |
125 |
if (evt == null) |
126 |
return; |
127 |
String key = evt.getKey(); |
128 |
if (JAVA_COMPLETION_BLACKLIST.equals(key)) |
129 |
update(exclude, evt.getNewValue()); |
130 |
else if (JAVA_COMPLETION_WHITELIST.equals(key)) |
131 |
update(include, evt.getNewValue()); |
132 |
} |
133 |
|
134 |
private void update(Collection<String> existing, String updated) { |
135 |
// ??: how thread safe does this class need to be with regards to preferences? |
136 |
existing.clear(); |
137 |
if (updated == null || updated.length() == 0) |
138 |
return; |
139 |
String[] entries = updated.split(","); //NOI18N |
140 |
for (String entry : entries) { |
141 |
if (entry != null && entry.length() > 0) |
142 |
existing.add(entry); |
143 |
} |
144 |
} |
145 |
} |