1 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 ; 26 import javax.swing.text.JTextComponent ; 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 37 public class GenericAction extends BaseAction { 38 39 String performerName = null; 40 String enablerName = null; 41 Feature performer = null; 42 Feature enabler = null; 43 44 public GenericAction(String name, String performerName, String 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 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 e, JTextComponent comp) { 78 ASTNode node = getASTNode(comp); 79 if (node != null) { 80 getPerformer().getValue (new Object [] {node, comp}); 81 } 82 } 83 84 public boolean isEnabled() { 85 JTextComponent 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 result = em.getValue (new Object [] {node, comp}); 96 return ((Boolean )result).booleanValue(); 97 } 98 99 } 100 | Popular Tags |