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.spi.palette;
043:
044: import java.io.FileNotFoundException;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import javax.swing.SwingUtilities;
048: import org.netbeans.modules.palette.DefaultModel;
049: import org.netbeans.modules.palette.DefaultSettings;
050: import org.netbeans.modules.palette.Model;
051: import org.netbeans.modules.palette.RootNode;
052: import org.netbeans.modules.palette.Settings;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileSystem;
055: import org.openide.filesystems.Repository;
056: import org.openide.loaders.DataFolder;
057: import org.openide.nodes.Node;
058: import org.openide.util.lookup.Lookups;
059:
060: /**
061: * <p>PaletteFactory creating new PaletteController instances.</p>
062: *
063: * @author S. Aubrecht
064: */
065: public final class PaletteFactory {
066:
067: /**
068: * Do not allow instances of this class.
069: */
070: private PaletteFactory() {
071: }
072:
073: /**
074: * Create a new palette from the given folder.
075: *
076: * @param rootFolderName Name of palette's root folder, its sub-folders are categories.
077: * Cannot be null.
078: * @param customActions Import actions for palette customizer.
079: *
080: * @throws IOException If the given folder cannot be found.
081: */
082: public static PaletteController createPalette(
083: String rootFolderName, PaletteActions customActions)
084: throws IOException {
085: return createPalette(rootFolderName, customActions, null,
086: DragAndDropHandler.getDefault());
087: }
088:
089: /**
090: * Create a new palette from the given folder.
091: *
092: * @param rootFolderName Name of palette's root folder, its sub-folders are categories.
093: * Cannot be null.
094: * @param customActions Import actions for palette customizer.
095: * @param filter A filter that can dynamically hide some categories and items.
096: * @param dndHandler Handle drop of new items into palette window and add
097: * custom DataFlavors to the Transferable of items being dragged from
098: * the palette to editor window. Can be null.
099: *
100: * @throws IOException If the given folder cannot be found.
101: */
102: public static PaletteController createPalette(
103: String rootFolderName, PaletteActions customActions,
104: PaletteFilter filter, DragAndDropHandler dndHandler)
105: throws IOException {
106:
107: if (null == rootFolderName) {
108: throw new IllegalArgumentException(
109: "Folder name cannot be null.");
110: }
111:
112: DataFolder paletteFolder = DataFolder
113: .findFolder(getPaletteFolder(rootFolderName));
114: return createPalette(paletteFolder.getNodeDelegate(),
115: customActions, filter, dndHandler);
116: }
117:
118: /**
119: * Create a new palette from the given root Node.
120: *
121: * @param paletteRoot Palette's root <code>Node</code>, its children are categories,
122: * their children are palette items.
123: * @param customActions Import actions for palette customizer.
124: */
125: public static PaletteController createPalette(Node paletteRoot,
126: PaletteActions customActions) {
127: return createPalette(paletteRoot, customActions, null,
128: DragAndDropHandler.getDefault());
129: }
130:
131: /**
132: * Create a new palette from the given root Node.
133: *
134: * @param paletteRoot Palette's root <code>Node</code>, its children are categories,
135: * their children are palette items. Cannot be null.
136: * @param customActions Import actions for palette customizer. Cannot be null.
137: * @param filter A filter that can dynamically hide some categories and items. Can be null.
138: * @param dndHandler Handle drop of new items into palette window and add
139: * custom DataFlavors to the Transferable of items being dragged from
140: * the palette to editor window. Can be null.
141: */
142: public static PaletteController createPalette(Node paletteRoot,
143: PaletteActions customActions, PaletteFilter filter,
144: DragAndDropHandler dndHandler) {
145:
146: if (null == paletteRoot) {
147: throw new IllegalArgumentException(
148: "Palette root Node cannot be null.");
149: }
150: if (null == customActions) {
151: throw new IllegalArgumentException(
152: "Palette custom actions must be provided.");
153: }
154:
155: ArrayList<Object> lookupObjects = new ArrayList<Object>(3);
156: lookupObjects.add(customActions);
157: if (null != filter)
158: lookupObjects.add(filter);
159:
160: if (null == dndHandler)
161: dndHandler = DragAndDropHandler.getDefault();
162: lookupObjects.add(dndHandler);
163:
164: RootNode root = new RootNode(paletteRoot, Lookups
165: .fixed(lookupObjects.toArray()));
166: Model model = createModel(root);
167: Settings settings = new DefaultSettings(model);
168:
169: SwingUtilities.invokeLater(new Runnable() {
170: public void run() {
171: PaletteSwitch.getDefault().startListening();
172: }
173: });
174:
175: return new PaletteController(model, settings);
176: }
177:
178: private static Model createModel(RootNode root) {
179: return new DefaultModel(root);
180: }
181:
182: private static FileObject getPaletteFolder(String folderName)
183: throws IOException {
184: FileObject paletteFolder;
185: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
186: paletteFolder = fs.findResource(folderName);
187: if (paletteFolder == null) { // not found, cannot continue
188: throw new FileNotFoundException(folderName);
189: }
190: return paletteFolder;
191: }
192: }
|