~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/swing/ChemObjectPropertyEditorTableModel.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) 2003-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
 * All I ask is that proper credit is given for my work, which includes
 
15
 * - but is not limited to - adding the above copyright notice to the beginning
 
16
 * of your source code files, and to any copyright notice that you may distribute
 
17
 * with programs based on this work.
 
18
 * 
 
19
 * This program is distributed in the hope that it will be useful,
 
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 * GNU Lesser General Public License for more details.
 
23
 * 
 
24
 * You should have received a copy of the GNU Lesser General Public License
 
25
 * along with this program; if not, write to the Free Software
 
26
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
27
 */
 
28
package org.openscience.cdk.applications.swing;
 
29
 
 
30
import java.util.Iterator;
 
31
import java.util.Map;
 
32
import java.util.Vector;
 
33
 
 
34
import javax.swing.table.AbstractTableModel;
 
35
 
 
36
import org.openscience.cdk.ChemObject;
 
37
 
 
38
/**
 
39
 * @cdk.module applications
 
40
 * @cdk.require swing
 
41
 */
 
42
public class ChemObjectPropertyEditorTableModel extends AbstractTableModel {
 
43
    
 
44
        private static final long serialVersionUID = -9026379867364393754L;
 
45
        
 
46
        private String[] columnNames;
 
47
    private Vector names = new Vector();
 
48
    private Vector values = new Vector();
 
49
    
 
50
    public ChemObjectPropertyEditorTableModel() {
 
51
        columnNames = new String[2];
 
52
        columnNames[0] = "Name";
 
53
        columnNames[1] = "Value";
 
54
        
 
55
        insertBlankRow(0);
 
56
    }
 
57
    
 
58
    
 
59
    public void setValueAt(Object value, int row, int column) {
 
60
        if (((String)value).length() == 0 ) {
 
61
            names.removeElementAt(row);
 
62
            values.removeElementAt(row);
 
63
            fireTableRowsDeleted(row, row);
 
64
            if (values.size() < 1) {
 
65
                insertBlankRow(row);
 
66
            }
 
67
        } else if (column == 0) {
 
68
            names.setElementAt(value, row);
 
69
        } else if (column == 1) {
 
70
            values.setElementAt(value, row);
 
71
        }
 
72
 
 
73
        if (row == values.size()-1) {
 
74
            insertBlankRow(row);
 
75
        }
 
76
        
 
77
        fireTableCellUpdated(row, column);
 
78
    }
 
79
    
 
80
    
 
81
    public int getColumnCount() {
 
82
        return columnNames.length;
 
83
    }
 
84
    
 
85
    public int getRowCount() {
 
86
        return values.size();
 
87
    }
 
88
    
 
89
    public String getColumnName(int col) {
 
90
        return columnNames[col];
 
91
    }
 
92
    
 
93
    public Class getColumnClass(int col) {
 
94
        return getColumnName(col).getClass();
 
95
    }
 
96
    
 
97
    public Object getValueAt(int row, int col) {
 
98
        if ( row >= values.size() || col >= columnNames.length ) {
 
99
            return null;
 
100
        }
 
101
        
 
102
        if (col == 0) {
 
103
            return names.elementAt(row);
 
104
        } else if (col == 1) {
 
105
            return values.elementAt(row);
 
106
        } else {
 
107
            return null;
 
108
        }
 
109
    }
 
110
    
 
111
    public boolean isCellEditable(int row, int column) {
 
112
        return true;
 
113
    }
 
114
    
 
115
    public void setChemObject(ChemObject object) {
 
116
        cleanTable();
 
117
        Map properties = object.getProperties();
 
118
        Iterator iter = properties.keySet().iterator();
 
119
        while (iter.hasNext()) {
 
120
            Object key = iter.next();
 
121
            if (key instanceof String) {
 
122
                String keyName = (String)key;
 
123
                names.addElement(keyName);
 
124
                String value = (String)properties.get(keyName);
 
125
                values.addElement(value);
 
126
            }
 
127
        }
 
128
    }
 
129
    
 
130
    private void cleanTable() {
 
131
        names.clear();
 
132
        values.clear();
 
133
        fireTableDataChanged();
 
134
        insertBlankRow(0);
 
135
    }
 
136
    
 
137
    private void insertBlankRow(int row) {
 
138
        names.addElement("");
 
139
        values.addElement("");
 
140
        fireTableRowsInserted(row+1, row+1);
 
141
    }
 
142
}
 
143