001    package cnslab.gui;
002    
003    import java.io.*;
004    import java.util.*;
005
006    import org.slf4j.Logger;
007    import org.slf4j.LoggerFactory;
008
009    import thinlet.*;
010
011    import cnslab.cnsnetwork.SimulatorParser;
012    
013    /***********************************************************************
014    * Presents a list of XML models to choose from and validates selected.
015    *  
016    * @version
017    *   $Date: 2011-11-01 15:06:31 -0500 (Tue, 01 Nov 2011) $
018    *   $Rev: 273 $
019    *   $Author: croft $
020    * @author
021    *   Yi Dong
022    * @author
023    *   David Wallace Croft
024    ***********************************************************************/
025    public final class  ContentProcess
026      implements Runnable
027    ////////////////////////////////////////////////////////////////////////
028    ////////////////////////////////////////////////////////////////////////
029    {
030      
031    private static final Logger
032      LOGGER = LoggerFactory.getLogger ( ContentProcess.class );
033    
034    //
035    
036    private final Thinlet  thinlet;
037    
038    ////////////////////////////////////////////////////////////////////////
039    ////////////////////////////////////////////////////////////////////////
040    
041    public  ContentProcess ( final Thinlet  thinlet )
042    ////////////////////////////////////////////////////////////////////////
043    {
044      this.thinlet = thinlet;
045    }
046
047    ////////////////////////////////////////////////////////////////////////
048    ////////////////////////////////////////////////////////////////////////
049
050    @Override
051    public void  run ( )
052    ////////////////////////////////////////////////////////////////////////
053    {
054      final File  dir = new File ( "model" );
055      
056      final FilenameFilter
057        filter = new FilenameFilter ( )
058        {
059          @Override
060          public boolean  accept (
061            final File    dirFile,
062            final String  name )
063          {
064            return name.endsWith ( ".xml" ) || name.endsWith ( ".XML" );
065          }
066        };
067
068      // System.out.println(thin.getCount(thin.find("xmlModles")));
069        
070      final String [ ]  children = dir.list ( filter );
071      
072      if ( thinlet.getCount ( thinlet.find ( "xmlModles" ) )
073        != children.length )
074      {
075        Arrays.sort ( children );
076        
077        thinlet.removeAll ( thinlet.find ( "xmlModles" ) );
078        
079        //System.out.println("files:" +children.length);
080        
081        for ( int i = 0; i < children.length; i++ )
082        {
083          //    System.out.println("file:"+children[i]);
084          
085          final Object
086            o = Thinlet.create ( "choice" );
087          
088          thinlet.setString (
089            o,
090            "text",
091            children [ i ] );
092          
093          thinlet.add (
094            thinlet.find ( "xmlModles" ),
095            o );
096        }
097      }
098
099      if ( thinlet.getSelectedItem ( thinlet.find ( "xmlModles" ) )
100        != null )
101      {
102        // System.out.println(
103        //   getString(getSelectedItem(find("xmlModles")),"text"));
104        
105        final SimulatorParser
106          simulatorParser = new SimulatorParser ( );
107        
108        final String
109          out = simulatorParser.validate (
110            thinlet.getString (
111              thinlet.getSelectedItem ( thinlet.find ( "xmlModles" ) ),
112              "text" ) );
113        
114        LOGGER.debug ( "parser validation output:  {}", out );
115        
116        if ( out.equals ( "" ) )
117        {
118          thinlet.setString (
119            thinlet.find ( "simStatusDisplay" ),
120            "text",
121            thinlet.getString (
122              thinlet.getSelectedItem ( thinlet.find ( "xmlModles" ) ),
123              "text" )
124              + " ...OK!\n" );
125          
126          thinlet.setBoolean (
127            thinlet.find ( "startSim" ),
128            "enabled",
129            true );
130          
131          thinlet.setBoolean (
132            thinlet.find ( "stopSim" ),
133            "enabled",
134            false );
135          
136          thinlet.setBoolean (
137            thinlet.find ( "showXML" ),
138            "enabled",
139            true );
140        }
141        else
142        {
143          thinlet.setString (
144            thinlet.find ( "simStatusDisplay" ),
145            "text",
146            thinlet.getString (
147              thinlet.getSelectedItem ( thinlet.find ( "xmlModles" ) ),
148              "text" )
149              + " ...Error!\n" + out );
150          
151          thinlet.setBoolean (
152            thinlet.find ( "startSim" ),
153            "enabled",
154            false );
155          
156          thinlet.setBoolean (
157            thinlet.find ( "stopSim" ),
158            "enabled",
159            false );
160          
161          thinlet.setBoolean (
162            thinlet.find ( "showXML" ),
163            "enabled",
164            true );
165        }
166      }
167    }
168
169    ////////////////////////////////////////////////////////////////////////
170    ////////////////////////////////////////////////////////////////////////
171    }