KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > PaletteEnvironmentProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.palette;
21 import java.io.IOException JavaDoc;
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
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 JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.XMLReader JavaDoc;
40
41 /**
42  *
43  * @author Libor Kotouc
44  */

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 // ---------------- Environment.Provider ----------------------------
55

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 JavaDoc,PaletteItemNode> {
64
65 // private static final String URL_PREFIX_INSTANCES = "PaletteItems/";
66

67         private XMLDataObject xmlDataObject = null;
68
69         private Lookup lookup = null;
70         
71         Reference JavaDoc<PaletteItemNode> refNode = new WeakReference JavaDoc<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         // ---------------- InstanceContent.Convertor ----------------------------
88

89         public Class JavaDoc<? extends PaletteItemNode> type(Class JavaDoc obj) {
90             if( obj == Node.class )
91                 return PaletteItemNode.class;
92             return null;
93         }
94
95         public String JavaDoc id(Class JavaDoc obj) {
96             return obj.toString();
97         }
98
99         public String JavaDoc displayName(Class JavaDoc obj) {
100             return obj.getName();
101         }
102
103         public PaletteItemNode convert(Class JavaDoc obj) {
104             PaletteItemNode o = null;
105             if (obj == Node.class) {
106                 try {
107                     o = getInstance();
108                 } catch (Exception JavaDoc ex) {
109                     ErrorManager.getDefault().notify(ex);
110                 }
111             }
112            
113             return o;
114         }
115         
116         // ---------------- helper methods ----------------------------
117

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) // item file is empty
126
return null;
127
128             PaletteItemHandler handler = new PaletteItemHandler();
129             try {
130                 XMLReader JavaDoc reader = XMLUtil.createXMLReader(true);
131                 reader.setContentHandler(handler);
132                 reader.setEntityResolver(EntityCatalog.getDefault());
133                 FileObject fo = xmlDataObject.getPrimaryFile();
134                 String JavaDoc urlString = fo.getURL().toExternalForm();
135                 InputSource JavaDoc is = new InputSource JavaDoc(fo.getInputStream());
136                 is.setSystemId(urlString);
137                 reader.parse(is);
138             }
139             catch (SAXException JavaDoc saxe) {
140                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, saxe);
141             }
142             catch (IOException JavaDoc ioe) {
143                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
144             }
145
146             node = createPaletteItemNode(handler);
147             refNode = new WeakReference JavaDoc<PaletteItemNode>(node);
148
149             return node;
150         }
151
152         private PaletteItemNode createPaletteItemNode(PaletteItemHandler handler) {
153
154             String JavaDoc name = xmlDataObject.getName();
155             
156             InstanceContent ic = new InstanceContent();
157             String JavaDoc 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