001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing.table;
024:
025: import java.util.Map;
026:
027: import javax.swing.undo.AbstractUndoableEdit;
028: import javax.swing.undo.CannotRedoException;
029: import javax.swing.undo.CannotUndoException;
030:
031: /**
032: * Undo/Redo support for table modifications.
033: * <p>
034: *
035: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
036: * @version 1.0
037: */
038: public class UndoableTableModelEdit extends AbstractUndoableEdit {
039:
040: private static final long serialVersionUID = -1568455574731768310L;
041: private EnhancedTableModel tableModel = null;
042: private int rowAffected = Integer.MIN_VALUE;
043: private Map<String, Object> rowData = null;
044: private boolean isAdd = false;
045:
046: /**
047: * Constructs a standard table model edit.
048: * <p>
049: * This construct is suited for table model deletions by default.
050: *
051: * @param affected the row that has been affected by the edit.
052: * @param data row mapping of column names to objects.
053: * @param model model that was modified as a result of the edit.
054: * @see #UndoableTableModelEdit(int, Map, EnhancedTableModel, boolean)
055: */
056: public UndoableTableModelEdit(int affected,
057: Map<String, Object> data, EnhancedTableModel model) {
058:
059: this (affected, data, model, false);
060: }
061:
062: /**
063: * Constructs a standard table model edit.
064: * <p>
065: *
066: * @param affected the row that has been affected by the edit.
067: * @param data row mapping of column names to objects.
068: * @param model model that was modified as a result of the edit.
069: * @param doAdd flag indicating this edit is an addition operation.
070: */
071: public UndoableTableModelEdit(int affected,
072: Map<String, Object> data, EnhancedTableModel model,
073: boolean doAdd) {
074:
075: rowAffected = affected;
076: rowData = data;
077: tableModel = model;
078: isAdd = doAdd;
079: }
080:
081: @Override
082: public void redo() throws CannotRedoException {
083:
084: super .redo();
085: if (isAdd) {
086: tableModel.insertRow(rowAffected, rowData);
087: } else {
088: tableModel.removeRow(rowAffected);
089: }
090: }
091:
092: @Override
093: public void undo() throws CannotUndoException {
094:
095: super.undo();
096: if (isAdd) {
097: tableModel.removeRow(rowAffected);
098: } else {
099: tableModel.insertRow(rowAffected, rowData);
100: }
101: }
102: }
|