Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ExternalProcess.java

00001 /*
00002  * ExternalProcess.java
00003  *
00004  * Created on 02 May 2003, 13:24
00005  */
00006 
00007 package GuiComponents.Console;
00008 
00009 import javax.swing.*;
00010 import java.awt.event.*;
00011 import java.awt.GridBagConstraints;
00012 import java.io.*;
00013 
00018 public class ExternalProcess extends javax.swing.JFrame implements ActionListener {
00019     
00020     protected JConsole console;
00021     protected Process process;
00022     private StreamThread stdout;
00023     private StreamThread stderr;
00024     private Writer stdin;
00025     
00027     public ExternalProcess(String cmd) throws IOException{        
00028         initComponents();
00029         myInit(cmd);
00030         setTitle(cmd);
00031         setSize(800,500);
00032         show();
00033     }
00034     
00035     private void myInit(String cmd) throws IOException{
00036         console = new JConsole();
00037         console.addActionListener(this);
00038         GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
00039         gridBagConstraints.gridx = 0;
00040         gridBagConstraints.gridy = 0;
00041         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);     
00042         gridBagConstraints.fill = GridBagConstraints.BOTH;
00043         gridBagConstraints.weightx = 1;
00044         gridBagConstraints.weighty = 1;
00045         getContentPane().add(new JScrollPane(console), gridBagConstraints);
00046                 
00047         process = Runtime.getRuntime().exec(cmd);
00048         stdout = new StreamThread(process.getInputStream());
00049         stderr = new StreamThread(process.getErrorStream());
00050         stdin = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
00051         stdout.start();
00052         stderr.start();
00053     }
00054     
00060     private void initComponents() {//GEN-BEGIN:initComponents
00061         java.awt.GridBagConstraints gridBagConstraints;
00062         
00063         bClose = new javax.swing.JButton();
00064         
00065         getContentPane().setLayout(new java.awt.GridBagLayout());
00066         
00067         addWindowListener(new java.awt.event.WindowAdapter() {
00068             public void windowClosing(java.awt.event.WindowEvent evt) {
00069                 exitForm(evt);
00070             }
00071         });
00072         
00073         bClose.setText("Close");
00074         bClose.addActionListener(new java.awt.event.ActionListener() {
00075             public void actionPerformed(java.awt.event.ActionEvent evt) {
00076                 bCloseActionPerformed(evt);
00077             }
00078         });
00079         
00080         gridBagConstraints = new java.awt.GridBagConstraints();
00081         gridBagConstraints.gridx = 0;
00082         gridBagConstraints.gridy = 1;
00083         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00084         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00085         getContentPane().add(bClose, gridBagConstraints);
00086         
00087         pack();
00088     }//GEN-END:initComponents
00089 
00090     private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
00091         close();
00092     }//GEN-LAST:event_bCloseActionPerformed
00093     
00095     private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
00096         close();
00097     }//GEN-LAST:event_exitForm
00098 
00099     
00100     public void close() {        
00101         process.destroy();
00102         stdout.stop = true;
00103         stderr.stop = true;
00104         this.dispose();
00105     }
00106     
00110     public void actionPerformed(ActionEvent e) {
00111         String line = e.getActionCommand();
00112         try {
00113             System.out.println("Writing: " + line);
00114             stdin.write(line);
00115             System.out.println("Done");
00116         } catch (IOException ioe) {            
00117         }
00118     }
00119     
00120     class StreamThread extends Thread {
00121         InputStream inputStream;
00122         volatile boolean stop = false;
00123 
00124         StreamThread(InputStream inputStream) {
00125             this.inputStream = inputStream;
00126         } 
00127                 
00128         public void run() {
00129             try {
00130                 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
00131 
00132                 StringBuffer line = new StringBuffer(2000);  
00133                 String temp;
00134                 int nLines;
00135                 while (!stop) {
00136                     line.setLength(0);                    
00137                                         
00138                     for (nLines = 0; in.ready() && nLines < 100; ++nLines) {                         
00139                         line.append(in.readLine()).append("\n");
00140                     }
00141                     //System.out.println(nLines);
00142                     if (nLines == 0) {
00143                         temp = in.readLine();
00144                         if (temp == null) break;
00145                         line.append(temp).append("\n");
00146                     }
00147                     
00148                     if (console != null) {
00149                         console.print(line.toString());
00150                     } 
00151                     try {
00152                         sleep(100);
00153                     } catch (java.lang.InterruptedException e){}
00154                 }
00155                 
00156                 in.close();
00157             }
00158             catch(Exception e) {
00159                 if(console != null) console.println(e.getMessage());
00160                 else System.out.println(e.getMessage());
00161             } 
00162         } 
00163     } 
00164     
00165     // Variables declaration - do not modify//GEN-BEGIN:variables
00166     private javax.swing.JButton bClose;
00167     // End of variables declaration//GEN-END:variables
00168     
00169 }

Generated on Thu Jul 15 09:55:42 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5