001    package cnslab.gui;
002    
003    import java.io.*;
004
005    import org.slf4j.Logger;
006    import org.slf4j.LoggerFactory;
007    import thinlet.Thinlet;
008
009    import cnslab.cnsmath.Seed;
010    import cnslab.cnsnetwork.SimulatorParser;
011    
012    /***********************************************************************
013    * @version
014    *   $Date: 2011-09-10 18:17:21 -0500 (Sat, 10 Sep 2011) $
015    *   $Rev: 211 $
016    *   $Author: croft $
017    * @author
018    *   Yi Dong
019    * @author
020    *   David Wallace Croft
021    ***********************************************************************/
022    public final class  OutputListen
023      extends Thread
024    ////////////////////////////////////////////////////////////////////////
025    ////////////////////////////////////////////////////////////////////////
026    {
027      
028    private static final Class<OutputListen>
029      CLASS = OutputListen.class;
030   
031//    private static final String
032//      CLASS_NAME = CLASS.getName ( );
033 
034    private static final Logger
035      LOGGER = LoggerFactory.getLogger ( CLASS );    
036
037    private final InputStream  in;
038
039    private final Thinlet      thinlet;
040
041    ////////////////////////////////////////////////////////////////////////
042    ////////////////////////////////////////////////////////////////////////
043    
044    public  OutputListen (
045      final InputStream  in,
046      final Thinlet      thinlet )
047    ////////////////////////////////////////////////////////////////////////
048    {
049      this.in      = in;
050      
051      this.thinlet = thinlet;
052    }
053
054    ////////////////////////////////////////////////////////////////////////
055    ////////////////////////////////////////////////////////////////////////
056    
057    @Override
058    public void  run ( )
059    ////////////////////////////////////////////////////////////////////////
060    {
061      try
062      {
063        final Object  dis = thinlet.find ( "simStatusDisplay" );
064        
065        thinlet.setString (
066          dis,
067          "text",
068          "Simulation starts ........\n" );
069
070        final BufferedReader
071          input = new BufferedReader ( new InputStreamReader ( in ) );
072        
073        String  a = input.readLine ( );
074        
075        while ( a != null )
076        {
077          if ( a.startsWith ( "E" ) )
078          {
079            thinlet.setString (
080              thinlet.find ( "showProgress" ),
081              "text",
082              a );
083          }
084          else
085          {
086            thinlet.setString (
087              dis,
088              "text",
089              thinlet.getString (
090                dis,
091                "text" )
092                + a + "\n" );
093          }
094
095          new Thread (
096            new UpdateScroll (
097              thinlet,
098              dis,
099              -1,
100              0.99f ) ).start ( );
101          
102          // thin.setScroll(dis, (float)(-1), (float)0.99);
103          
104          // Print file line to screen
105          
106          // System.out.println(a);
107          
108          a = input.readLine ( );
109        }
110        
111        input.close ( );
112        
113        thinlet.setString (
114          dis,
115          "text",
116          thinlet.getString (
117            dis,
118            "text" )
119            + "Simulation done\n" );
120        
121        new Thread (
122          new UpdateScroll (
123            thinlet,
124            dis,
125            -1,
126            0.99f ) ).start ( );
127        
128        thinlet.setBoolean (
129          thinlet.find ( "startSim" ),
130          "enabled",
131          true );
132        
133        thinlet.setBoolean (
134          thinlet.find ( "stopSim" ),
135          "enabled",
136          false );
137        
138        thinlet.setString (
139          thinlet.find ( "showProgress" ),
140          "text",
141          "" );
142        
143        final SimulatorParser
144          sim = new SimulatorParser (
145            new Seed ( -1 ),
146            new File (
147              "model/" + thinlet.getString (
148                thinlet.getSelectedItem ( thinlet.find ( "xmlModles" ) ),
149                "text" ) ) );
150        
151        sim.parseMapCells ( );
152        
153        // System.out.println(sim.outFile);
154        
155        new Thread (
156          new ContentProcessResult2 (
157            thinlet,
158            sim.outFile ) ).start ( );
159
160        // NodeList nList = root.getElementsByTagName("GlobalVariable");
161        
162        // Element global = (Element) nList.item(0);
163
164        // if(global.getElementsByTagName("DataFileName").getLength()>0)
165      }
166      catch ( final Exception  e )
167      {
168        LOGGER.error (
169          e.getMessage ( ),
170          e );
171        
172        e.printStackTrace ( );
173      }
174    }
175
176    ////////////////////////////////////////////////////////////////////////
177    ////////////////////////////////////////////////////////////////////////
178    }