📄 dbftablemodel.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/dataAccess/shape/DbfTableModel.java,v $// $RCSfile: DbfTableModel.java,v $// $Revision: 1.6.2.6 $// $Date: 2006/01/04 23:07:48 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.dataAccess.shape;import java.awt.BorderLayout;import java.awt.Component;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.ArrayList;import java.util.Iterator;import javax.swing.BorderFactory;import javax.swing.DefaultListSelectionModel;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.ListSelectionModel;import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener;import javax.swing.table.AbstractTableModel;import com.bbn.openmap.dataAccess.shape.input.DbfInputStream;import com.bbn.openmap.dataAccess.shape.output.DbfOutputStream;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.FileUtils;import com.bbn.openmap.util.PropUtils;/** * An implemention of TableModel that manages tabular data read from a * dbf file and enables the user to stored data store herein to be * saved to a file conforming to the DBF III file format * specification. * * To create a three one column model: * * <pre> * DbfTableModel model = new DbfTableModel(1); * * model.setDecimalCount(0, (byte) 0); * model.setLength(0, (byte) 10); * model.setColumnName(0, "Column1"); * model.setType(0, (byte) DbfTableModel.TYPE_CHARACTER); * * model.setDecimalCount(1, (byte) 0); * model.setLength(1, (byte) 10); * model.setColumnName(1, "Column1"); * model.setType(1, (byte) DbfTableModel.TYPE_NUMERIC); * * model.setDecimalCount(2, (byte) 0); * model.setLength(2, (byte) 10); * model.setColumnName(2, "Column1"); * model.setType(2, (byte) DbfTableModel.TYPE_CHARACTER); * * esriLayer.setModel(model); * </pre> * * @author Doug Van Auken */public class DbfTableModel extends AbstractTableModel implements ShapeConstants, TableModelListener { public static final int TYPE_CHARACTER = 67; public static final int TYPE_DATE = 68; public static final int TYPE_NUMERIC = 78; public static final int TYPE_LOGICAL = 76; public static final int TYPE_MEMO = 77; /** * Edit button mask, to allow adding/removing rows. Be very * careful with this option if you plan on using this file with a * shape file - the number of records has to match the number of * graphics in a shape file, so if you add or delete, you should * add/delete the graphic in the shape file, too. */ public static final int MODIFY_ROW_MASK = 1 << 0; /** * Edit button mask, to allow adding/removing columns in the * attribute table. */ public static final int MODIFY_COLUMN_MASK = 1 << 1; /** * Button mask to drop the frame quietly, with the modifications * to the table complete. */ public static final int DONE_MASK = 1 << 3; /** * Button mask to show a save button to write out any changes. */ public static final int SAVE_MASK = 1 << 4; /** * An array of bytes that contain the character lengths for each * column */ protected int[] _lengths = null; /** * An array of bytes that contain the number of decimal places for * each column */ protected byte[] _decimalCounts = null; /** An array of bytes that contain the column types for each column */ protected byte[] _types = null; /** An array of bytes that contain the names for each column */ protected String[] _names = null; /** Class scope reference to a list of data formatted by row */ protected ArrayList _records = null; /** * Class scope variable for the number of columns that exist in * the model */ protected int _columnCount = -1; protected boolean writable = false; protected JTable table; protected final DbfTableModel parent; protected boolean dirty = false; protected boolean exitOnClose = false; protected boolean DEBUG = false; /** * Creates a blank DbfTableModel * * @param columnCount The number of columns this model will manage */ public DbfTableModel(int columnCount) { _columnCount = columnCount; _records = new ArrayList(); _lengths = new int[columnCount]; _decimalCounts = new byte[columnCount]; _types = new byte[columnCount]; _names = new String[columnCount]; parent = this; DEBUG = Debug.debugging("shape"); } /** * Creates a DbfTableModel based on an InputStream * * @param is The dbf file */ public DbfTableModel(DbfInputStream is) { _lengths = is.getLengths(); _decimalCounts = is.getDecimalCounts(); _names = is.getColumnNames(); _types = is.getTypes(); _records = is.getRecords(); _columnCount = is.getColumnCount(); parent = this; } /** * Adds a row of data to the the model * * @param columns A collection of columns that comprise the row of * data * @exception An exception is thrown if the number of elements in * the passed in collection does not match the number * of columns in the model */ public void addRecord(ArrayList columns) { if (columns.size() != _columnCount) { throw new RuntimeException("DbfTableModel: Mismatched Column Count"); } _records.add(columns); } /** * Remove the record at the index. */ public ArrayList remove(int index) { return (ArrayList) _records.remove(index); } public void addBlankRecord() { ArrayList record = new ArrayList(); for (int i = 0; i < _columnCount; i++) { record.add(getEmptyDefaultForType(getType(i))); } addRecord(record); if (DEBUG) Debug.output("Adding record: " + record); } public Object getEmptyDefaultForType(byte type) { // May need to be updated to provide real values. if (type == DBF_TYPE_NUMERIC.byteValue()) { return new Integer(0); } else if (type == DBF_TYPE_LOGICAL.byteValue()) { return new Boolean(false); } else { return ""; } } /** * Retrieves the record array list for the passed record number * * @param recordnumber The record number * @return An ArrayList for the given record number */ public Object getRecord(int recordnumber) { return _records.get(recordnumber); } /** * Get an iterator over the records. */ public Iterator getRecords() { return _records.iterator(); } /** * Retrieves the column class for the passed in column index * * @param c The column index * @return The column class for the given column index */ public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /** * Retrieves the number of columns that exist in the model * * @return The number of columns that exist in the model */ public int getColumnCount() { return _columnCount; } /** * Retrieves the number of decimal places for the passed in column * index * * @param column The column index * @return The number of decimal places for the given column index */ public byte getDecimalCount(int column) { return _decimalCounts[column]; } /** * Retrieves the column name for the passed in column index * * @param column The column index * @return The column name for the given column index */ public String getColumnName(int column) { return _names[column]; } /** * Retrieves the character length for the passed in column index * * @param column The column index * @return The charcter length for the given column index */ public int getLength(int column) { return _lengths[column]; } /** * Retrieves the number of rows that exist in the model * * @return The number rows that exist in the model */ public int getRowCount() { if (_records == null) { return 0; } else { return _records.size(); } } /** * Retrieves the column type for the passed in column index * * @param column The column index * @return The column type for the given column index */ public byte getType(int column) { return _types[column]; } /** * Retrieves a value for a specific column and row index * * @return Object A value for a specific column and row index */ public Object getValueAt(int row, int column) { ArrayList cells = (ArrayList) _records.get(row); Object cell = cells.get(column); return cell; } /** * Sets the column name for the passed-in field index * * @param column The column index * @param name The name to assign for the passed-in column index */ public void setColumnName(int column, String name) { _names[column] = name; } /** * Sets the decimal count for the passed in field index * * @param column The index to the column * @param decimalCount The number of decimals places to assign to * the passed in column */ public void setDecimalCount(int column, byte decimalCount) { _decimalCounts[column] = decimalCount; } /** * Set the character length fro the passed-in field index * * @param column The column index * @param length The character length to assign for the passed-in * column index */ public void setLength(int column, int length) { _lengths[column] = length; } /** * Sets the column type for the passed-in field index * * @param column The column index * @param type The type of column to assign for the passed-in * column index */ public void setType(int column, byte type) { _types[column] = type; } public void setValueAt(Object object, int row, int column) { ArrayList columns = (ArrayList) _records.get(row); columns.set(column, object); } public boolean isCellEditable(int rowIndex, int columnIndex) { return writable; } public void setWritable(boolean set) { writable = set; } public boolean getWritable() { return writable; } /** * Needs to be called before displaying the DbfTableModel. */ public JTable getTable(ListSelectionModel lsm) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -