001: package net.sourceforge.squirrel_sql.client.session.mainpanel;
002:
003: /*
004: * Copyright (C) 2004 Gerd Wagner
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import javax.swing.undo.UndoManager;
021: import javax.swing.undo.UndoableEdit;
022: import javax.swing.text.AbstractDocument;
023: import javax.swing.event.DocumentEvent;
024:
025: /**
026: * The trick of this UndoManager is to jump over UndoableEdits
027: * of type DocumentEvent.EventType.CHANGE. These are events like
028: * coloring that we don't want to see in undo/redo.
029: */
030: public class SquirrelDefaultUndoManager extends UndoManager {
031: private static final long serialVersionUID = 1L;
032:
033: /**
034: * Default ctor.
035: */
036: public SquirrelDefaultUndoManager() {
037: super ();
038: // Prepare to have a lot of DocumentEvent.EventType.CHANGE
039: setLimit(200000);
040: }
041:
042: /**
043: * The same as super.editToBeUndone() just that we treat DocumentEvent.EventType.CHANGE
044: * the same way as true == edit.isSignificant().
045: */
046: protected UndoableEdit editToBeUndone() {
047: UndoableEdit ue = super .editToBeUndone();
048:
049: if (ue == null) {
050: return null;
051: }
052:
053: int i = edits.indexOf(ue);
054: while (i >= 0) {
055: UndoableEdit edit = edits.elementAt(i--);
056: if (edit.isSignificant()) {
057: if (edit instanceof AbstractDocument.DefaultDocumentEvent) {
058: if (DocumentEvent.EventType.CHANGE != ((AbstractDocument.DefaultDocumentEvent) edit)
059: .getType()) {
060: return edit;
061: }
062: } else {
063: return edit;
064: }
065: }
066: }
067: return null;
068: }
069:
070: /**
071: * The same as super.editToBeUndone() just that we treat DocumentEvent.EventType.CHANGE
072: * the same way as true == edit.isSignificant().
073: *
074: * The method of the super class already seems to be a bit buggy.
075: * The DocumentEvent.EventType.CHANGE fix doesn't remove the bugs but makes it behave
076: */
077: protected UndoableEdit editToBeRedone() {
078: int count = edits.size();
079: UndoableEdit ue = super .editToBeRedone();
080:
081: if (null == ue) {
082: return null;
083: }
084:
085: int i = edits.indexOf(ue);
086:
087: while (i < count) {
088: UndoableEdit edit = edits.elementAt(i++);
089: if (edit.isSignificant()) {
090: if (edit instanceof AbstractDocument.DefaultDocumentEvent) {
091: if (DocumentEvent.EventType.CHANGE != ((AbstractDocument.DefaultDocumentEvent) edit)
092: .getType()) {
093: return edit;
094: }
095: } else {
096: return edit;
097: }
098: }
099: }
100: return null;
101: }
102: }
|