Back to index

wims  3.65+svn20090927
FileMolPreview.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem;
00012 
00013 import java.util.*;
00014 import java.text.*;
00015 import java.lang.*;
00016 import java.io.*;
00017 import javax.swing.*;
00018 import java.beans.*;
00019 import java.awt.*;
00020 
00021 // Previewing molecule-type files within the file choose mechanism.
00022 
00023 public class FileMolPreview extends EditorPane implements PropertyChangeListener
00024 {
00025     ImageIcon thumbnail=null;
00026     File file=null;
00027 
00028     public FileMolPreview(JFileChooser fc)
00029     {
00030        super(200,200);
00031         fc.addPropertyChangeListener(this);
00032        setBackground(Color.WHITE);
00033        SetBorder(true);
00034        SetToolCursor();
00035        SetEditable(false);
00036     }
00037 
00038     public void propertyChange(PropertyChangeEvent ev) 
00039     {
00040         boolean update=false;
00041         String prop=ev.getPropertyName();
00042 
00043         if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) // changed directory, do nothing much
00044        {
00045             file=null;
00046             update=true;
00047        }
00048         else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) // file just got selected
00049        {
00050             file=(File)ev.getNewValue();
00051             update=true;
00052         }
00053 
00054         if (update) 
00055        {
00056             thumbnail=null;
00057            Molecule mol=null;
00058            if (file!=null && file.isFile())
00059            {
00060               try
00061               {
00062                   FileInputStream istr=new FileInputStream(file);
00063                   mol=MoleculeStream.ReadUnknown(istr);
00064                   istr.close();
00065               }
00066               catch (IOException e) 
00067               {
00068                   mol=null;
00069               }
00070            }
00071            if (mol==null) mol=new Molecule();
00072            Replace(mol);
00073            ScaleToFit();
00074             if (isShowing()) repaint();
00075         }
00076     }
00077     
00078     protected void paintComponent(Graphics g) 
00079     {
00080        Dimension sz=getSize();
00081        int width=(int)sz.getWidth(),height=(int)sz.getHeight();
00082        g.setColor(Color.WHITE);
00083        g.fillRect(0,0,width,height);
00084        g.setColor(Color.BLACK);
00085        g.drawRect(0,0,width,height);
00086        
00087        super.paintComponent(g);
00088     }
00089 }