Back to index

wims  3.65+svn20090927
DialogEdit.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.awt.*;
00016 import java.awt.geom.*;
00017 import java.awt.event.*;
00018 import javax.swing.*;
00019 import javax.swing.event.*;
00020 
00021 /*
00022     A dialog box which allows tabulated editing of molecular data, as an alternative to doing all editing graphically.
00023 */
00024 
00025 public class DialogEdit extends JDialog implements ActionListener
00026 {
00027     Molecule mol,retMol=null;
00028     ArrayList<Integer> aselidx,bselidx;
00029     
00030     JTabbedPane tabs;
00031     JButton accept,reject;
00032     JTable atoms,bonds;
00033 
00034     static final String BOND_TYPES[]={"Normal","Inclined","Declined","Unknown"};
00035 
00036     public DialogEdit(Frame Parent,Molecule Mol,ArrayList<Integer> SelIdx)
00037     {
00038        super(Parent,"Edit Molecule",true);
00039        mol=Mol.Clone();
00040        aselidx=SelIdx;
00041        bselidx=new ArrayList<Integer>();
00042        for (int n=1;n<=mol.NumBonds();n++) if (aselidx.indexOf(mol.BondFrom(n))>=0 && aselidx.indexOf(mol.BondTo(n))>=0) bselidx.add(n);
00043        
00044        setLayout(new BorderLayout());
00045        
00046        atoms=new JTable(CompileAtomData(),new String[]{"#","El","X","Y","Charge","Unpaired","HExplicit","MapNum"})
00047            {public boolean isCellEditable(int row,int column) {return column>0;}};
00048        bonds=new JTable(CompileBondData(),new String[]{"#","From","To","Order","Type"})
00049            {public boolean isCellEditable(int row,int column) {return column>2;}};
00050 
00051        atoms.getColumnModel().getColumn(0).setCellEditor(null);
00052        JComboBox bondTypes=new JComboBox();
00053        for (int n=0;n<BOND_TYPES.length;n++) bondTypes.addItem(BOND_TYPES[n]);
00054        bonds.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(bondTypes));
00055 
00056        JPanel tabAtoms=new JPanel(),tabBonds=new JPanel();
00057        tabAtoms.setLayout(new BorderLayout());
00058        tabBonds.setLayout(new BorderLayout());
00059 
00060        atoms.setPreferredScrollableViewportSize(new Dimension(350,200));
00061        bonds.setPreferredScrollableViewportSize(new Dimension(350,200));
00062 
00063        tabAtoms.add(new JScrollPane(atoms));
00064        tabBonds.add(new JScrollPane(bonds));
00065 
00066        tabs=new JTabbedPane();
00067        tabs.addTab("Atoms",tabAtoms);
00068        tabs.addTab("Bonds",tabBonds);
00069        add(tabs,BorderLayout.CENTER);
00070        
00071        JPanel buttons=new JPanel();
00072        buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
00073        accept=new JButton("Accept"); accept.addActionListener(this); accept.setMnemonic(KeyEvent.VK_A);
00074        reject=new JButton("Reject"); reject.addActionListener(this); reject.setMnemonic(KeyEvent.VK_R);
00075        buttons.add(accept);
00076        buttons.add(reject);
00077        add(buttons,BorderLayout.SOUTH);
00078        
00079        pack();
00080     }
00081     
00082     public Molecule exec()
00083     {
00084        setVisible(true);
00085        return retMol;
00086     }
00087     
00088     public void actionPerformed(ActionEvent e)
00089     {
00090        if (e.getSource()==accept) 
00091        {
00092            if (!ReadData()) return;
00093            retMol=mol; 
00094            setVisible(false);
00095        }
00096        if (e.getSource()==reject) setVisible(false);
00097     }
00098     
00099     Object[][] CompileAtomData()
00100     {
00101        Object[][] data=new Object[aselidx.size()][];
00102        
00103        DecimalFormat fmt=new DecimalFormat("0.0000");
00104 
00105        for (int n=0;n<aselidx.size();n++)
00106        {
00107            int i=aselidx.get(n).intValue();
00108            Object[] da=new Object[8];
00109            da[0]=new Integer(i);
00110            da[1]=new String(mol.AtomElement(i));
00111            da[2]=fmt.format(mol.AtomX(i));
00112            da[3]=fmt.format(mol.AtomY(i));
00113            da[4]=String.valueOf(mol.AtomCharge(i));
00114            da[5]=String.valueOf(mol.AtomUnpaired(i));
00115            da[6]=mol.AtomHExplicit(i)==Molecule.HEXPLICIT_UNKNOWN ? "?" : String.valueOf(mol.AtomHExplicit(i));
00116            da[7]=String.valueOf(mol.AtomMapNum(i));
00117            data[n]=da;
00118        }
00119        
00120        return data;
00121     }
00122 
00123     Object[][] CompileBondData()
00124     {
00125        Object[][] data=new Object[bselidx.size()][];
00126        
00127        for (int n=0;n<bselidx.size();n++)
00128        {
00129            int i=bselidx.get(n).intValue();
00130            Object[] db=new Object[5];
00131            db[0]=new Integer(i);
00132            db[1]=new Integer(mol.BondFrom(i));
00133            db[2]=new Integer(mol.BondTo(i));
00134            db[3]=String.valueOf(mol.BondOrder(i));
00135            db[4]=new String(BOND_TYPES[mol.BondType(i)]);
00136            data[n]=db;
00137        }
00138        
00139        return data;
00140     }
00141     
00142     boolean ReadData()
00143     {
00144        for (int n=0;n<atoms.getRowCount();n++)
00145        {
00146            int i=(Integer)atoms.getValueAt(n,0);
00147            mol.SetAtomElement(i,(String)atoms.getValueAt(n,1));
00148            mol.SetAtomPos(i,Utils.safeDouble((String)atoms.getValueAt(n,2)),Utils.safeDouble((String)atoms.getValueAt(n,3)));
00149            
00150            mol.SetAtomCharge(i,Utils.safeInt((String)atoms.getValueAt(n,4)));
00151            mol.SetAtomUnpaired(i,Utils.safeInt((String)atoms.getValueAt(n,5)));
00152            String hyStr=(String)atoms.getValueAt(n,6);
00153            int hy=Utils.safeInt(hyStr);
00154            mol.SetAtomHExplicit(i,hyStr.compareTo("0")==0 ? 0 : hy>0 ? hy : Molecule.HEXPLICIT_UNKNOWN);
00155            mol.SetAtomMapNum(i,Utils.safeInt((String)atoms.getValueAt(n,7)));
00156        }
00157        for (int n=0;n<bonds.getRowCount();n++)
00158        {
00159            int i=(Integer)bonds.getValueAt(n,0);
00160            mol.SetBondOrder(i,new Integer((String)bonds.getValueAt(n,3)).intValue());
00161            int type;
00162            for (int j=BOND_TYPES.length-1;j>=0;j--) if (BOND_TYPES[j].compareTo((String)bonds.getValueAt(n,4))==0) 
00163               {mol.SetBondType(i,j); break;}
00164        }
00165        return true;
00166     }
00167 }