Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
/* |
14 |
* NbPropertyEditorManager.java |
15 |
* |
16 |
* Created on January 12, 2003, 8:06 PM |
17 |
*/ |
18 |
|
19 |
package org.netbeans.core; |
20 |
import java.beans.*; |
21 |
import java.util.*; |
22 |
import org.openide.filesystems.*; |
23 |
import org.openide.ErrorManager; |
24 |
import org.openide.util.NbPropertyEditorManager; |
25 |
/** Provides property editors registered via XML, falling |
26 |
* back to <code>java.beans.PropertyEditorManager</code>. |
27 |
* @author Tim Boudreau |
28 |
*/ |
29 |
final class NbPropertyEditorManagerImpl extends NbPropertyEditorManager { |
30 |
private final HashMap map = new HashMap(); |
31 |
private String folder; |
32 |
public static final String ATTR_EDITORFOR = "editorFor"; //NOI18N |
33 |
public static final String ATTR_EDITORCLASS = "editorClass"; //NOI18N |
34 |
|
35 |
public NbPropertyEditorManagerImpl () { |
36 |
this ("/propertyeditors"); |
37 |
} |
38 |
|
39 |
private NbPropertyEditorManagerImpl (String editorFolder) { |
40 |
//XXX may want to expose this constructor later, for e.g. form |
41 |
//editor which wants its own private property editor registry |
42 |
folder = editorFolder; |
43 |
} |
44 |
|
45 |
public PropertyEditor findEditor (Class clazz) { |
46 |
return findEditor (clazz, true); |
47 |
} |
48 |
|
49 |
private static NbPropertyEditorManager instance=null; |
50 |
public static NbPropertyEditorManager getInstance() { |
51 |
if (instance == null) { |
52 |
instance = new NbPropertyEditorManagerImpl(); //NOI18N |
53 |
} |
54 |
return instance; |
55 |
} |
56 |
|
57 |
public PropertyEditor findEditor (Class clazz, boolean newInstance) { |
58 |
synchronized (map) { |
59 |
if (map.isEmpty()) buildMap(); |
60 |
} |
61 |
System.out.println("Looking for editor for " + clazz.getName()); |
62 |
String cname = clazz.getName(); |
63 |
|
64 |
System.out.println("Map lookup for " + cname); |
65 |
System.out.println("Map contents:"); |
66 |
Iterator i = map.keySet().iterator(); |
67 |
while (i.hasNext()) |
68 |
System.out.println(" -" + i.next()); |
69 |
|
70 |
Info in = (Info) map.get(cname); |
71 |
PropertyEditor result = null; |
72 |
if (in != null) |
73 |
result = in.get (newInstance); |
74 |
//Result will only be null if there was a cnfe fetching the editor |
75 |
//class, or if no editor is registered via XML |
76 |
if (result != null) return result; |
77 |
System.out.println("Falling back to property editor manager"); |
78 |
//Fall back to PropertyEditorManager |
79 |
return PropertyEditorManager.findEditor (clazz); |
80 |
} |
81 |
|
82 |
private boolean listening=false; |
83 |
private void buildMap() { |
84 |
synchronized (map) { |
85 |
FileObject fo = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem().findResource(folder); |
86 |
FileObject[] kids = fo.getChildren(); |
87 |
synchronized (map) { |
88 |
for (int i=0; i < kids.length; i++) { |
89 |
String editorFor = (String)kids[i].getAttribute ("editorFor"); //NOI18N |
90 |
map.put (editorFor, new InfoImpl (kids[i])); |
91 |
} |
92 |
} |
93 |
if (listening == false) { |
94 |
fo.addFileChangeListener(new FileChangeListener () { |
95 |
public void fileFolderCreated (FileEvent fe){ |
96 |
//meaningless |
97 |
} |
98 |
public void fileDataCreated (FileEvent fe){ |
99 |
synchronized (map) { |
100 |
map.clear(); |
101 |
} |
102 |
} |
103 |
public void fileChanged (FileEvent fe) { |
104 |
synchronized (map) { |
105 |
map.clear(); |
106 |
} |
107 |
} |
108 |
public void fileDeleted (FileEvent fe) { |
109 |
synchronized (map) { |
110 |
map.clear(); |
111 |
} |
112 |
} |
113 |
|
114 |
public void fileRenamed (FileRenameEvent fe) { |
115 |
//no effect |
116 |
} |
117 |
|
118 |
public void fileAttributeChanged (FileAttributeEvent fe) { |
119 |
//in the case that editors are added or removed, |
120 |
//dump the cache - the next call will rebuild it |
121 |
synchronized (map) { |
122 |
map.clear(); |
123 |
} |
124 |
} |
125 |
|
126 |
}); |
127 |
listening = true; |
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
public boolean hasEditor (Class clazz) { |
133 |
synchronized (map) { |
134 |
if (map.isEmpty()) buildMap(); |
135 |
} |
136 |
String cname = clazz.getName(); |
137 |
return map.keySet().contains (cname); |
138 |
} |
139 |
|
140 |
public Info findInfo(Class clazz) { |
141 |
if (map.isEmpty()) buildMap(); |
142 |
return (Info) map.get (clazz.getName()); |
143 |
} |
144 |
|
145 |
class InfoImpl implements NbPropertyEditorManager.Info { |
146 |
private String editorClassName; |
147 |
private String editorFor; |
148 |
private Class editorClass; |
149 |
private Class edForClass; |
150 |
private boolean badClassInfo=false; |
151 |
private PropertyEditor defaultInstance=null; //make weak/soft ref? |
152 |
private boolean initFromPe=false; |
153 |
public InfoImpl (FileObject fo) { |
154 |
editorClassName = (String) fo.getAttribute (ATTR_EDITORCLASS); //NOI18N |
155 |
editorFor = (String) fo.getAttribute (ATTR_EDITORFOR); //NOI18N |
156 |
System.out.println("Registered " + editorClassName + " for " + editorFor); //XXX debug code |
157 |
} |
158 |
|
159 |
public PropertyEditor get(boolean newInstance) { |
160 |
PropertyEditor result = null; |
161 |
if (newInstance) { |
162 |
try { |
163 |
result = (PropertyEditor) getEditorClass().newInstance(); |
164 |
System.out.println("Created new instance of " + result.getClass().getName()); |
165 |
return result; |
166 |
} catch (Exception e) { |
167 |
e.printStackTrace(); |
168 |
return null; |
169 |
} |
170 |
} else if (defaultInstance == null) { |
171 |
try { |
172 |
result = (PropertyEditor) getEditorClass().newInstance(); |
173 |
defaultInstance = result; |
174 |
System.out.println("Created default instance of " + result.getClass().getName()); |
175 |
} catch (Exception e) { |
176 |
e.printStackTrace(); |
177 |
} |
178 |
} |
179 |
return result; |
180 |
} |
181 |
|
182 |
private Class getEditorClass () { |
183 |
//If we know we have a bad registration, throwing the |
184 |
//exception once is enough. |
185 |
if (badClassInfo) return null; |
186 |
if (editorClass == null) { |
187 |
try { |
188 |
editorClass = Class.forName(editorClassName); |
189 |
} catch (ClassNotFoundException cnfe) { |
190 |
ErrorManager.getDefault().log (ErrorManager.WARNING, |
191 |
"The class \"" + editorClassName + "\" which is a property editor class for \"" //NOI18N |
192 |
+ editorFor + "\" is registered cannot be loaded."); //NOI18N |
193 |
ErrorManager.getDefault().notify(ErrorManager.WARNING, cnfe); |
194 |
badClassInfo = true; |
195 |
} |
196 |
if (editorClass != null) editorClassName = null; |
197 |
} |
198 |
return editorClass; |
199 |
} |
200 |
|
201 |
private Class getEdForClass () { |
202 |
//If we know we have a bad registration, throwing the |
203 |
//exception once is enough. |
204 |
if (badClassInfo) { |
205 |
return null; |
206 |
} |
207 |
if (edForClass == null) { |
208 |
try { |
209 |
edForClass = Class.forName(editorFor); |
210 |
} catch (ClassNotFoundException cnfe) { |
211 |
ErrorManager.getDefault().log (ErrorManager.WARNING, |
212 |
"The class \"" + editorFor + "\" for which the property editor class \"" //NOI18N |
213 |
+ editorClassName + "\" is registered cannot be loaded."); //NOI18N |
214 |
ErrorManager.getDefault().notify(ErrorManager.WARNING, cnfe); |
215 |
badClassInfo = true; |
216 |
} |
217 |
if (edForClass != null) editorFor = null; |
218 |
} |
219 |
return edForClass; |
220 |
} |
221 |
|
222 |
public int hashCode () { |
223 |
if (badClassInfo) return super.hashCode(); |
224 |
return getEditorClass().getName().hashCode() ^ 31; |
225 |
} |
226 |
|
227 |
public boolean equals (Object o) { |
228 |
if (!(o instanceof NbPropertyEditorManager.Info)) |
229 |
return false; |
230 |
return o.hashCode() == hashCode(); |
231 |
} |
232 |
|
233 |
public Object getAttribute(Object key) { |
234 |
//do nothing for now, maybe return fo's attributes |
235 |
return null; |
236 |
} |
237 |
} |
238 |
|
239 |
} |