001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.palette;
043:
044: import java.io.IOException;
045: import java.lang.ref.Reference;
046: import java.lang.ref.WeakReference;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import org.openide.filesystems.FileObject;
050: import org.openide.loaders.DataNode;
051: import org.openide.loaders.DataObject;
052: import org.openide.loaders.Environment;
053: import org.openide.loaders.XMLDataObject;
054: import org.openide.nodes.Children;
055: import org.openide.nodes.Node;
056: import org.openide.util.Lookup;
057: import org.openide.util.lookup.AbstractLookup;
058: import org.openide.util.lookup.InstanceContent;
059: import org.openide.xml.EntityCatalog;
060: import org.openide.xml.XMLUtil;
061: import org.xml.sax.InputSource;
062: import org.xml.sax.SAXException;
063: import org.xml.sax.XMLReader;
064:
065: /**
066: *
067: * @author Libor Kotouc
068: */
069: public class PaletteEnvironmentProvider implements Environment.Provider {
070:
071: private static PaletteEnvironmentProvider createProvider() {
072: return new PaletteEnvironmentProvider();
073: }
074:
075: private PaletteEnvironmentProvider() {
076: }
077:
078: // ---------------- Environment.Provider ----------------------------
079:
080: public Lookup getEnvironment(DataObject obj) {
081:
082: PaletteItemNodeFactory nodeFactory = new PaletteItemNodeFactory(
083: (XMLDataObject) obj);
084: return nodeFactory.getLookup();
085: }
086:
087: private static class PaletteItemNodeFactory implements
088: InstanceContent.Convertor<Class, PaletteItemNode> {
089:
090: // private static final String URL_PREFIX_INSTANCES = "PaletteItems/";
091:
092: private XMLDataObject xmlDataObject = null;
093:
094: private Lookup lookup = null;
095:
096: Reference<PaletteItemNode> refNode = new WeakReference<PaletteItemNode>(
097: null);
098:
099: PaletteItemNodeFactory(XMLDataObject obj) {
100:
101: xmlDataObject = obj;
102:
103: InstanceContent content = new InstanceContent();
104: content.add(Node.class, this );
105:
106: lookup = new AbstractLookup(content);
107: }
108:
109: Lookup getLookup() {
110: return lookup;
111: }
112:
113: // ---------------- InstanceContent.Convertor ----------------------------
114:
115: public Class<? extends PaletteItemNode> type(Class obj) {
116: if (obj == Node.class)
117: return PaletteItemNode.class;
118: return null;
119: }
120:
121: public String id(Class obj) {
122: return obj.toString();
123: }
124:
125: public String displayName(Class obj) {
126: return obj.getName();
127: }
128:
129: public PaletteItemNode convert(Class obj) {
130: PaletteItemNode o = null;
131: if (obj == Node.class) {
132: try {
133: o = getInstance();
134: } catch (Exception ex) {
135: Logger.getLogger(getClass().getName()).log(
136: Level.INFO, null, ex);
137: }
138: }
139:
140: return o;
141: }
142:
143: // ---------------- helper methods ----------------------------
144:
145: public synchronized PaletteItemNode getInstance() {
146:
147: PaletteItemNode node = refNode.get();
148: if (node != null)
149: return node;
150:
151: FileObject file = xmlDataObject.getPrimaryFile();
152: if (file.getSize() == 0L) // item file is empty
153: return null;
154:
155: PaletteItemHandler handler = new PaletteItemHandler();
156: try {
157: XMLReader reader = XMLUtil.createXMLReader(true);
158: reader.setContentHandler(handler);
159: reader.setEntityResolver(EntityCatalog.getDefault());
160: FileObject fo = xmlDataObject.getPrimaryFile();
161: String urlString = fo.getURL().toExternalForm();
162: InputSource is = new InputSource(fo.getInputStream());
163: is.setSystemId(urlString);
164: reader.parse(is);
165: } catch (SAXException saxe) {
166: Logger.getLogger(getClass().getName()).log(Level.INFO,
167: null, saxe);
168: } catch (IOException ioe) {
169: Logger.getLogger(getClass().getName()).log(Level.INFO,
170: null, ioe);
171: }
172:
173: node = createPaletteItemNode(handler);
174: refNode = new WeakReference<PaletteItemNode>(node);
175:
176: return node;
177: }
178:
179: private PaletteItemNode createPaletteItemNode(
180: PaletteItemHandler handler) {
181:
182: String name = xmlDataObject.getName();
183:
184: InstanceContent ic = new InstanceContent();
185: String s = handler.getClassName();
186: if (s != null)
187: ic.add(s, ActiveEditorDropProvider.getInstance());
188: else {
189: s = handler.getBody();
190: if (s != null)
191: ic.add(s, ActiveEditorDropDefaultProvider
192: .getInstance());
193: }
194:
195: return (null == handler.getDisplayName()) ? new PaletteItemNode(
196: new DataNode(xmlDataObject, Children.LEAF), name,
197: handler.getBundleName(), handler
198: .getDisplayNameKey(), handler
199: .getClassName(), handler.getTooltipKey(),
200: handler.getIcon16URL(), handler.getIcon32URL(), ic)
201: : new PaletteItemNode(new DataNode(xmlDataObject,
202: Children.LEAF), name, handler
203: .getDisplayName(), handler.getTooltip(),
204: handler.getIcon16URL(), handler
205: .getIcon32URL(), ic);
206: }
207: }
208:
209: }
|