001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.table;
022:
023: import java.io.Serializable;
024: import java.util.EventListener;
025:
026: import javax.swing.event.EventListenerList;
027: import javax.swing.event.TableModelEvent;
028: import javax.swing.event.TableModelListener;
029:
030: public abstract class AbstractTableModel implements TableModel,
031: Serializable {
032: protected EventListenerList listenerList = new EventListenerList();
033:
034: public String getColumnName(final int column) {
035: StringBuffer result = new StringBuffer();
036: int rest = column;
037: do {
038: int remainder = rest % 26;
039: result.append(indexToLetter(remainder));
040: rest = rest / 26 - 1;
041: if (rest == 0) {
042: result.append(indexToLetter(0));
043: }
044: } while (rest > 0);
045:
046: return result.reverse().toString();
047: }
048:
049: public int findColumn(final String columnName) {
050: int result = -1;
051: for (int i = 0; i < columnName.length(); i++) {
052: int index = letterToIndex(columnName.charAt(i));
053: if (index == -1) {
054: return -1;
055: }
056: result = 26 * (result + 1) + index;
057: }
058: return result < getColumnCount() ? result : -1;
059: }
060:
061: public Class<?> getColumnClass(final int columnIndex) {
062: return Object.class;
063: }
064:
065: public boolean isCellEditable(final int rowIndex,
066: final int columnIndex) {
067: return false;
068: }
069:
070: public void setValueAt(final Object value, final int rowIndex,
071: final int columnIndex) {
072:
073: }
074:
075: public void addTableModelListener(final TableModelListener l) {
076: listenerList.add(TableModelListener.class, l);
077: }
078:
079: public void removeTableModelListener(final TableModelListener l) {
080: listenerList.remove(TableModelListener.class, l);
081: }
082:
083: public TableModelListener[] getTableModelListeners() {
084: return (TableModelListener[]) listenerList
085: .getListeners(TableModelListener.class);
086: }
087:
088: public <T extends EventListener> T[] getListeners(
089: final Class<T> listenerType) {
090: return listenerList.getListeners(listenerType);
091: }
092:
093: public void fireTableDataChanged() {
094: TableModelListener[] listeners = getTableModelListeners();
095: if (listeners.length == 0) {
096: return;
097: }
098:
099: fireTableChanged(new TableModelEvent(this ));
100: }
101:
102: public void fireTableStructureChanged() {
103: TableModelListener[] listeners = getTableModelListeners();
104: if (listeners.length == 0) {
105: return;
106: }
107:
108: fireTableChanged(new TableModelEvent(this ,
109: TableModelEvent.HEADER_ROW));
110: }
111:
112: public void fireTableRowsInserted(final int firstRow,
113: final int lastRow) {
114: TableModelListener[] listeners = getTableModelListeners();
115: if (listeners.length == 0) {
116: return;
117: }
118:
119: fireTableChanged(new TableModelEvent(this , firstRow, lastRow,
120: TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
121: }
122:
123: public void fireTableRowsUpdated(final int firstRow,
124: final int lastRow) {
125: TableModelListener[] listeners = getTableModelListeners();
126: if (listeners.length == 0) {
127: return;
128: }
129:
130: fireTableChanged(new TableModelEvent(this , firstRow, lastRow,
131: TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
132: }
133:
134: public void fireTableRowsDeleted(final int firstRow,
135: final int lastRow) {
136: TableModelListener[] listeners = getTableModelListeners();
137: if (listeners.length == 0) {
138: return;
139: }
140:
141: fireTableChanged(new TableModelEvent(this , firstRow, lastRow,
142: TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
143: }
144:
145: public void fireTableCellUpdated(final int row, final int column) {
146: TableModelListener[] listeners = getTableModelListeners();
147: if (listeners.length == 0) {
148: return;
149: }
150:
151: fireTableChanged(new TableModelEvent(this , row, row, column,
152: TableModelEvent.UPDATE));
153: }
154:
155: public void fireTableChanged(final TableModelEvent e) {
156: TableModelListener[] listeners = getTableModelListeners();
157: for (int i = 0; i < listeners.length; i++) {
158: listeners[i].tableChanged(e);
159: }
160: }
161:
162: private char indexToLetter(final int index) {
163: return (char) ('A' + index);
164: }
165:
166: private int letterToIndex(final char letter) {
167: int result = letter - 'A';
168: return (result >= 0 && result < 26) ? result : -1;
169: }
170: }
|