Subversion Repositories wimsdev

Rev

Rev 3662 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     Sketch Elements: Chemistry molecular diagram drawing tool.
  3.    
  4.     (c) 2005 Dr. Alex M. Clark
  5.    
  6.     Released as GNUware, under the Gnu Public License (GPL)
  7.    
  8.     See www.gnu.org for details.
  9. */
  10.  
  11. package WIMSchem;
  12.  
  13. import java.util.*;
  14. import java.text.*;
  15. import java.lang.*;
  16. import java.io.*;
  17. import javax.swing.*;
  18. import java.beans.*;
  19. import java.awt.*;
  20.  
  21. // Previewing molecule-type files within the file choose mechanism.
  22.  
  23. public class FileMolPreview extends EditorPane implements PropertyChangeListener
  24. {
  25.     ImageIcon thumbnail=null;
  26.     File file=null;
  27.  
  28.     public FileMolPreview(JFileChooser fc)
  29.     {
  30.         super(200,200);
  31.         fc.addPropertyChangeListener(this);
  32.         setBackground(Color.WHITE);
  33.         setBorder(true);
  34.         setToolCursor();
  35.         setEditable(false);
  36.     }
  37.  
  38.     public void propertyChange(PropertyChangeEvent ev)
  39.     {
  40.         boolean update=false;
  41.         String prop=ev.getPropertyName();
  42.  
  43.         if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) // changed directory, do nothing much
  44.         {
  45.             file=null;
  46.             update=true;
  47.         }
  48.         else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) // file just got selected
  49.         {
  50.             file=(File)ev.getNewValue();
  51.             update=true;
  52.         }
  53.  
  54.         if (update)
  55.         {
  56.             thumbnail=null;
  57.             Molecule mol=null;
  58.             if (file!=null && file.isFile())
  59.             {
  60.                 try
  61.                 {
  62.                     FileInputStream istr=new FileInputStream(file);
  63.                     mol=MoleculeStream.readUnknown(istr);
  64.                     istr.close();
  65.                 }
  66.                 catch (IOException e)
  67.                 {
  68.                     mol=null;
  69.                 }
  70.             }
  71.             if (mol==null) mol=new Molecule();
  72.             replace(mol);
  73.             scaleToFit();
  74.             if (isShowing()) repaint();
  75.         }
  76.     }
  77.    
  78.     protected void paintComponent(Graphics g)
  79.     {
  80.         Dimension sz=getSize();
  81.         int width=(int)sz.getWidth(),height=(int)sz.getHeight();
  82.         g.setColor(Color.WHITE);
  83.         g.fillRect(0,0,width,height);
  84.         g.setColor(Color.BLACK);
  85.         g.drawRect(0,0,width,height);
  86.        
  87.         super.paintComponent(g);
  88.     }
  89. }
  90.