KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > layers > PickIconAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.layers;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import javax.swing.JFileChooser JavaDoc;
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 /**
42  * Lets user pick an icon for a given layer file.
43  * @author Jesse Glick
44  */

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 JavaDoc location = (URL JavaDoc) f.getAttribute("WritableXMLFileSystem.location"); // NOI18N
50
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 JavaDoc 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         // XXX might instead get WritableXMLFileSystem.cp and search for it in there:
61
String JavaDoc iconPath = FileUtil.getRelativePath(src, icon);
62         try {
63             if (iconPath == null) {
64                 String JavaDoc folderPath;
65                 String JavaDoc 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"; // NOI18N
70
}
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 JavaDoc("nbresloc:/" + iconPath)); // NOI18N
76
} catch (IOException JavaDoc 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 JavaDoc location = (URL JavaDoc) f.getAttribute("WritableXMLFileSystem.location"); // NOI18N
87
return location != null; // #63458
88
}
89
90     public String JavaDoc getName() {
91         return NbBundle.getMessage(PickIconAction.class, "LBL_pick_icon");
92     }
93     
94     protected Class JavaDoc[] cookieClasses() {
95         return new Class JavaDoc[] {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