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 javax.swing.JFileChooser ; 26 import org.netbeans.api.project.FileOwnerQuery; 27 import org.netbeans.modules.apisupport.project.ManifestManager; 28 import org.netbeans.modules.apisupport.project.NbModuleProject; 29 import org.netbeans.modules.apisupport.project.Util; 30 import org.netbeans.modules.apisupport.project.ui.UIUtil; 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 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 import org.openide.windows.WindowManager; 40 41 45 public class PickIconAction extends CookieAction { 46 47 protected void performAction(Node[] activatedNodes) { 48 FileObject f = ((DataObject) activatedNodes[0].getCookie(DataObject.class)).getPrimaryFile(); 49 URL location = (URL ) f.getAttribute("WritableXMLFileSystem.location"); assert location != null : f; 51 NbModuleProject p = (NbModuleProject) FileOwnerQuery.getOwner(URI.create(location.toExternalForm())); 52 assert p != null : location; 53 FileObject src = p.getSourceDirectory(); 54 JFileChooser chooser = UIUtil.getIconFileChooser(); 55 FileUtil.preventFileChooserSymlinkTraversal(chooser, FileUtil.toFile(src)); 56 if (chooser.showOpenDialog(WindowManager.getDefault().getMainWindow()) != JFileChooser.APPROVE_OPTION) { 57 return; 58 } 59 FileObject icon = FileUtil.toFileObject(chooser.getSelectedFile()); 60 String iconPath = FileUtil.getRelativePath(src, icon); 62 try { 63 if (iconPath == null) { 64 String folderPath; 65 String layerPath = ManifestManager.getInstance(p.getManifest(), false).getLayer(); 66 if (layerPath != null) { 67 folderPath = layerPath.substring(0, layerPath.lastIndexOf('/')); 68 } else { 69 folderPath = p.getCodeNameBase().replace('.', '/') + "/resources"; } 71 FileObject folder = FileUtil.createFolder(src, folderPath); 72 FileUtil.copyFile(icon, folder, icon.getName(), icon.getExt()); 73 iconPath = folderPath + '/' + icon.getNameExt(); 74 } 75 f.setAttribute("SystemFileSystem.icon", new URL ("nbresloc:/" + iconPath)); } catch (IOException e) { 77 Util.err.notify(ErrorManager.INFORMATIONAL, e); 78 } 79 } 80 81 protected boolean enable(Node[] activatedNodes) { 82 if (!super.enable(activatedNodes)) { 83 return false; 84 } 85 FileObject f = ((DataObject) activatedNodes[0].getCookie(DataObject.class)).getPrimaryFile(); 86 URL location = (URL ) f.getAttribute("WritableXMLFileSystem.location"); return location != null; } 89 90 public String getName() { 91 return NbBundle.getMessage(PickIconAction.class, "LBL_pick_icon"); 92 } 93 94 protected Class [] cookieClasses() { 95 return new Class [] {DataObject.class}; 96 } 97 98 protected int mode() { 99 return MODE_EXACTLY_ONE; 100 } 101 102 public HelpCtx getHelpCtx() { 103 return null; 104 } 105 106 protected boolean asynchronous() { 107 return false; 108 } 109 110 } 111 | Popular Tags |