001: /*
002: * WbCompoundEdit.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.editor;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import javax.swing.undo.CannotRedoException;
017: import javax.swing.undo.CannotUndoException;
018: import javax.swing.undo.UndoableEdit;
019:
020: /**
021: *
022: * @author support@sql-workbench.net
023: */
024: public class WbCompoundEdit implements UndoableEdit {
025: private ArrayList edits;
026: private boolean acceptNew = true;
027:
028: public WbCompoundEdit() {
029: edits = new ArrayList();
030: }
031:
032: public int getSize() {
033: return edits.size();
034: }
035:
036: public void clear() {
037: this .edits.clear();
038: }
039:
040: public void finished() {
041: acceptNew = false;
042: }
043:
044: public UndoableEdit getLast() {
045: if (edits.size() == 0)
046: return null;
047: return (UndoableEdit) edits.get(edits.size() - 1);
048: }
049:
050: public void undo() throws CannotUndoException {
051: if (edits.size() == 0)
052: return;
053: for (int i = edits.size() - 1; i > -1; i--) {
054: UndoableEdit edit = (UndoableEdit) edits.get(i);
055: if (edit.canUndo() && edit.isSignificant())
056: edit.undo();
057: }
058: }
059:
060: public boolean canUndo() {
061: if (edits.size() == 0)
062: return false;
063: for (int i = 0; i < edits.size(); i++) {
064: UndoableEdit edit = (UndoableEdit) edits.get(i);
065: if (!edit.canUndo())
066: return false;
067: }
068: return true;
069: }
070:
071: public void redo() throws CannotRedoException {
072: if (edits.size() == 0)
073: return;
074:
075: for (int i = 0; i < edits.size(); i++) {
076: UndoableEdit edit = (UndoableEdit) edits.get(i);
077: edit.redo();
078: }
079: }
080:
081: public boolean canRedo() {
082: if (edits.size() == 0)
083: return false;
084: for (int i = 0; i < edits.size(); i++) {
085: UndoableEdit edit = (UndoableEdit) edits.get(i);
086: if (!edit.canRedo())
087: return false;
088: }
089: return true;
090: }
091:
092: public void die() {
093: Iterator itr = edits.iterator();
094: while (itr.hasNext()) {
095: UndoableEdit edit = (UndoableEdit) itr.next();
096: edit.die();
097: }
098: }
099:
100: public boolean addEdit(UndoableEdit anEdit) {
101: if (!acceptNew)
102: return false;
103: return edits.add(anEdit);
104: }
105:
106: public boolean replaceEdit(UndoableEdit anEdit) {
107: return false;
108: }
109:
110: public boolean isSignificant() {
111: Iterator itr = edits.iterator();
112: while (itr.hasNext()) {
113: UndoableEdit edit = (UndoableEdit) itr.next();
114: if (edit.isSignificant())
115: return true;
116: ;
117: }
118: return false;
119: }
120:
121: public String getPresentationName() {
122: UndoableEdit edit = getLast();
123: if (edit == null)
124: return "";
125: return edit.getPresentationName();
126: }
127:
128: public String getUndoPresentationName() {
129: UndoableEdit edit = getLast();
130: if (edit == null)
131: return "";
132: return edit.getUndoPresentationName();
133: }
134:
135: public String getRedoPresentationName() {
136: UndoableEdit edit = getLast();
137: if (edit == null)
138: return "";
139: return edit.getRedoPresentationName();
140: }
141:
142: }
|