/*
Sketch Elements: Chemistry molecular diagram drawing tool.
(c) 2005 Dr. Alex M. Clark
Released as GNUware, under the Gnu Public License (GPL)
See www.gnu.org for details.
*/
package WIMSchem;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/*
A dialog box which allows tabulated editing of molecular data, as an alternative to doing all editing graphically.
*/
{
Molecule mol,retMol=null;
static final String BOND_TYPES
[]=
{"Normal",
"Inclined",
"Declined",
"Unknown"};
{
super(Parent,"Edit Molecule",true);
mol=Mol.clone();
aselidx=SelIdx;
for (int n=1;n<=mol.numBonds();n++) if (aselidx.indexOf(mol.bondFrom(n))>=0 && aselidx.indexOf(mol.bondTo(n))>=0) bselidx.add(n);
atoms=
new JTable(compileAtomData
(),
new String[]{"#",
"El",
"X",
"Y",
"Charge",
"Unpaired",
"HExplicit",
"MapNum"})
{public boolean isCellEditable(int row,int column) {return column>0;}};
bonds=
new JTable(compileBondData
(),
new String[]{"#",
"From",
"To",
"Order",
"Type"})
{public boolean isCellEditable(int row,int column) {return column>2;}};
atoms.getColumnModel().getColumn(0).setCellEditor(null);
for (int n=0;n<BOND_TYPES.length;n++) bondTypes.addItem(BOND_TYPES[n]);
atoms.
setPreferredScrollableViewportSize(new Dimension(350,
200));
bonds.
setPreferredScrollableViewportSize(new Dimension(350,
200));
tabs.addTab("Atoms",tabAtoms);
tabs.addTab("Bonds",tabBonds);
accept=
new JButton("Accept"); accept.
addActionListener(this); accept.
setMnemonic(KeyEvent.
VK_A);
reject=
new JButton("Reject"); reject.
addActionListener(this); reject.
setMnemonic(KeyEvent.
VK_R);
buttons.add(accept);
buttons.add(reject);
pack();
}
public Molecule exec()
{
setVisible(true);
return retMol;
}
{
if (e.getSource()==accept)
{
if (!ReadData()) return;
retMol=mol;
setVisible(false);
}
if (e.getSource()==reject) setVisible(false);
}
private Object[][] compileAtomData
()
{
for (int n=0;n<aselidx.size();n++)
{
int i=aselidx.get(n).intValue();
da
[1]=
new String(mol.
atomElement(i
));
da[2]=fmt.format(mol.atomX(i));
da[3]=fmt.format(mol.atomY(i));
da
[4]=
String.
valueOf(mol.
atomCharge(i
));
da
[5]=
String.
valueOf(mol.
atomUnpaired(i
));
da
[6]=mol.
atomHExplicit(i
)==Molecule.
HEXPLICIT_UNKNOWN ? "?" :
String.
valueOf(mol.
atomHExplicit(i
));
da
[7]=
String.
valueOf(mol.
atomMapNum(i
));
data[n]=da;
}
return data;
}
private Object[][] compileBondData
()
{
for (int n=0;n<bselidx.size();n++)
{
int i=bselidx.get(n).intValue();
db
[1]=
new Integer(mol.
bondFrom(i
));
db
[3]=
String.
valueOf(mol.
bondOrder(i
));
db
[4]=
new String(BOND_TYPES
[mol.
bondType(i
)]);
data[n]=db;
}
return data;
}
private boolean ReadData()
{
for (int n=0;n<atoms.getRowCount();n++)
{
int i=
(Integer)atoms.
getValueAt(n,
0);
mol.
setAtomElement(i,
(String)atoms.
getValueAt(n,
1));
mol.
setAtomPos(i,
Util.
safeDouble((String)atoms.
getValueAt(n,
2)),
Util.
safeDouble((String)atoms.
getValueAt(n,
3)));
mol.
setAtomCharge(i,
Util.
safeInt((String)atoms.
getValueAt(n,
4)));
mol.
setAtomUnpaired(i,
Util.
safeInt((String)atoms.
getValueAt(n,
5)));
int hy=
Util.
safeInt(hyStr
);
mol.setAtomHExplicit(i,hyStr.compareTo("0")==0 ? 0 : hy>0 ? hy : Molecule.HEXPLICIT_UNKNOWN);
mol.
setAtomMapNum(i,
Util.
safeInt((String)atoms.
getValueAt(n,
7)));
}
for (int n=0;n<bonds.getRowCount();n++)
{
int i=
(Integer)bonds.
getValueAt(n,
0);
mol.
setBondOrder(i,
new Integer((String)bonds.
getValueAt(n,
3)).
intValue());
int type;
for (int j=BOND_TYPES.
length-
1;j
>=
0;j--
) if (BOND_TYPES
[j
].
compareTo((String)bonds.
getValueAt(n,
4))==
0)
{mol.setBondType(i,j); break;}
}
return true;
}
}