1 19 20 package org.openide.actions; 21 22 23 import java.io.IOException ; 24 import org.openide.cookies.SaveCookie; 25 import org.openide.loaders.*; 26 import org.openide.nodes.*; 27 import org.openide.util.*; 28 import org.openide.util.actions.NodeAction; 29 30 35 public final class SaveAsTemplateAction extends NodeAction { 36 37 public HelpCtx getHelpCtx () { 38 return new HelpCtx (SaveAsTemplateAction.class); 39 } 40 41 public String getName () { 42 return NbBundle.getMessage(org.openide.loaders.DataObject.class, "SaveAsTemplate"); 43 } 44 45 46 @Deprecated 47 public String iconResource () { 48 return super.iconResource (); 49 } 50 51 protected boolean surviveFocusChange () { 52 return false; 53 } 54 55 protected boolean enable (Node[] activatedNodes) { 56 if (activatedNodes == null || activatedNodes.length == 0) 57 return false; 58 DataObject curCookie; 60 for (int i = 0; i < activatedNodes.length; i++) { 61 curCookie = (DataObject)activatedNodes[i].getCookie(DataObject.class); 62 if ((curCookie == null) || (!curCookie.isCopyAllowed())) 63 return false; 65 } 66 return true; 67 } 68 69 75 protected void performAction (Node[] activatedNodes) { 76 NodeAcceptor acceptor = FolderNodeAcceptor.getInstance(); 78 String title = NbBundle.getMessage(org.openide.loaders.DataObject.class, "Title_SaveAsTemplate"); 79 String rootTitle = NbBundle.getMessage(org.openide.loaders.DataObject.class, "CTL_SaveAsTemplate"); 80 Node templatesNode = NewTemplateAction.getTemplateRoot (); 81 templatesNode.setDisplayName(NbBundle.getMessage(org.openide.loaders.DataObject.class, "CTL_SaveAsTemplate_TemplatesRoot")); 82 Node[] selected; 83 try { 85 selected = NodeOperation.getDefault(). 86 select(title, rootTitle, templatesNode, acceptor, null); 87 } catch (UserCancelException ex) { 88 return; 90 } 91 DataFolder targetFolder = 95 (DataFolder)selected[0].getCookie(DataFolder.class); 96 for (int i = 0; i < activatedNodes.length; i++ ) { 97 createNewTemplate( 98 (DataObject)activatedNodes[i].getCookie(DataObject.class), 99 targetFolder); 100 } 101 } 102 103 protected boolean asynchronous() { 104 return false; 105 } 106 107 108 private void createNewTemplate(DataObject source, 109 DataFolder targetFolder) { 110 try { 111 SaveCookie cookie = (SaveCookie)source.getCookie (SaveCookie.class); 112 if (cookie != null) { 113 cookie.save (); 114 } 115 DataObject newTemplate = source.copy(targetFolder); 116 newTemplate.setTemplate(true); 117 } catch (IOException ex) { 118 Exceptions.printStackTrace(ex); 119 } 120 } 121 122 126 static final class FolderNodeAcceptor implements NodeAcceptor { 127 128 129 private static FolderNodeAcceptor instance; 130 131 132 private FolderNodeAcceptor() { 133 } 134 135 136 public final boolean acceptNodes(Node[] nodes) { 137 if (nodes == null || nodes.length != 1) return false; 138 return nodes[0].getCookie(DataFolder.class) != null; 139 } 140 141 142 static FolderNodeAcceptor getInstance() { 143 if (instance == null) instance = new FolderNodeAcceptor(); 144 return instance; 145 } 146 } 148 } 149 | Popular Tags |