001:package com.xoetrope.svgscanner;
002:
003:import com.xoetrope.svgscanner.color.ColorTable;
004:import com.xoetrope.svgscanner.Footer;
005:import java.io.File;
006:import java.io.FileWriter;
007:import java.io.BufferedInputStream;
008:import java.io.ByteArrayOutputStream;
009:import java.io.IOException;
010:import java.util.prefs.Preferences;
011:import java.util.Hashtable;
012:import javax.swing.JFileChooser;
013:
014:import com.xoetrope.svgscanner.model.SynthModel;
015:
016:import com.xoetrope.helper.AFileFilter;
017:import net.xoetrope.optional.annotation.Find;
018:import net.xoetrope.swing.XButton;
019:import net.xoetrope.swing.XEdit;
020:import net.xoetrope.swing.XLabel;
021:import net.xoetrope.xui.XPage;
022:import net.xoetrope.xui.style.XStyle;
023:
024:/**
025: *
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p>$Revision: 1.6 $</p>
031: */
032:public class Welcome extends XPage
033:{
034: private String synthFile, outputFolder, sourceFolder, colorizedFolder;
035:
036: private Footer footer;
037:
038: @Find
039: private XButton dirChooserBtn;
040:
041: @Find
042: private XEdit synthEdit, sourceEdit, outputEdit, colorizedEdit, svgEditorEdit, xmlEditorEdit;
043:
044: private Preferences prefs;
045: private Hashtable<String,ColorTable> colorTables;
046:
047: private SynthModel synthModel;
048:
049: private String svgEditorPath, xmlEditorPath;
050:
051: public Welcome()
052: {
053: prefs = Preferences.userNodeForPackage( Welcome.class );
054: project.getTranslator().setResourceBundle( project.getResourceBundle( "en" ));
055: }
056:
057: public void pageActivated()
058: {
059: footer = ((Footer)pageMgr.getPage( "Footer" ));
060: synthEdit.setText( synthFile = prefs.get( "synthFile", "" ));
061: sourceEdit.setText( sourceFolder = prefs.get( "sourceFolder", "" ));
062: outputEdit.setText( outputFolder = prefs.get( "outputFolder", "" ));
063: svgEditorEdit.setText( svgEditorPath = prefs.get( "svgEditor", "" ));
064: xmlEditorEdit.setText( xmlEditorPath = prefs.get( "xmlEditor", "" ));
065: rootModel.set( "svgEditor", svgEditorPath );
066: rootModel.set( "xmlEditor", xmlEditorPath );
067:
068: colorizedEdit.setText( colorizedFolder = prefs.get( "colorizedFolder", "" ));
069:
070: rootModel.set( "sourceFolder", sourceFolder );
071: }
072:
073: public void doProcessing()
074: {
075: footer.setStatus( "Processing" );
076:
077: /** @todo put these resources into the model */
078: synthModel = new SynthModel( project, synthFile );
079:
080: File of = new File( outputFolder );
081: if ( !of.exists())
082: of.mkdir();
083:
084: File cf = new File( colorizedFolder );
085: if ( !cf.exists())
086: cf.mkdir();
087:
088: colorTables = new Hashtable<String,ColorTable>();
089: rootModel.set( "colorTables", colorTables );
090: rootModel.set( "sourceFolder", sourceFolder );
091: rootModel.set( "outputFolder", outputFolder );
092: rootModel.set( "colorizedFolder", colorizedFolder );
093:
094: rootModel.set( "sourceFolder", sourceFolder );
095:
096: // Iterate the files in the source folder
097: File sf = new File( sourceFolder );
098: File[] files = sf.listFiles();
099: for ( File f : files ) {
100: if ( f.isDirectory())
101: continue;
102:
103: String ext = f.getName();
104: if ( ext.indexOf( ".svg" ) < 0 )
105: continue;
106:
107: ColorTable ct = new ColorTable();
108: String name = f.getName();
109: ct.addImage( name );
110: scanImage( f, name, ct );
111:
112: colorTables.put( name, ct );
113: }
114:
115: footer.setStatus( "Resources loaded, ready." );
116: footer.setNext( "SynthBindings" );
117: }
118:
119: public void scanImage( File srcFile, String imageName, ColorTable colorTable )
120: {
121: try {
122: BufferedInputStream bis = project.getBufferedInputStream( srcFile );
123: byte[] b = new byte[ 1000 ];
124: if ( bis != null ) {
125: ByteArrayOutputStream baos = new ByteArrayOutputStream();
126: int i = bis.read( b );
127: while ( i != -1 ) {
128: baos.write( b, 0, i );
129: b = new byte[ 1000 ];
130: i = bis.read( b );
131: }
132:
133: colorTable.outputImage = outputFolder + File.separator + imageName;
134: String res = findAndReplace( baos.toString(), colorTable );
135: FileWriter fw = new FileWriter( colorTable.outputImage );
136: fw.write( res );
137: fw.flush();
138: fw.close();
139: }
140: }
141: catch (IOException ex) {
142: ex.printStackTrace();
143: }
144: }
145:
146: /**
147: * Search and replace a function references embedded in the synth file
148: * @param originalText the original config file text
149: * @return the processed text
150: */
151: protected String findAndReplace( String originalText, ColorTable colorTable )
152: {
153: /**
154: * @todo Use something like a StringTokenizer to split the string and allow for recursion
155: */
156: String resultString = "";
157: int startPos = 0;
158: int espressionStart = 0;
159: int argsStart = 0;
160: int argsEnd = 0;
161: while ( ( espressionStart = originalText.indexOf( "#", startPos ) ) > 0 ) {
162: resultString += originalText.substring( startPos, espressionStart );
163: argsStart = espressionStart + 1;
164: argsEnd = originalText.indexOf( "\"", argsStart );
165: String args = originalText.substring( argsStart, argsEnd );
166: if ( args.startsWith( "XMLID" ))
167: resultString += args;
168: else {
169: int argsPrefix = originalText.substring( 0, argsEnd ).lastIndexOf( '=');
170: if ( argsPrefix > 0 ) {
171: String prefix = originalText.substring( argsPrefix, argsEnd );
172: if ( prefix.contains( "url" )) {
173: startPos = argsEnd;
174: continue;
175: }
176: }
177:
178: resultString += "${getColor(" + colorTable.addColor( args ) + ")}";
179: }
180: startPos = argsEnd;
181: }
182:
183: if ( argsEnd == 0 )
184: return originalText;
185:
186: if ( argsEnd < originalText.length() )
187: resultString += originalText.substring( argsEnd );
188:
189: return resultString;
190: }
191:
192: public void chooseDirectory()
193: {
194: footer.setStatus( "" );
195:
196: boolean chooseSource = false;
197: if ( getCurrentEvent().getSource() == dirChooserBtn )
198: chooseSource = true;
199:
200: try {
201: // Ask the user to locate the file
202: JFileChooser chooser = new JFileChooser();
203: AFileFilter myFilter = new AFileFilter( "svg", "SVG files" );
204: myFilter.setDirMustExist( true );
205: myFilter.setFileMustExist( true );
206: chooser.setDialogTitle( chooseSource ? "Please specify the location of the SVG file" :
207: "Please specify the location of the output files" );
208: chooser.setFileFilter( myFilter );
209: chooser.setFileSelectionMode( chooser.DIRECTORIES_ONLY );
210: if ( sourceFolder.length() > 0 )
211: chooser.setCurrentDirectory( new File( sourceFolder ));
212:
213: if ( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) {
214: if ( chooseSource ) {
215: sourceFolder = chooser.getSelectedFile().toString();
216: sourceEdit.setText( sourceFolder );
217:
218: outputFolder = sourceFolder + "/output";
219: outputEdit.setText( outputFolder );
220:
221: colorizedFolder = sourceFolder + "/colorized";
222: colorizedEdit.setText( colorizedFolder );
223: }
224: else {
225: outputFolder = chooser.getSelectedFile().toString();
226: outputEdit.setText( outputFolder );
227: }
228:
229: prefs.put( "sourceFolder", sourceFolder );
230: prefs.put( "outputFolder", outputFolder );
231: prefs.put( "colorizedFolder", colorizedFolder );
232: }
233: }
234: catch ( Exception ex1 ) {
235: }
236: }
237:
238: public void chooseSynth()
239: {
240: footer.setStatus( "" );
241:
242: try {
243: // Ask the user to locate the file
244: JFileChooser chooser = new JFileChooser();
245: AFileFilter myFilter = new AFileFilter( "synth", "Synth files" );
246: myFilter.setDirMustExist( true );
247: myFilter.setFileMustExist( true );
248: chooser.setDialogTitle( "Please specify the location of the Synth file" );
249: chooser.setFileFilter( myFilter );
250: chooser.setFileSelectionMode( chooser.FILES_ONLY );
251: if ( synthFile.length() > 0 )
252: chooser.setCurrentDirectory( new File( synthFile ).getParentFile());
253:
254: if ( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) {
255: synthFile = chooser.getSelectedFile().toString();
256: synthEdit.setText( synthFile );
257: prefs.put( "synthFile", synthFile );
258: }
259: }
260: catch ( Exception ex1 ) {
261: }
262: }
263:
264: public void svgEditorBrowse()
265: {
266: String p = browseFile( "exe", "Application files", "Please choose your preferred SVG Editor", "svgEditorPath", "svgEditor" );
267: if ( p != null )
268: svgEditorEdit.setText( p );
269: }
270:
271: public void xmlEditorBrowse()
272: {
273: String p = browseFile( "exe", "Application files", "Please choose your preferred XML Editor", "xmlEditorPath", "xmlEditor" );
274: if ( p != null )
275: xmlEditorEdit.setText( p );
276: }
277:
278: /**
279: * Browse for a file
280: * @param ext the file extension
281: * @param descrip the file description
282: * @param caption the chooser title/caption
283: * @param defDir the default directory
284: * @param key the preferences key
285: * @return the selected file or null
286: */
287: private String browseFile( String ext, String descrip, String caption, String defDir, String key )
288: {
289: try {
290: // Ask the user to locate the file
291: JFileChooser chooser = new JFileChooser();
292: AFileFilter myFilter = new AFileFilter( ext, descrip );
293: myFilter.setDirMustExist( true );
294: myFilter.setFileMustExist( true );
295: chooser.setDialogTitle( caption );
296: chooser.setFileFilter( myFilter );
297: chooser.setFileSelectionMode( chooser.FILES_ONLY );
298: if ( synthFile.length() > 0 )
299: chooser.setCurrentDirectory( new File( defDir ).getParentFile());
300:
301: if ( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) {
302: String p = chooser.getSelectedFile().toString();
303: prefs.put( key, p );
304: rootModel.set( key, p );
305: return p;
306: }
307: }
308: catch ( Exception ex1 ) {
309: }
310:
311: return null;
312: }
313:}
|