20 package net.sf.gridarta.textedit.scripteditor;
22 import java.util.HashMap;
24 import javax.swing.Action;
25 import javax.swing.event.UndoableEditEvent;
26 import javax.swing.event.UndoableEditListener;
27 import javax.swing.text.Document;
28 import javax.swing.undo.CannotRedoException;
29 import javax.swing.undo.CannotUndoException;
30 import javax.swing.undo.UndoManager;
31 import javax.swing.undo.UndoableEdit;
33 import net.
sf.japi.swing.action.ActionBuilder;
34 import net.
sf.japi.swing.action.ActionBuilderFactory;
35 import net.
sf.japi.swing.action.ActionMethod;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
49 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
67 private final Map<Document, UndoManager>
undo =
new HashMap<>();
83 public void undoableEditHappened(
final UndoableEditEvent e) {
95 assert !
undo.containsKey(document);
97 undo.put(document,
new UndoManager());
105 assert
undo.containsKey(document);
106 undo.remove(document);
117 public void resetUndo(@NotNull
final Document document) {
128 if (undoManager !=
null && undoManager.canUndo()) {
132 }
catch (
final CannotUndoException ignored) {
144 if (undoManager !=
null && undoManager.canRedo()) {
148 }
catch (
final CannotRedoException ignored) {
158 final boolean canUndo = undoManager !=
null && undoManager.canUndo();
159 aUndo.setEnabled(canUndo);
161 aUndo.putValue(Action.NAME,
ACTION_BUILDER.format(
"scriptEditUndo.name", undoManager.getUndoPresentationName()));
166 final boolean canRedo = undoManager !=
null && undoManager.canRedo();
167 aRedo.setEnabled(canRedo);
169 aRedo.putValue(Action.NAME,
ACTION_BUILDER.format(
"scriptEditRedo.name", undoManager.getRedoPresentationName()));
193 final UndoManager result =
undo.get(document);
194 assert result !=
null;