001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: import javax.swing.event.TableModelEvent;
040: import javax.swing.event.TableModelListener;
041: import javax.swing.table.TableModel;
042:
043: import com.vividsolutions.jts.util.Assert;
044:
045: /**
046: * Provides a column based {@link TableModel}.
047: */
048:
049: public abstract class ColumnBasedTableModel implements TableModel {
050: private ArrayList columns = new ArrayList();
051: private ArrayList listeners = new ArrayList();
052:
053: //I got a strange error from JBuilder when I tried to make Column protected:
054: //"ColorThemingTableModel.java": Error #: 300 : method Column(com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingTableModel, java.lang.String, java.lang.Class) not found in class com.vividsolutions.jump.workbench.ui.ColumnBasedTableModel.Column at line 32, column 52
055: //"ColorThemingTableModel.java": Error #: 300 : constructor Column(com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingTableModel) not found in class com.vividsolutions.jump.workbench.ui.ColumnBasedTableModel.Column at line 32, column 72
056: //Funny -- Eclipse didn't give me an error. [Jon Aquino]
057: public abstract class Column {
058: private String name;
059: private Class dataClass;
060:
061: public Column(String name, Class dataClass) {
062: this .name = name;
063: this .dataClass = dataClass;
064: }
065:
066: public String getName() {
067: return name;
068: }
069:
070: public Class getDataClass() {
071: return dataClass;
072: }
073:
074: public abstract Object getValueAt(int rowIndex);
075:
076: public abstract void setValueAt(Object value, int rowIndex);
077: }
078:
079: protected Column getColumn(int column) {
080: return (Column) columns.get(column);
081: }
082:
083: public void addTableModelListener(TableModelListener l) {
084: listeners.add(l);
085: }
086:
087: public void removeTableModelListener(TableModelListener l) {
088: listeners.remove(l);
089: }
090:
091: public int getColumnCount() {
092: return columns.size();
093: }
094:
095: public String getColumnName(int columnIndex) {
096: return getColumn(columnIndex).getName();
097: }
098:
099: public int indexOfColumn(String name) {
100: for (int i = 0; i < columns.size(); i++) {
101: Column column = (Column) columns.get(i);
102:
103: if (column.getName().equals(name)) {
104: return i;
105: }
106: }
107:
108: Assert.shouldNeverReachHere(name);
109:
110: return -1;
111: }
112:
113: protected void setColumns(Collection columns) {
114: this .columns.clear();
115: this .columns.addAll(columns);
116: }
117:
118: public Class getColumnClass(int columnIndex) {
119: return getColumn(columnIndex).getDataClass();
120: }
121:
122: private boolean firingEvents = true;
123:
124: protected void setFiringEvents(boolean firingEvents) {
125: this .firingEvents = firingEvents;
126: }
127:
128: protected boolean isFiringEvents() {
129: return firingEvents;
130: }
131:
132: protected void fireTableChanged(TableModelEvent e) {
133: if (!firingEvents) {
134: return;
135: }
136: for (Iterator i = listeners.iterator(); i.hasNext();) {
137: TableModelListener listener = (TableModelListener) i.next();
138: listener.tableChanged(e);
139: }
140: }
141:
142: public Object getValueAt(int rowIndex, int columnIndex) {
143: return getColumn(columnIndex).getValueAt(rowIndex);
144: }
145:
146: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
147: getColumn(columnIndex).setValueAt(aValue, rowIndex);
148: }
149:
150: }
|