KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > palette > PaletteFactory


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.spi.palette;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import org.netbeans.modules.palette.DefaultModel;
26 import org.netbeans.modules.palette.DefaultSettings;
27 import org.netbeans.modules.palette.Model;
28 import org.netbeans.modules.palette.RootNode;
29 import org.netbeans.modules.palette.Settings;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.Repository;
33 import org.openide.loaders.DataFolder;
34 import org.openide.nodes.Node;
35 import org.openide.util.lookup.Lookups;
36
37
38 /**
39  * <p>PaletteFactory creating new PaletteController instances.</p>
40  *
41  * @author S. Aubrecht
42  */

43 public final class PaletteFactory {
44     
45     /**
46      * Do not allow instances of this class.
47      */

48     private PaletteFactory() {
49     }
50
51     /**
52      * Create a new palette from the given folder.
53      *
54      * @param rootFolderName Name of palette's root folder, its sub-folders are categories.
55      * Cannot be null.
56      * @param customActions Import actions for palette customizer.
57      *
58      * @throws IOException If the given folder cannot be found.
59      */

60     public static PaletteController createPalette( String JavaDoc rootFolderName, PaletteActions customActions )
61             throws IOException JavaDoc {
62         return createPalette( rootFolderName, customActions, null, DragAndDropHandler.getDefault() );
63     }
64     
65     
66     /**
67      * Create a new palette from the given folder.
68      *
69      * @param rootFolderName Name of palette's root folder, its sub-folders are categories.
70      * Cannot be null.
71      * @param customActions Import actions for palette customizer.
72      * @param filter A filter that can dynamically hide some categories and items.
73      * @param dndHandler Handle drop of new items into palette window and add
74      * custom DataFlavors to the Transferable of items being dragged from
75      * the palette to editor window. Can be null.
76      *
77      * @throws IOException If the given folder cannot be found.
78      */

79     public static PaletteController createPalette( String JavaDoc rootFolderName,
80                                                    PaletteActions customActions,
81                                                    PaletteFilter filter,
82                                                    DragAndDropHandler dndHandler )
83             throws IOException JavaDoc {
84         
85         if( null == rootFolderName ) {
86             throw new IllegalArgumentException JavaDoc( "Folder name cannot be null." );
87         }
88         
89         DataFolder paletteFolder = DataFolder.findFolder( getPaletteFolder( rootFolderName ) );
90         return createPalette( paletteFolder.getNodeDelegate(), customActions, filter, dndHandler );
91     }
92     
93     
94     /**
95      * Create a new palette from the given root Node.
96      *
97      * @param paletteRoot Palette's root <code>Node</code>, its children are categories,
98      * their children are palette items.
99      * @param customActions Import actions for palette customizer.
100      */

101     public static PaletteController createPalette( Node paletteRoot, PaletteActions customActions ) {
102         return createPalette( paletteRoot, customActions, null, DragAndDropHandler.getDefault() );
103     }
104     
105     /**
106      * Create a new palette from the given root Node.
107      *
108      * @param paletteRoot Palette's root <code>Node</code>, its children are categories,
109      * their children are palette items. Cannot be null.
110      * @param customActions Import actions for palette customizer. Cannot be null.
111      * @param filter A filter that can dynamically hide some categories and items. Can be null.
112      * @param dndHandler Handle drop of new items into palette window and add
113      * custom DataFlavors to the Transferable of items being dragged from
114      * the palette to editor window. Can be null.
115      */

116     public static PaletteController createPalette( Node paletteRoot,
117                                                    PaletteActions customActions,
118                                                    PaletteFilter filter,
119                                                    DragAndDropHandler dndHandler ) {
120         
121         if( null == paletteRoot ) {
122             throw new IllegalArgumentException JavaDoc( "Palette root Node cannot be null." );
123         }
124         if( null == customActions ) {
125             throw new IllegalArgumentException JavaDoc( "Palette custom actions must be provided." );
126         }
127         
128         ArrayList JavaDoc<Object JavaDoc> lookupObjects = new ArrayList JavaDoc<Object JavaDoc>(3);
129         lookupObjects.add( customActions );
130         if( null != filter )
131             lookupObjects.add( filter );
132         
133         if( null == dndHandler )
134             dndHandler = DragAndDropHandler.getDefault();
135         lookupObjects.add( dndHandler );
136         
137
138         RootNode root = new RootNode( paletteRoot, Lookups.fixed( lookupObjects.toArray() ) );
139         Model model = createModel( root );
140         Settings settings = new DefaultSettings( model );
141         
142         PaletteSwitch.getDefault().startListening();
143         
144         return new PaletteController( model, settings );
145     }
146     
147     private static Model createModel( RootNode root ) {
148         return new DefaultModel( root );
149     }
150     
151     private static FileObject getPaletteFolder( String JavaDoc folderName ) throws IOException JavaDoc {
152         FileObject paletteFolder;
153         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
154         paletteFolder = fs.findResource( folderName );
155         if (paletteFolder == null) { // not found, cannot continue
156
throw new FileNotFoundException JavaDoc( folderName );
157         }
158         return paletteFolder;
159     }
160 }
161
Popular Tags