KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > comments > CorrectJavaDocAction


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.javadoc.comments;
21
22 import javax.swing.SwingUtilities JavaDoc;
23 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
24 import org.openide.ErrorManager;
25 import org.openide.nodes.Node;
26 import org.openide.util.NbBundle;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.RequestProcessor;
29 import org.openide.util.actions.NodeAction;
30 import org.netbeans.jmi.javamodel.*;
31
32 import javax.jmi.reflect.JmiException;
33 import org.netbeans.modules.javacore.api.JavaModel;
34
35 /**
36  * Corrects selected java source (or descendant) Javadoc.
37  *
38  * @author Mauro Botelho
39  */

40 public final class CorrectJavaDocAction extends NodeAction {
41         
42     static final long serialVersionUID =4989490116568783623L;
43
44     public CorrectJavaDocAction () {
45         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
46
}
47     
48     /** Human presentable name of the action. This should be
49      * presented as an item in a menu.
50      * @return the name of the action
51      */

52     public String JavaDoc getName () {
53         return NbBundle.getBundle( CorrectJavaDocAction.class ).getString("CTL_CORRECTJAVADOC_MenuItem"); //NOI18N
54
}
55     
56     /** Help context where to find more about the action.
57      * @return the help context for this action
58      */

59     public HelpCtx getHelpCtx () {
60         return new HelpCtx (CorrectJavaDocAction.class);
61     }
62     
63     /** Enable this action only if it is really possible to correct the javadoc
64      * for this element.
65      */

66     protected boolean enable( Node[] activatedNodes ) {
67         if (activatedNodes.length != 1) {
68             return false;
69         }
70         ClassMember element = (ClassMember) activatedNodes[0].getLookup().lookup(ClassMember.class);
71         if (element == null)
72             return false;
73         
74         try {
75             JavaModel.getJavaRepository().beginTrans(false);
76             try {
77                 if (!element.isValid()) {
78                     return false;
79                 }
80                 AutoCommenter.Element jdElement = AutoCommenter.createAutoCommenterElement(element);
81                 if( jdElement == null ) //it is for example static initializer
82
return false;
83         
84                 return jdElement.isCorrectable();
85             } finally {
86                 JavaModel.getJavaRepository().endTrans();
87             }
88         } catch (JmiException e) {
89             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
90         }
91         return false;
92     }
93
94     /** This method is called by one of the "invokers" as a result of
95      * some user's action that should lead to actual "performing" of the action.
96      * This default implementation calls the assigned actionPerformer if it
97      * is not null otherwise the action is ignored.
98      */

99     public void performAction (final Node[] nodes ) {
100         Runnable JavaDoc run = new Runnable JavaDoc() {
101             public void run() {
102                 if (SwingUtilities.isEventDispatchThread()) {
103                     RequestProcessor.getDefault().post(this);
104                     return ;
105                 }
106                 ClassMember element = (ClassMember) nodes[0].getLookup().lookup(ClassMember.class);
107                 if(element == null)
108                     return;
109                 
110                 try {
111                     JavaModel.getJavaRepository().beginTrans(true);
112                     boolean fail = true;
113                     try {
114                         if (!element.isValid()) {
115                             fail = false;
116                             return;
117                         }
118                         AutoCommenter.Element jdElement = AutoCommenter.createAutoCommenterElement(element);
119                         if (jdElement.isCorrectable()) {
120                             jdElement.autoCorrect();
121                         }
122                         fail = false;
123                     } finally {
124                         JavaModel.getJavaRepository().endTrans(fail);
125                     }
126                 } catch (JmiException e) {
127                     ErrorManager.getDefault().notify(e);
128                 } catch (org.openide.src.SourceException e) {
129                     ErrorManager.getDefault().notify(e);
130                 }
131             }
132         };
133         JavaMetamodel.getManager().invokeAfterScanFinished(run, getName());
134     }
135
136     /**
137      * no reason to run this in the AWT-event thread, moreover it touches MDR
138      * @return <code>true</code>
139      */

140     protected boolean asynchronous() {
141         return false;
142     }
143 }
144
Popular Tags