PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rcdkjar/src/org/guha/rcdk/view/ViewMolecule2DTable.java

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