⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbftablemodel.java

📁 OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你就能够快速构建用于访问legacy数据库的应用程序与applets。OpenMap提供了允许用户查看和操作地理空间信息的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public boolean getWritable() {        return writable;    }    /**     * Needs to be called before displaying the DbfTableModel.     */    public JTable getTable(ListSelectionModel lsm) {        JTable t = getTable();        t.setModel(this);        t.setSelectionModel(lsm);        t.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);        return t;    }    protected JTable getTable() {        if (table == null) {            table = new JTable();        }        return table;    }    // In case you want to add options to modify the table.    JPanel controlPanel = null;    public Component getGUI(String filename, int actionMask) {        JPanel panel = new JPanel();        if (filename != null) {            panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),                    filename));        } else {            panel.setBorder(BorderFactory.createEtchedBorder());        }        panel.setLayout(new BorderLayout());        JScrollPane pane = new JScrollPane(getTable(new DefaultListSelectionModel()));        panel.add(pane, BorderLayout.CENTER);        controlPanel = new JPanel();        panel.add(controlPanel, BorderLayout.SOUTH);        if ((actionMask & MODIFY_ROW_MASK) != 0) {            JButton addButton = new JButton("Add");            addButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    addBlankRecord();                    fireTableDataChanged();                }            });            JButton deleteButton = new JButton("Delete");            deleteButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    int[] index = getTable().getSelectedRows();                    if (index.length > 0) {                        // Ask to make sure...                        int check = JOptionPane.showConfirmDialog(null,                                ("Are you sure you want to delete " + (index.length > 1 ? "these rows?"                                        : "this row?")),                                "Confirm Delete",                                JOptionPane.OK_CANCEL_OPTION);                        if (check == JOptionPane.YES_OPTION) {                            for (int i = index.length - 1; i >= 0; i--) {                                if (DEBUG)                                    Debug.output("Deleting record " + index[i]);                                ArrayList removed = remove(index[i]);                                if (DEBUG)                                    Debug.output("Deleted records: " + removed);                            }                            fireTableDataChanged();                        }                    }                }            });            controlPanel.add(addButton);            controlPanel.add(deleteButton);        }        if ((actionMask & MODIFY_COLUMN_MASK) != 0) {            JButton editTableButton = new JButton("Edit Table Format");            editTableButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    MetaDbfTableModel mdtm = new MetaDbfTableModel(parent);                    mdtm.addTableModelListener(parent);                    mdtm.showGUI(filePath.toString());                }            });            controlPanel.add(editTableButton);        }        if ((actionMask & SAVE_MASK) != 0) {            JButton saveButton = new JButton("Save");            saveButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    try {                        String filePath = FileUtils.getFilePathToSaveFromUser("Select DBF file name...");                        if (!filePath.endsWith(".dbf")) {                            filePath = filePath + ".dbf";                        }                        if (filePath != null) {                            DbfOutputStream dos = new DbfOutputStream(new FileOutputStream(new File(filePath)));                            dos.writeModel(parent);                            dos.close();                        }                    } catch (FileNotFoundException fnfe) {                    } catch (IOException ioe) {                    }                }            });            controlPanel.add(saveButton);        }        if ((actionMask & DONE_MASK) != 0) {            JButton doneButton = new JButton("Done");            doneButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    frame.dispose();                }            });            controlPanel.add(doneButton);        }        return panel;    }    protected final StringBuffer filePath = new StringBuffer();    protected JFrame frame = null;    public void hideGUI() {        if (frame != null) {            frame.setVisible(false);        }    }    public void showGUI(String filename, int actionMask) {        if (frame == null) {            frame = new JFrame(filename);            filePath.replace(0, filePath.capacity(), filename);            frame.getContentPane().add(getGUI(null, actionMask),                    BorderLayout.CENTER);            frame.setSize(400, 300);            frame.addWindowListener(new WindowAdapter() {                public void windowClosing(WindowEvent e) {                    // need a shutdown event to notify other gui beans                    // and                    // then exit.                    exitWindowClosed();                }            });        }        frame.setVisible(true);    }    public void exitWindowClosed() {        if (exitOnClose) {            System.exit(0);        }    }    public void tableChanged(TableModelEvent e) {        dirty = true;        if (DEBUG)            Debug.output("DbfTableModel sensing change");        int row = e.getFirstRow();        // Of course, the only thing we're listening to here is the        // MetaDbfTableModel changes, and if we get a HEADER_ROW        // change it's telling us to modify *OUR* headers, to commit        // all the changes that were made to it. Otherwise, we should        // just track the events and make them happen when we get        // this.        if (row == TableModelEvent.HEADER_ROW) {            commitEvents((DbfTableModel) e.getSource());        }    }    protected void commitEvents(DbfTableModel model) {        if (DEBUG)            Debug.output("Committing changes");        Iterator modelRecords = model.getRecords();        int index = -1;        while (modelRecords.hasNext()) {            ArrayList modelRecord = (ArrayList) modelRecords.next();            String modelColumnName = (String) modelRecord.get(0);            index++;            if (index < _columnCount) {                String columnName = _names[index];                if (DEBUG)                    Debug.output(columnName + ", " + modelColumnName);                while (!columnName.equalsIgnoreCase(modelColumnName)) {                    deleteColumn(index);                    if (index >= _columnCount) {                        addColumn(modelRecord);                        break;                    }                    columnName = _names[index];                }            } else {                // Add Column                if (DEBUG)                    Debug.output("Add column " + modelColumnName);                addColumn(modelRecord);            }        }        while (++index < _columnCount) {            if (DEBUG)                Debug.output("Deleting extra column");            deleteColumn(index);        }        if (DEBUG) {            Debug.output("New Table:");            for (int j = 0; j < _names.length; j++) {                Debug.output("  " + _names[j]);            }        }        fireTableStructureChanged();        dirty = false;    }    /**     * Delete a column, iterating through all the records and deleting that part     * of each record.     */    protected void deleteColumn(int columnIndex) {        Iterator rows = getRecords();        while (rows.hasNext()) {            ArrayList row = (ArrayList) rows.next();            row.remove(columnIndex);        }        _columnCount -= 1;        _lengths = remove(_lengths, columnIndex);        _decimalCounts = remove(_decimalCounts, columnIndex);        _types = remove(_types, columnIndex);        _names = remove(_names, columnIndex);    }    /**     */    protected int[] remove(int[] current, int index) {        int[] newBytes = new int[current.length - 1];        System.arraycopy(current, 0, newBytes, 0, index);        System.arraycopy(current, index + 1, newBytes, index, current.length                - index - 1);        return newBytes;    }    /**     */    protected byte[] remove(byte[] current, int index) {        byte[] newBytes = new byte[current.length - 1];        System.arraycopy(current, 0, newBytes, 0, index);        System.arraycopy(current, index + 1, newBytes, index, current.length                - index - 1);        return newBytes;    }    /**     */    protected String[] remove(String[] current, int index) {        String[] newStrings = new String[current.length - 1];        System.arraycopy(current, 0, newStrings, 0, index);        System.arraycopy(current, index + 1, newStrings, index, current.length                - index - 1);        return newStrings;    }    /**     * The types in the ArrayList are set - String, Byte, Integer, Integer - to     * match the format of the header.     */    protected void addColumn(ArrayList recordColumn) {        Iterator rows = getRecords();        while (rows.hasNext()) {            ArrayList row = (ArrayList) rows.next();            row.add("");        }        _columnCount++;        _names = add(_names, ((String) recordColumn.get(0)));        _types = add(_types, ((Byte) recordColumn.get(1)).byteValue());        _lengths = add(_lengths, ((Integer) recordColumn.get(2)).byteValue());        _decimalCounts = add(_decimalCounts,                ((Integer) recordColumn.get(3)).byteValue());    }    /**     */    protected int[] add(int[] current, byte nb) {        int[] newBytes = new int[current.length + 1];        System.arraycopy(current, 0, newBytes, 0, current.length);        newBytes[current.length] = nb;        return newBytes;    }    /**     */    protected byte[] add(byte[] current, byte nb) {        byte[] newBytes = new byte[current.length + 1];        System.arraycopy(current, 0, newBytes, 0, current.length);        newBytes[current.length] = nb;        return newBytes;    }    protected String[] add(String[] current, String string) {        String[] newStrings = new String[current.length + 1];        System.arraycopy(current, 0, newStrings, 0, current.length);        newStrings[current.length] = string;        return newStrings;    }    public void cleanupChanges() {        if (DEBUG)            Debug.output("DbfTableModel cleaning up changes.");        dirty = false;    }    /**     * Create another DbfTableModel with the same structure as this one (number     * of columns, column names, lengths and decimal counts).     */    public DbfTableModel headerClone() {        int size = getColumnCount();        DbfTableModel dtm = new DbfTableModel(size);        for (int i = 0; i < size; i++) {            dtm.setColumnName(i, this.getColumnName(i));            dtm.setDecimalCount(i, this.getDecimalCount(i));            dtm.setLength(i, this.getLength(i));            dtm.setType(i, this.getType(i));        }        return dtm;    }    /**     * Creates a DbfTableModel for a given .dbf file     *      * @param dbf The url of the file to retrieve.     * @return The DbfTableModel, null if there is a problem.     */    public static DbfTableModel getDbfTableModel(URL dbf) {        DbfTableModel model = null;        try {            InputStream is = dbf.openStream();            model = new DbfTableModel(new DbfInputStream(is));            is.close();        } catch (Exception exception) {            if (Debug.debugging("shape")) {                Debug.error("problem loading DBF file" + exception.getMessage());            }        }        return model;    }    public static void main(String[] args) {        Debug.init();        if (args.length < 1) {            System.exit(0);        }        try {            URL dbf = PropUtils.getResourceOrFileOrURL(args[0]);            InputStream is = dbf.openStream();            DbfInputStream dis = new DbfInputStream(is);            DbfTableModel dtm = new DbfTableModel(dis);            dtm.setWritable(true);            dtm.exitOnClose = true;            dtm.showGUI(args[0], MODIFY_ROW_MASK | MODIFY_COLUMN_MASK                    | SAVE_MASK);            is.close();        } catch (Exception e) {            Debug.error(e.getMessage());            e.printStackTrace();        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -