KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > jsploader > EditServletAction


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.netbeans.modules.web.core.jsploader;
21
22 import org.openide.DialogDisplayer;
23 import org.openide.NotifyDescriptor;
24 import org.openide.cookies.EditorCookie;
25 import org.openide.filesystems.FileObject;
26 import org.openide.nodes.Node;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.NbBundle;
29 import org.openide.util.actions.CookieAction;
30
31 /**
32 * Edit an object.
33 * @see EditCookie
34 *
35 * @author Jaroslav Tulach
36 */

37 public class EditServletAction extends CookieAction {
38
39     /** serialVersionUID */
40     private static final long serialVersionUID = 183706095337315796L;
41
42     /** File types extensions which cannot be run separately so they do not have their 'own' servlet.*/
43     private static final String JavaDoc[] UNSUPPORTED_EXTENSIONS = new String JavaDoc[]{"jspf", "tagf"};
44     
45     /* Returns false - action should be disabled when a window with no
46     * activated nodes is selected.
47     *
48     * @return false do not survive the change of focus
49     */

50     protected boolean surviveFocusChange () {
51         return false;
52     }
53
54     /* Human presentable name of the action. This should be
55     * presented as an item in a menu.
56     * @return the name of the action
57     */

58     public String JavaDoc getName () {
59         return NbBundle.getBundle(EditServletAction.class).getString("EditServlet");
60     }
61
62     /* Help context where to find more about the action.
63     * @return the help context for this action
64     */

65     public HelpCtx getHelpCtx () {
66         return new HelpCtx (EditServletAction.class);
67     }
68     
69     /*
70      * We always enable View Servlet action, but show an error message
71      * in case when JSP has not been compiled yet.
72      */

73     protected boolean enable(Node[] activatedNodes) {
74         for (int i = 0; i < activatedNodes.length; i++) {
75             JspDataObject jspdo = (JspDataObject)activatedNodes[i].getCookie(JspDataObject.class);
76             if(jspdo != null) {
77                 FileObject jspfo = jspdo.getPrimaryFile();
78                 if(jspfo != null) {
79                     String JavaDoc fileExt = jspfo.getExt();
80                     if(fileExt != null && isUnsupportedExtension(fileExt)) {
81                         return false;
82                     }
83                 }
84             }
85         }
86         return true;
87     }
88
89     private boolean isUnsupportedExtension(String JavaDoc ext) {
90         for(String JavaDoc uext : UNSUPPORTED_EXTENSIONS) {
91             if(uext.equals(ext)) return true;
92         }
93         return false;
94     }
95     
96     /* @return the mode of action. */
97     protected int mode() {
98         return MODE_ANY;
99     }
100
101     /* Creates a set of classes that are tested by this cookie.
102     * Here only HtmlDataObject class is tested.
103     *
104     * @return list of classes the that this cookie tests
105     */

106     protected Class JavaDoc[] cookieClasses () {
107         return new Class JavaDoc[] { JspDataObject.class };
108     }
109
110     /* Actually performs the action.
111     * Calls edit on all activated nodes which supports
112     * HtmlDataObject cookie.
113     */

114     protected void performAction (final Node[] activatedNodes) {
115         for (int i = 0; i < activatedNodes.length; i++) {
116             JspDataObject jspdo = (JspDataObject)activatedNodes[i].getCookie(JspDataObject.class);
117             if (jspdo != null) {
118                 jspdo.refreshPlugin(true);
119                 EditorCookie cook = jspdo.getServletEditor();
120                 if (cook != null)
121                     cook.open ();
122                 else {
123                     //show error dialog
124
String JavaDoc msg = NbBundle.getMessage(EditServletAction.class, "ERR_CantEditServlet");
125                     String JavaDoc title = NbBundle.getMessage(EditServletAction.class, "EditServlet");
126                     NotifyDescriptor descriptor = new NotifyDescriptor(msg, title,
127                             NotifyDescriptor.DEFAULT_OPTION, NotifyDescriptor.ERROR_MESSAGE,
128                             new Object JavaDoc[]{NotifyDescriptor.OK_OPTION}, null);
129                     DialogDisplayer.getDefault().notify(descriptor);
130                 }
131             }
132         }
133     }
134     
135     protected boolean asynchronous() {
136         return false;
137     }
138     
139 }
140
Popular Tags