Package net.xoetrope.optional.laf

Source Code of net.xoetrope.optional.laf.SynthLafInstaller

package net.xoetrope.optional.laf;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.UIManager;
import net.xoetrope.debug.DebugLogger;
import net.xoetrope.optional.filter.FilePreprocessor;

import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.optional.laf.synth.SynthClassLoader;



/**
* Install theSynth look and feel
* <p>Copyright (c) Xoetrope Ltd., 2001-2006, see license.txt for details</p>
*/
public class SynthLafInstaller
{
  /**
   * Creates a new instance of SynthLafInstaller
   */
  public SynthLafInstaller() {
  }

  /**
   * Install the look and feel
   */
  public static void installLaf()
  {
    try {
      SynthLookAndFeel synth = new SynthLookAndFeel();
      XProject currentProject = XProjectManager.getCurrentProject();
      String synthConfigFile = currentProject.getStartupParam( "SynthConfigFile" );
      String synthResourceLoader = currentProject.getStartupParam( "SynthResourceLoader" );
      if (( synthResourceLoader == null ) || ( synthResourceLoader.length() == 0 ))
        synthResourceLoader = "net.xoetrope.optional.laf.synth.SynthStub";
      SynthLafInstaller.class.getClassLoader().getResource( synthResourceLoader );
      Class resourceLoader = null;
      try {
        SynthClassLoader scl = new SynthClassLoader( SynthLafInstaller.class.getClassLoader());
        resourceLoader = scl.findClass( synthResourceLoader );
        String s = currentProject.getStartupParam( "SynthImageConveter" );
        if (( s != null ) && ( s.length() > 0 ))
          scl.setConvertClassName( s );
 
        s = currentProject.getStartupParam( "SynthOverwriteImages" );
        if (( s != null ) && ( s.length() > 0 ))
          scl.setOverwriteImages( Boolean.parseBoolean( s ));
      }
      catch ( Throwable e ) {
        resourceLoader = Class.forName( synthResourceLoader.trim() );
      }
      //loadStubClass( "net.xoetrope.optional.laf.synth.SynthStub" );

      BufferedInputStream bis = null;


      // Does the file need processing?
      int pos;
      if (( pos = synthConfigFile.indexOf( ".xsynth" )) > 0 ) {
        String outputFile = synthConfigFile.substring( 0, pos ) + ".synth";

          // Try to find a preprocessor for the file, at runtime this load should fail as the preprocessor
          // will not be present.
          try {
              FilePreprocessor processor = (FilePreprocessor)Class.forName( "net.xoetrope.optional.laf.synth.SynthPreprocessor" ).newInstance();
              DebugLogger.trace( currentProject.findResourceAsString( synthConfigFile ));
              bis = currentProject.getBufferedInputStream ( synthConfigFile );
              String processedStr = processor.process( bis );
              ByteArrayInputStream bais = new ByteArrayInputStream( processedStr.getBytes() );
              bis = new BufferedInputStream( bais );

              // Save the config file
              outputFile = getOutputPath( currentProject, "resources", synthConfigFile.substring( 0, pos ), ".synth" );
              FileWriter fw = new FileWriter( outputFile  );
              fw.write ( processedStr );
              fw.flush();
              fw.close();
          }
          catch ( Exception e )
          {
              // Assume that the file has previously been processed
              bis = currentProject.getBufferedInputStream ( outputFile );
          }
      }
      else
         bis = currentProject.getBufferedInputStream ( synthConfigFile );

      DebugLogger.log( "Synth config file: " + currentProject.findResourceAsString( synthConfigFile ));
      synth.load( bis, resourceLoader );
      UIManager.setLookAndFeel( synth );
    }
    catch ( Exception ex ) {
      ex.printStackTrace();
    }
  }

  public static String getOutputPath( XProject currentProject, String path, String prefix, String suffix ) throws IOException
  {
    String startupPath = currentProject.getStartupParam ( "ProjectPath" );
    if ( startupPath.indexOf( ".jar!" ) > 0 )
      startupPath = File.createTempFile( prefix, suffix ).getCanonicalPath();
    else
      startupPath += File.separator + path + File.separator + prefix + suffix;

    return startupPath;
  }
}
TOP

Related Classes of net.xoetrope.optional.laf.SynthLafInstaller

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.