Subversion Repositories wimsdev

Rev

Rev 7246 | 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.awt.*;
  16. import java.awt.geom.*;
  17. import java.awt.event.*;
  18. import javax.swing.*;
  19. import javax.swing.event.*;
  20.  
  21. /*
  22.     A dialog box which allows tabulated editing of molecular data, as an alternative to doing all editing graphically.
  23. */
  24.  
  25. public class DialogEdit extends JDialog implements ActionListener
  26. {
  27.     Molecule mol,retMol=null;
  28.     ArrayList<Integer> aselidx,bselidx;
  29.    
  30.     JTabbedPane tabs;
  31.     JButton accept,reject;
  32.     JTable atoms,bonds;
  33.  
  34.     static final String BOND_TYPES[]={"Normal","Inclined","Declined","Unknown"};
  35.  
  36.     public DialogEdit(Frame Parent,Molecule Mol,ArrayList<Integer> SelIdx)
  37.     {
  38.         super(Parent,"Edit Molecule",true);
  39.         mol=Mol.clone();
  40.         aselidx=SelIdx;
  41.         bselidx=new ArrayList<Integer>();
  42.         for (int n=1;n<=mol.numBonds();n++) if (aselidx.indexOf(mol.bondFrom(n))>=0 && aselidx.indexOf(mol.bondTo(n))>=0) bselidx.add(n);
  43.        
  44.         setLayout(new BorderLayout());
  45.        
  46.         atoms=new JTable(compileAtomData(),new String[]{"#","El","X","Y","Charge","Unpaired","HExplicit","MapNum"})
  47.             {public boolean isCellEditable(int row,int column) {return column>0;}};
  48.         bonds=new JTable(compileBondData(),new String[]{"#","From","To","Order","Type"})
  49.             {public boolean isCellEditable(int row,int column) {return column>2;}};
  50.  
  51.         atoms.getColumnModel().getColumn(0).setCellEditor(null);
  52.         JComboBox bondTypes=new JComboBox();
  53.         for (int n=0;n<BOND_TYPES.length;n++) bondTypes.addItem(BOND_TYPES[n]);
  54.         bonds.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(bondTypes));
  55.  
  56.         JPanel tabAtoms=new JPanel(),tabBonds=new JPanel();
  57.         tabAtoms.setLayout(new BorderLayout());
  58.         tabBonds.setLayout(new BorderLayout());
  59.  
  60.         atoms.setPreferredScrollableViewportSize(new Dimension(350,200));
  61.         bonds.setPreferredScrollableViewportSize(new Dimension(350,200));
  62.  
  63.         tabAtoms.add(new JScrollPane(atoms));
  64.         tabBonds.add(new JScrollPane(bonds));
  65.  
  66.         tabs=new JTabbedPane();
  67.         tabs.addTab("Atoms",tabAtoms);
  68.         tabs.addTab("Bonds",tabBonds);
  69.         add(tabs,BorderLayout.CENTER);
  70.        
  71.         JPanel buttons=new JPanel();
  72.         buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
  73.         accept=new JButton("Accept"); accept.addActionListener(this); accept.setMnemonic(KeyEvent.VK_A);
  74.         reject=new JButton("Reject"); reject.addActionListener(this); reject.setMnemonic(KeyEvent.VK_R);
  75.         buttons.add(accept);
  76.         buttons.add(reject);
  77.         add(buttons,BorderLayout.SOUTH);
  78.        
  79.         pack();
  80.     }
  81.    
  82.     public Molecule exec()
  83.     {
  84.         setVisible(true);
  85.         return retMol;
  86.     }
  87.    
  88.     public void actionPerformed(ActionEvent e)
  89.     {
  90.         if (e.getSource()==accept)
  91.         {
  92.             if (!ReadData()) return;
  93.             retMol=mol;
  94.             setVisible(false);
  95.         }
  96.         if (e.getSource()==reject) setVisible(false);
  97.     }
  98.    
  99.     private Object[][] compileAtomData()
  100.     {
  101.         Object[][] data=new Object[aselidx.size()][];
  102.        
  103.         DecimalFormat fmt = new DecimalFormat("0.0000",new DecimalFormatSymbols(Locale.US));
  104.  
  105.         for (int n=0;n<aselidx.size();n++)
  106.         {
  107.             int i=aselidx.get(n).intValue();
  108.             Object[] da=new Object[8];
  109.             da[0]=new Integer(i);
  110.             da[1]=new String(mol.atomElement(i));
  111.             da[2]=fmt.format(mol.atomX(i));
  112.             da[3]=fmt.format(mol.atomY(i));
  113.             da[4]=String.valueOf(mol.atomCharge(i));
  114.             da[5]=String.valueOf(mol.atomUnpaired(i));
  115.             da[6]=mol.atomHExplicit(i)==Molecule.HEXPLICIT_UNKNOWN ? "?" : String.valueOf(mol.atomHExplicit(i));
  116.             da[7]=String.valueOf(mol.atomMapNum(i));
  117.             data[n]=da;
  118.         }
  119.        
  120.         return data;
  121.     }
  122.  
  123.     private Object[][] compileBondData()
  124.     {
  125.         Object[][] data=new Object[bselidx.size()][];
  126.        
  127.         for (int n=0;n<bselidx.size();n++)
  128.         {
  129.             int i=bselidx.get(n).intValue();
  130.             Object[] db=new Object[5];
  131.             db[0]=new Integer(i);
  132.             db[1]=new Integer(mol.bondFrom(i));
  133.             db[2]=new Integer(mol.bondTo(i));
  134.             db[3]=String.valueOf(mol.bondOrder(i));
  135.             db[4]=new String(BOND_TYPES[mol.bondType(i)]);
  136.             data[n]=db;
  137.         }
  138.        
  139.         return data;
  140.     }
  141.    
  142.     private boolean ReadData()
  143.     {
  144.         for (int n=0;n<atoms.getRowCount();n++)
  145.         {
  146.             int i=(Integer)atoms.getValueAt(n,0);
  147.             mol.setAtomElement(i,(String)atoms.getValueAt(n,1));
  148.             mol.setAtomPos(i,Util.safeDouble((String)atoms.getValueAt(n,2)),Util.safeDouble((String)atoms.getValueAt(n,3)));
  149.            
  150.             mol.setAtomCharge(i,Util.safeInt((String)atoms.getValueAt(n,4)));
  151.             mol.setAtomUnpaired(i,Util.safeInt((String)atoms.getValueAt(n,5)));
  152.             String hyStr=(String)atoms.getValueAt(n,6);
  153.             int hy=Util.safeInt(hyStr);
  154.             mol.setAtomHExplicit(i,hyStr.compareTo("0")==0 ? 0 : hy>0 ? hy : Molecule.HEXPLICIT_UNKNOWN);
  155.             mol.setAtomMapNum(i,Util.safeInt((String)atoms.getValueAt(n,7)));
  156.         }
  157.         for (int n=0;n<bonds.getRowCount();n++)
  158.         {
  159.             int i=(Integer)bonds.getValueAt(n,0);
  160.             mol.setBondOrder(i,new Integer((String)bonds.getValueAt(n,3)).intValue());
  161.             int type;
  162.             for (int j=BOND_TYPES.length-1;j>=0;j--) if (BOND_TYPES[j].compareTo((String)bonds.getValueAt(n,4))==0)
  163.                 {mol.setBondType(i,j); break;}
  164.         }
  165.         return true;
  166.     }
  167. }
  168.