1 19 20 package org.netbeans.core.windows.services; 21 22 23 import java.awt.datatransfer.Transferable ; 24 import java.util.*; 25 import javax.swing.JSeparator ; 26 import org.netbeans.core.NbPlaces; 27 import org.openide.NotifyDescriptor; 28 import org.openide.actions.*; 29 import org.openide.cookies.InstanceCookie; 30 import org.openide.filesystems.FileObject; 31 import org.openide.loaders.*; 32 import org.openide.nodes.*; 33 import org.openide.nodes.FilterNode.Children; 34 import org.openide.util.*; 35 import org.openide.util.actions.SystemAction; 36 import org.openide.util.datatransfer.*; 37 38 44 public final class MenuFolderNode extends DataFolder.FolderNode { 45 46 47 static SystemAction[] staticActions; 48 49 static SystemAction[] topStaticActions; 50 51 private static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0]; 52 53 private DataFolder folder; 54 55 public MenuFolderNode () { 56 this (NbPlaces.getDefault().menus ()); 57 } 58 59 61 MenuFolderNode (DataFolder folder) { 62 folder.super(new MenuFolderChildren(folder)); 63 this.folder = folder; 64 setShortDescription(NbBundle.getMessage(MenuFolderNode.class, "CTL_Menu_hint")); 66 67 setIconBaseWithExtension ("org/netbeans/core/resources/menu.gif"); } 69 70 public HelpCtx getHelpCtx () { 71 return new HelpCtx (MenuFolderNode.class); 72 } 73 74 protected void createPasteTypes(Transferable t, List<PasteType> s) { 75 PasteType pType = ActionPasteType.getPasteType((DataFolder)getDataObject() , t); 76 if (pType != null) { 77 s.add(pType); 79 } 80 } 81 82 85 public NewType[] getNewTypes () { 86 NewType newMenu = new NewType() { 87 public String getName () { 88 return NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuName"); 89 } 90 public void create () throws java.io.IOException { 91 newMenu(); 92 } 93 }; 94 95 if(getParentNode() instanceof MenuFolderNode) { 96 return new NewType[] { 97 newMenu, 98 new NewType () { 100 public String getName () { 101 return NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuSeparator"); 102 } 103 public void create () throws java.io.IOException { 104 newSeparator(); 105 } 106 } 107 }; 108 } else { 109 return new NewType[] {newMenu}; 111 } 112 } 113 114 void newMenu () { 115 NotifyDescriptor.InputLine il = new NotifyDescriptor.InputLine 116 (NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuLabel"), 117 NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuDialog")); 118 il.setInputText (NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenu")); 119 120 Object ok = org.openide.DialogDisplayer.getDefault ().notify (il); 121 if (ok == NotifyDescriptor.OK_OPTION) { 122 String s = il.getInputText(); 123 if (!s.equals ("")) { FileObject mnFO = folder.getPrimaryFile(); 125 try { 126 FileObject newFO = mnFO.getFileObject(s); 127 if (newFO == null) { 128 String lastName = getLastName(); 129 130 newFO = mnFO.createFolder (s); 131 132 if(lastName != null) { 134 mnFO.setAttribute( 135 lastName + "/" + newFO.getNameExt(), Boolean.TRUE 137 ); 138 } 139 } 140 } catch (java.io.IOException e) { 141 Exceptions.printStackTrace(e); 142 } 143 } 144 } 145 } 146 147 void newSeparator () { 149 try { 150 InstanceDataObject instData = InstanceDataObject.find 151 (folder,null,"javax.swing.JSeparator"); 153 String lastName = getLastName(); 154 DataObject d; 155 156 if (instData == null) { 157 d = InstanceDataObject.create 158 (folder,null,"javax.swing.JSeparator"); } else { 160 d = instData.copy(folder); 161 } 162 163 if(lastName != null) { 165 folder.getPrimaryFile().setAttribute( 166 lastName + "/" + d.getPrimaryFile().getNameExt(), Boolean.TRUE 168 ); 169 } 170 } catch (java.io.IOException e) { 171 Exceptions.printStackTrace(e); 172 } 173 } 174 176 178 private String getLastName() { 179 String lastName = null; 180 Node[] ch = getChildren().getNodes(); 181 if(ch.length > 0) { 182 Node last = ch[ch.length - 1]; 183 DataObject d = (DataObject)last.getCookie(DataObject.class); 184 if(d != null) { 185 lastName = d.getPrimaryFile().getNameExt(); 186 } 187 } 188 189 return lastName; 190 } 191 192 195 protected SystemAction[] createActions () { 196 if (isTopLevel()) { 197 if (topStaticActions == null) 198 topStaticActions = new SystemAction [] { 199 SystemAction.get (FileSystemAction.class), 200 null, 201 SystemAction.get(ReorderAction.class), 202 null, 203 SystemAction.get(PasteAction.class), 204 null, 205 SystemAction.get(NewAction.class), 206 null, 207 SystemAction.get(ToolsAction.class), 208 SystemAction.get(PropertiesAction.class), 209 }; 210 return topStaticActions; 211 } else { 212 if (staticActions == null) 213 staticActions = new SystemAction [] { 214 SystemAction.get (FileSystemAction.class), 215 null, 216 SystemAction.get(MoveUpAction.class), 217 SystemAction.get(MoveDownAction.class), 218 SystemAction.get(ReorderAction.class), 219 null, 220 SystemAction.get(CutAction.class), 221 SystemAction.get(CopyAction.class), 222 SystemAction.get(PasteAction.class), 223 null, 224 SystemAction.get(DeleteAction.class), 225 SystemAction.get(RenameAction.class), 226 null, 227 SystemAction.get(NewAction.class), 228 null, 229 SystemAction.get(ToolsAction.class), 230 SystemAction.get(PropertiesAction.class), 231 }; 232 return staticActions; 233 } 234 } 235 236 237 public Node.PropertySet[] getPropertySets () { 238 if (isTopLevel()) { 239 return NO_PROPERTIES; 240 } else { 241 Sheet sheet = Sheet.createDefault(); 243 sheet.get(Sheet.PROPERTIES).put( 244 new PropertySupport.Name( 245 this, 246 NbBundle.getMessage(MenuFolderNode.class, "PROP_MenuName"), 247 NbBundle.getMessage(MenuFolderNode.class, "HINT_MenuName") 248 ) 249 ); 250 return sheet.toArray(); 251 } 252 } 253 254 271 272 273 boolean isTopLevel () { 274 final Node n = getParentNode(); 275 return (n == null) || !(n instanceof MenuFolderNode); 276 } 277 278 public boolean canDestroy () { 279 if (isTopLevel ()) return false; 280 return super.canDestroy (); 281 } 282 283 public boolean canCut () { 284 if (isTopLevel ()) return false; 285 return super.canCut (); 286 } 287 288 public boolean canRename () { 289 if (isTopLevel ()) return false; 290 return super.canRename (); 291 } 292 293 296 static final class MenuFolderChildren extends FilterNode.Children { 297 298 299 public MenuFolderChildren (DataFolder folder) { 300 super(folder.getNodeDelegate ()); 301 } 302 303 308 protected Node copyNode (Node node) { 309 DataFolder df = (DataFolder)node.getCookie(DataFolder.class); 310 if (df != null) { 311 return new MenuFolderNode(df); 312 } 313 return new MenuItemNode(node); 314 } 315 316 } 317 318 static final class MenuItemNode extends FilterNode { 319 320 321 static SystemAction[] staticActions; 322 323 static SystemAction[] separatorStaticActions; 324 325 326 MenuItemNode (Node filter) { 327 super(filter, Children.LEAF); 328 } 329 330 331 public boolean equals (Object o) { 332 if (o == null) return false; 333 return this == o || getOriginal ().equals (o) || o.equals (getOriginal ()); 334 } 335 336 339 public SystemAction[] getActions () { 340 InstanceCookie.Of ic = (InstanceCookie.Of)getCookie(InstanceCookie.Of.class); 341 if (ic != null && ic.instanceOf(JSeparator .class)) { 342 if( null == separatorStaticActions ) { 344 separatorStaticActions = new SystemAction [] { 345 SystemAction.get(MoveUpAction.class), 346 SystemAction.get(MoveDownAction.class), 347 null, 348 SystemAction.get(DeleteAction.class), 349 null, 350 SystemAction.get(ToolsAction.class), 351 SystemAction.get(PropertiesAction.class), 352 }; 353 } 354 return separatorStaticActions; 355 } 356 357 if (staticActions == null) { 358 staticActions = new SystemAction [] { 359 SystemAction.get(MoveUpAction.class), 360 SystemAction.get(MoveDownAction.class), 361 null, 362 SystemAction.get(CutAction.class), 363 SystemAction.get(CopyAction.class), 364 null, 365 SystemAction.get(DeleteAction.class), 366 null, 367 SystemAction.get(ToolsAction.class), 368 SystemAction.get(PropertiesAction.class), 369 }; 370 } 371 return staticActions; 372 } 373 374 376 public boolean canRename () { 377 return false; 378 } 379 380 381 public Node.PropertySet[] getPropertySets () { 382 394 return new Node.PropertySet[] { }; 395 } 396 397 } 399 } 400 | Popular Tags |