KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > core > EventLoggingEditorPane


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 package org.netbeans.test.editor.app.core;
20
21 import org.netbeans.test.editor.app.gui.*;
22 import javax.swing.JEditorPane JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import javax.swing.text.Keymap JavaDoc;
25 import javax.swing.KeyStroke JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.util.*;
29 import java.awt.event.*;
30 import org.netbeans.modules.editor.NbEditorDocument;
31 import javax.swing.text.EditorKit JavaDoc;
32 import javax.swing.plaf.TextUI JavaDoc;
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 /**
40  *
41  * @author pnejedly
42  * @version
43  */

44 public class EventLoggingEditorPane extends JEditorPane JavaDoc {
45     
46     private Logger logger=null;
47     public Hashtable namesToActions;
48     private Action JavaDoc[] actions;
49     
50     /** Creates new EventLoggingEditorPane */
51     public EventLoggingEditorPane() {
52         super();
53     }
54     
55     public String JavaDoc[] getActionsNames() {
56         Action JavaDoc[] as = getEditorKit().getActions();
57         String JavaDoc[] ret;
58         ret=new String JavaDoc[as.length];
59         for( int i=0; i < as.length; i++ )
60             ret[i]=(String JavaDoc)(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 JavaDoc e) {
74         Keymap JavaDoc binding = getKeymap();
75         Completion comp=((ExtEditorUI)(Utilities.getEditorUI(this))).getCompletion();
76         
77         if (comp.isPaneVisible()) {
78             KeyStroke JavaDoc kst=KeyStroke.getKeyStroke(e.getKeyCode(),e.getModifiers(),false);
79             
80             if (logger != null) {
81                 String JavaDoc com=(String JavaDoc)(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 JavaDoc k = KeyStroke.getKeyStrokeForEvent(e);
91             Action JavaDoc a = binding.getAction(k);
92             if (a != null) {
93                 String JavaDoc command = null;
94                 if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
95                     command = String.valueOf(e.getKeyChar());
96                 }
97                 ActionEvent JavaDoc ae = new ActionEvent JavaDoc(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 JavaDoc e) {
109         int id = e.getID();
110         switch(id) {
111             case KeyEvent.KEY_TYPED:
112                 if (myMapEventToAction(e) == false) {
113                     // default behavior is to input translated
114
// characters as content if the character
115
// hasn't been mapped in the keymap.
116
Keymap JavaDoc binding = getKeymap();
117                     if (binding != null) {
118                         Action JavaDoc a = binding.getDefaultAction();
119                         if (a != null) {
120                             ActionEvent JavaDoc ae = new ActionEvent JavaDoc(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 JavaDoc[] getActions() {
140         if (actions == null)
141             return new Action JavaDoc[0];
142         return actions;
143     }
144     
145     private void poorSetEditorKit(int index) {
146         EditorKit JavaDoc 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 ); // prepare hashtable for them and fill it with all actions
157
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 JavaDoc p1) {
163         Action JavaDoc a = (Action JavaDoc)namesToActions.get((String JavaDoc)(p1.getActionCommand()));
164         if (logger != null)
165             logger.logAction( a, p1 );
166         a.actionPerformed(p1);
167     }
168 }
169
Popular Tags