Lines 41-49
Link Here
|
41 |
*/ |
41 |
*/ |
42 |
package org.netbeans.modules.openide.nodes; |
42 |
package org.netbeans.modules.openide.nodes; |
43 |
|
43 |
|
|
|
44 |
import java.beans.Introspector; |
44 |
import java.beans.PropertyEditorManager; |
45 |
import java.beans.PropertyEditorManager; |
45 |
import java.lang.reflect.Array; |
46 |
import java.lang.reflect.Array; |
46 |
import java.util.Arrays; |
47 |
import java.util.Arrays; |
|
|
48 |
import java.util.Iterator; |
47 |
import java.util.LinkedHashSet; |
49 |
import java.util.LinkedHashSet; |
48 |
import java.util.List; |
50 |
import java.util.List; |
49 |
import java.util.Map; |
51 |
import java.util.Map; |
Lines 59-79
Link Here
|
59 |
* |
61 |
* |
60 |
* @author Jan Horvath <jhorvath@netbeans.org> |
62 |
* @author Jan Horvath <jhorvath@netbeans.org> |
61 |
*/ |
63 |
*/ |
62 |
public final class PERegistrationSupport { |
64 |
public final class NodesRegistrationSupport { |
63 |
|
65 |
|
64 |
static final String LOOKUP_PATH = "Services/PropertyEditorManager"; //NOI18N |
66 |
static final String PE_LOOKUP_PATH = "Services/PropertyEditorManager"; //NOI18N |
|
|
67 |
static final String BEANINFO_LOOKUP_PATH = "Services/Introspector"; //NOI18N |
65 |
static final String PACKAGE = "packagePath"; //NOI18N |
68 |
static final String PACKAGE = "packagePath"; //NOI18N |
66 |
static final String EDITOR_CLASS = "propertyEditorClass"; //NOI18N |
69 |
static final String EDITOR_CLASS = "propertyEditorClass"; //NOI18N |
67 |
|
70 |
|
68 |
static ClassReg clsReg = null; |
71 |
private static AbstractRegistrator clsReg = null; |
69 |
static PkgReg pkgReg = null; |
72 |
private static AbstractRegistrator beanInfoReg = null; |
|
|
73 |
private static AbstractRegistrator pkgReg = null; |
70 |
|
74 |
|
71 |
public static synchronized void registerPropertyEditors() { |
75 |
public static synchronized void registerPropertyEditors() { |
|
|
76 |
|
72 |
if (clsReg == null) { |
77 |
if (clsReg == null) { |
73 |
clsReg = new ClassReg(); |
78 |
clsReg = new AbstractRegistrator(PEClassRegistration.class) { |
|
|
79 |
|
80 |
@Override |
81 |
void register() { |
82 |
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader(); |
83 |
for (Iterator it = lookupResult.allInstances().iterator(); it.hasNext();) { |
84 |
PEClassRegistration clsReg = (PEClassRegistration) it.next(); |
85 |
for (String type : clsReg.targetTypes) { |
86 |
try { |
87 |
Class<?> cls = getClassFromCanonicalName(type); |
88 |
Class<?> editorCls = Class.forName(clsReg.editorClass, true, clsLoader); |
89 |
PropertyEditorManager.registerEditor(cls, editorCls); |
90 |
} catch (ClassNotFoundException ex) { |
91 |
Exceptions.printStackTrace(ex); |
92 |
} |
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
@Override |
98 |
void init() { |
99 |
} |
100 |
}; |
74 |
} |
101 |
} |
|
|
102 |
|
75 |
if (pkgReg == null) { |
103 |
if (pkgReg == null) { |
76 |
pkgReg = new PkgReg(); |
104 |
pkgReg = new AbstractRegistrator(PEPackageRegistration.class) { |
|
|
105 |
private List<String> originalPath = null; |
106 |
|
107 |
@Override |
108 |
void register() { |
109 |
Set<String> newPath = new LinkedHashSet<String> (); |
110 |
for (Iterator it = lookupResult.allInstances().iterator(); it.hasNext();) { |
111 |
PEPackageRegistration pkgReg = (PEPackageRegistration) it.next(); |
112 |
newPath.add(pkgReg.pkg); |
113 |
} |
114 |
newPath.addAll(originalPath); |
115 |
PropertyEditorManager.setEditorSearchPath(newPath.toArray(new String[newPath.size()])); |
116 |
} |
117 |
|
118 |
@Override |
119 |
void init() { |
120 |
if (originalPath == null) { |
121 |
originalPath = Arrays.asList(PropertyEditorManager.getEditorSearchPath()); |
122 |
} |
123 |
} |
124 |
}; |
125 |
} |
126 |
|
127 |
if (beanInfoReg == null) { |
128 |
beanInfoReg = new AbstractRegistrator(BeanInfoRegistration.class) { |
129 |
private List<String> originalPath = null; |
130 |
|
131 |
@Override |
132 |
void register() { |
133 |
Set<String> newPath = new LinkedHashSet<String> (); |
134 |
for (Iterator it = lookupResult.allInstances().iterator(); it.hasNext();) { |
135 |
BeanInfoRegistration biReg = (BeanInfoRegistration) it.next(); |
136 |
newPath.add(biReg.searchPath); |
137 |
} |
138 |
newPath.addAll(originalPath); |
139 |
Introspector.setBeanInfoSearchPath(newPath.toArray(new String[newPath.size()])); |
140 |
} |
141 |
|
142 |
@Override |
143 |
void init() { |
144 |
if (originalPath == null) { |
145 |
originalPath = Arrays.asList(Introspector.getBeanInfoSearchPath()); |
146 |
} |
147 |
} |
148 |
}; |
77 |
} |
149 |
} |
78 |
} |
150 |
} |
79 |
|
151 |
|
Lines 109-114
Link Here
|
109 |
return new PEClassRegistration(editorClass, targetTypes); |
181 |
return new PEClassRegistration(editorClass, targetTypes); |
110 |
} |
182 |
} |
111 |
|
183 |
|
|
|
184 |
public static BeanInfoRegistration createBeanInfoRegistration(final Map attrs) { |
185 |
String pkg = (String) attrs.get(PACKAGE); |
186 |
return new BeanInfoRegistration(pkg); |
187 |
} |
188 |
|
112 |
/** |
189 |
/** |
113 |
* returns Class from canonical class name like <code>java.lang.String[]</code> |
190 |
* returns Class from canonical class name like <code>java.lang.String[]</code> |
114 |
*/ |
191 |
*/ |
Lines 149-155
Link Here
|
149 |
} |
226 |
} |
150 |
return result; |
227 |
return result; |
151 |
} |
228 |
} |
152 |
|
229 |
|
153 |
public static class PEPackageRegistration { |
230 |
public static class PEPackageRegistration { |
154 |
final String pkg; |
231 |
final String pkg; |
155 |
|
232 |
|
Lines 168-231
Link Here
|
168 |
} |
245 |
} |
169 |
} |
246 |
} |
170 |
|
247 |
|
171 |
private static final class ClassReg implements LookupListener { |
248 |
public static class BeanInfoRegistration { |
172 |
Result<PEClassRegistration> lookupResult = null; |
249 |
final String searchPath; |
173 |
|
250 |
|
174 |
ClassReg() { |
251 |
BeanInfoRegistration(String searchPath) { |
175 |
lookupResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEClassRegistration.class); |
252 |
this.searchPath = searchPath; |
|
|
253 |
} |
254 |
} |
255 |
|
256 |
private static abstract class AbstractRegistrator implements LookupListener { |
257 |
Result lookupResult; |
258 |
private final Class cls; |
259 |
|
260 |
AbstractRegistrator(Class cls) { |
261 |
this.cls = cls; |
262 |
init(); |
263 |
lookupResult = Lookup.getDefault().lookupResult(cls); |
176 |
register(); |
264 |
register(); |
177 |
lookupResult.addLookupListener(this); |
265 |
lookupResult.addLookupListener(this); |
178 |
} |
266 |
} |
179 |
|
267 |
|
180 |
synchronized void register() { |
268 |
abstract void register(); |
181 |
lookupResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEClassRegistration.class); |
269 |
|
182 |
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader(); |
270 |
abstract void init(); |
183 |
|
|
|
184 |
for (PEClassRegistration clsReg : lookupResult.allInstances()) { |
185 |
for (String type : clsReg.targetTypes) { |
186 |
try { |
187 |
Class<?> cls = getClassFromCanonicalName(type); |
188 |
Class<?> editorCls = Class.forName(clsReg.editorClass, true, clsLoader); |
189 |
PropertyEditorManager.registerEditor(cls, editorCls); |
190 |
} catch (ClassNotFoundException ex) { |
191 |
Exceptions.printStackTrace(ex); |
192 |
} |
193 |
} |
194 |
} |
195 |
} |
196 |
|
271 |
|
197 |
@Override |
272 |
@Override |
198 |
public void resultChanged(LookupEvent ev) { |
273 |
public void resultChanged(LookupEvent ev) { |
|
|
274 |
lookupResult = Lookup.getDefault().lookupResult(cls); |
199 |
register(); |
275 |
register(); |
200 |
} |
276 |
} |
201 |
} |
277 |
} |
202 |
|
278 |
|
203 |
static final class PkgReg implements LookupListener { |
|
|
204 |
Result<PEPackageRegistration> pkgResult; |
205 |
private static List<String> originalPath = null; |
206 |
|
207 |
PkgReg() { |
208 |
if (originalPath == null) { |
209 |
originalPath = Arrays.asList(PropertyEditorManager.getEditorSearchPath()); |
210 |
} |
211 |
pkgResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEPackageRegistration.class); |
212 |
register(); |
213 |
pkgResult.addLookupListener(this); |
214 |
} |
215 |
|
216 |
synchronized void register() { |
217 |
pkgResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEPackageRegistration.class); |
218 |
Set<String> newPath = new LinkedHashSet<String> (); |
219 |
for (PEPackageRegistration pkgReg : pkgResult.allInstances()) { |
220 |
newPath.add(pkgReg.pkg); |
221 |
} |
222 |
newPath.addAll(originalPath); |
223 |
PropertyEditorManager.setEditorSearchPath(newPath.toArray(new String[newPath.size()])); |
224 |
} |
225 |
|
226 |
@Override |
227 |
public void resultChanged(LookupEvent ev) { |
228 |
register(); |
229 |
} |
230 |
} |
231 |
} |
279 |
} |