1 19 20 package org.netbeans.modules.palette; 21 import java.awt.datatransfer.Transferable ; 22 import java.text.MessageFormat ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import javax.swing.Action ; 26 import org.netbeans.spi.palette.PaletteActions; 27 import org.netbeans.spi.palette.PaletteController; 28 import org.netbeans.spi.palette.PaletteFilter; 29 import org.openide.DialogDisplayer; 30 import org.openide.NotifyDescriptor; 31 32 import org.openide.filesystems.FileObject; 33 import org.openide.loaders.DataFolder; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.DataShadow; 36 import org.openide.nodes.FilterNode; 37 import org.openide.nodes.Node; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.Utilities; 40 import org.openide.util.Lookup; 41 import org.openide.util.LookupEvent; 42 import org.openide.util.datatransfer.PasteType; 43 import org.openide.util.lookup.AbstractLookup; 44 import org.openide.util.lookup.InstanceContent; 45 import org.openide.util.lookup.ProxyLookup; 46 47 52 class CategoryNode extends FilterNode { 53 54 static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0]; 55 56 static final String CAT_NAME = "categoryName"; 58 private Action [] actions; 59 60 CategoryNode( Node originalNode, Lookup lkp ) { 61 this( originalNode, new InstanceContent(), lkp ); 62 } 63 64 private CategoryNode( Node originalNode, InstanceContent content, Lookup lkp ) { 65 super( originalNode, 66 new Children( originalNode, lkp ), 67 new ProxyLookup( new Lookup[] { lkp, new AbstractLookup(content), originalNode.getLookup() } ) ); 68 69 DataFolder folder = (DataFolder)originalNode.getCookie( DataFolder.class ); 70 if( null != folder ) { 71 content.add( new DataFolder.Index( folder, this ) ); 72 FileObject fob = folder.getPrimaryFile(); 73 Object catName = fob.getAttribute( CAT_NAME ); 74 if (catName instanceof String ) 75 setDisplayName((String )catName); 76 } 77 content.add( this ); 78 } 79 80 82 public String getDisplayName() { 83 84 String retValue = null; 85 DataFolder folder = (DataFolder)getCookie( DataFolder.class ); 86 if( null != folder ) { 87 FileObject fob = folder.getPrimaryFile(); 88 Object catName = fob.getAttribute( CAT_NAME ); 89 if (catName instanceof String ) 90 retValue = catName.toString(); 91 } 92 if( null == retValue ) { 93 retValue = super.getDisplayName(); 94 } 95 if( null != retValue && retValue.indexOf("\u2192") > 0 ) { 97 DataShadow shadow = (DataShadow)getCookie( DataShadow.class ); 98 if( null != shadow ) { 99 DataObject dobj = shadow.getOriginal(); 100 if( null != dobj ) { 101 Node origNode = dobj.getNodeDelegate(); 102 if( null != origNode && null != origNode.getDisplayName() ) { 103 retValue = origNode.getDisplayName(); 104 } 105 } 106 } 107 } 108 return retValue; 109 } 110 111 public void setDisplayName( String displayName ) { 112 try { 113 DataFolder folder = (DataFolder)getCookie( DataFolder.class ); 114 if( null != folder ) { 115 FileObject fo = folder.getPrimaryFile(); 116 fo.setAttribute( CAT_NAME, displayName ); 117 } 118 } catch (java.io.IOException ex) { 119 RuntimeException e = new IllegalArgumentException (); 120 org.openide.ErrorManager.getDefault().annotate(e, ex); 121 throw e; 122 } 123 super.setDisplayName( displayName ); 124 } 125 126 public String getShortDescription() { 127 return getDisplayName(); 128 } 129 130 public Action [] getActions(boolean context) { 131 if (actions == null) { 132 Node n = getParentNode(); 133 actions = new Action [] { 134 new Utils.PasteItemAction( this ), 135 null, 136 new Utils.NewCategoryAction( n ), 137 null, 138 new Utils.DeleteCategoryAction(this), 139 new Utils.RenameCategoryAction(this), 140 null, 141 new Utils.SortItemsAction(this), 142 null, 143 new Utils.SortCategoriesAction( n ), 144 null, 145 new Utils.RefreshPaletteAction() 146 }; 147 } 148 PaletteActions customActions = (PaletteActions)getParentNode().getLookup().lookup( PaletteActions.class ); 149 if( null != customActions ) { 150 return Utils.mergeActions( actions, customActions.getCustomCategoryActions( getLookup() ) ); 151 } 152 return actions; 153 } 154 155 public Node.PropertySet[] getPropertySets() { 156 return NO_PROPERTIES; 157 } 158 159 public boolean canDestroy() { 160 return !Utils.isReadonly( getOriginal() ); 161 } 162 163 public HelpCtx getHelpCtx() { 164 return Utils.getHelpCtx( this, super.getHelpCtx() ); 165 } 166 167 private static class Children extends FilterNode.Children { 168 169 private Lookup lkp; 170 private PaletteFilter filter; 171 172 public Children(Node original, Lookup lkp) { 173 super(original); 174 this.lkp = lkp; 175 this.filter = (PaletteFilter)lkp.lookup( PaletteFilter.class ); 176 } 177 178 protected Node copyNode(Node node) { 179 return new ItemNode( node ); 180 } 181 182 protected Node[] createNodes(Node key) { 183 if( null == filter || filter.isValidItem( key.getLookup() ) ) { 184 return new Node[] { copyNode(key) }; 185 } 186 187 return null; 188 } 189 190 public void resultChanged(LookupEvent ev) { 191 Node[] nodes = original.getChildren().getNodes(); 192 List <Node> empty = Collections.emptyList(); 193 setKeys( empty ); 194 setKeys( nodes ); 195 } 196 } 197 198 204 static boolean checkCategoryName( Node parentNode, String name, Node namedNode) { 205 boolean invalid = false; 206 if (name == null || "".equals(name)) invalid = true; 208 else for (int i=0, n=name.length(); i < n; i++) { 210 char ch = name.charAt(i); 211 if (ch == '.' || (ch == ' ' && i+1 == n)) { 212 invalid = true; 213 break; 214 } 215 else if (ch != ' ') 216 break; 217 } 218 219 if (invalid) { 220 DialogDisplayer.getDefault().notify( 221 new NotifyDescriptor.Message(MessageFormat.format( 222 Utils.getBundleString("ERR_InvalidName"), new Object [] { name }), 224 NotifyDescriptor.INFORMATION_MESSAGE)); 225 return false; 226 } 227 228 Node[] nodes = parentNode.getChildren().getNodes(); 229 for (int i=0; i < nodes.length; i++) 230 if (name.equals(nodes[i].getName()) && nodes[i] != namedNode) { 231 DialogDisplayer.getDefault().notify( 232 new NotifyDescriptor.Message(MessageFormat.format( 233 Utils.getBundleString("FMT_CategoryExists"), new Object [] { name }), 235 NotifyDescriptor.INFORMATION_MESSAGE)); 236 return false; 237 } 238 239 return true; 240 } 241 242 245 static String convertCategoryToFolderName( FileObject paletteFO, 246 String name, 247 String currentName) 248 { 249 if (name == null || "".equals(name)) return null; 251 252 int i; 253 int n = name.length(); 254 StringBuffer nameBuff = new StringBuffer (n); 255 256 char ch = name.charAt(0); 257 if (Character.isJavaIdentifierStart(ch)) { 258 nameBuff.append(ch); 259 i = 1; 260 } 261 else { 262 nameBuff.append('_'); 263 i = 0; 264 } 265 266 while (i < n) { 267 ch = name.charAt(i); 268 if (Character.isJavaIdentifierPart(ch)) 269 nameBuff.append(ch); 270 i++; 271 } 272 273 String fName = nameBuff.toString(); 274 if ("_".equals(fName)) fName = "Category"; if (fName.equals(currentName)) 277 return fName; 278 279 String freeName = null; 281 boolean nameOK = false; 282 283 for (i=0; !nameOK; i++) { 284 freeName = i > 0 ? fName + "_" + i : fName; 286 if (Utilities.isWindows()) { 287 nameOK = true; 288 java.util.Enumeration en = paletteFO.getChildren(false); 289 while (en.hasMoreElements()) { 290 FileObject fo = (FileObject)en.nextElement(); 291 String fn = fo.getName(); 292 String fe = fo.getExt(); 293 294 if ((fe == null || "".equals(fe)) && fn.equalsIgnoreCase(freeName)) { nameOK = false; 297 break; 298 } 299 } 300 } 301 else nameOK = paletteFO.getFileObject(freeName) == null; 302 } 303 return freeName; 304 } 305 306 public PasteType getDropType(Transferable t, int action, int index) { 307 if( t.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) ) 308 return super.getDropType(t, action, index); 309 return null; 310 } 311 } 312 | Popular Tags |