KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > RenameAction


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 package org.openide.actions;
20
21 import org.openide.DialogDisplayer;
22 import org.openide.NotifyDescriptor;
23 import org.openide.nodes.Node;
24 import org.openide.util.Exceptions;
25 import org.openide.util.HelpCtx;
26 import org.openide.util.NbBundle;
27 import org.openide.util.actions.*;
28
29
30 /** Rename a node.
31 * @see Node#setName
32 *
33 * @author Petr Hamernik, Dafe Simonek
34 */

35 public class RenameAction extends NodeAction {
36     protected boolean surviveFocusChange() {
37         return false;
38     }
39
40     public String JavaDoc getName() {
41         return NbBundle.getMessage(RenameAction.class, "Rename");
42     }
43
44     public HelpCtx getHelpCtx() {
45         return new HelpCtx(RenameAction.class);
46     }
47
48     protected boolean enable(Node[] activatedNodes) {
49         // exactly one node should be selected
50
if ((activatedNodes == null) || (activatedNodes.length != 1)) {
51             return false;
52         }
53
54         // and must support renaming
55
return activatedNodes[0].canRename();
56     }
57
58     protected void performAction(Node[] activatedNodes) {
59         Node n = activatedNodes[0]; // we supposed that one node is activated
60

61         NotifyDescriptor.InputLine dlg = new NotifyDescriptor.InputLine(
62                 NbBundle.getMessage(RenameAction.class, "CTL_RenameLabel"),
63                 NbBundle.getMessage(RenameAction.class, "CTL_RenameTitle")
64             );
65         dlg.setInputText(n.getName());
66
67         if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(dlg))) {
68             String JavaDoc newname = null;
69
70             try {
71                 newname = dlg.getInputText();
72
73                 if (!newname.equals("")) {
74                     n.setName(dlg.getInputText()); // NOI18N
75
}
76             } catch (IllegalArgumentException JavaDoc e) {
77                 // determine if "printStackTrace" and "new annotation" of this exception is needed
78
boolean needToAnnotate = Exceptions.findLocalizedMessage(e) == null;
79
80                 // annotate new localized message only if there is no localized message yet
81
if (needToAnnotate) {
82                     Exceptions.attachLocalizedMessage(e,
83                                                       NbBundle.getMessage(RenameAction.class,
84                                                                           "MSG_BadFormat",
85                                                                           n.getName(),
86                                                                           newname));
87                 }
88
89                 Exceptions.printStackTrace(e);
90             }
91         }
92     }
93
94     protected boolean asynchronous() {
95         return false;
96     }
97 }
98
Popular Tags