~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/swing/AtomicTable.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$   
 
2
 * $Author: egonw $   
 
3
 * $Date: 2007-01-04 18:26:00 +0100 (Thu, 04 Jan 2007) $    
 
4
 * $Revision: 7634 $
 
5
 * 
 
6
 * Copyright (C) 2001-2007  The Chemistry Development Kit (CDK) project
 
7
 * 
 
8
 * Contact: cdk-devel@lists.sourceforge.net
 
9
 * 
 
10
 * This program is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public License
 
12
 * as published by the Free Software Foundation; either version 2.1
 
13
 * of the License, or (at your option) any later version.
 
14
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
 
23
 */
 
24
package org.openscience.cdk.applications.swing;
 
25
 
 
26
import java.awt.BorderLayout;
 
27
import java.awt.Dimension;
 
28
import java.util.EventObject;
 
29
 
 
30
import javax.swing.JFrame;
 
31
import javax.swing.JPanel;
 
32
import javax.swing.JScrollPane;
 
33
import javax.swing.JTable;
 
34
import javax.swing.table.AbstractTableModel;
 
35
 
 
36
import org.openscience.cdk.event.ICDKChangeListener;
 
37
 
 
38
/**
 
39
 * @cdk.module  applications
 
40
 * @cdk.require swing
 
41
 */
 
42
public class AtomicTable extends JPanel implements ICDKChangeListener {
 
43
 
 
44
        private static final long serialVersionUID = -7017202227040620173L;
 
45
        
 
46
        private JTable table;
 
47
        private String title = "Molecule Viewer";
 
48
  
 
49
        public AtomicTable(org.openscience.cdk.interfaces.IAtomContainer atomContainer) {
 
50
    AtomContainerModel acm = new AtomContainerModel(atomContainer);
 
51
    table = new JTable(acm);
 
52
    table.setPreferredScrollableViewportSize(new Dimension(500,300));
 
53
    JScrollPane scrollPane = new JScrollPane(table);
 
54
    this.add(scrollPane, BorderLayout.CENTER);
 
55
        }
 
56
        
 
57
        /**
 
58
         * Contructs a JFrame into which this JPanel is
 
59
         * put and displays the frame with the molecule
 
60
         */
 
61
        public void display()
 
62
        {
 
63
                JFrame frame = new JFrame();
 
64
                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
 
65
                frame.getContentPane().add(this);
 
66
                frame.setTitle(title);
 
67
                frame.pack();
 
68
                frame.setVisible(true);         
 
69
        }
 
70
 
 
71
  public void stateChanged(EventObject e) {
 
72
    // not implemented yet
 
73
  }
 
74
        
 
75
  class AtomContainerModel extends AbstractTableModel {
 
76
 
 
77
        private static final long serialVersionUID = -3331835007097257315L;
 
78
 
 
79
        private org.openscience.cdk.interfaces.IAtomContainer atomContainer;
 
80
    
 
81
    final String[] columnNames = {"atom", "x2", "y2", "x3", 
 
82
                                  "y3", "z3", "charge"};
 
83
    
 
84
    public AtomContainerModel(org.openscience.cdk.interfaces.IAtomContainer ac) {
 
85
      atomContainer = ac;
 
86
    }
 
87
    
 
88
    public int getColumnCount() {
 
89
      return columnNames.length;
 
90
    }
 
91
    
 
92
    public int getRowCount() {
 
93
      return atomContainer.getAtomCount();
 
94
    }
 
95
    
 
96
    public String getColumnName(int col) {
 
97
      return columnNames[col];
 
98
    }
 
99
    
 
100
    public Class getColumnClass(int col) {
 
101
      Object o = getValueAt(0,col);
 
102
      if (o == null) {
 
103
        return (new String()).getClass();
 
104
      } else {
 
105
        return o.getClass();
 
106
      }
 
107
    }
 
108
    
 
109
    public Object getValueAt(int row, int col) {
 
110
      if (getColumnName(col).equals("atom")) {
 
111
        return (atomContainer.getAtom(row)).getSymbol();
 
112
      } else if (getColumnName(col).equals("x2")) {
 
113
        return new Double((atomContainer.getAtom(row)).getPoint2d().x);
 
114
      } else if (getColumnName(col).equals("y2")) {
 
115
        return new Double((atomContainer.getAtom(row)).getPoint2d().y);
 
116
      } else if (getColumnName(col).equals("x3")) {
 
117
        return new Double((atomContainer.getAtom(row)).getPoint3d().x);
 
118
      } else if (getColumnName(col).equals("y3")) {
 
119
        return new Double((atomContainer.getAtom(row)).getPoint3d().y);
 
120
      } else if (getColumnName(col).equals("z3")) {
 
121
        return new Double((atomContainer.getAtom(row)).getPoint3d().z);
 
122
      } else if (getColumnName(col).equals("charge")) {
 
123
        return new Double((atomContainer.getAtom(row)).getCharge());
 
124
      } else {
 
125
        return null;
 
126
      }
 
127
    }
 
128
    
 
129
    public boolean isCellEditable(int row, int col) {
 
130
      return false;
 
131
    }
 
132
  }
 
133
}
 
134
 
 
135