KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > GenericAction


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.languages.features;
21
22 import org.netbeans.modules.languages.*;
23 import org.netbeans.api.languages.ParseException;
24 import org.netbeans.api.languages.ASTNode;
25 import java.awt.event.ActionEvent JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.editor.BaseAction;
28 import org.netbeans.modules.editor.NbEditorDocument;
29 import org.netbeans.api.languages.ASTNode;
30 import org.netbeans.api.languages.ParseException;
31 import org.openide.ErrorManager;
32
33
34 /**
35  * @author Dan Prusa
36  */

37 public class GenericAction extends BaseAction {
38     
39     String JavaDoc performerName = null;
40     String JavaDoc enablerName = null;
41     Feature performer = null;
42     Feature enabler = null;
43     
44     public GenericAction(String JavaDoc name, String JavaDoc performerName, String JavaDoc enablerName) {
45         super(name);
46         this.performerName = performerName;
47         this.enablerName = enablerName;
48     }
49     
50     private Feature getPerformer() {
51         if (performer == null) {
52             performer = Feature.createMethodCallFeature (null, null, performerName);
53         }
54         return performer;
55     }
56     
57     private Feature getEnabler() {
58         if (enablerName == null) {
59             return null;
60         }
61         if (enabler == null) {
62             performer = Feature.createMethodCallFeature (null, null, enablerName);
63         }
64         return enabler;
65     }
66     
67     private ASTNode getASTNode(JTextComponent JavaDoc comp) {
68         try {
69             return ParserManagerImpl.get((NbEditorDocument)comp.getDocument()).getAST();
70         } catch (ParseException ex) {
71             ErrorManager.getDefault().notify(ex);
72         }
73         return null;
74     }
75     
76     
77     public void actionPerformed(ActionEvent JavaDoc e, JTextComponent JavaDoc comp) {
78         ASTNode node = getASTNode(comp);
79         if (node != null) {
80             getPerformer().getValue (new Object JavaDoc[] {node, comp});
81         }
82     }
83     
84     public boolean isEnabled() {
85         JTextComponent JavaDoc comp = getTextComponent(null);
86         if (comp == null)
87             return false;
88         ASTNode node = getASTNode(comp);
89         if (node == null)
90             return false;
91         Feature em = getEnabler();
92         if (em == null) {
93             return super.isEnabled();
94         }
95         Object JavaDoc result = em.getValue (new Object JavaDoc[] {node, comp});
96         return ((Boolean JavaDoc)result).booleanValue();
97     }
98     
99 }
100
Popular Tags