Commit in lcsim/src/org/lcsim/contrib/garfield/util on MAIN
GDriver.java+76added 1.1
Added org.lcsim,contrib.garfield.util.GDriver class as a temporary solution for https://jira.slac.stanford.edu/browse/LCSIM-128

lcsim/src/org/lcsim/contrib/garfield/util
GDriver.java added at 1.1
diff -N GDriver.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ GDriver.java	1 Feb 2007 01:25:38 -0000	1.1
@@ -0,0 +1,76 @@
+package org.lcsim.contrib.garfield.util;
+
+import org.lcsim.util.Driver;
+import java.io.PrintStream;
+import org.freehep.application.Application;
+import org.freehep.application.studio.Studio;
+import org.openide.util.Lookup;
+import org.freehep.jas.plugin.console.ConsoleService;
+import org.freehep.jas.plugin.console.Console;
+import org.freehep.jas.plugin.console.ConsoleOutputStream;
+import javax.swing.SwingUtilities;
+import java.util.logging.Logger;
+
+
+/**
+ *
+ * @author D. Onoprienko
+ * @version $Id: GDriver.java,v 1.1 2007/02/01 01:25:38 onoprien Exp $
+ */
+public class GDriver extends Driver {
+
+  /**
+   * Returns {@link PrintStream} object that can be used to print to Jas3 "Record Loop" console.
+   */
+  public PrintStream getConsolePrintStream() {
+    return getConsolePrintStream("Record Loop");
+  }
+  
+  /**
+   * Returns {@link PrintStream} object that can be used to print to Jas3 console.
+   * @parameter name - Name of the console where the output is to be sent. If no
+   *                   console with this name exists, it will be created.
+   */
+  public PrintStream getConsolePrintStream(String name) {
+    PrintStream cout;
+    try {
+      Studio studio = (Studio) Application.getApplication();
+      Lookup lookup = studio.getLookup();
+      ConsoleService cs = (ConsoleService) lookup.lookup(ConsoleService.class);
+      Console console = cs.getConsole("Record Loop");
+      if (console == null) {
+        if (SwingUtilities.isEventDispatchThread()) {
+          console = cs.createConsole(name, null);
+        } else {
+          ConsoleCreator cc = new ConsoleCreator(name, cs);
+          SwingUtilities.invokeAndWait(cc);
+          console = cc.getConsole();
+        }
+      }
+      cout = new PrintStream(console.getOutputStream(null,true), true);
+    } catch (Exception e) {
+      cout = System.out;
+    }
+    return cout;
+  }
+
+  /**
+   * Redirects everything printed to <tt>System.out</tt> and <tt>System.err</tt> to Jas3 "Record Loop" console.
+   * Redirection affects all drivers.
+   */
+  public void redirectOutputToConsole() {
+    PrintStream cout = getConsolePrintStream();
+    System.setOut(cout);
+    System.setErr(cout);
+  }
+  
+   private class ConsoleCreator implements Runnable {
+     private String _name;
+     private ConsoleService _cs;
+     private Console _console;
+     ConsoleCreator(String name, ConsoleService cs) {_name = name; _cs = cs;}
+     public void run() {_console = _cs.createConsole(_name, null);}
+     Console getConsole() {return _console;}
+   }
+
+}
CVSspam 0.2.8