1 19 20 21 package org.netbeans.modules.palette; 22 23 import java.awt.datatransfer.Transferable ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import javax.swing.Action ; 27 import org.netbeans.spi.palette.PaletteActions; 28 import org.netbeans.spi.palette.PaletteFilter; 29 30 import org.openide.*; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.*; 33 import org.openide.nodes.*; 34 import org.openide.util.HelpCtx; 35 import org.openide.util.datatransfer.NewType; 36 import org.openide.util.datatransfer.PasteType; 37 import org.openide.util.Lookup; 38 import org.openide.util.lookup.AbstractLookup; 39 import org.openide.util.lookup.InstanceContent; 40 import org.openide.util.lookup.ProxyLookup; 41 42 47 public final class RootNode extends FilterNode { 48 49 static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0]; 50 51 private Action [] actions; 52 53 54 56 public RootNode( Node originalRoot, Lookup lkp ) { 57 this( originalRoot, new InstanceContent(), lkp ); 58 } 59 60 private RootNode( Node originalRoot, InstanceContent content, Lookup lkp ) { 61 super( originalRoot, 62 new Children( originalRoot, lkp ), 63 new ProxyLookup( new Lookup[] { lkp, new AbstractLookup( content ), originalRoot.getLookup() } ) ); 64 DataFolder df = (DataFolder)getOriginal().getCookie( DataFolder.class ); 65 if( null != df ) { 66 content.add( new DataFolder.Index( df, this ) ); 67 } 68 content.add( this ); 69 setDisplayName(Utils.getBundleString("CTL_Component_palette")); } 71 72 74 public NewType[] getNewTypes() { 75 NewType[] res = super.getNewTypes(); 76 if( null == res || res.length == 0 ) 77 res = new NewType[] { new NewCategory() }; 78 return res; 79 } 80 81 public Action [] getActions(boolean context) { 82 if (actions == null) { 83 actions = new Action [] { 84 new Utils.NewCategoryAction( this ), 85 null, 86 new Utils.SortCategoriesAction( this ), 87 null, 88 new Utils.RefreshPaletteAction() 89 }; 90 } 91 PaletteActions customActions = (PaletteActions)getLookup().lookup( PaletteActions.class ); 92 if( null != customActions ) { 93 return Utils.mergeActions( actions, customActions.getCustomPaletteActions() ); 94 } 95 return actions; 96 } 97 98 public Node.PropertySet[] getPropertySets() { 99 return NO_PROPERTIES; 100 } 101 102 public PasteType getDropType(Transferable t, int action, int index) { 103 return null; 105 } 106 107 108 public void refreshChildren() { 109 ((Children)getChildren()).refreshNodes(); 110 } 111 112 114 void createNewCategory() throws java.io.IOException { 115 java.util.ResourceBundle bundle = Utils.getBundle(); 116 NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine( 117 bundle.getString("CTL_NewCategoryName"), bundle.getString("CTL_NewCategoryTitle")); input.setInputText(bundle.getString("CTL_NewCategoryValue")); 121 while (DialogDisplayer.getDefault().notify(input) 122 == NotifyDescriptor.OK_OPTION) 123 { 124 String categoryName = input.getInputText(); 125 if( CategoryNode.checkCategoryName( this, categoryName, null ) ) { 126 DataFolder paletteFolder = (DataFolder)getCookie( DataFolder.class ); 127 FileObject parentFolder = paletteFolder.getPrimaryFile(); 128 String folderName = CategoryNode.convertCategoryToFolderName( parentFolder, categoryName, null ); 129 FileObject folder = parentFolder.createFolder(folderName); 130 if (!folderName.equals(categoryName)) 131 folder.setAttribute( CategoryNode.CAT_NAME, categoryName ); 132 break; 133 } 134 } 135 } 136 137 public boolean canCut() { 138 return false; 139 } 140 141 public boolean canDestroy() { 142 return false; 143 } 144 145 public HelpCtx getHelpCtx() { 146 return Utils.getHelpCtx( this, super.getHelpCtx() ); 147 } 148 149 151 153 private static class Children extends FilterNode.Children { 154 155 private PaletteFilter filter; 156 private Lookup lkp; 157 158 public Children(Node original, Lookup lkp) { 159 super(original); 160 this.lkp = lkp; 161 filter = (PaletteFilter)lkp.lookup( PaletteFilter.class ); 162 } 163 164 protected Node copyNode(Node node) { 165 return new CategoryNode( node, lkp ); 166 } 167 168 protected Node[] createNodes(Node key) { 169 if( null == filter || filter.isValidCategory( key.getLookup() ) ) { 170 return new Node[] { copyNode(key) }; 171 } 172 173 return null; 174 } 175 176 public void refreshNodes() { 177 Node[] nodes = original.getChildren().getNodes(); 178 List <Node> empty = Collections.emptyList(); 179 setKeys( empty ); 180 setKeys( nodes ); 181 } 182 } 183 184 186 187 191 final class NewCategory extends NewType { 192 193 public String getName() { 194 return Utils.getBundleString("CTL_NewCategory"); } 196 197 public HelpCtx getHelpCtx() { 198 return new HelpCtx(NewCategory.class); 199 } 200 201 public void create() throws java.io.IOException { 202 RootNode.this.createNewCategory(); 203 } 204 } 205 } 206 | Popular Tags |