1 19 package org.netbeans.test.editor.app.core; 20 21 import org.netbeans.test.editor.app.gui.*; 22 import java.io.Serializable ; 23 import org.netbeans.modules.editor.*; 24 import javax.swing.text.JTextComponent ; 25 import javax.swing.text.EditorKit ; 26 import javax.swing.Action ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.io.IOException ; 30 import java.io.Serializable ; 31 import java.io.DataOutputStream ; 32 import java.io.DataInputStream ; 33 import java.util.Vector ; 34 import java.beans.PropertyChangeListener ; 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeSupport ; 37 import java.lang.reflect.Method ; 38 import org.netbeans.test.editor.app.util.Scheduler; 39 import javax.swing.SwingUtilities ; 40 import org.netbeans.test.editor.app.Main; 41 import org.netbeans.test.editor.app.core.TestAction; 42 43 public class Logger implements Serializable { 44 45 static final String COMPLETION_ACTION="completion-action"; 46 47 static final long serialVersionUID = 8269484241745322111L; 48 static final String PERFORMING="Performing"; 49 static final String LOGGING="Logging"; 50 51 private Vector actions = new Vector (); 52 private Vector events = new Vector (); 53 private Vector testActions = new Vector (); 54 55 56 private int delay = 20; 57 58 private boolean logging=false; 59 60 private boolean performing=false; 61 62 private PropertyChangeSupport changeSupport; 63 public EventLoggingEditorPane editor; 64 65 66 public Logger(EventLoggingEditorPane editor){ 67 this.editor=(EventLoggingEditorPane)editor; 68 changeSupport = new PropertyChangeSupport (this); 69 } 70 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 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 102 103 public boolean isLogging() { 104 return logging; 105 } 106 107 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 sim = new SimulationPerformer( delay, this ); 120 sim.start(); 121 } 122 123 129 130 public synchronized void stopPerforming() { 131 if( performing == false ) return; 132 performing = false; 133 firePropertyChange(PERFORMING, Boolean.TRUE, Boolean.FALSE ); 134 } 135 136 139 140 public synchronized boolean isPerforming() { 141 return performing; 142 } 143 144 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 (); 158 events = new Vector (); 159 testActions = new Vector (); 160 } 161 162 public TestNode[] saveActions( TestStep step ) { 163 TestNode[] nodes; 164 String name; 165 String cmd; 166 167 nodes=new TestNode[actions.size()]; 168 for( int i=0; i < actions.size(); i++ ) { 169 name=(String )(actions.get(i)); 170 if (name.compareTo(COMPLETION_ACTION) != 0) { 171 cmd = ((ActionEvent )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 )(events.get(i))); 176 } 177 } 178 step.addNodes(nodes); 179 return nodes; 180 } 181 182 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 208 public synchronized void addPropertyChangeListener( PropertyChangeListener listener ) { 209 if( changeSupport == null ) changeSupport = new java.beans.PropertyChangeSupport ( this ); 210 changeSupport.addPropertyChangeListener( listener ); 211 } 212 217 public synchronized void removePropertyChangeListener( PropertyChangeListener listener ) { 218 if( changeSupport != null ) changeSupport.removePropertyChangeListener( listener ); 219 } 220 227 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 228 if (changeSupport != null) changeSupport.firePropertyChange(propertyName, oldValue, newValue); 229 } 230 231 232 235 public void logAction( Action a, ActionEvent evt ) { 236 if( logging && a.getValue(Action.NAME) != null) { 237 actions.add( a.getValue( Action.NAME ) ); 238 events.add( evt ); 239 } 240 } 241 242 245 public void logCompletionAction(String name) { 246 actions.add(COMPLETION_ACTION); 247 events.add(name); 248 } 249 250 public void performAction(TestStringAction act) { 251 String s=act.getString(); 252 Action a = (Action )editor.namesToActions.get(TestStringAction.STRINGED_NAME); 253 if (a == null) return; 254 255 for (int i=0;i < s.length();i++) { 256 ActionEvent evt = new ActionEvent (editor,ActionEvent.ACTION_PERFORMED, new String (new char[] {s.charAt(i)})); 257 editor.grabFocus(); 258 a.actionPerformed(evt); 259 } 260 } 261 262 public void performAction(TestCompletionAction act) { 263 String c=act.getCommand(); 264 Action a=editor.getCompletion().getJDCPopupPanel().getActionMap().get(c); 265 if (a == null) return; 266 editor.grabFocus(); 267 a.actionPerformed(new ActionEvent (editor,ActionEvent.ACTION_PERFORMED,"")); 268 } 269 270 public void performAction(TestLogAction act) { 271 Action a = (Action )editor.namesToActions.get(act.getName()); 272 if (a == null) return; 273 ActionEvent evt = new ActionEvent (editor,ActionEvent.ACTION_PERFORMED, 274 act.getCommand()); 275 editor.grabFocus(); 276 a.actionPerformed(evt); 277 } 278 private void performAction(int index) { 280 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 { 311 private int delay; 312 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 ex) { 332 ex.printStackTrace(); 333 } 334 335 Scheduler.getDefault().addTask(new Thread () { 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 if (testActions.get(cntr) instanceof TestLogAction && 350 ((TestLogAction)(testActions.get(cntr))).getName().compareTo("completion-show") == 0) { 351 int time=20; 352 while (!editor.getCompletion().isPaneVisible() && time > 0) { 354 try { 355 sleep(100); 356 } catch (InterruptedException 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 e) { 367 System.err.println("Throwable: " + e); 368 } 369 } 370 } } 372 373 | Popular Tags |