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-2007 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.websvc.rest.component.palette;
043:
044: import java.io.FileNotFoundException;
045: import java.io.IOException;
046: import java.io.OutputStreamWriter;
047: import java.util.ArrayList;
048: import java.util.List;
049: import javax.xml.parsers.*;
050: import javax.xml.xpath.*;
051: import org.netbeans.spi.palette.PaletteController;
052: import org.netbeans.spi.palette.PaletteFactory;
053: import org.netbeans.spi.palette.PaletteController;
054: import org.netbeans.spi.palette.PaletteFactory;
055: import org.openide.filesystems.*;
056: import org.openide.loaders.DataObject;
057: import org.openide.nodes.Node;
058: import org.openide.text.ActiveEditorDrop;
059: import org.openide.util.Exceptions;
060: import org.openide.util.Lookup;
061: import org.w3c.dom.Document;
062: import org.xml.sax.SAXException;
063:
064: /**
065: *
066: * @author Ayub Khan
067: */
068: public class RestPaletteFactory {
069:
070: public static final String REST_PALETTE_FOLDER = "RestPalette";
071:
072: public static final String REST_COMPONENTS_FOLDER = "RestComponents";
073:
074: private static final String REST_COMPONENT_DATA = "RestComponentData";
075:
076: private static PaletteController pc = null;
077:
078: private static boolean paletteUpdateInProgress = false;
079:
080: public RestPaletteFactory() {
081: }
082:
083: public static PaletteController createPalette() {
084: try {
085: getPaletteRoot();
086: PaletteController pc = PaletteFactory.createPalette(
087: REST_PALETTE_FOLDER, new RestPaletteActions());
088: setPaletteController(pc);
089: updateAllPaletteItems();
090: return pc;
091: } catch (IOException ex) {
092: Exceptions.printStackTrace(ex);
093: }
094: return null;
095: }
096:
097: public synchronized static PaletteController getPaletteController() {
098: return pc;
099: }
100:
101: public synchronized static void setPaletteController(
102: PaletteController pController) {
103: pc = pController;
104: }
105:
106: public synchronized static boolean isPaletteUpdateInProgress() {
107: return paletteUpdateInProgress;
108: }
109:
110: public synchronized static void setPaletteUpdateInProgress(
111: boolean inProgress) {
112: paletteUpdateInProgress = inProgress;
113: }
114:
115: //---------------------------------- helpers ------------------------------------------------------------------
116:
117: public static void updateAllPaletteItems() {
118: try {
119: setPaletteUpdateInProgress(true);
120: for (org.openide.filesystems.FileObject fo : getAllRestComponentFiles()) {
121: try {
122: createPaletteItemFromComponent(fo, false);
123: } catch (javax.xml.parsers.ParserConfigurationException ex) {
124: } catch (org.xml.sax.SAXException ex) {
125: } catch (java.io.IOException ex) {
126: }
127: }
128:
129: } catch (IOException ex) {
130: }
131: setPaletteUpdateInProgress(false);
132: }
133:
134: public static boolean createPaletteItemFromComponent(FileObject fo)
135: throws IOException, ParserConfigurationException,
136: SAXException {
137: return createPaletteItemFromComponent(fo, true);
138: }
139:
140: private static boolean createPaletteItemFromComponent(
141: FileObject fo, boolean skipInProgress) throws IOException,
142: ParserConfigurationException, SAXException {
143: if (skipInProgress && isPaletteUpdateInProgress())
144: return false;
145: RestComponentData data = getComponentData(fo);
146:
147: String itemsFolderPath = data.getCategoryPath();
148: FileObject root = getPaletteRoot();
149: FileObject itemsFolder1 = createItemsFolder(root,
150: itemsFolderPath);
151: String itemsFolderDisplay = data.getCategoryName();
152: if (itemsFolderDisplay != null) {
153: DataObject d = DataObject.find(itemsFolder1);
154: if (d != null && d.getNodeDelegate() != null)
155: d.getNodeDelegate().setDisplayName(itemsFolderDisplay);
156: }
157: String displayName = data.getDisplayName();
158: String description = data.getDescription();
159: Node itemNode1 = createItemNode(itemsFolder1,
160: getComponentFileName(data), data.getClassName(), data
161: .getIcon16(), data.getIcon32(), displayName,
162: description);
163: setRestComponentData(itemNode1, data);
164: return true;
165: }
166:
167: public static RestComponentData getRestComponentData(Node itemNode) {
168: if (itemNode != null)
169: return (RestComponentData) itemNode
170: .getValue(REST_COMPONENT_DATA);
171: return null;
172: }
173:
174: public static void setRestComponentData(Node itemNode,
175: RestComponentData data) {
176: itemNode.setValue(REST_COMPONENT_DATA, data);
177: }
178:
179: public static RestComponentData getComponentData(FileObject fo)
180: throws SAXException, ParserConfigurationException,
181: IOException, FileNotFoundException {
182: DocumentBuilderFactory dbf = DocumentBuilderFactory
183: .newInstance();
184: DocumentBuilder db = dbf.newDocumentBuilder();
185: Document doc = db.parse(fo.getInputStream());
186: return new RestComponentData(doc);
187: }
188:
189: public static List<FileObject> getAllRestComponentFiles()
190: throws IOException {
191: List<FileObject> files = new ArrayList<FileObject>();
192: FileObject rcFolder = getRestComponentsFolder();
193: getAllRestComponentFiles(rcFolder, files);
194: return files;
195: }
196:
197: public static void getAllRestComponentFiles(FileObject rcFolder,
198: List<FileObject> files) throws IOException {
199: for (FileObject fo : rcFolder.getChildren()) {
200: if (fo.isFolder())
201: getAllRestComponentFiles(fo, files);
202: else
203: files.add(fo);
204: }
205: }
206:
207: public static String getComponentFileName(RestComponentData data) {
208: return data.getName() + ".xml";
209: }
210:
211: public static FileObject createItemsFolder(FileObject root,
212: String itemsFolder) throws IOException {
213: if (root == null)
214: throw new IOException("Rest Palette Folder null");
215: if (itemsFolder == null)
216: throw new IOException("Folder name null");
217: FileObject fooCategory = root.getFileObject(itemsFolder);
218: if (fooCategory == null)
219: fooCategory = FileUtil.createFolder(root, itemsFolder);
220: return fooCategory;
221: }
222:
223: public static Node createItemNode(FileObject itemsFolder,
224: String name, String className, String icon16,
225: String icon32, String displayName, String description)
226: throws IOException, ParserConfigurationException,
227: SAXException {
228: FileObject itemFile = createItemFileWithActiveEditorDrop(
229: itemsFolder, name, className, icon16, icon32,
230: displayName, description);
231: Node itemNode = DataObject.find(itemFile).getNodeDelegate();
232: itemNode.setHidden(false);
233:
234: getPaletteRoot().refresh();
235:
236: Object o = itemNode.getLookup().lookup(ActiveEditorDrop.class);
237: return itemNode;
238: }
239:
240: public static Node createItemNode(FileObject itemsFolder,
241: String name, String className, String icon16,
242: String icon32, String bundleName, String nameKey,
243: String toolTipKey) throws IOException,
244: ParserConfigurationException, SAXException {
245: FileObject itemFile = createItemFileWithActiveEditorDrop(
246: itemsFolder, name, className, icon16, icon32,
247: bundleName, nameKey, toolTipKey);
248: Node itemNode = DataObject.find(itemFile).getNodeDelegate();
249: itemNode.setHidden(false);
250:
251: getPaletteRoot().refresh();
252:
253: Object o = itemNode.getLookup().lookup(ActiveEditorDrop.class);
254: return itemNode;
255: }
256:
257: public static FileObject createItemFileWithActiveEditorDrop(
258: FileObject itemsFolder, String itemFile, String className,
259: String icon16, String icon32, String displayName,
260: String description) throws IOException {
261: FileObject fo = itemsFolder.getFileObject(itemFile);
262: if (fo != null)
263: fo.delete();
264: fo = itemsFolder.createData(itemFile);
265: FileLock lock = fo.lock();
266: try {
267: OutputStreamWriter writer = new OutputStreamWriter(fo
268: .getOutputStream(lock), "UTF-8");
269: try {
270: writer.write("<?xml version='1.0' encoding='UTF-8'?>");
271: writer
272: .write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.1//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_1.dtd'>");
273: writer.write("<editor_palette_item version='1.1'>");
274: writer.write("<class name='" + className + "' />");
275: writer.write("<icon16 urlvalue='" + icon16 + "' />");
276: writer.write("<icon32 urlvalue='" + icon32 + "' />");
277: writer.write("<inline-description>");
278: writer.write("<display-name>" + displayName
279: + "</display-name>");
280: writer.write("<tooltip> <![CDATA[ " + description
281: + " ]]> </tooltip>");
282: writer.write("</inline-description>");
283: writer.write("</editor_palette_item>");
284: } finally {
285: writer.close();
286: }
287: } finally {
288: lock.releaseLock();
289: }
290: return fo;
291: }
292:
293: public static FileObject createItemFileWithActiveEditorDrop(
294: FileObject itemsFolder, String itemFile, String className,
295: String icon16, String icon32, String bundleName,
296: String nameKey, String toolTipKey) throws IOException {
297: FileObject fo = itemsFolder.getFileObject(itemFile);
298: if (fo != null)
299: fo.delete();
300: fo = itemsFolder.createData(itemFile);
301: FileLock lock = fo.lock();
302: try {
303: OutputStreamWriter writer = new OutputStreamWriter(fo
304: .getOutputStream(lock), "UTF-8");
305: try {
306: writer.write("<?xml version='1.0' encoding='UTF-8'?>");
307: writer
308: .write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.1//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_1.dtd'>");
309: writer.write("<editor_palette_item version='1.1'>");
310: writer.write("<class name='" + className + "' />");
311: writer.write("<icon16 urlvalue='" + icon16 + "' />");
312: writer.write("<icon32 urlvalue='" + icon32 + "' />");
313: writer.write("<description localizing-bundle='"
314: + bundleName + "' display-name-key='" + nameKey
315: + "' tooltip-key='" + toolTipKey + "' />");
316: writer.write("</editor_palette_item>");
317: } finally {
318: writer.close();
319: }
320: } finally {
321: lock.releaseLock();
322: }
323: return fo;
324: }
325:
326: public static FileObject getPaletteRoot() throws IOException {
327: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
328: FileObject paletteRoot = fs.findResource(REST_PALETTE_FOLDER);
329: if (paletteRoot == null)
330: paletteRoot = createItemsFolder(fs.getRoot(),
331: REST_PALETTE_FOLDER);
332: return paletteRoot;
333: }
334:
335: public static FileObject getRestComponentsFolder()
336: throws IOException {
337: String compFolderName = REST_COMPONENTS_FOLDER;
338: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
339: return fs.findResource(compFolderName);
340: }
341:
342: public static Lookup getCurrentPaletteItem() {
343: return getPaletteController().getSelectedItem();
344: }
345: }
|