001: /*
002: * Project: Gulden Utilies
003: * Class: de.gulden.util.swing.MapTableModel
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the Gulden Utilities,
009: * it is not released as a seperate version.
010: *
011: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
012: *
013: * This is licensed under the GNU Lesser General Public License (LGPL)
014: * and comes with NO WARRANTY.
015: *
016: * Author: Jens Gulden
017: * Email: amoda@jensgulden.de
018: */
019:
020: package de.gulden.util.swing;
021:
022: import java.util.*;
023: import java.util.Map;
024: import javax.swing.table.AbstractTableModel;
025:
026: /**
027: * Class MapTableModel.
028: *
029: * @author Jens Gulden
030: * @version snapshot-beautyj-1.1
031: */
032: public class MapTableModel extends AbstractTableModel {
033:
034: // ------------------------------------------------------------------------
035: // --- fields ---
036: // ------------------------------------------------------------------------
037:
038: /**
039: * The map.
040: */
041: protected Map map;
042:
043: /**
044: * The column names array.
045: */
046: protected String[] columnNames;
047:
048: // ------------------------------------------------------------------------
049: // --- constructors ---
050: // ------------------------------------------------------------------------
051:
052: /**
053: * Creates a new instance of MapTableModel.
054: */
055: public MapTableModel() {
056: super ();
057: }
058:
059: /**
060: * Creates a new instance of MapTableModel.
061: */
062: public MapTableModel(Map map) {
063: this (map, "Entry", "Value");
064: }
065:
066: /**
067: * Creates a new instance of MapTableModel.
068: */
069: public MapTableModel(Map map, String keyName, String valueName) {
070: this ();
071: setMap(map);
072: setColumnNames(keyName, valueName);
073: }
074:
075: // ------------------------------------------------------------------------
076: // --- methods ---
077: // ------------------------------------------------------------------------
078:
079: /**
080: * Returns the row count.
081: */
082: public int getRowCount() {
083: return map.size();
084: }
085:
086: /**
087: * Returns the column count.
088: */
089: public int getColumnCount() {
090: return 2;
091: }
092:
093: /**
094: * Returns the value at.
095: */
096: public Object getValueAt(int row, int column) {
097: Object[] entries = map.entrySet().toArray();
098: Map.Entry entry = (Map.Entry) entries[row];
099: if (column == 0) {
100: return entry.getKey();
101: } else if (column == 1) { // column==1
102: return entry.getValue();
103: } else {
104: throw new IndexOutOfBoundsException(
105: "MapTableModel provides a 2-column table, column-index "
106: + column + " is illegal.");
107: }
108: }
109:
110: /**
111: * Returns the column name.
112: */
113: public String getColumnName(int column) {
114: return columnNames[column];
115: }
116:
117: /**
118: * Sets the column names.
119: */
120: public void setColumnNames(String keyName, String valueName) {
121: String[] names = { keyName, valueName };
122: columnNames = names;
123: }
124:
125: /**
126: * Returns the map.
127: */
128: public Map getMap() {
129: return map;
130: }
131:
132: /**
133: * Sets the map.
134: */
135: public void setMap(Map _map) {
136: map = _map;
137: }
138:
139: } // end MapTableModel
|