Okay, uh... I have a
working model of undo/redo in a text editor and the
source code.
I'll give you the details behind the code as long as you import the right classes and such.
Code:
private UndoManagerTeam undoManageTeam = new UndoManagerTeam();
private UndoAction undoActionObj = new UndoAction();
private RedoAction redoActionObj = new RedoAction();
private JMenuItem undoItem;
private JMenuItem redoItem;
Those were some of the fields, i.e. class variables, of my text editor GUI. There is a menu item for undo, a menu item for redo (which are attached to Ctrl+Z and Ctrl+Y respectively), an undo object, a redo object, and a team of undoManager objects.
The only reason I have a "team" of undo managers instead of just one is that each text edit tab requires its own undo/redo capabilities. I assume you want the same feature for your TSC editor or whatever you're building.
Code:
private class UndoManagerTeam {
private ArrayList<UndoManager> undoManageTeam =
new ArrayList<UndoManager>();
private void addManager() {
undoManageTeam.add(new UndoManager());
}
private void removeManager(int index) {
undoManageTeam.remove(index);
}
private UndoManager getCurrentManager() {
int currentIndex = tabManageObj.getCurrentTabIndex();
return undoManageTeam.get(currentIndex);
}
}
The above code is the code for the undo manager team. It's just an abstraction for an ArrayList. UndoManager is a built-in java class.
Code:
private class CustomUndoableEditListener implements UndoableEditListener {
@Override
public void undoableEditHappened(UndoableEditEvent event) {
undoManageTeam.getCurrentManager().addEdit(event.getEdit());
}
}
private class UndoAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent event) {
try {
UndoManager currentManager = undoManageTeam.getCurrentManager();
if (currentManager.canUndo())
currentManager.undo();
} catch (CannotUndoException exceptionInfo) {
//Ignore the exception.
}
}
}
private class RedoAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent event) {
try {
UndoManager currentManager = undoManageTeam.getCurrentManager();
if (currentManager.canRedo())
currentManager.redo();
} catch (CannotRedoException exceptionInfo) {
//Ignore the exception.
}
}
}
The above code for RedoAction, UndoAction, and CustomUndoableEditListener are the basis for your undo/redo stuff.
Code:
private ArrayList<CustomUndoableEditListener> editListeners =
new ArrayList<CustomUndoableEditListener>();
private void addTab() {
JScrollPane newScrollPane = new JScrollPane();
JTextPane newTextPane = new JTextPane();
newScrollPane.setViewportView(newTextPane);
textPaneList.add(newTextPane);
tabList.add(newScrollPane);
mainTabbedPane.addTab("New File",newScrollPane);
filesBeingEdited.add("");
undoManageTeam.addManager();
editListeners.add(new [COLOR="Red"][B]CustomUndoableEditListener[/B][/COLOR]());
addPropertiesToTextPane(newTextPane);
switchToLastTab();
}
Finally, whenever a new tab is created by the user, an new CustomUndoableEditListener must be created for that tab.
I'm sorry if that made little sense. I copy-pasta'd most of the undo/redo code from the Java tutorials and made modifications so that it'd work for multiple tabs. I got my information here:
http://download.oracle.com/javase/tutorial/uiswing/components/generaltext.html#undoableedits
So yeah. Feel free to bombard me with additional questions. The undo/redo stuff is not just limited to text, so you could use it to undo CS map editing actions and emulate the other stuff you see in CaveEditor.