1 19 package org.netbeans.test.editor.app.core; 20 21 import org.netbeans.test.editor.app.gui.*; 22 import javax.swing.JEditorPane ; 23 import java.awt.event.KeyEvent ; 24 import javax.swing.text.Keymap ; 25 import javax.swing.KeyStroke ; 26 import javax.swing.Action ; 27 import java.awt.event.ActionEvent ; 28 import java.util.*; 29 import java.awt.event.*; 30 import org.netbeans.modules.editor.NbEditorDocument; 31 import javax.swing.text.EditorKit ; 32 import javax.swing.plaf.TextUI ; 33 import org.netbeans.editor.Utilities; 34 import org.netbeans.editor.ext.Completion; 35 import org.netbeans.editor.ext.ExtEditorUI; 36 import org.netbeans.test.editor.app.Main; 37 import org.openide.text.CloneableEditorSupport; 38 39 44 public class EventLoggingEditorPane extends JEditorPane { 45 46 private Logger logger=null; 47 public Hashtable namesToActions; 48 private Action [] actions; 49 50 51 public EventLoggingEditorPane() { 52 super(); 53 } 54 55 public String [] getActionsNames() { 56 Action [] as = getEditorKit().getActions(); 57 String [] ret; 58 ret=new String [as.length]; 59 for( int i=0; i < as.length; i++ ) 60 ret[i]=(String )(actions[i].getValue( Action.NAME )); 61 return ret; 62 63 } 64 65 public void setLogger(Logger log) { 66 logger = log; 67 } 68 69 public Completion getCompletion() { 70 return ((ExtEditorUI)(Utilities.getEditorUI(Main.frame.getEditor()))).getCompletion(); 71 } 72 73 private final boolean myMapEventToAction(KeyEvent e) { 74 Keymap binding = getKeymap(); 75 Completion comp=((ExtEditorUI)(Utilities.getEditorUI(this))).getCompletion(); 76 77 if (comp.isPaneVisible()) { 78 KeyStroke kst=KeyStroke.getKeyStroke(e.getKeyCode(),e.getModifiers(),false); 79 80 if (logger != null) { 81 String com=(String )(comp.getJDCPopupPanel().getInputMap().get(kst)); 82 if (com != null) { 83 logger.logCompletionAction(com); 84 return true; 85 } 86 } 87 } 88 89 if (binding != null) { 90 KeyStroke k = KeyStroke.getKeyStrokeForEvent(e); 91 Action a = binding.getAction(k); 92 if (a != null) { 93 String command = null; 94 if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { 95 command = String.valueOf(e.getKeyChar()); 96 } 97 ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED,command, e.getModifiers()); 98 if (logger != null) 99 logger.logAction( a, ae ); 100 a.actionPerformed(ae); 101 e.consume(); 102 return true; 103 } 104 } 105 return false; 106 } 107 108 protected void processComponentKeyEvent(KeyEvent e) { 109 int id = e.getID(); 110 switch(id) { 111 case KeyEvent.KEY_TYPED: 112 if (myMapEventToAction(e) == false) { 113 Keymap binding = getKeymap(); 117 if (binding != null) { 118 Action a = binding.getDefaultAction(); 119 if (a != null) { 120 ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 121 String.valueOf(e.getKeyChar()), e.getModifiers()); 122 if (logger != null) 123 logger.logAction( a, ae ); 124 a.actionPerformed(ae); 125 e.consume(); 126 } 127 } 128 } 129 break; 130 case KeyEvent.KEY_PRESSED: 131 myMapEventToAction(e); 132 break; 133 case KeyEvent.KEY_RELEASED: 134 myMapEventToAction(e); 135 break; 136 } 137 } 138 139 public Action [] getActions() { 140 if (actions == null) 141 return new Action [0]; 142 return actions; 143 } 144 145 private void poorSetEditorKit(int index) { 146 EditorKit kit = CloneableEditorSupport.getEditorKit(TestSetKitAction.kitsTypes[index]); 147 setDocument(new NbEditorDocument(kit.getClass())); 148 super.setEditorKit(kit); 149 actions = null; 150 } 151 152 public void setEditorKit(int index) { 153 poorSetEditorKit(index); 154 System.err.println("Starting kit setting."); 155 actions = getEditorKit().getActions(); 156 namesToActions = new Hashtable( actions.length ); for( int i=0; i < actions.length; i++ ) 158 namesToActions.put( actions[i].getValue( Action.NAME ), actions[i] ); 159 System.err.println("Ending kit setting."); 160 } 161 162 public void perform(final java.awt.event.ActionEvent p1) { 163 Action a = (Action )namesToActions.get((String )(p1.getActionCommand())); 164 if (logger != null) 165 logger.logAction( a, p1 ); 166 a.actionPerformed(p1); 167 } 168 } 169 | Popular Tags |