Source Code Cross Referenced for ImageViewer.java in  » XML-UI » xui32 » com » xoetrope » svgscanner » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » xui32 » com.xoetrope.svgscanner 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:package com.xoetrope.svgscanner;
002:
003:import com.xoetrope.svgscanner.ColorListCellRenderer;
004:import com.xoetrope.svgscanner.color.ColorMapping;
005:import com.xoetrope.svgscanner.color.ColorTable;
006:import com.xoetrope.svgscanner.color.HsbColorComparator;
007:import com.xoetrope.svgscanner.color.ColorTreeCellRenderer;
008:
009:import com.xoetrope.svgscanner.color.process.ColorProcess;
010:import com.xoetrope.svgscanner.color.process.FindColor;
011:import com.xoetrope.svgscanner.color.process.ResetColor;
012:import com.xoetrope.svgscanner.color.process.RotateColor;
013:import com.xoetrope.svgscanner.color.process.SaturateColor;
014:import com.xoetrope.svgscanner.color.process.BrightenColor;
015:
016:import java.awt.event.ActionListener;
017:import java.awt.Component;
018:import java.awt.Point;
019:import java.awt.Color;
020:import java.awt.event.ActionEvent;
021:import java.awt.event.ActionListener;
022:import java.awt.event.MouseEvent;
023:import java.io.File;
024:import java.net.MalformedURLException;
025:import java.net.URL;
026:import java.util.Vector;
027:import java.util.ArrayList;
028:import java.util.Collection;
029:import java.util.Collections;
030:import java.util.Hashtable;
031:import java.util.Enumeration;
032:import java.io.IOException;
033:import java.io.InputStream;
034:import java.io.InputStreamReader;
035:import java.io.Writer;
036:import java.io.BufferedReader;
037:import java.io.BufferedWriter;
038:import java.io.FileWriter;
039:import java.io.BufferedInputStream;
040:import java.io.ByteArrayOutputStream;
041:
042:import javax.swing.SwingUtilities;
043:import javax.swing.JColorChooser;
044:import javax.swing.JDialog;
045:import javax.swing.JPopupMenu;
046:import javax.swing.JMenuItem;
047:import javax.swing.DefaultListModel;
048:import javax.swing.colorchooser.AbstractColorChooserPanel;
049:import javax.swing.tree.TreePath;
050:import javax.swing.tree.TreeNode;
051:import javax.swing.tree.DefaultMutableTreeNode;
052:import javax.swing.text.Position;
053:
054:import net.xoetrope.debug.DebugLogger;
055:import net.xoetrope.optional.annotation.Find;
056:import net.xoetrope.swing.XLabel;
057:import net.xoetrope.swing.XList;
058:import net.xoetrope.swing.XImage;
059:import net.xoetrope.swing.XTree;
060:import net.xoetrope.swing.tree.XTreeModelAdapter;
061:import net.xoetrope.xui.XPage;
062:import net.xoetrope.xui.data.XModel;
063:import net.xoetrope.xui.data.XModelHelper;
064:import net.xoetrope.xui.style.XStyle;
065:import net.xoetrope.xui.data.XModelHelper;
066:import net.xoetrope.optional.svg.svgsalamander.XSvgPainter;
067:import net.xoetrope.xui.data.XBaseModel;
068:import net.xoetrope.xui.helper.XuiUtilities;
069:import net.xoetrope.editor.XEditorUtilities;
070:import net.xoetrope.editor.color.ColorWheelPanel;
071:import net.xoetrope.xui.helper.SwingWorker;
072:
073:import com.xoetrope.svgscanner.model.SynthModel;
074:import com.xoetrope.svgscanner.model.SynthElementHandler;
075:
076:import com.xoetrope.swing.list.XAltListCellRenderer;
077:
078:import net.xoetrope.xml.XmlElement;
079:import net.xoetrope.xml.nanoxml.NanoXmlElement;
080:import net.xoetrope.xml.nanoxml.NanoXmlWriter;
081:
082:/**
083: *
084: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
085: * the GNU Public License (GPL), please see license.txt for more details. If
086: * you make commercial use of this software you must purchase a commercial
087: * license from Xoetrope.</p>
088: * <p>$Revision: 1.12 $</p>
089: */
090:public class ImageViewer extends XPage implements  ActionListener
091:{
092:  private Hashtable<String,ColorTable> colorTables;
093:  private String selectedImage, selectedColor;
094:  private ColorMapping selectedColorMap;
095:  private Color newColor;
096:  private ColorProcess colorProcessor;
097:  
098:  @Find
099:  private XList imageList, originalList, colorizedList, usageList;
100:  
101:  @Find
102:  private XImage originalImage, colorizedImage;
103:  
104:  @Find
105:  private XLabel usageLbl;
106:
107:  
108:  @Find
109:  private XTree colorTree;
110:  
111:  private XSvgPainter originalPainter, colorizedPainter;
112:  
113:  
114:  /** Creates a new instance of ImageViewer */
115:  public ImageViewer() 
116:  {
117:    XBaseModel obj = (XBaseModel)rootModel.get( "colorTables" );
118:    colorTables = (Hashtable<String,ColorTable>)obj.get();
119:  }
120:  
121:  public void pageCreated()
122:  {
123:    XStyle style = project.getStyleManager().getStyle( "list/alt" );
124:    
125:    originalImage.setPainter( originalPainter = new XSvgPainter( originalImage ));
126:    colorizedImage.setPainter( colorizedPainter = new XSvgPainter( colorizedImage ));
127:    
128:    ColorListCellRenderer colorRenderer = new ColorListCellRenderer();
129:    colorRenderer.setAltUnselectedColors( style.getStyleAsColor( XStyle.COLOR_FORE ), style.getStyleAsColor( XStyle.COLOR_BACK ));
130:    originalList.setCellRenderer( colorRenderer );
131:    colorizedList.setCellRenderer( colorRenderer );
132:
133:    XAltListCellRenderer renderer = new XAltListCellRenderer();
134:    renderer.setAltUnselectedColors( style.getStyleAsColor( XStyle.COLOR_FORE ), style.getStyleAsColor( XStyle.COLOR_BACK ));
135:    
136:    imageList.setCellRenderer( renderer );
137:    usageList.setCellRenderer( renderer );
138:  }
139:  
140:  public void pageActivated()
141:  {
142:    imageList.removeAll();
143:    Collection<ColorTable> values = colorTables.values();
144:    ArrayList<String> names = new ArrayList<String>();
145:    for ( ColorTable ct : values )
146:      names.add( ct.name );
147:      
148:    Collections.sort( names );
149:    for ( String name : names )
150:      imageList.addItem( name );
151:    
152:    imageList.setSelectedIndex( 0 );
153:    
154:    colorTree.setCellRenderer( new ColorTreeCellRenderer( HsbColorComparator.COMPARE_COLORIZED ));
155:    //colorTree.setRootVisible( false );
156:    fillColorTree();
157:  }
158:  
159:  public void chooseImage()
160:  {
161:    String lastImage = selectedImage;
162:    Object selObj = imageList.getSelectedObject();
163:    if ( selObj != null ) 
164:      selectedImage = selObj.toString();
165:    else
166:      return;
167:    
168:    if ( selectedImage.equals( lastImage ))
169:      return;
170:    
171:    SwingUtilities.invokeLater( new Runnable()
172:    {
173:      public void run()
174:      {
175:        originalList.removeAll();
176:        colorizedList.removeAll();
177:
178:        ColorTable ct = colorTables.get( selectedImage );
179:        Collection<ColorMapping> values = ct.colors.values();
180:        for ( ColorMapping cm : values ) {
181:          originalList.addItem( cm.getOriginalColor());
182:          colorizedList.addItem( cm.getColorizedColor() );        
183:        }
184:        originalList.setSelectedIndex( 0 );
185:        colorizedList.setSelectedIndex( 0 );
186:        modifyColor();
187:        adjustColorizedImage();
188:
189:        try {             
190:          // Now show image
191:          String sourceFolder = XModelHelper.getString( project, rootModel, "sourceFolder" );
192:          String colorizedFolder = XModelHelper.getString( project, rootModel, "colorizedFolder" );
193:          URL url = new File( sourceFolder + File.separator + selectedImage ).toURL();
194:          File colorizedFile = new File( colorizedFolder + File.separator + selectedImage );
195:          URL clrUrl = colorizedFile.toURL();
196:          if ( !colorizedFile.exists())
197:            clrUrl = url;
198:          
199:          originalPainter.setImage( url );
200:          colorizedPainter.setImage( clrUrl );
201:
202:          originalImage.repaint();
203:          colorizedImage.repaint();
204:        } 
205:        catch ( MalformedURLException ex ) {
206:          ex.printStackTrace();
207:        }      
208:      }
209:    });
210:  }
211:  
212:  private void fillColorTree()
213:  {
214:    Hashtable<String,ColorMapping> commonColors = ColorTable.getCommonColors();
215:    ArrayList<ColorMapping> values = new ArrayList<ColorMapping>( commonColors.values());
216:    HsbColorComparator hsbComparator = new HsbColorComparator( HsbColorComparator.COMPARE_COLORIZED, 0.02F );
217:    
218:    Collections.sort( values, hsbComparator );
219:    XBaseModel root = (XBaseModel)rootModel.get( "colorTree" );
220:    XBaseModel newNode, lastNode = null;
221:    Color lastColor = null;
222:    for ( ColorMapping cm : values ) {
223:      String newColor = cm.getColorizedColor();
224:      if (( lastNode != null ) && hsbComparator.isSameHue( lastColor, cm.getColorized() ))
225:        newNode = new XBaseModel( lastNode, newColor, cm );
226:      else { 
227:        newNode = new XBaseModel( root, newColor, cm );
228:        lastColor = cm.getColorized();
229:      }
230:      lastNode = newNode;
231:    }
232:    
233:    updateBinding( getBinding( colorTree ));
234:  }    
235:  
236:  /**
237:   * The selection on the original color list has changed
238:   */
239:  public void chooseOriginalColor()
240:  {
241:    Object selObj = originalList.getSelectedObject();
242:    colorizedList.setSelectedValue( selObj, true );
243:    modifyColor();
244:  }
245:  
246:  public void modifyColor()
247:  {
248:    final MouseEvent me = (MouseEvent)getCurrentEvent();
249:    final Object selObj = colorizedList.getSelectedObject();
250:    final String lastColor = selectedColor;
251:    if ( selObj != null )           
252:      selectedColor = selObj.toString();
253:    else 
254:      return;
255:      
256:    
257:    SwingUtilities.invokeLater( new Runnable()
258:    {
259:      public void run()
260:      {
261:        String imageUsage = "Usage: ";
262:        if ( !selectedColor.equals( lastColor )) {
263:          usageList.removeAll();
264:          
265:          Hashtable<String,ColorMapping> mappings = colorTables.get( selectedImage ).colors;
266:          Enumeration<String> keys = mappings.keys();
267:          while ( keys.hasMoreElements()) {
268:            ColorMapping map = mappings.get( keys.nextElement());
269:            String clr = map.getColorizedColor();            
270:            if ( clr == null )
271:              clr = map.getOriginalColor();
272:            
273:            if ( clr.equals( selectedColor )) {
274:              selectedColorMap = map;
275:              
276:              originalList.setSelectedValue( map.getOriginalColor(), true );
277:              
278:              Hashtable<String,ColorTable> imageNames = map.getUsages();
279:              Enumeration<String> files = imageNames.keys();
280:              while( files.hasMoreElements()) 
281:                usageList.addItem( files.nextElement());
282:              
283:              findColorInTree( selectedColor );
284:              break;
285:            }
286:          }
287:        }
288:
289:        if (( me != null ) && ( me.getClickCount() > 1 )) {
290:          Color clr = (Color)XStyle.parseColor( selectedColor );
291:          final JColorChooser colorChooser = new JColorChooser();
292:
293:          AbstractColorChooserPanel[] oldPanels = colorChooser.getChooserPanels();
294:          AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[ oldPanels.length + 1 ];
295:          System.arraycopy( oldPanels, 0, newPanels, 1, oldPanels.length );
296:          newPanels[ 0 ] = new ColorWheelPanel();
297:          colorChooser.setChooserPanels( newPanels );
298:
299:          //colorChooser.addChooserPanel( new ColorWheelPanel());
300:          colorChooser.setColor( clr );
301:          XEditorUtilities.setDefaultFont( colorChooser );
302:
303:          JDialog dialog = JColorChooser.createDialog(
304:            ImageViewer.this , 
305:            "Choose the new Color", 
306:            true,
307:            colorChooser, 
308:            new ActionListener() 
309:            {
310:              public void actionPerformed( ActionEvent e ) 
311:              {
312:                newColor = colorChooser.getColor();
313:                String colorStr = XuiUtilities.colorToHexString( newColor );
314:                selectedColorMap.setColorizedColor( colorStr );
315://                int r = newColor.getRed();
316://                int g = newColor.getGreen();
317://                int b = newColor.getBlue();
318://                int a = newColor.getAlpha();
319://                selectedColorMap.setColorizedColor((( r < 16 ) ? "0" : "" ) + Integer.toString( r, 16 ) + 
320://                    (( g < 16 ) ? "0" : "" ) + Integer.toString( g, 16 ) + 
321://                    (( b < 16 ) ? "0" : "" ) + Integer.toString( b, 16 ) + 
322://                    (( a < 16 ) ? "0" : "" ) + ( a != 255 ? Integer.toString( a, 16 ) : "" ));
323:                int selIdx = colorizedList.getSelectedIndex();
324:                DefaultListModel lm = (DefaultListModel)colorizedList.getModel();
325:                lm.setElementAt( selectedColorMap.getColorizedColor(), selIdx );
326:                
327:                adjustColorizedImage();
328:                fillColorTree();
329:                findColorInTree( selectedColorMap.getColorizedColor());
330:              }
331:            }, 
332:            null );
333:          XuiUtilities.centerOnScreen( dialog );
334:          dialog.setVisible( true );   
335:          
336:        }
337:      }
338:    });
339:  }
340:  
341:  private void adjustColorizedImage()
342:  {
343:    try {
344:      String sourceFolder = XModelHelper.getString( project, rootModel, "sourceFolder" );
345:      String colorizedFolder = XModelHelper.getString( project, rootModel, "colorizedFolder" );
346:      File sourceFile = new File( sourceFolder + File.separator + selectedImage );
347:      File colorizedFile = new File( colorizedFolder + File.separator + selectedImage );
348:
349:      BufferedInputStream bis = project.getBufferedInputStream( sourceFile );
350:      byte[] b = new byte[ 1000 ];
351:      if ( bis != null ) {
352:        ByteArrayOutputStream baos = new ByteArrayOutputStream();
353:        int i = bis.read( b );
354:        while ( i != -1 ) {
355:          baos.write( b, 0, i );
356:          b = new byte[ 1000 ];
357:          i = bis.read( b );
358:        }
359:
360:        String res = baos.toString();
361:        Hashtable<String,ColorMapping> mappings = colorTables.get( selectedImage ).colors;
362:        Enumeration<String> keys = mappings.keys();
363:        while ( keys.hasMoreElements()) {
364:          ColorMapping map = mappings.get( keys.nextElement());
365:          String srcClr = map.getOriginalColor();            
366:          String resClr = map.getColorizedColor();            
367:          if ( resClr == null )
368:            continue;
369:          
370:          res = res.replace( srcClr, resClr );
371:        }
372:        
373:        FileWriter fw = new FileWriter( colorizedFile );
374:        fw.write( res );
375:        fw.flush();
376:        fw.close();
377:      
378:        colorizedPainter.setImage( colorizedFile.toURL(), true );
379:        colorizedImage.repaint();
380:      }    
381:    } 
382:    catch (IOException ex) {
383:      ex.printStackTrace();
384:    }    
385:  }
386:  
387:  private void findColorInTree( String selectedColor )
388:  {
389:    TreeNode tn = (TreeNode)colorTree.getModel().getRoot();
390:    
391:    TreePath tp = processNodes( tn, new FindColor( selectedColor ));
392:    if ( tp != null ) {
393:      colorTree.setSelectionPath( tp );
394:      colorTree.expandPath( tp );
395:      colorTree.scrollPathToVisible( tp );
396:    }
397:  }
398:  
399:  private TreePath processNodes( TreeNode tn, ColorProcess cp )
400:  {
401:    if ( tn.getChildCount() >= 0 ) {
402:      for ( Enumeration e = tn.children(); e.hasMoreElements(); ) {
403:        TreeNode n = (TreeNode)e.nextElement();
404:        if ( n instanceof  XTreeModelAdapter ) {
405:          Object obj = ((XTreeModelAdapter)n).getModel().get();
406:          if ( obj instanceof  ColorMapping ) {
407:            ColorMapping colorMapping = (ColorMapping)obj;
408:            if ( cp.process( colorMapping ))
409:              return new TreePath(((DefaultMutableTreeNode)n).getPath());
410:            else {
411:              TreePath p = processNodes( n, cp );
412:              if ( p != null )
413:                return p;
414:            }
415:          }
416:        }
417:      }
418:    }
419:    
420:    return null;
421:  }
422:  
423:  private ColorMapping getSelectedColor( TreeNode tn )
424:  {
425:    if (( tn != null ) && ( tn instanceof  XTreeModelAdapter )) 
426:      return (ColorMapping)((XTreeModelAdapter)tn).getModel().get();
427:    
428:    return null;
429:  }
430:  
431:  public void showPrev()
432:  {    
433:    pageMgr.showPage( "Welcome" );
434:  }
435:
436:  public void colorTreePopup()
437:  {
438:    MouseEvent me = (MouseEvent)getCurrentEvent();
439:    if ( me.isPopupTrigger() ) {
440:      JPopupMenu popupMenu = new JPopupMenu( "Styles" );
441:      if ( colorTree.getSelectionPath() != null ){
442:        if ( colorTree.getSelectionPath().getPathCount() == 1 ) {
443:          JMenuItem mi = new JMenuItem( "Reset All" );
444:          mi.addActionListener( this  );
445:          popupMenu.add( mi );
446:
447:          mi = new JMenuItem( "Hue..." );
448:          mi.addActionListener( this  );
449:          popupMenu.add( mi );
450:
451:          mi = new JMenuItem( "Saturation..." );
452:          mi.addActionListener( this  );
453:          popupMenu.add( mi );
454:
455:          mi = new JMenuItem( "Brightness..." );
456:          mi.addActionListener( this  );
457:          popupMenu.add( mi );
458:        }
459:        else {
460:          JMenuItem mi = new JMenuItem( "Reset" );
461:          mi.addActionListener( this  );
462:          popupMenu.add( mi );
463:
464:          mi = new JMenuItem( "Set hue..." );
465:          mi.addActionListener( this  );
466:          popupMenu.add( mi );
467:
468://          popupMenu.addSeparator();
469://
470://          mi = new JMenuItem( "Delete" );
471://          //mi.addActionListener( this );
472://          popupMenu.add( mi );
473://
474://          popupMenu.addSeparator();
475://
476://          mi = new JMenuItem( "Color Scheme..." );
477://          //mi.addActionListener( this );
478://          popupMenu.add( mi );
479:        }
480:        
481:        Point pt = me.getPoint();
482:        Point vp = colorTree.getLocation();
483:        vp = SwingUtilities.convertPoint( (Component)me.getSource(), 0, 0, colorTree );
484:        popupMenu.show( colorTree, pt.x + vp.x, pt.y + vp.y );
485:      }
486:    }
487:  }
488:  
489:  public void actionPerformed( ActionEvent ae )
490:  {
491:    final String cmd = ae.getActionCommand();
492:    if ( cmd.equals( "Reset" )) {
493:      TreeNode tn = (TreeNode)colorTree.getModel().getRoot();
494:      processNodes( tn, new ResetColor());
495:      adjustColorizedImage();
496:      fillColorTree();
497:    }
498:    else if ( cmd.equals( "Reset All" )) {
499:      TreeNode tn = (TreeNode)colorTree.getSelectionPath().getLastPathComponent();
500:      processNodes( tn, new ResetColor());
501:      adjustColorizedImage();
502:      fillColorTree();
503:    }
504:    else if ( cmd.equals( "Set hue..." ) || cmd.equals( "Hue..." ) || cmd.equals( "Saturation..." ) || cmd.equals( "Brightness..." )) {
505:      final TreeNode tn = (TreeNode)colorTree.getSelectionPath().getLastPathComponent();
506:      boolean useRoot = true;
507:      if ( cmd.equals( "Set hue..." )) 
508:        useRoot = false;
509:      final ColorMapping cm = useRoot ? null : getSelectedColor( tn );
510:      if ( !useRoot && ( cm == null ))
511:        return;
512:      
513:      final JColorChooser colorChooser = new JColorChooser();
514:
515:      AbstractColorChooserPanel[] oldPanels = colorChooser.getChooserPanels();
516:      AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[ oldPanels.length + 1 ];
517:      System.arraycopy( oldPanels, 0, newPanels, 1, oldPanels.length );
518:      newPanels[ 0 ] = new ColorWheelPanel();
519:      colorChooser.setChooserPanels( newPanels );
520:
521:      //colorChooser.addChooserPanel( new ColorWheelPanel());
522:      final Color originalColor = useRoot ? Color.blue : cm.getColorized();
523:      colorChooser.setColor( originalColor );
524:      XEditorUtilities.setDefaultFont( colorChooser );
525:
526:      JDialog dialog = JColorChooser.createDialog(
527:        ImageViewer.this , 
528:        "Choose the new Color", 
529:        true,
530:        colorChooser, 
531:        new ActionListener() 
532:        {
533:          public void actionPerformed( ActionEvent e ) 
534:          {
535:            newColor = colorChooser.getColor();
536:            if ( cmd.equals( "Saturation..." ))
537:                colorProcessor = new SaturateColor( originalColor, newColor );
538:            else if ( cmd.equals( "Brightness..." ))
539:                colorProcessor = new BrightenColor( originalColor, newColor );
540:            else
541:                colorProcessor = new RotateColor( originalColor, newColor );
542:            processNodes( tn, colorProcessor );
543:            adjustColorizedImage();
544:            fillColorTree();
545:            
546:            writeAllColorizedImages();
547:            writeColorizedSynthFile();
548:          }
549:        }, 
550:        null );
551:      XuiUtilities.centerOnScreen( dialog );
552:      dialog.setVisible( true );   
553:    }
554:  }
555:
556:  public void openImage()
557:  {
558:    if ( wasMouseDoubleClicked()) {
559:      String svgEditorPath = XModelHelper.getString( rootModel, "svgEditor" );
560:      String selectedImage;
561:      Object selObj = imageList.getSelectedObject();
562:      if ( selObj != null ) 
563:        selectedImage = selObj.toString();
564:      else
565:        return;
566:      
567:      String sourceFolder = XModelHelper.getString( project, rootModel, "sourceFolder" );
568:      final String programName = svgEditorPath;
569:      final String fileName = sourceFolder + File.separator + selectedImage;
570:      
571:      SwingWorker worker = new SwingWorker() 
572:      {
573:        public Object construct() 
574:        {
575:          XuiUtilities.exec( new String[]{ "\"" + programName + "\"",  "\"" + fileName + "\"" });
576:          return "null";
577:        }
578:
579:        public void finished() 
580:        {
581:          SwingUtilities.invokeLater( new Runnable()    
582:          {
583:            public void run() 
584:            {
585:            }
586:          });    
587:        }
588:      };
589:      worker.start();    
590:    }
591:  }
592:  
593:  public void showNext()
594:  {    
595:    try {
596:      String outputFolder = XModelHelper.getString( project, rootModel, "outputFolder" );
597:      FileWriter fw = new FileWriter( new File( outputFolder + File.separator + "colors.xml" ));
598:      Writer writer = new BufferedWriter( fw );
599:      NanoXmlWriter xmlWriter = new NanoXmlWriter( writer );
600:
601:      XmlElement colorsElement = new NanoXmlElement( "Colors" );
602:
603:      Collection<ColorMapping> mappings = ColorTable.getCommonColors().values();
604:      for ( ColorMapping map : mappings ) {
605:        XmlElement mapElement = new NanoXmlElement( "Color" );
606:        mapElement.setAttribute( "name", map.getName());
607:        mapElement.setAttribute( "original", map.getOriginalColor());
608:        mapElement.setAttribute( "colorized", map.getColorizedColor() );
609:        colorsElement.addChild( mapElement );
610:      }
611:
612:      xmlWriter.write( colorsElement, true, 4 );
613:      writer.flush();
614:      writer.close();
615:      fw.close();
616:    }
617:    catch ( IOException ex ) {
618:      System.out.println( "Error generating file" );
619:    }
620:    
621:    // Now try to launch the sample application
622:    try {
623:      final String path = new File( project.findResourceAsString( "startup.properties" )).getParent();
624:      
625:      SwingWorker worker = new SwingWorker() 
626:      {
627:        public Object construct() 
628:        {
629:          try {
630:      /** @todo replace the output path with the ones from page 1 */      
631:            String cmd = "java -cp " +
632:                          path + "/SwingSet2/resources;" +
633://                          "C:/svn/xui/branches/xui3/build/classes1.5debug;" +
634:                          getJarPath( "XuiCoreSwing.jar" ) + ";" +
635:                          getJarPath( "XuiOptional.jar" ) + ";" +
636:                          path + "/svg/colorized;" +
637:                          path + "/svg/output;" +
638:                          path + "/svg;" +
639:                          path + "/sampleApp;" +
640:                          path + "/SwingSet2/build/classes;" +
641:                          path + ";" +
642:                          path + "/SwingSet2/dist/SwingSet2.jar;" +
643:                          path + "/SwingSet2/resources;" +
644:                          path + "/SwingSet2/add_to_jar;" +
645:                          getJarPath( "svgSalamander.jar" ) +
646:                          " SwingSet2";
647:            
648:            cmd = cmd.replace( "\\", "/" );
649:            DebugLogger.trace( "Launching: " + cmd );
650:            Process proc = Runtime.getRuntime().exec( cmd );
651://            InputStream is = proc.getInputStream();
652://            InputStreamReader isr = new InputStreamReader( is );
653://            BufferedReader br = new BufferedReader( isr );
654://            String line;
655://
656://            while ((line = br.readLine()) != null) {
657://              DebugLogger.trace( line );
658://            }
659:          } 
660:          catch ( Exception ex ) {
661:            ex.printStackTrace();
662:          }
663:
664:          return null;
665:        }
666:
667:        public void finished() 
668:        {
669:        }
670:      };
671:      worker.start();
672:    } 
673:    catch ( Exception e ) {
674:      e.printStackTrace();
675:    }
676:  }
677:  
678:  private String getJarPath( String jar )
679:  {
680:    String urlStr = project.findResourceAsString( jar );
681:    return urlStr;
682:  }
683:
684:  private void writeAllColorizedImages()
685:  {
686:    String initialImage = selectedImage;
687:
688:    Collection<ColorTable> values = colorTables.values();
689:    for ( ColorTable ct : values ) {
690:      selectedImage = ct.name;
691:      adjustColorizedImage();
692:    }
693:    
694:    // Reset the selected Image
695:    selectedImage = initialImage;
696:    adjustColorizedImage();
697:  }
698:  
699:  private void writeColorizedSynthFile()
700:  {
701:    try {
702:      //String outputFolder = XModelHelper.getString( project, rootModel, "colorizedFolder" );
703:      String outputFolder = project.getStartupParam( "ProjectPath" );
704:      outputFolder += "/resources/SwingSet2/resources";
705:      FileWriter fw = new FileWriter( new File( outputFolder + File.separator + "xui.synth" ));
706:      Writer writer = new BufferedWriter( fw );
707:      NanoXmlWriter xmlWriter = new NanoXmlWriter( writer );
708:
709:      SynthModel synthModel = (SynthModel)((XModel)rootModel.get( "SynthModel" )).get();
710:      XmlElement synthElement = synthModel.getXml();
711:      XmlElement clone = (XmlElement)synthElement.clone();
712:      rotateColors( clone );
713:      
714:      xmlWriter.write( clone, true, 4 );
715:      writer.flush();
716:      writer.close();
717:      fw.close();
718:      
719:      return;
720:    }
721:    catch ( IOException ex ) {
722:      System.out.println( "Error generating file" );
723:    }
724:  }
725:  
726:  private void rotateColors( XmlElement clone )
727:  {
728:    if ( "color".equals( clone.getName()) && ( colorProcessor != null )) {
729:      String selectedColor = clone.getAttribute( "value" );
730:      // Remove the #
731:      if ( selectedColor.startsWith( "#" ))
732:        selectedColor = selectedColor.substring( 1 );
733:      Color clr = (Color)XStyle.parseColor( selectedColor );
734:      clone.setAttribute( "value",  "#" + XuiUtilities.colorToHexString( colorProcessor.process( clr )));
735:    }
736:    
737:    Vector children = clone.getChildren();
738:    int numChildren = children.size();
739:    for ( int i = 0; i < numChildren; i++ ) 
740:      rotateColors( (XmlElement)children.elementAt( i ));
741:  }  
742:}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.