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");
55 private final Action
aUndo = ACTION_BUILDER.createAction(
true,
"scriptEditUndo",
this);
61 private final Action
aRedo = ACTION_BUILDER.createAction(
true,
"scriptEditRedo",
this);
67 private final Map<Document, UndoManager>
undo =
new HashMap<>();
83 public void undoableEditHappened(
final UndoableEditEvent e) {
95 assert !undo.containsKey(document);
96 document.addUndoableEditListener(undoableEditListener);
97 undo.put(document,
new UndoManager());
105 assert undo.containsKey(document);
106 undo.remove(document);
107 document.removeUndoableEditListener(undoableEditListener);
108 if (currentDocument == 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()));
180 if (this.currentDocument != currentDocument) {
193 final UndoManager result = undo.get(document);
194 assert result != null;
205 return currentDocument != null ?
getUndoManager(currentDocument) : null;
void setCurrentDocument(@Nullable final Document currentDocument)
Sets the current document.
final UndoableEditListener undoableEditListener
The UndoableEditListener to detect document changes.
void resetUndo(@NotNull final Document document)
Forget all undo-able operations for a document.
final Action aRedo
Action called for "redo".
void scriptEditRedo()
Action method for "redo".
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Implements undo and redo actions.
Base package of all Gridarta classes.
void removeDocument(@NotNull final Document document)
Removes a document.
Document currentDocument
The currently active document.
static final ActionBuilder ACTION_BUILDER
Action Builder.
void scriptEditUndo()
Action method for "undo".
final Action aUndo
Action called for "undo".
final Map< Document, UndoManager > undo
Records an UndoManager for each known Document.
Utility class for ActionBuilder related functions.
UndoManager getCurrentUndoManager()
Returns the undo manager for the current document.
void refresh()
Refreshes menu actions.
void addDocument(@NotNull final Document document)
Adds a document.
UndoManager getUndoManager(@NotNull final Document document)
Returns the undo manager for a document.