001: package org.jsqltool.model;
002:
003: import javax.swing.*;
004: import javax.swing.table.*;
005: import java.awt.*;
006: import java.awt.event.*;
007: import java.util.*;
008:
009: /**
010: * <p>Title: JSqlTool Project</p>
011: * <p>Description: Table Model, whose editability can be swiched.
012: * </p>
013: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
014: *
015: * <p> This file is part of JSqlTool project.
016: * This library is free software; you can redistribute it and/or
017: * modify it under the terms of the (LGPL) Lesser General Public
018: * License as published by the Free Software Foundation;
019: *
020: * GNU LESSER GENERAL PUBLIC LICENSE
021: * Version 2.1, February 1999
022: *
023: * This library is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
026: * Library General Public License for more details.
027: *
028: * You should have received a copy of the GNU Library General Public
029: * License along with this library; if not, write to the Free
030: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
031: *
032: * The author may be contacted at:
033: * maurocarniel@tin.it</p>
034: *
035: * @author Mauro Carniel
036: * @version 1.0
037: */
038: public class CustomTableModel extends DefaultTableModel {
039:
040: public static final int DETAIL_REC = 0;
041: public static final int INSERT_REC = 1;
042: public static final int EDIT_REC = 2;
043:
044: private int editMode = DETAIL_REC;
045: private int insertRowIndex = -1;
046: private boolean[] editableCols = null;
047: private Vector colNames = new Vector();
048: private Class[] colTypes = null;
049: private int[] colSizes = null;
050:
051: public CustomTableModel(String[] colNames, Class[] colTypes) {
052: this (colNames, colTypes, null);
053: }
054:
055: public CustomTableModel(String[] colNames, Class[] colTypes,
056: int[] colSizes) {
057: super (colNames, 0);
058: for (int i = 0; i < colNames.length; i++) {
059: this .colNames.add(colNames[i]);
060: }
061: this .colTypes = colTypes;
062: if (colSizes == null) {
063: colSizes = new int[colNames.length];
064: for (int i = 0; i < colNames.length; i++)
065: colSizes[i] = colNames[i].length() * 10;
066: }
067: this .colSizes = colSizes;
068: }
069:
070: public void addColumn(Object columnName, Vector columnData,
071: Class colType) {
072: this .addColumn(columnName, columnData, colType, columnName
073: .toString().length() * 10);
074: }
075:
076: public void addColumn(Object columnName, Vector columnData,
077: Class colType, int colSize) {
078: super .addColumn(columnName, columnData);
079:
080: Class[] newColTypes = new Class[colTypes.length + 1];
081: System.arraycopy(colTypes, 0, newColTypes, 0, colTypes.length);
082: newColTypes[colTypes.length] = colType;
083: colTypes = newColTypes;
084:
085: int[] newColSizes = new int[colSizes.length + 1];
086: System.arraycopy(colSizes, 0, newColSizes, 0, colSizes.length);
087: newColSizes[colSizes.length] = colSize;
088: colSizes = newColSizes;
089: }
090:
091: public Class getColumnClass(int col) {
092: return colTypes[col].equals(java.sql.Blob.class) ? String.class
093: : colTypes[col];
094: }
095:
096: public boolean isBlob(int col) {
097: return colTypes[col].equals(java.sql.Blob.class);
098: }
099:
100: /**
101: * Returns true regardless of parameter values.
102: *
103: * @param row the row whose value is to be queried
104: * @param column the column whose value is to be queried
105: * @return true
106: * @see #setValueAt
107: */
108: public boolean isCellEditable(int row, int column) {
109: if (editMode == DETAIL_REC || isBlob(column))
110: return false;
111: else if (editMode == INSERT_REC)
112: return (row == insertRowIndex);
113: else
114: return editableCols[column];
115: }
116:
117: public void setEditMode(int editMode) {
118: this .editMode = editMode;
119: if (editMode == INSERT_REC) {
120: this .addRow(new Object[this .getColumnCount()]);
121: insertRowIndex = this .getRowCount() - 1;
122: } else
123: insertRowIndex = -1;
124: if (editMode == EDIT_REC && editableCols == null) {
125: editableCols = new boolean[colNames.size()];
126: for (int i = 0; i < colNames.size(); i++)
127: editableCols[i] = true;
128: }
129: }
130:
131: public void setEditableCols(boolean[] editableCols) {
132: this .editableCols = editableCols;
133: }
134:
135: public void setDataVector(Vector data) {
136: this .setDataVector(data, colNames);
137: }
138:
139: public int getEditMode() {
140: return editMode;
141: }
142:
143: public int[] getColSizes() {
144: return colSizes;
145: }
146:
147: }
|