PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/rajarshi/cdkr
Java | 295 lines | 218 code | 58 blank | 19 comment | 9 complexity | 72457c6c626bf6142705b9a73ea677d8 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.MyTable;
  5. import org.guha.rcdk.view.table.StructureTableCellEditor2D;
  6. import org.guha.rcdk.view.table.StructureTableCellRenderer2D;
  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.event.ChangeEvent;
  13. import javax.swing.event.ListSelectionEvent;
  14. import javax.swing.event.TableColumnModelEvent;
  15. import javax.swing.event.TableColumnModelListener;
  16. import javax.swing.table.AbstractTableModel;
  17. import javax.swing.table.DefaultTableCellRenderer;
  18. import javax.swing.table.TableCellRenderer;
  19. import javax.swing.table.TableColumn;
  20. import java.awt.*;
  21. import java.awt.event.WindowAdapter;
  22. import java.awt.event.WindowEvent;
  23. import java.io.IOException;
  24. public class ViewMolecule2DDataTable {
  25. private static int STRUCTURE_COL = 0;
  26. private IAtomContainer[] molecules;
  27. private String[] cnames;
  28. private Object[][] tabledata;
  29. private RcdkDepictor depictor;
  30. private int fontSize = 14;
  31. private int cellx = 200;
  32. private int celly = 200;
  33. private int ncol;
  34. private int nrow;
  35. private JFrame frame;
  36. class ApplicationCloser extends WindowAdapter {
  37. public void windowClosing(WindowEvent e) {
  38. frame.dispose();
  39. }
  40. }
  41. class RowLabelRenderer extends DefaultTableCellRenderer {
  42. public RowLabelRenderer() {
  43. super();
  44. setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  45. setVerticalAlignment(javax.swing.SwingConstants.VERTICAL);
  46. }
  47. }
  48. public ViewMolecule2DDataTable(String[] fnames, String[] cnames,
  49. Object[][] tabledata, RcdkDepictor depictor) {
  50. try {
  51. molecules = Misc.loadMolecules(fnames, true, true, true);
  52. } catch (CDKException e) {
  53. e.printStackTrace();
  54. } catch (IOException e) {
  55. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  56. }
  57. frame = new JFrame("Table of Molecules");
  58. frame.addWindowListener(new ApplicationCloser());
  59. this.cnames = cnames;
  60. this.tabledata = tabledata;
  61. this.depictor = depictor;
  62. }
  63. public ViewMolecule2DDataTable(IAtomContainer[] molecules, String[] cnames,
  64. Object[][] tabledata, RcdkDepictor depictor) {
  65. frame = new JFrame("Table of Molecules");
  66. frame.addWindowListener(new ApplicationCloser());
  67. this.cnames = cnames;
  68. this.tabledata = tabledata;
  69. this.molecules = molecules;
  70. this.depictor = depictor;
  71. }
  72. public void setCellX(int cellx) {
  73. this.cellx = cellx;
  74. }
  75. public void setCellY(int celly) {
  76. this.celly = celly;
  77. }
  78. public void setFontSize(int f) {
  79. fontSize = f;
  80. }
  81. public void display() throws IOException, CDKException {
  82. if (depictor == null)
  83. depictor = Misc.getDefaultDepictor();
  84. ncol = cnames.length;
  85. nrow = molecules.length;
  86. Object[][] data = new Object[molecules.length][cnames.length];
  87. for (int i = 0; i < molecules.length; i++) {
  88. try {
  89. CDKHueckelAromaticityDetector.detectAromaticity(molecules[i]);
  90. molecules[i] = Misc.getMoleculeWithCoordinates(molecules[i]);
  91. molecules[i] = AtomContainerManipulator.removeHydrogens(molecules[i]);
  92. } catch (CDKException e) {
  93. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  94. } catch (Exception e) {
  95. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  96. }
  97. data[i][0] = new MoleculeCell(molecules[i], depictor);
  98. }
  99. // set the data
  100. for (int i = 0; i < molecules.length; i++) {
  101. for (int j = 1; j < cnames.length; j++) {
  102. data[i][j] = tabledata[i][j - 1];
  103. }
  104. }
  105. MyTable mtable = new MyTable(new Render2DPanelJTableModel(data, cnames));
  106. mtable.setShowGrid(true);
  107. mtable.getTableHeader().setFont(new Font("Lucida", Font.BOLD, fontSize));
  108. // disable movement of columns. This is needed since we
  109. // set the CellRenderer and CellEditor for a specific column
  110. mtable.getTableHeader().setReorderingAllowed(false);
  111. // set row heights
  112. for (int i = 0; i < molecules.length; i++) {
  113. mtable.setRowHeight(i, celly);
  114. }
  115. mtable.getColumnModel().getColumn(STRUCTURE_COL).setPreferredWidth(cellx);
  116. // add a TableolumnModelListener so we can catch column
  117. // resizes and change row heights accordingly
  118. mtable.getColumnModel().addColumnModelListener(new Render2DColumnModelListener(mtable));
  119. // allow cell selections
  120. mtable.setColumnSelectionAllowed(true);
  121. mtable.setRowSelectionAllowed(true);
  122. // set up scroll bars
  123. JScrollPane scrollpane = new JScrollPane(mtable);
  124. scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  125. mtable.setPreferredScrollableViewportSize(new Dimension(ncol * cellx, nrow));
  126. frame.getContentPane().add(scrollpane);
  127. // set the cell renderer for the structure column
  128. // we also set up a TableCellEditor so that events on a JmolPanel
  129. // cell get forwarded to the actual JmolPanel
  130. TableColumn col = mtable.getColumnModel().getColumn(0);
  131. col.setCellRenderer(new StructureTableCellRenderer2D());
  132. col.setCellEditor(new StructureTableCellEditor2D());
  133. RowLabelRenderer myRowRenderer = new RowLabelRenderer();
  134. for (int i = 1; i < ncol; i++) {
  135. col = mtable.getColumnModel().getColumn(i);
  136. col.setCellRenderer(myRowRenderer);
  137. }
  138. // start the show!
  139. frame.pack();
  140. frame.setSize(cellx * (ncol > 3 ? 3 : ncol), celly);
  141. frame.setVisible(true);
  142. //mtable.pack(TablePacker.VISIBLE_ROWS, true);
  143. }
  144. static class Render2DColumnModelListener implements TableColumnModelListener {
  145. JTable table;
  146. public Render2DColumnModelListener(JTable t) {
  147. this.table = t;
  148. }
  149. public void columnAdded(TableColumnModelEvent e) {
  150. }
  151. public void columnRemoved(TableColumnModelEvent e) {
  152. }
  153. public void columnMoved(TableColumnModelEvent e) {
  154. }
  155. public void columnMarginChanged(ChangeEvent e) {
  156. // int colwidth = this.table.getColumnModel().getColumn(STRUCTURE_COL).getWidth();
  157. // for (int i = 0; i < this.table.getRowCount(); i++) {
  158. // this.table.setRowHeight(i, colwidth);
  159. // }
  160. }
  161. public void columnSelectionChanged(ListSelectionEvent e) {
  162. }
  163. }
  164. static class Render2DPanelJTableModel extends AbstractTableModel {
  165. private static final long serialVersionUID = -1029080447213047474L;
  166. private Object[][] rows;
  167. private String[] columns;
  168. public Render2DPanelJTableModel(Object[][] objs, String[] cols) {
  169. rows = objs;
  170. columns = cols;
  171. }
  172. public String getColumnName(int column) {
  173. return columns[column];
  174. }
  175. public int getRowCount() {
  176. return rows.length;
  177. }
  178. public int getColumnCount() {
  179. return columns.length;
  180. }
  181. public Object getValueAt(int row, int column) {
  182. return rows[row][column];
  183. }
  184. public boolean isCellEditable(int row, int column) {
  185. return column == STRUCTURE_COL;
  186. }
  187. public Class getColumnClass(int column) {
  188. return getValueAt(0, column).getClass();
  189. }
  190. }
  191. static class Render2DPanelCellRenderer extends JPanel implements
  192. TableCellRenderer {
  193. private static final long serialVersionUID = 3990689120717795379L;
  194. public Component getTableCellRendererComponent(JTable table,
  195. Object value, boolean isSelected, boolean hasFocus,
  196. int rowIndex, int vColIndex) {
  197. // return plist[rowIndex];
  198. return (MoleculeCell) value;
  199. }
  200. // The following methods override the defaults for performance reasons
  201. public void validate() {
  202. }
  203. public void revalidate() {
  204. }
  205. protected void firePropertyChange(String propertyName, Object oldValue,
  206. Object newValue) {
  207. }
  208. public void firePropertyChange(String propertyName, boolean oldValue,
  209. boolean newValue) {
  210. }
  211. }
  212. public static void main(String[] args) throws IOException, CDKException {
  213. String home = "/Users/rguha/";
  214. String[] fname = {home + "src/R/trunk/rcdk/data/dan001.sdf",
  215. home + "src/R/trunk/rcdk/data/dan002.sdf",
  216. home + "src/R/trunk/rcdk/data/dan003.sdf"};
  217. IAtomContainer[] acs = null;
  218. try {
  219. acs = Misc.loadMolecules(fname, true, true, true);
  220. } catch (CDKException e) {
  221. e.printStackTrace();
  222. } catch (IOException e) {
  223. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  224. }
  225. String[] cnames = {"X", "Y", "Z", "A", "B", "C"};
  226. Object[][] dat = new Object[3][5];
  227. for (int i = 0; i < 3; i++) {
  228. dat[i][0] = new Integer(i);
  229. dat[i][1] = new Double(i) / 3.4;
  230. dat[i][2] = "Hello " + i;
  231. dat[i][3] = "By " + i;
  232. dat[i][4] = 3;
  233. }
  234. ViewMolecule2DDataTable d = new ViewMolecule2DDataTable(acs, cnames, dat, Misc.getDefaultDepictor());
  235. d.display();
  236. }
  237. }