~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/common/org/crosswire/common/swing/data/HashtableTableModel.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.common.swing.data;
3
 
 
4
 
import java.util.*;
5
 
import javax.swing.table.*;
6
 
 
7
 
/**
8
 
* TableModel using a Hashtable internally. Note that an AbstractTableModel
9
 
* (this is-a AbstractTableModel) reports changes to the data to the table
10
 
* itself. However since a Hashtable does not have a addChangeListener
11
 
* interface we can't do the same - SO if you change the Hashtable whilst
12
 
* we are displaying it then don't expect the changes to be automatically
13
 
* reflected in the JTable.
14
 
*
15
 
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
16
 
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
17
 
* Distribution Licence:<br />
18
 
* Project B is free software; you can redistribute it
19
 
* and/or modify it under the terms of the GNU General Public License,
20
 
* version 2 as published by the Free Software Foundation.<br />
21
 
* This program is distributed in the hope that it will be useful,
22
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24
 
* General Public License for more details.<br />
25
 
* The License is available on the internet
26
 
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
27
 
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28
 
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
29
 
* The copyright to this program is held by it's authors.
30
 
* </font></td></tr></table>
31
 
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
32
 
* @see <{docs.Licence}>
33
 
* @author Joe Walker
34
 
* @version D8.I5.T0
35
 
*/
36
 
public class HashtableTableModel extends AbstractTableModel
37
 
{
38
 
    /**
39
 
    * Create an internal store from a 2D array
40
 
    * @param hash The table to model
41
 
    */
42
 
    public HashtableTableModel(Hashtable hash)
43
 
    {
44
 
        this.hash = hash;
45
 
    }
46
 
 
47
 
    /**
48
 
    * Accessor for our source data
49
 
    * @return The hashtable we are getting our data from
50
 
    */
51
 
    public Hashtable getHashtable()
52
 
    {
53
 
        return hash;
54
 
    }
55
 
 
56
 
    /**
57
 
    * Change the hashtable that we report on
58
 
    * @param hash The hashtable we are getting our data from
59
 
    */
60
 
    public void setHashtable(Hashtable hash)
61
 
    {
62
 
        this.hash = hash;
63
 
    }
64
 
 
65
 
    /**
66
 
    * How many Cols are there in this store
67
 
    * @return The number of columns - always 2 in this case
68
 
    */
69
 
    public int getColumnCount()
70
 
    {
71
 
        return 2;
72
 
    }
73
 
 
74
 
    /**
75
 
    * How many Rows are there in this store
76
 
    * @return the number of row in the TableModel = elements in the Hashtable
77
 
    */
78
 
    public int getRowCount()
79
 
    {
80
 
        return hash.size();
81
 
    }
82
 
 
83
 
    /**
84
 
    * Return the Object at row, col
85
 
    * @param row The element in the hash
86
 
    * @param col 1=keys, 2=values
87
 
    * @return The key/value of the given element
88
 
    */
89
 
    public Object getValueAt(int row, int col)
90
 
    {
91
 
        Enumeration en = (col == 0) ? hash.keys() : hash.elements();
92
 
 
93
 
        try
94
 
        {
95
 
            for (int i=0; i<row; i++)
96
 
                en.nextElement();
97
 
 
98
 
            return en.nextElement();
99
 
        }
100
 
        catch (Exception ex)
101
 
        {
102
 
            return null;
103
 
        }
104
 
    }
105
 
 
106
 
    /**
107
 
    * An easy way to add stuff to the table
108
 
    */
109
 
    public void put(Object key, Object value)
110
 
    {
111
 
        hash.put(key, value);
112
 
        fireTableStructureChanged();
113
 
    }
114
 
 
115
 
    /**
116
 
    * An easy way to add stuff to the table
117
 
    */
118
 
    public void remove(Object key)
119
 
    {
120
 
        hash.remove(key);
121
 
        fireTableStructureChanged();
122
 
    }
123
 
 
124
 
    /**
125
 
    * Set the Object at row, coll
126
 
    * @param obj The key/value to set
127
 
    * @param row The element in the hash
128
 
    * @param col 1=keys, 2=values
129
 
    */
130
 
    public void setValueAt(Object obj, int row, int col)
131
 
    {
132
 
        Enumeration en = hash.keys();
133
 
 
134
 
        for (int i=0; i<row; i++)
135
 
            en.hasMoreElements();
136
 
 
137
 
        Object old_key = en.nextElement();
138
 
        Object old_val = hash.get(old_key);
139
 
 
140
 
        if (col == 0)
141
 
        {
142
 
            // Changing a key
143
 
            hash.remove(old_key);
144
 
            hash.put(obj, old_val);
145
 
        }
146
 
        else
147
 
        {
148
 
            // Changing a value
149
 
            hash.put(old_key, obj);
150
 
        }
151
 
    }
152
 
 
153
 
    /**
154
 
    * Can the specified cell be changed?
155
 
    * @param row The element in the hash
156
 
    * @param col 1=keys, 2=values
157
 
    */
158
 
    public boolean isCellEditable(int row, int col)
159
 
    {
160
 
        return true;
161
 
    }
162
 
 
163
 
    /**
164
 
    * Get the default class
165
 
    * @param col 1=keys, 2=values
166
 
    */
167
 
    public Class getColumnClass(int col)
168
 
    {
169
 
        return String.class;
170
 
    }
171
 
 
172
 
    /**
173
 
    * The name of the of the <code>col</code>th column
174
 
    * @param col The column index
175
 
    * @return The column name
176
 
    */
177
 
    public String getColumnName(int col)
178
 
    {
179
 
        return col_names[col];
180
 
    }
181
 
 
182
 
    /**
183
 
    * The name of the of the <code>col</code>th column
184
 
    * @param col The column index
185
 
    * @param name The column name
186
 
    */
187
 
    public void setColumnName(int col, String name)
188
 
    {
189
 
        col_names[col] = name;
190
 
    }
191
 
 
192
 
    /** The Hashtable that we are providing an interface to */
193
 
    private Hashtable hash;
194
 
 
195
 
    /** The default column names */
196
 
    private String[] col_names = new String[] { "Keys", "Values" };
197
 
}