1 19 20 package org.netbeans.modules.palette; 21 import java.io.IOException ; 22 import java.lang.ref.Reference ; 23 import java.lang.ref.WeakReference ; 24 import org.openide.ErrorManager; 25 import org.openide.filesystems.FileObject; 26 import org.openide.loaders.DataNode; 27 import org.openide.loaders.DataObject; 28 import org.openide.loaders.Environment; 29 import org.openide.loaders.XMLDataObject; 30 import org.openide.nodes.Children; 31 import org.openide.nodes.Node; 32 import org.openide.util.Lookup; 33 import org.openide.util.lookup.AbstractLookup; 34 import org.openide.util.lookup.InstanceContent; 35 import org.openide.xml.EntityCatalog; 36 import org.openide.xml.XMLUtil; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.XMLReader ; 40 41 45 public class PaletteEnvironmentProvider implements Environment.Provider { 46 47 private static PaletteEnvironmentProvider createProvider() { 48 return new PaletteEnvironmentProvider(); 49 } 50 51 private PaletteEnvironmentProvider() { 52 } 53 54 56 public Lookup getEnvironment(DataObject obj) { 57 58 PaletteItemNodeFactory nodeFactory = new PaletteItemNodeFactory((XMLDataObject)obj); 59 return nodeFactory.getLookup(); 60 } 61 62 63 private static class PaletteItemNodeFactory implements InstanceContent.Convertor<Class ,PaletteItemNode> { 64 65 67 private XMLDataObject xmlDataObject = null; 68 69 private Lookup lookup = null; 70 71 Reference <PaletteItemNode> refNode = new WeakReference <PaletteItemNode>(null); 72 73 PaletteItemNodeFactory(XMLDataObject obj) { 74 75 xmlDataObject = obj; 76 77 InstanceContent content = new InstanceContent(); 78 content.add(Node.class, this); 79 80 lookup = new AbstractLookup(content); 81 } 82 83 Lookup getLookup() { 84 return lookup; 85 } 86 87 89 public Class <? extends PaletteItemNode> type(Class obj) { 90 if( obj == Node.class ) 91 return PaletteItemNode.class; 92 return null; 93 } 94 95 public String id(Class obj) { 96 return obj.toString(); 97 } 98 99 public String displayName(Class obj) { 100 return obj.getName(); 101 } 102 103 public PaletteItemNode convert(Class obj) { 104 PaletteItemNode o = null; 105 if (obj == Node.class) { 106 try { 107 o = getInstance(); 108 } catch (Exception ex) { 109 ErrorManager.getDefault().notify(ex); 110 } 111 } 112 113 return o; 114 } 115 116 118 public synchronized PaletteItemNode getInstance() { 119 120 PaletteItemNode node = refNode.get(); 121 if (node != null) 122 return node; 123 124 FileObject file = xmlDataObject.getPrimaryFile(); 125 if (file.getSize() == 0L) return null; 127 128 PaletteItemHandler handler = new PaletteItemHandler(); 129 try { 130 XMLReader reader = XMLUtil.createXMLReader(true); 131 reader.setContentHandler(handler); 132 reader.setEntityResolver(EntityCatalog.getDefault()); 133 FileObject fo = xmlDataObject.getPrimaryFile(); 134 String urlString = fo.getURL().toExternalForm(); 135 InputSource is = new InputSource (fo.getInputStream()); 136 is.setSystemId(urlString); 137 reader.parse(is); 138 } 139 catch (SAXException saxe) { 140 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, saxe); 141 } 142 catch (IOException ioe) { 143 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 144 } 145 146 node = createPaletteItemNode(handler); 147 refNode = new WeakReference <PaletteItemNode>(node); 148 149 return node; 150 } 151 152 private PaletteItemNode createPaletteItemNode(PaletteItemHandler handler) { 153 154 String name = xmlDataObject.getName(); 155 156 InstanceContent ic = new InstanceContent(); 157 String s = handler.getClassName(); 158 if (s != null) 159 ic.add(s, ActiveEditorDropProvider.getInstance()); 160 else { 161 s = handler.getBody(); 162 if (s != null) 163 ic.add(s, ActiveEditorDropDefaultProvider.getInstance()); 164 } 165 166 return (null == handler.getDisplayName()) 167 ? new PaletteItemNode( 168 new DataNode(xmlDataObject, Children.LEAF), 169 name, 170 handler.getBundleName(), 171 handler.getDisplayNameKey(), 172 handler.getClassName(), 173 handler.getTooltipKey(), 174 handler.getIcon16URL(), 175 handler.getIcon32URL(), 176 ic ) 177 : new PaletteItemNode( 178 new DataNode(xmlDataObject, Children.LEAF), 179 name, 180 handler.getDisplayName(), 181 handler.getTooltip(), 182 handler.getIcon16URL(), 183 handler.getIcon32URL(), 184 ic ); 185 } 186 } 187 188 } 189 | Popular Tags |