KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.io.IOException JavaDoc;
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 /** Saves a data object to a folder under in the
31 * system's templates area.
32 *
33 * @author Ales Novak, Dafe Simonek
34 */

35 public final class SaveAsTemplateAction extends NodeAction {
36
37     public HelpCtx getHelpCtx () {
38         return new HelpCtx (SaveAsTemplateAction.class);
39     }
40
41     public String JavaDoc getName () {
42         return NbBundle.getMessage(org.openide.loaders.DataObject.class, "SaveAsTemplate");
43     }
44
45     /** @deprecated Should never be called publically. */
46     @Deprecated JavaDoc
47     public String JavaDoc 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         // test if all nodes support saving as template
59
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                 // not supported
64
return false;
65         }
66         return true;
67     }
68
69     /* Performs the action - launches new file dialog,
70     * saves as a template ...
71     * Overrides abstract enable(..) from superclass.
72     *
73     * @param activatedNodes Array of activated nodes
74     */

75     protected void performAction (Node[] activatedNodes) {
76         // prepare variables
77
NodeAcceptor acceptor = FolderNodeAcceptor.getInstance();
78         String JavaDoc title = NbBundle.getMessage(org.openide.loaders.DataObject.class, "Title_SaveAsTemplate");
79         String JavaDoc 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         // ask user: where to save the templates?
84
try {
85             selected = NodeOperation.getDefault().
86                        select(title, rootTitle, templatesNode, acceptor, null);
87         } catch (UserCancelException ex) {
88             // user cancelled the operation
89
return;
90         }
91         // create & save them all
92
// we know DataFolder and DataObject cookies must be supported
93
// so we needn't check for null values
94
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     /** Performs the work of creating a new template */
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 JavaDoc ex) {
118             Exceptions.printStackTrace(ex);
119         }
120     }
121
122     /** Inner class functioning like node acceptor for
123     * user dialogs when selecting where to save as template.
124     * Accepts folders only. Singleton.
125     */

126     static final class FolderNodeAcceptor implements NodeAcceptor {
127
128         /** an instance */
129         private static FolderNodeAcceptor instance;
130
131         /** singleton */
132         private FolderNodeAcceptor() {
133         }
134
135         /** accepts a selected folder */
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         /** getter for an instance */
142         static FolderNodeAcceptor getInstance() {
143             if (instance == null) instance = new FolderNodeAcceptor();
144             return instance;
145         }
146     } // end of FolderNodeAcceptor inner class
147

148 }
149
Popular Tags