Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.openide.nodes; |
43 |
|
44 |
import java.beans.PropertyEditorManager; |
45 |
import java.lang.reflect.Array; |
46 |
import java.util.Arrays; |
47 |
import java.util.HashSet; |
48 |
import java.util.LinkedHashSet; |
49 |
import java.util.List; |
50 |
import java.util.Map; |
51 |
import java.util.Set; |
52 |
import org.openide.util.Exceptions; |
53 |
import org.openide.util.Lookup; |
54 |
import org.openide.util.Lookup.Result; |
55 |
import org.openide.util.LookupEvent; |
56 |
import org.openide.util.LookupListener; |
57 |
|
58 |
/** |
59 |
* Class for registering property editors. |
60 |
* |
61 |
* @author Jan Horvath <jhorvath@netbeans.org> |
62 |
*/ |
63 |
public final class PERegistrationSupport { |
64 |
|
65 |
static final String LOOKUP_PATH = "Services/PropertyEditorManager"; //NOI18N |
66 |
static final String PACKAGE = "packagePath"; //NOI18N |
67 |
static final String EDITOR_CLASS = "propertyEditorClass"; //NOI18N |
68 |
|
69 |
static ClassReg clsReg = null; |
70 |
static PkgReg pkgReg = null; |
71 |
|
72 |
public static synchronized void registerPropertyEditors() { |
73 |
if (clsReg == null) { |
74 |
clsReg = new ClassReg(); |
75 |
} |
76 |
if (pkgReg == null) { |
77 |
pkgReg = new PkgReg(); |
78 |
} |
79 |
} |
80 |
|
81 |
/** |
82 |
* Creates instance of <code>PEPackageRegistration</code> based on layer.xml |
83 |
* attribute values |
84 |
* |
85 |
* @param attrs attributes loaded from layer.xml |
86 |
* @return |
87 |
*/ |
88 |
public static PEPackageRegistration createPackageRegistration(final Map attrs) { |
89 |
String pkg = (String) attrs.get(PACKAGE); |
90 |
return new PEPackageRegistration(pkg); |
91 |
} |
92 |
|
93 |
/** |
94 |
* Creates instance of <code>PEClassRegistration</code> based on layer.xml |
95 |
* attribute values |
96 |
* |
97 |
* @param attrs attributes loaded from layer.xml |
98 |
* @return |
99 |
*/ |
100 |
public static PEClassRegistration createClassRegistration(final Map attrs) { |
101 |
String editorClass = (String) attrs.get(EDITOR_CLASS); |
102 |
Set<String> targetTypes = new LinkedHashSet<String> (); |
103 |
for (int i = 1; ; i++) { |
104 |
String targetType = (String) attrs.get("targetType." + i); //NOI18N |
105 |
if (targetType == null) { |
106 |
break; |
107 |
} |
108 |
targetTypes.add(targetType); |
109 |
} |
110 |
return new PEClassRegistration(editorClass, targetTypes); |
111 |
} |
112 |
|
113 |
/** |
114 |
* returns Class from canonical class name like <code>java.lang.String[]</code> |
115 |
*/ |
116 |
protected static Class<?> getClassFromCanonicalName(String name) throws ClassNotFoundException { |
117 |
Class<?> result; |
118 |
String type = name; |
119 |
int dimensions = 0; |
120 |
while (type.endsWith("[]")) { //NOI18N |
121 |
dimensions++; |
122 |
type = type.substring(0, type.length() - 2); |
123 |
} |
124 |
if ("byte".equals(type)) { //NOI18N |
125 |
result = byte.class; |
126 |
} else if ("short".equals(type)) { //NOI18N |
127 |
result = short.class; |
128 |
} else if ("char".equals(type)) { //NOI18N |
129 |
result = char.class; |
130 |
} else if ("int".equals(type)) { //NOI18N |
131 |
result = int.class; |
132 |
} else if ("long".equals(type)) { //NOI18N |
133 |
result = long.class; |
134 |
} else if ("float".equals(type)) { //NOI18N |
135 |
result = float.class; |
136 |
} else if ("double".equals(type)) { //NOI18N |
137 |
result = double.class; |
138 |
} else if ("boolean".equals(type)) { //NOI18N |
139 |
result = boolean.class; |
140 |
} else { |
141 |
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader(); |
142 |
result = Class.forName(type, true, clsLoader); |
143 |
} |
144 |
if (dimensions > 0) { |
145 |
int d[] = new int[dimensions]; |
146 |
for (int i = 0; i < d.length; i++) { |
147 |
d[i] = 0; |
148 |
} |
149 |
result = Array.newInstance(result, d).getClass(); |
150 |
} |
151 |
return result; |
152 |
} |
153 |
|
154 |
public static class PEPackageRegistration { |
155 |
final String pkg; |
156 |
|
157 |
PEPackageRegistration(String pkg) { |
158 |
this.pkg = pkg; |
159 |
} |
160 |
} |
161 |
|
162 |
public static class PEClassRegistration { |
163 |
final Set<String> targetTypes; |
164 |
final String editorClass; |
165 |
|
166 |
PEClassRegistration(String editorClass, Set<String> targetTypes) { |
167 |
this.editorClass = editorClass; |
168 |
this.targetTypes = targetTypes; |
169 |
} |
170 |
} |
171 |
|
172 |
private static final class ClassReg implements LookupListener { |
173 |
Result<PEClassRegistration> lookupResult = null; |
174 |
|
175 |
ClassReg() { |
176 |
lookupResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEClassRegistration.class); |
177 |
register(); |
178 |
lookupResult.addLookupListener(this); |
179 |
} |
180 |
|
181 |
synchronized void register() { |
182 |
lookupResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEClassRegistration.class); |
183 |
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader(); |
184 |
|
185 |
for (PEClassRegistration clsReg : lookupResult.allInstances()) { |
186 |
for (String type : clsReg.targetTypes) { |
187 |
try { |
188 |
Class<?> cls = getClassFromCanonicalName(type); |
189 |
Class<?> editorCls = Class.forName(clsReg.editorClass, true, clsLoader); |
190 |
PropertyEditorManager.registerEditor(cls, editorCls); |
191 |
} catch (ClassNotFoundException ex) { |
192 |
Exceptions.printStackTrace(ex); |
193 |
} |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
@Override |
199 |
public void resultChanged(LookupEvent ev) { |
200 |
register(); |
201 |
} |
202 |
} |
203 |
|
204 |
static final class PkgReg implements LookupListener { |
205 |
Result<PEPackageRegistration> pkgResult; |
206 |
private static List<String> originalPath = null; |
207 |
|
208 |
PkgReg() { |
209 |
if (originalPath == null) { |
210 |
originalPath = Arrays.asList(PropertyEditorManager.getEditorSearchPath()); |
211 |
} |
212 |
pkgResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEPackageRegistration.class); |
213 |
register(); |
214 |
pkgResult.addLookupListener(this); |
215 |
} |
216 |
|
217 |
synchronized void register() { |
218 |
pkgResult = Lookup.getDefault().lookupResult(PERegistrationSupport.PEPackageRegistration.class); |
219 |
Set<String> newPath = new HashSet<String> (); |
220 |
for (PEPackageRegistration pkgReg : pkgResult.allInstances()) { |
221 |
newPath.add(pkgReg.pkg); |
222 |
} |
223 |
newPath.addAll(originalPath); |
224 |
PropertyEditorManager.setEditorSearchPath(newPath.toArray(new String[newPath.size()])); |
225 |
} |
226 |
|
227 |
@Override |
228 |
public void resultChanged(LookupEvent ev) { |
229 |
register(); |
230 |
} |
231 |
} |
232 |
} |