KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > swing > MapTableModel


1 /*
2  * Project: Gulden Utilies
3  * Class: de.gulden.util.swing.MapTableModel
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the Gulden Utilities,
9  * it is not released as a seperate version.
10  *
11  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
12  *
13  * This is licensed under the GNU Lesser General Public License (LGPL)
14  * and comes with NO WARRANTY.
15  *
16  * Author: Jens Gulden
17  * Email: amoda@jensgulden.de
18  */

19
20 package de.gulden.util.swing;
21
22 import java.util.*;
23 import java.util.Map JavaDoc;
24 import javax.swing.table.AbstractTableModel JavaDoc;
25
26 /**
27  * Class MapTableModel.
28  *
29  * @author Jens Gulden
30  * @version snapshot-beautyj-1.1
31  */

32 public class MapTableModel extends AbstractTableModel JavaDoc {
33
34     // ------------------------------------------------------------------------
35
// --- fields ---
36
// ------------------------------------------------------------------------
37

38     /**
39      * The map.
40      */

41     protected Map JavaDoc map;
42
43     /**
44      * The column names array.
45      */

46     protected String JavaDoc[] columnNames;
47
48
49     // ------------------------------------------------------------------------
50
// --- constructors ---
51
// ------------------------------------------------------------------------
52

53     /**
54      * Creates a new instance of MapTableModel.
55      */

56     public MapTableModel() {
57         super();
58     }
59
60     /**
61      * Creates a new instance of MapTableModel.
62      */

63     public MapTableModel(Map JavaDoc map) {
64         this(map,"Entry","Value");
65     }
66
67     /**
68      * Creates a new instance of MapTableModel.
69      */

70     public MapTableModel(Map JavaDoc map, String JavaDoc keyName, String JavaDoc valueName) {
71         this();
72         setMap(map);
73         setColumnNames(keyName,valueName);
74     }
75
76
77     // ------------------------------------------------------------------------
78
// --- methods ---
79
// ------------------------------------------------------------------------
80

81     /**
82      * Returns the row count.
83      */

84     public int getRowCount() {
85         return map.size();
86     }
87
88     /**
89      * Returns the column count.
90      */

91     public int getColumnCount() {
92         return 2;
93     }
94
95     /**
96      * Returns the value at.
97      */

98     public Object JavaDoc getValueAt(int row, int column) {
99         Object JavaDoc[] entries=map.entrySet().toArray();
100         Map.Entry JavaDoc entry=(Map.Entry JavaDoc)entries[row];
101         if (column==0) {
102             return entry.getKey();
103         } else if (column==1) { // column==1
104
return entry.getValue();
105         } else {
106             throw new IndexOutOfBoundsException JavaDoc("MapTableModel provides a 2-column table, column-index "+column+" is illegal.");
107         }
108     }
109
110     /**
111      * Returns the column name.
112      */

113     public String JavaDoc getColumnName(int column) {
114         return columnNames[column];
115     }
116
117     /**
118      * Sets the column names.
119      */

120     public void setColumnNames(String JavaDoc keyName, String JavaDoc valueName) {
121         String JavaDoc[] names={keyName,valueName};
122         columnNames=names;
123     }
124
125     /**
126      * Returns the map.
127      */

128     public Map JavaDoc getMap() {
129         return map;
130     }
131
132     /**
133      * Sets the map.
134      */

135     public void setMap(Map JavaDoc _map) {
136         map = _map;
137     }
138
139 } // end MapTableModel
140
Popular Tags