| 1 |
package org.guha.rcdk.view; |
| 2 |
|
| 3 |
import org.guha.rcdk.util.Misc; |
| 4 |
import org.guha.rcdk.view.panels.MoleculeCell; |
| 5 |
import org.guha.rcdk.view.table.StructureTableCellEditor2D; |
| 6 |
import org.guha.rcdk.view.table.StructureTableCellRenderer2D; |
| 7 |
import org.guha.rcdk.view.table.StructureTableModel; |
| 8 |
import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector; |
| 9 |
import org.openscience.cdk.exception.CDKException; |
| 10 |
import org.openscience.cdk.interfaces.IAtomContainer; |
| 11 |
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator; |
| 12 |
|
| 13 |
import javax.swing.*; |
| 14 |
import javax.swing.table.DefaultTableCellRenderer; |
| 15 |
import javax.swing.table.TableColumn; |
| 16 |
import java.awt.*; |
| 17 |
import java.io.IOException; |
| 18 |
|
| 19 |
|
| 20 |
class RowLabelRenderer extends DefaultTableCellRenderer { |
| 21 |
public RowLabelRenderer() { |
| 22 |
super(); |
| 23 |
setHorizontalAlignment(javax.swing.SwingConstants.CENTER); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
class StructureTable2D { |
| 28 |
|
| 29 |
private IAtomContainer[] v; |
| 30 |
RcdkDepictor depictor; |
| 31 |
private int cellx = 200; |
| 32 |
private int celly = 200; |
| 33 |
private int ncol = 4; // excludes the 1st column for row numbers |
| 34 |
|
| 35 |
public StructureTable2D(IAtomContainer[] structs) { |
| 36 |
this.v = structs; |
| 37 |
} |
| 38 |
|
| 39 |
public StructureTable2D(IAtomContainer[] structs, int ncol, RcdkDepictor depictor) { |
| 40 |
this.v = structs; |
| 41 |
this.ncol = ncol; |
| 42 |
this.depictor = depictor; |
| 43 |
} |
| 44 |
|
| 45 |
public StructureTable2D(IAtomContainer[] structs, int ncol, int cellx, int celly, RcdkDepictor depictor) { |
| 46 |
this.v = structs; |
| 47 |
this.ncol = ncol; |
| 48 |
this.cellx = cellx; |
| 49 |
this.celly = celly; |
| 50 |
this.depictor = depictor; |
| 51 |
} |
| 52 |
|
| 53 |
public void display() throws IOException, CDKException { |
| 54 |
|
| 55 |
int i = 0; |
| 56 |
int j = 0; |
| 57 |
int pad = 10; |
| 58 |
|
| 59 |
Object[][] ndata; |
| 60 |
String[] nm = new String[this.ncol + 1]; |
| 61 |
|
| 62 |
int extra = v.length % this.ncol; |
| 63 |
int block = v.length - extra; |
| 64 |
int nrow = block / this.ncol; |
| 65 |
|
| 66 |
if (extra == 0) { |
| 67 |
ndata = new Object[nrow][this.ncol + 1]; |
| 68 |
} else { |
| 69 |
ndata = new Object[nrow + 1][this.ncol + 1]; |
| 70 |
} |
| 71 |
|
| 72 |
int cnt = 0; |
| 73 |
for (i = 0; i < nrow; i++) { |
| 74 |
for (j = 1; j < this.ncol + 1; j++) { |
| 75 |
ndata[i][j] = new MoleculeCell(v[cnt], depictor); |
| 76 |
cnt += 1; |
| 77 |
} |
| 78 |
} |
| 79 |
j = 1; |
| 80 |
while (cnt < v.length) { |
| 81 |
ndata[nrow][j] = new MoleculeCell(v[cnt], depictor); |
| 82 |
cnt += 1; |
| 83 |
j += 1; |
| 84 |
} |
| 85 |
|
| 86 |
if (extra != 0) nrow += 1; |
| 87 |
|
| 88 |
for (i = 0; i < nrow; i++) { |
| 89 |
ndata[i][0] = i * this.ncol + 1; |
| 90 |
} |
| 91 |
|
| 92 |
JFrame frame = new JFrame("2D Structure Grid"); |
| 93 |
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 94 |
|
| 95 |
JTable mtable = new JTable(new StructureTableModel(ndata, nm)); |
| 96 |
mtable.setShowGrid(true); |
| 97 |
|
| 98 |
// set row heights |
| 99 |
for (i = 0; i < nrow; i++) { |
| 100 |
mtable.setRowHeight(i, this.celly); |
| 101 |
} |
| 102 |
|
| 103 |
// disallow cell selections |
| 104 |
mtable.setColumnSelectionAllowed(false); |
| 105 |
mtable.setRowSelectionAllowed(false); |
| 106 |
|
| 107 |
// set the TableCellRenderer for the all columns |
| 108 |
// we also set up a TableCellEditor so that events on a render2dPanel |
| 109 |
// cell get forwarded to the actual render2dPanel. Right now this does nothing |
| 110 |
TableColumn col = mtable.getColumnModel().getColumn(0); |
| 111 |
col.setCellRenderer(new RowLabelRenderer()); |
| 112 |
for (i = 1; i < this.ncol + 1; i++) { |
| 113 |
col = mtable.getColumnModel().getColumn(i); |
| 114 |
col.setCellRenderer(new StructureTableCellRenderer2D()); |
| 115 |
col.setCellEditor(new StructureTableCellEditor2D()); |
| 116 |
} |
| 117 |
|
| 118 |
// set up scroll bars |
| 119 |
JScrollPane scrollpane = new JScrollPane(mtable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 120 |
if (nrow > 3) { |
| 121 |
mtable.setPreferredScrollableViewportSize(new Dimension(this.ncol * this.cellx + pad, 3 * this.celly + pad)); |
| 122 |
} else { |
| 123 |
mtable.setPreferredScrollableViewportSize(new Dimension(this.ncol * this.cellx + pad, nrow * this.celly + pad)); |
| 124 |
} |
| 125 |
frame.getContentPane().add(scrollpane); |
| 126 |
|
| 127 |
// start the show! |
| 128 |
frame.pack(); |
| 129 |
if (nrow > 3) { |
| 130 |
frame.setSize(this.ncol * this.cellx + pad, 3 * this.celly + pad); |
| 131 |
} else { |
| 132 |
frame.setSize(this.ncol * this.cellx + pad, nrow * this.celly + pad); |
| 133 |
} |
| 134 |
|
| 135 |
frame.setVisible(true); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public class ViewMolecule2DTable { |
| 140 |
public ViewMolecule2DTable(IAtomContainer[] molecules, int ncol, int cellx, int celly, RcdkDepictor depictor) throws IOException { |
| 141 |
|
| 142 |
if (depictor == null) |
| 143 |
depictor = Misc.getDefaultDepictor(); |
| 144 |
|
| 145 |
// set some default values |
| 146 |
boolean showH = false; |
| 147 |
|
| 148 |
try { |
| 149 |
IAtomContainer[] v = new IAtomContainer[molecules.length]; |
| 150 |
for (int i = 0; i < v.length; i++) { |
| 151 |
CDKHueckelAromaticityDetector.detectAromaticity(molecules[i]); |
| 152 |
v[i] = AtomContainerManipulator.removeHydrogens(molecules[i]); |
| 153 |
v[i] = Misc.getMoleculeWithCoordinates(v[i]); |
| 154 |
} |
| 155 |
|
| 156 |
// some checks for visual niceness |
| 157 |
if (v.length < ncol) { |
| 158 |
StructureTable2D st = new StructureTable2D(v, v.length, cellx, celly, depictor); |
| 159 |
st.display(); |
| 160 |
} else { |
| 161 |
StructureTable2D st = new StructureTable2D(v, ncol, cellx, celly, depictor); |
| 162 |
st.display(); |
| 163 |
} |
| 164 |
|
| 165 |
} catch (Exception e) { |
| 166 |
System.out.println(e); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public static void main(String[] args) throws IOException { |
| 171 |
String home = "/Users/rguha/"; |
| 172 |
String[] fname = {home + "src/R/trunk/rcdk/data/dan001.sdf", |
| 173 |
home + "src/R/trunk/rcdk/data/dan002.sdf", |
| 174 |
home + "src/R/trunk/rcdk/data/dan003.sdf"}; |
| 175 |
IAtomContainer[] acs = null; |
| 176 |
try { |
| 177 |
acs = Misc.loadMolecules(fname, true, true, true); |
| 178 |
} catch (CDKException e) { |
| 179 |
e.printStackTrace(); |
| 180 |
} catch (IOException e) { |
| 181 |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| 182 |
} |
| 183 |
|
| 184 |
try { |
| 185 |
acs = Misc.loadMolecules(fname, true, false, true); |
| 186 |
} catch (CDKException e) { |
| 187 |
e.printStackTrace(); |
| 188 |
} catch (IOException e) { |
| 189 |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| 190 |
} |
| 191 |
ViewMolecule2DTable v2dt = new ViewMolecule2DTable(acs, 3, 200, 200, Misc.getDefaultDepictor()); |
| 192 |
|
| 193 |
} |
| 194 |
} |