1 19 20 package org.netbeans.spi.palette; 21 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 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 43 public final class PaletteFactory { 44 45 48 private PaletteFactory() { 49 } 50 51 60 public static PaletteController createPalette( String rootFolderName, PaletteActions customActions ) 61 throws IOException { 62 return createPalette( rootFolderName, customActions, null, DragAndDropHandler.getDefault() ); 63 } 64 65 66 79 public static PaletteController createPalette( String rootFolderName, 80 PaletteActions customActions, 81 PaletteFilter filter, 82 DragAndDropHandler dndHandler ) 83 throws IOException { 84 85 if( null == rootFolderName ) { 86 throw new IllegalArgumentException ( "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 101 public static PaletteController createPalette( Node paletteRoot, PaletteActions customActions ) { 102 return createPalette( paletteRoot, customActions, null, DragAndDropHandler.getDefault() ); 103 } 104 105 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 ( "Palette root Node cannot be null." ); 123 } 124 if( null == customActions ) { 125 throw new IllegalArgumentException ( "Palette custom actions must be provided." ); 126 } 127 128 ArrayList <Object > lookupObjects = new ArrayList <Object >(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 folderName ) throws IOException { 152 FileObject paletteFolder; 153 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 154 paletteFolder = fs.findResource( folderName ); 155 if (paletteFolder == null) { throw new FileNotFoundException ( folderName ); 157 } 158 return paletteFolder; 159 } 160 } 161 | Popular Tags |