1 19 20 package org.netbeans.modules.apisupport.project.layers; 21 22 import java.io.IOException ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import org.netbeans.api.project.FileOwnerQuery; 26 import org.netbeans.modules.apisupport.project.ManifestManager; 27 import org.netbeans.modules.apisupport.project.NbModuleProject; 28 import org.netbeans.modules.apisupport.project.Util; 29 import org.netbeans.spi.project.support.ant.EditableProperties; 30 import org.openide.DialogDisplayer; 31 import org.openide.ErrorManager; 32 import org.openide.NotifyDescriptor; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.nodes.Node; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.NbBundle; 38 import org.openide.util.actions.CookieAction; 39 40 44 public class PickNameAction extends CookieAction { 45 46 private static FileObject findFile(Node[] activatedNodes) { 47 return ((DataObject) activatedNodes[0].getCookie(DataObject.class)).getPrimaryFile(); 48 } 49 50 private static NbModuleProject findProject(FileObject f) { 51 URL location = (URL ) f.getAttribute("WritableXMLFileSystem.location"); if (location == null) { 53 return null; 54 } 55 NbModuleProject p = (NbModuleProject) FileOwnerQuery.getOwner(URI.create(location.toExternalForm())); 56 assert p != null : location; 57 return p; 58 } 59 60 private static String findBundlePath(NbModuleProject p) { 61 FileObject src = p.getSourceDirectory(); 62 ManifestManager mm = ManifestManager.getInstance(p.getManifest(), false); 63 String bundlePath = mm.getLocalizingBundle(); 64 if (bundlePath != null && bundlePath.endsWith(".properties") && src.getFileObject(bundlePath) != null) { 65 return bundlePath; 66 } else { 67 return null; 68 } 69 } 70 71 protected void performAction(Node[] activatedNodes) { 72 NotifyDescriptor.InputLine d = new NotifyDescriptor.InputLine( 73 NbBundle.getMessage(PickNameAction.class, "PickNameAction_dialog_label"), 74 NbBundle.getMessage(PickNameAction.class, "PickNameAction_dialog_title")); 75 if (DialogDisplayer.getDefault().notify(d) != NotifyDescriptor.OK_OPTION) { 76 return; 77 } 78 String name = d.getInputText(); 79 FileObject f = findFile(activatedNodes); 80 NbModuleProject p = findProject(f); 81 String bundlePath = findBundlePath(p); 82 try { 83 FileObject properties = p.getSourceDirectory().getFileObject(bundlePath); 84 EditableProperties ep = Util.loadProperties(properties); 85 ep.setProperty(f.getPath(), name); 86 Util.storeProperties(properties, ep); 87 f.setAttribute("SystemFileSystem.localizingBundle", bundlePath.substring(0, bundlePath.length() - ".properties".length()).replace('/', '.')); } catch (IOException e) { 89 Util.err.notify(ErrorManager.INFORMATIONAL, e); 90 } 91 } 92 93 protected boolean enable(Node[] activatedNodes) { 94 if (!super.enable(activatedNodes)) { 95 return false; 96 } 97 FileObject f = findFile(activatedNodes); 98 if (f == null) { 99 return false; 100 } 101 NbModuleProject p = findProject(f); 102 if (p == null) { 103 return false; 104 } 105 return findBundlePath(p) != null; 106 } 107 108 public String getName() { 109 return NbBundle.getMessage(PickIconAction.class, "LBL_pick_name"); 110 } 111 112 protected Class [] cookieClasses() { 113 return new Class [] {DataObject.class}; 114 } 115 116 protected int mode() { 117 return MODE_EXACTLY_ONE; 118 } 119 120 public HelpCtx getHelpCtx() { 121 return null; 122 } 123 124 protected boolean asynchronous() { 125 return false; 126 } 127 128 } 129 | Popular Tags |