KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
23 import org.netbeans.modules.editor.*;
24 import javax.swing.text.JTextComponent JavaDoc;
25 import javax.swing.text.EditorKit JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.io.DataOutputStream JavaDoc;
32 import java.io.DataInputStream JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.beans.PropertyChangeSupport JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38 import org.netbeans.test.editor.app.util.Scheduler;
39 import javax.swing.SwingUtilities JavaDoc;
40 import org.netbeans.test.editor.app.Main;
41 import org.netbeans.test.editor.app.core.TestAction;
42
43 public class Logger implements Serializable JavaDoc {
44     
45     static final String JavaDoc COMPLETION_ACTION="completion-action";
46     
47     static final long serialVersionUID = 8269484241745322111L;
48     static final String JavaDoc PERFORMING="Performing";
49     static final String JavaDoc LOGGING="Logging";
50     
51     private Vector JavaDoc actions = new Vector JavaDoc();
52     private Vector JavaDoc events = new Vector JavaDoc();
53     private Vector JavaDoc testActions = new Vector JavaDoc();
54     
55     /** Property about delay between steps of simulation */
56     private int delay = 20;
57     /** Property about logging */
58     private boolean logging=false;
59     /** Property about running gathered actions */
60     private boolean performing=false;
61     /** Property change support. */
62     private PropertyChangeSupport JavaDoc changeSupport;
63     public EventLoggingEditorPane editor;
64     
65     
66     public Logger(EventLoggingEditorPane editor){
67         this.editor=(EventLoggingEditorPane)editor;
68         changeSupport = new PropertyChangeSupport JavaDoc(this);
69     }
70     /**
71      * Start/Restart the logging of actions into this logger. Everybody interested
72      * can hook-up listener
73      *
74      * @see addPropertyChangeListener
75      */

76     
77     public void startLogging() {
78         if( logging == true ) return;
79         logging = true;
80         editor.setLogger(this);
81         firePropertyChange(LOGGING, Boolean.FALSE, Boolean.TRUE );
82     }
83     
84     
85     /**
86      * Stop the logging of actions into this logger. Everybody interested
87      * can hook-up listener
88      *
89      * @see addPropertyChangeListener
90      */

91     
92     public void stopLogging() {
93         if( logging == false ) return;
94         logging = false;
95         editor.setLogger(null);
96         firePropertyChange(LOGGING, Boolean.TRUE, Boolean.FALSE );
97     }
98     
99     /**
100      * Returns the actual state of logging.
101      */

102     
103     public boolean isLogging() {
104         return logging;
105     }
106     
107     /**
108      * Start performing of logged actions from beginning. Everybody interested
109      * can hook-up listener.
110      *
111      * @see addPropertyChangeListener
112      */

113     
114     public synchronized void startPerforming() {
115         if( performing == true ) return;
116         performing = true;
117         firePropertyChange(PERFORMING, Boolean.FALSE, Boolean.TRUE );
118         System.err.println("creating SimulationPerformer");
119         Thread JavaDoc sim = new SimulationPerformer( delay, this );
120         sim.start();
121     }
122     
123     /**
124      * Stop the logging of actions into this logger. Everybody interested
125      * can hook-up listener.
126      *
127      * @see addPropertyChangeListener
128      */

129     
130     public synchronized void stopPerforming() {
131         if( performing == false ) return;
132         performing = false;
133         firePropertyChange(PERFORMING, Boolean.TRUE, Boolean.FALSE );
134     }
135     
136     /**
137      * Returns the actual state of logging.
138      */

139     
140     public synchronized boolean isPerforming() {
141         return performing;
142     }
143     
144     /**
145      * Forget all the bufferred actions
146      */

147     
148     public void setDelay(int value) {
149         delay=value;
150     }
151     
152     public int getDelay() {
153         return delay;
154     }
155     
156     public void clear() {
157         actions = new Vector JavaDoc();
158         events = new Vector JavaDoc();
159         testActions = new Vector JavaDoc();
160     }
161     
162     public TestNode[] saveActions( TestStep step ) {
163         TestNode[] nodes;
164         String JavaDoc name;
165         String JavaDoc cmd;
166         
167         nodes=new TestNode[actions.size()];
168         for( int i=0; i < actions.size(); i++ ) {
169             name=(String JavaDoc)(actions.get(i));
170             if (name.compareTo(COMPLETION_ACTION) != 0) {
171                 cmd = ((ActionEvent JavaDoc)events.get(i)).getActionCommand();
172                 if (cmd == null) cmd="";
173                 nodes[i]=new TestLogAction(name,cmd);
174             } else {
175                 nodes[i]=new TestCompletionAction(name,(String JavaDoc)(events.get(i)));
176             }
177         }
178         step.addNodes(nodes);
179         return nodes;
180     }
181     
182 /* public void loadAction(TestLogAction action) {
183         actions.add(action.getName());
184         events.add(new ActionEvent(editor,ActionEvent.ACTION_PERFORMED,
185         action.getCommand()));
186     }
187  
188     public void loadActions(TestStep step) {
189         TestLogAction a;
190         TestAction ta;
191  
192         for(int i=0;i < step.getChildCount();i++) {
193             a=(TestLogAction)(step.get(i));
194             loadAction(a);
195         }
196    }*/

197     
198     public void loadActions(TestStep step) {
199         for(int i=0;i < step.getChildCount();i++) {
200             testActions.add(step.get(i));
201         }
202     }
203     /**
204      * Add a {@link PropertyChangeListener} to the listener list.
205      *
206      * @param listener the <code>PropertyChangeListener</code> to be added
207      */

208     public synchronized void addPropertyChangeListener( PropertyChangeListener JavaDoc listener ) {
209         if( changeSupport == null ) changeSupport = new java.beans.PropertyChangeSupport JavaDoc( this );
210         changeSupport.addPropertyChangeListener( listener );
211     }
212     /**
213      * Remove a {@link PropertyChangeListener} from the listener list.
214      *
215      * @param listener the <code>PropertyChangeListener</code> to be removed
216      */

217     public synchronized void removePropertyChangeListener( PropertyChangeListener JavaDoc listener ) {
218         if( changeSupport != null ) changeSupport.removePropertyChangeListener( listener );
219     }
220     /**
221      * Fire a {@link PropertyChangeEvent} to each listener.
222      *
223      * @param propertyName the programmatic name of the property that was changed
224      * @param oldValue the old value of the property
225      * @param newValue the new value of the property
226      */

227     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
228         if (changeSupport != null) changeSupport.firePropertyChange(propertyName, oldValue, newValue);
229     }
230     
231     
232     /**
233      * Add an Action/ActionEvent pair to the list of events
234      */

235     public void logAction( Action JavaDoc a, ActionEvent JavaDoc evt ) {
236         if( logging && a.getValue(Action.NAME) != null) {
237             actions.add( a.getValue( Action.NAME ) );
238             events.add( evt );
239         }
240     }
241     
242     /**
243      * Add an Action/ActionEvent pair to the list of events
244      */

245     public void logCompletionAction(String JavaDoc name) {
246         actions.add(COMPLETION_ACTION);
247         events.add(name);
248     }
249     
250     public void performAction(TestStringAction act) {
251         String JavaDoc s=act.getString();
252         Action JavaDoc a = (Action JavaDoc)editor.namesToActions.get(TestStringAction.STRINGED_NAME);
253         if (a == null) return;
254         
255         for (int i=0;i < s.length();i++) {
256             ActionEvent JavaDoc evt = new ActionEvent JavaDoc(editor,ActionEvent.ACTION_PERFORMED, new String JavaDoc(new char[] {s.charAt(i)}));
257             editor.grabFocus();
258             a.actionPerformed(evt);
259         }
260     }
261     
262     public void performAction(TestCompletionAction act) {
263         String JavaDoc c=act.getCommand();
264         Action JavaDoc a=editor.getCompletion().getJDCPopupPanel().getActionMap().get(c);
265         if (a == null) return;
266         editor.grabFocus();
267         a.actionPerformed(new ActionEvent JavaDoc(editor,ActionEvent.ACTION_PERFORMED,""));
268     }
269     
270     public void performAction(TestLogAction act) {
271         Action JavaDoc a = (Action JavaDoc)editor.namesToActions.get(act.getName());
272         if (a == null) return;
273         ActionEvent JavaDoc evt = new ActionEvent JavaDoc(editor,ActionEvent.ACTION_PERFORMED,
274         act.getCommand());
275         editor.grabFocus();
276         a.actionPerformed(evt);
277     }
278     ////////////////////////////////////////////////////////////////////////////
279
private void performAction(int index) {
280        /* if (index+1 < testActions.size() && testActions.get(index) instanceof TestCompletionAction &&
281         testActions.get(index) instanceof TestCompletionAction) {
282             new Thread() {
283                 int time=20;
284                 public void run() {
285                     try {
286                         sleep(50);
287                     } catch (Exception ex) {
288                         ex.printStackTrace();
289                     }
290                     System.err.println("CC has index: "+editor.getCompletion().getView().getSelectedIndex());
291                 }
292             }.start();
293         }*/

294         
295         if (editor != Main.frame.getEditor()) {
296             System.err.println("Logger Editor isn't same as in MainFrame.");
297             System.err.println("Logger.editor="+editor);
298             System.err.println("Main.Frame.editor="+Main.frame.getEditor());
299             editor = Main.frame.getEditor();
300         }
301         TestAction ta=(TestAction)(testActions.get(index));
302         if (ta instanceof TestLogAction)
303             performAction((TestLogAction)ta);
304         else if (ta instanceof TestStringAction)
305             performAction((TestStringAction)ta);
306         else if (ta instanceof TestCompletionAction)
307             performAction((TestCompletionAction)ta);
308     }
309     
310     private class SimulationPerformer extends Thread JavaDoc {
311         private int delay;
312         //private boolean performing;
313
private Logger master;
314         
315         SimulationPerformer( int delay, Logger master ) {
316             super();
317             this.delay = delay;
318             this.master = master;
319         }
320         
321         public void run() {
322             System.err.println("SimulationPerformer started.");
323             try {
324                 System.err.println("Logger: Starts performing.");
325                 for( int i=0; i < testActions.size(); i++ ) {
326                     if (!master.isPerforming()) break;
327                     final int cntr = i;
328                     final boolean isLast = (cntr + 1) == testActions.size();
329                     try {
330                         sleep(delay);
331                     } catch (InterruptedException JavaDoc ex) {
332                         ex.printStackTrace();
333                     }
334                     
335                     Scheduler.getDefault().addTask(new Thread JavaDoc() {
336                         private boolean last = isLast;
337                         
338                         public void run() {
339                             if (!master.isPerforming())
340                                 return;
341                             performAction(cntr);
342                             if (last) {
343                                 System.err.println("Logger: Stops performing.");
344                                 master.stopPerforming();
345                             }
346                         }
347                     });
348                     //special timeout for completion-show action
349
if (testActions.get(cntr) instanceof TestLogAction &&
350                     ((TestLogAction)(testActions.get(cntr))).getName().compareTo("completion-show") == 0) {
351                         int time=20;
352                         //wait max two seconds for completion
353
while (!editor.getCompletion().isPaneVisible() && time > 0) {
354                             try {
355                                 sleep(100);
356                             } catch (InterruptedException JavaDoc ex) {
357                                 time=0;
358                             }
359                             time--;
360                         }
361                         if (!editor.getCompletion().isPaneVisible()) {
362                             System.err.println("Warning: Completion isn't visible after \"completion-show\" action.");
363                         }
364                     }
365                 }
366             } catch (Throwable JavaDoc e) {
367                 System.err.println("Throwable: " + e);
368             }
369         }
370     } // SimulationPerformer
371
}
372
373
Popular Tags