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-2005 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.modules.palette; |
15 |
import java.awt.Image; |
16 |
import java.io.IOException; |
17 |
import java.lang.ref.Reference; |
18 |
import java.lang.ref.WeakReference; |
19 |
import java.net.URL; |
20 |
import java.util.ArrayList; |
21 |
import org.openide.ErrorManager; |
22 |
import org.openide.filesystems.FileObject; |
23 |
import org.openide.loaders.DataObject; |
24 |
import org.openide.loaders.Environment; |
25 |
import org.openide.loaders.XMLDataObject; |
26 |
import org.openide.nodes.Node; |
27 |
import org.openide.text.ActiveEditorDrop; |
28 |
import org.openide.util.Lookup; |
29 |
import org.openide.util.NbBundle; |
30 |
import org.openide.util.Utilities; |
31 |
import org.openide.util.lookup.AbstractLookup; |
32 |
import org.openide.util.lookup.InstanceContent; |
33 |
import org.openide.util.lookup.Lookups; |
34 |
import org.openide.xml.EntityCatalog; |
35 |
import org.openide.xml.XMLUtil; |
36 |
import org.xml.sax.SAXException; |
37 |
import org.xml.sax.XMLReader; |
38 |
|
39 |
/** |
40 |
* |
41 |
* @author Libor Kotouc |
42 |
*/ |
43 |
public class PaletteEnvironmentProvider implements Environment.Provider { |
44 |
|
45 |
private static PaletteEnvironmentProvider createProvider() { |
46 |
return new PaletteEnvironmentProvider(); |
47 |
} |
48 |
|
49 |
private PaletteEnvironmentProvider() { |
50 |
} |
51 |
|
52 |
// ---------------- Environment.Provider ---------------------------- |
53 |
|
54 |
public Lookup getEnvironment(DataObject obj) { |
55 |
|
56 |
PaletteItemNodeFactory nodeFactory = new PaletteItemNodeFactory((XMLDataObject)obj); |
57 |
return nodeFactory.getLookup(); |
58 |
} |
59 |
|
60 |
|
61 |
private static class PaletteItemNodeFactory implements InstanceContent.Convertor { |
62 |
|
63 |
// private static final String URL_PREFIX_INSTANCES = "PaletteItems/"; |
64 |
|
65 |
private XMLDataObject xmlDataObject = null; |
66 |
|
67 |
private Lookup lookup = null; |
68 |
|
69 |
Reference refNode = new WeakReference(null); |
70 |
|
71 |
PaletteItemNodeFactory(XMLDataObject obj) { |
72 |
|
73 |
xmlDataObject = obj; |
74 |
|
75 |
InstanceContent content = new InstanceContent(); |
76 |
content.add(Node.class, this); |
77 |
|
78 |
lookup = new AbstractLookup(content); |
79 |
} |
80 |
|
81 |
Lookup getLookup() { |
82 |
return lookup; |
83 |
} |
84 |
|
85 |
// ---------------- InstanceContent.Convertor ---------------------------- |
86 |
|
87 |
public Class type(Object obj) { |
88 |
return (Class)obj; |
89 |
} |
90 |
|
91 |
public String id(Object obj) { |
92 |
return obj.toString(); |
93 |
} |
94 |
|
95 |
public String displayName(Object obj) { |
96 |
return ((Class)obj).getName(); |
97 |
} |
98 |
|
99 |
public Object convert(Object obj) { |
100 |
Object o = null; |
101 |
if (obj == Node.class) { |
102 |
try { |
103 |
o = getInstance(); |
104 |
} catch (Exception ex) { |
105 |
ErrorManager.getDefault().notify(ex); |
106 |
} |
107 |
} |
108 |
return o; |
109 |
} |
110 |
|
111 |
// ---------------- helper methods ---------------------------- |
112 |
|
113 |
public PaletteItemNode getInstance() { |
114 |
|
115 |
synchronized (this) { |
116 |
PaletteItemNode node = (PaletteItemNode)refNode.get(); |
117 |
if (node != null) |
118 |
return node; |
119 |
|
120 |
FileObject file = xmlDataObject.getPrimaryFile(); |
121 |
if (file.getSize() == 0L) // item file is empty |
122 |
return null; |
123 |
|
124 |
PaletteItemHandler handler = new PaletteItemHandler(); |
125 |
try { |
126 |
XMLReader reader = XMLUtil.createXMLReader(true); |
127 |
reader.setContentHandler(handler); |
128 |
reader.setEntityResolver(EntityCatalog.getDefault()); |
129 |
String urlString = xmlDataObject.getPrimaryFile().getURL().toExternalForm(); |
130 |
reader.parse(urlString); |
131 |
} |
132 |
catch (SAXException saxe) { |
133 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, saxe); |
134 |
} |
135 |
catch (IOException ioe) { |
136 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); |
137 |
} |
138 |
|
139 |
node = createPaletteItemNode(handler); |
140 |
refNode = new WeakReference(node); |
141 |
|
142 |
return node; |
143 |
} |
144 |
} |
145 |
|
146 |
private PaletteItemNode createPaletteItemNode(PaletteItemHandler handler) { |
147 |
|
148 |
String displayName = getDisplayName(handler.getBundleName(), handler.getDisplayNameKey(), handler.getInstanceName()); |
149 |
String tooltip = getTooltip(handler.getBundleName(), handler.getTooltipKey(), handler.getInstanceName(), handler.getDisplayNameKey()); |
150 |
Image icon16 = getIcon(handler.getIcon16URL()); |
151 |
if (icon16 == null) |
152 |
icon16 = Utilities.loadImage("org/netbeans/editor/ext/palette/resources/unknown16.gif"); // NOI18N |
153 |
Image icon32 = getIcon(handler.getIcon32URL()); |
154 |
if (icon32 == null) |
155 |
icon32 = Utilities.loadImage("org/netbeans/editor/ext/palette/resources/unknown32.gif"); // NOI18N |
156 |
|
157 |
|
158 |
ArrayList objectsToLookup = new ArrayList(); |
159 |
|
160 |
ActiveEditorDrop drop = getActiveEditorDrop(handler.getInstanceName()); |
161 |
if (drop != null) |
162 |
objectsToLookup.add(drop); |
163 |
if (handler.getBody() != null) |
164 |
objectsToLookup.add(handler.getBody()); |
165 |
Lookup lookup = Lookups.fixed(objectsToLookup.toArray()); |
166 |
|
167 |
PaletteItemNode node = new PaletteItemNode(displayName, tooltip, icon16, icon32, lookup); |
168 |
|
169 |
return node; |
170 |
} |
171 |
|
172 |
public String getDisplayName( |
173 |
String bundleName, |
174 |
String displayNameKey, |
175 |
String instanceName) |
176 |
{ |
177 |
|
178 |
String displayName = null; |
179 |
try { |
180 |
displayName = NbBundle.getBundle(bundleName).getString(displayNameKey); |
181 |
|
182 |
if (displayName == null && displayNameKey != null) |
183 |
displayName = displayNameKey; |
184 |
|
185 |
if (displayName == null) {//derive name from the instance name |
186 |
if (instanceName != null && instanceName.trim().length() > 0) { |
187 |
int dashIndex = instanceName.lastIndexOf('-') + 1; // NOI18N |
188 |
int dotIndex = instanceName.indexOf('.'); // NOI18N |
189 |
displayName = instanceName.substring(dashIndex, dotIndex); |
190 |
} |
191 |
} |
192 |
|
193 |
if (displayName == null) // no name derived from the item |
194 |
displayName = xmlDataObject.getName(); |
195 |
|
196 |
} |
197 |
catch (Exception ex) { |
198 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); |
199 |
} |
200 |
|
201 |
return (displayName == null ? "" : displayName); |
202 |
} |
203 |
|
204 |
public String getTooltip( |
205 |
String bundleName, |
206 |
String tooltipKey, |
207 |
String instanceName, |
208 |
String displayNameKey) |
209 |
{ |
210 |
|
211 |
String tooltip = null; |
212 |
try { |
213 |
tooltip = NbBundle.getBundle(bundleName).getString(tooltipKey); |
214 |
|
215 |
if (tooltip == null && tooltipKey != null) |
216 |
tooltip = tooltipKey; |
217 |
|
218 |
if (tooltip == null) {//derive name from instance name |
219 |
if (instanceName != null && instanceName.trim().length() > 0) { |
220 |
int dotIndex = instanceName.indexOf('.'); // NOI18N |
221 |
tooltip = instanceName.substring(0, dotIndex).replace('-', '.'); // NOI18N |
222 |
} |
223 |
} |
224 |
|
225 |
if (tooltip == null) // no tooltip derived from the item |
226 |
tooltip = getDisplayName(bundleName, displayNameKey, instanceName); |
227 |
|
228 |
} |
229 |
catch (Exception ex) { |
230 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); |
231 |
} |
232 |
|
233 |
return (tooltip == null ? "" : tooltip); |
234 |
} |
235 |
|
236 |
public Image getIcon(String iconURL) { |
237 |
|
238 |
Image icon = null; |
239 |
try { |
240 |
URL url = new URL(iconURL); |
241 |
icon = java.awt.Toolkit.getDefaultToolkit().getImage(url); |
242 |
} |
243 |
catch (Exception ex) { |
244 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); |
245 |
} |
246 |
|
247 |
return icon; |
248 |
} |
249 |
|
250 |
private ActiveEditorDrop getActiveEditorDrop(String instanceName) { |
251 |
|
252 |
ActiveEditorDrop drop = null; |
253 |
|
254 |
if (instanceName != null && instanceName.trim().length() > 0) {//we should try to instantiate item drop |
255 |
try { |
256 |
// Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class); |
257 |
// FileObject fo = rep.getDefaultFileSystem().findResource(URL_PREFIX_INSTANCES + instanceName); |
258 |
// DataObject _do = DataObject.find(fo); |
259 |
// InstanceDataObject ido = (InstanceDataObject) _do; |
260 |
// drop = (ActiveEditorDrop)ido.instanceCreate(); |
261 |
|
262 |
ClassLoader loader = (ClassLoader)Lookup.getDefault().lookup(ClassLoader.class); |
263 |
if (loader == null) |
264 |
loader = getClass ().getClassLoader (); |
265 |
Class instanceClass = loader.loadClass (instanceName); |
266 |
drop = (ActiveEditorDrop)instanceClass.newInstance(); |
267 |
} |
268 |
catch (Exception ex) { |
269 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); |
270 |
} |
271 |
} |
272 |
|
273 |
return drop; |
274 |
} |
275 |
|
276 |
} |
277 |
|
278 |
|
279 |
} |