1 19 20 package org.openide.actions; 21 22 import java.io.IOException ; 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 36 public class SaveAction extends CookieAction { 37 private static Class dataObject; 38 private static java.lang.reflect.Method getNodeDelegate; 39 40 public SaveAction() { 41 putValue("noIconInMenu", Boolean.TRUE); } 43 44 protected Class [] cookieClasses() { 45 return new Class [] { 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 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 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 getSaveMessage(Node n) { 75 if (dataObject == null) { 76 ClassLoader l = (ClassLoader ) org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 78 79 if (l == null) { 80 l = getClass().getClassLoader(); 81 } 82 83 try { 84 dataObject = Class.forName("org.openide.loaders.DataObject", true, l); getNodeDelegate = dataObject.getMethod("getNodeDelegate", new Class [0]); } catch (Exception ex) { 87 Exceptions.printStackTrace(ex); 88 } 89 } 90 91 if (getNodeDelegate != null) { 92 Object obj = n.getCookie(dataObject); 94 95 if (obj != null) { 96 try { 97 n = (Node) getNodeDelegate.invoke(obj, new Object [0]); 98 } catch (Exception 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 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 iconResource() { 120 return "org/openide/resources/actions/save.png"; } 122 } 123 | Popular Tags |