KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.actions;
21
22 import java.io.IOException JavaDoc;
23 import org.openide.awt.StatusDisplayer;
24 import org.openide.cookies.SaveCookie;
25 import org.openide.nodes.Node;
26 import org.openide.util.Exceptions;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.NbBundle;
29 import org.openide.util.actions.CookieAction;
30
31 /** Save a single object.
32 * @see SaveCookie
33 *
34 * @author Jan Jancura, Petr Hamernik, Ian Formanek, Dafe Simonek
35 */

36 public class SaveAction extends CookieAction {
37     private static Class JavaDoc dataObject;
38     private static java.lang.reflect.Method JavaDoc getNodeDelegate;
39
40     public SaveAction() {
41         putValue("noIconInMenu", Boolean.TRUE); //NOI18N
42
}
43
44     protected Class JavaDoc[] cookieClasses() {
45         return new Class JavaDoc[] { SaveCookie.class };
46     }
47
48     protected void performAction(final Node[] activatedNodes) {
49         SaveCookie sc = (SaveCookie) activatedNodes[0].getCookie(SaveCookie.class);
50         assert sc != null : "SaveCookie must be present on " + activatedNodes[0] + ". " +
51                 "See http://www.netbeans.org/issues/show_bug.cgi?id=68285 for details on overriding " + activatedNodes[0].getClass().getName() + ".getCookie correctly.";
52         
53         // avoid NPE if disabled assertions
54
if (sc == null) return ;
55
56         try {
57             sc.save();
58             StatusDisplayer.getDefault().setStatusText(
59                 NbBundle.getMessage(SaveAction.class, "MSG_saved", getSaveMessage(activatedNodes[0]))
60             );
61         } catch (IOException JavaDoc e) {
62             Exceptions.attachLocalizedMessage(e,
63                                               NbBundle.getMessage(SaveAction.class,
64                                                                   "EXC_notsaved",
65                                                                   getSaveMessage(activatedNodes[0])));
66             Exceptions.printStackTrace(e);
67         }
68     }
69
70     protected boolean asynchronous() {
71         return false;
72     }
73
74     private String JavaDoc getSaveMessage(Node n) {
75         if (dataObject == null) {
76             // read the class
77
ClassLoader JavaDoc l = (ClassLoader JavaDoc) org.openide.util.Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
78
79             if (l == null) {
80                 l = getClass().getClassLoader();
81             }
82
83             try {
84                 dataObject = Class.forName("org.openide.loaders.DataObject", true, l); // NOI18N
85
getNodeDelegate = dataObject.getMethod("getNodeDelegate", new Class JavaDoc[0]); // NOI18N
86
} catch (Exception JavaDoc ex) {
87                 Exceptions.printStackTrace(ex);
88             }
89         }
90
91         if (getNodeDelegate != null) {
92             // try to search for a name on the class
93
Object JavaDoc obj = n.getCookie(dataObject);
94
95             if (obj != null) {
96                 try {
97                     n = (Node) getNodeDelegate.invoke(obj, new Object JavaDoc[0]);
98                 } catch (Exception JavaDoc ex) {
99                     Exceptions.printStackTrace(ex);
100                 }
101             }
102         }
103
104         return n.getDisplayName();
105     }
106
107     protected int mode() {
108         return MODE_EXACTLY_ONE;
109     }
110
111     public String JavaDoc getName() {
112         return NbBundle.getMessage(SaveAction.class, "Save");
113     }
114
115     public HelpCtx getHelpCtx() {
116         return new HelpCtx(SaveAction.class);
117     }
118
119     protected String JavaDoc iconResource() {
120         return "org/openide/resources/actions/save.png"; // NOI18N
121
}
122 }
123
Popular Tags