KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
41  * Lets user pick a localized display name for a given layer file.
42  * @author Jesse Glick
43  */

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 JavaDoc location = (URL JavaDoc) f.getAttribute("WritableXMLFileSystem.location"); // NOI18N
52
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 JavaDoc findBundlePath(NbModuleProject p) {
61         FileObject src = p.getSourceDirectory();
62         ManifestManager mm = ManifestManager.getInstance(p.getManifest(), false);
63         String JavaDoc 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 JavaDoc name = d.getInputText();
79         FileObject f = findFile(activatedNodes);
80         NbModuleProject p = findProject(f);
81         String JavaDoc 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('/', '.')); // NOI18N
88
} catch (IOException JavaDoc 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 JavaDoc getName() {
109         return NbBundle.getMessage(PickIconAction.class, "LBL_pick_name");
110     }
111     
112     protected Class JavaDoc[] cookieClasses() {
113         return new Class JavaDoc[] {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