
com.mxgraph.util.mxUndoManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yaoqiang-bpmn-editor Show documentation
Show all versions of yaoqiang-bpmn-editor Show documentation
an Open Source BPMN 2.0 Modeler
/**
* $Id: mxUndoManager.java,v 1.13 2010-08-02 14:17:34 david Exp $
* Copyright (c) 2007-2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.util;
import java.util.ArrayList;
import java.util.List;
import com.mxgraph.util.mxUndoableEdit.mxUndoableChange;
import com.mxgraph.view.mxGraph;
/**
* Implements an undo history.
*
* This class fires the following events:
*
* mxEvent.CLEAR fires after clear was executed. The event has no properties.
*
* mxEvent.UNDO fires afer a significant edit was undone in undo. The
* edit
property contains the mxUndoableEdit that was undone.
*
* mxEvent.REDO fires afer a significant edit was redone in redo. The
* edit
property contains the mxUndoableEdit that was redone.
*
* mxEvent.ADD fires after an undoable edit was added to the history. The
* edit
property contains the mxUndoableEdit that was added.
*/
public class mxUndoManager extends mxEventSource
{
protected mxGraph graph;
/**
* Maximum command history size. 0 means unlimited history. Default is 100.
*/
protected int size;
/**
* List that contains the steps of the command history.
*/
protected List history;
/**
* Index of the element to be added next.
*/
protected int indexOfNextAdd;
/**
* Constructs a new undo manager with a default history size.
*/
public mxUndoManager(mxGraph graph) {
this(100);
this.addListener(mxEvent.UNDO, undoHandler);
this.addListener(mxEvent.REDO, undoHandler);
if (this.graph != null) {
this.graph.removeListener(handler);
}
this.graph = graph;
if (this.graph != null) {
this.graph.getModel().addListener(mxEvent.UNDO, handler);
this.graph.getView().addListener(mxEvent.UNDO, handler);
}
}
/**
* Constructs a new undo manager for the specified size.
*/
public mxUndoManager(int size)
{
this.size = size;
clear();
}
protected mxIEventListener handler = new mxIEventListener() {
public void invoke(Object source, mxEventObject evt) {
undoableEditHappened((mxUndoableEdit) evt.getProperty("edit"));
}
};
// Keeps the selection in sync with the command history
mxIEventListener undoHandler = new mxIEventListener() {
public void invoke(Object source, mxEventObject evt) {
List changes = ((mxUndoableEdit) evt.getProperty("edit")).getChanges();
graph.setSelectionCells(graph.getSelectionCellsForChanges(changes));
}
};
/**
*
*/
public boolean isEmpty()
{
return history.isEmpty();
}
/**
* Clears the command history.
*/
public void clear()
{
history = new ArrayList(size);
indexOfNextAdd = 0;
fireEvent(new mxEventObject(mxEvent.CLEAR));
}
/**
* Returns true if an undo is possible.
*/
public boolean canUndo()
{
return indexOfNextAdd > 0;
}
/**
* Undoes the last change.
*/
public void undo()
{
while (indexOfNextAdd > 0)
{
mxUndoableEdit edit = history.get(--indexOfNextAdd);
edit.undo();
if (edit.isSignificant())
{
fireEvent(new mxEventObject(mxEvent.UNDO, "edit", edit));
break;
}
}
}
/**
* Returns true if a redo is possible.
*/
public boolean canRedo()
{
return indexOfNextAdd < history.size();
}
/**
* Redoes the last change.
*/
public void redo()
{
int n = history.size();
while (indexOfNextAdd < n)
{
mxUndoableEdit edit = history.get(indexOfNextAdd++);
edit.redo();
if (edit.isSignificant())
{
fireEvent(new mxEventObject(mxEvent.REDO, "edit", edit));
break;
}
}
}
/**
* Method to be called to add new undoable edits to the history.
*/
public void undoableEditHappened(mxUndoableEdit undoableEdit)
{
trim();
if (size > 0 && size == history.size())
{
history.remove(0);
}
history.add(undoableEdit);
indexOfNextAdd = history.size();
fireEvent(new mxEventObject(mxEvent.ADD, "edit", undoableEdit));
}
/**
* Removes all pending steps after indexOfNextAdd from the history,
* invoking die on each edit. This is called from undoableEditHappened.
*/
protected void trim()
{
while (history.size() > indexOfNextAdd)
{
mxUndoableEdit edit = history
.remove(indexOfNextAdd);
edit.die();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy