Gridarta Editor
ScriptEditUndoActions.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.textedit.scripteditor;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 import javax.swing.Action;
25 import javax.swing.event.UndoableEditListener;
26 import javax.swing.text.Document;
27 import javax.swing.undo.CannotRedoException;
28 import javax.swing.undo.CannotUndoException;
29 import javax.swing.undo.UndoManager;
30 import javax.swing.undo.UndoableEdit;
32 import net.sf.japi.swing.action.ActionBuilder;
33 import net.sf.japi.swing.action.ActionBuilderFactory;
34 import net.sf.japi.swing.action.ActionMethod;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public class ScriptEditUndoActions {
43 
47  @NotNull
48  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
49 
53  @NotNull
54  private final Action aUndo = ACTION_BUILDER.createAction(true, "scriptEditUndo", this);
55 
59  @NotNull
60  private final Action aRedo = ACTION_BUILDER.createAction(true, "scriptEditRedo", this);
61 
65  @NotNull
66  private final Map<Document, UndoManager> undo = new HashMap<>();
67 
71  @Nullable
72  private Document currentDocument;
73 
78  @NotNull
79  private final UndoableEditListener undoableEditListener = e -> {
80  getUndoManager((Document) e.getSource()).addEdit(e.getEdit());
81  refresh();
82  };
83 
88  public void addDocument(@NotNull final Document document) {
89  assert !undo.containsKey(document);
90  document.addUndoableEditListener(undoableEditListener);
91  undo.put(document, new UndoManager());
92  }
93 
98  public void removeDocument(@NotNull final Document document) {
99  assert undo.containsKey(document);
100  undo.remove(document);
101  document.removeUndoableEditListener(undoableEditListener);
102  if (currentDocument == document) {
103  setCurrentDocument(null);
104  }
105  }
106 
111  public void resetUndo(@NotNull final Document document) {
112  getUndoManager(document).discardAllEdits();
113  }
114 
118  @ActionMethod
119  public void scriptEditUndo() {
120  final UndoManager undoManager = getCurrentUndoManager();
121  try {
122  if (undoManager != null && undoManager.canUndo()) {
123  undoManager.undo();
124  refresh();
125  }
126  } catch (final CannotUndoException ignored) {
127  // ignore
128  }
129  }
130 
134  @ActionMethod
135  public void scriptEditRedo() {
136  final UndoManager undoManager = getCurrentUndoManager();
137  try {
138  if (undoManager != null && undoManager.canRedo()) {
139  undoManager.redo();
140  refresh();
141  }
142  } catch (final CannotRedoException ignored) {
143  }
144  }
145 
149  private void refresh() {
150  final UndoableEdit undoManager = getCurrentUndoManager();
151 
152  final boolean canUndo = undoManager != null && undoManager.canUndo();
153  aUndo.setEnabled(canUndo);
154  if (canUndo) {
155  aUndo.putValue(Action.NAME, ACTION_BUILDER.format("scriptEditUndo.name", undoManager.getUndoPresentationName()));
156  } else {
157  aUndo.putValue(Action.NAME, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEditUndo.text"));
158  }
159 
160  final boolean canRedo = undoManager != null && undoManager.canRedo();
161  aRedo.setEnabled(canRedo);
162  if (canRedo) {
163  aRedo.putValue(Action.NAME, ACTION_BUILDER.format("scriptEditRedo.name", undoManager.getRedoPresentationName()));
164  } else {
165  aRedo.putValue(Action.NAME, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEditRedo.text"));
166  }
167  }
168 
173  public void setCurrentDocument(@Nullable final Document currentDocument) {
174  if (this.currentDocument != currentDocument) {
175  this.currentDocument = currentDocument;
176  refresh();
177  }
178  }
179 
185  @NotNull
186  private UndoManager getUndoManager(@NotNull final Document document) {
187  final UndoManager result = undo.get(document);
188  assert result != null;
189  return result;
190  }
191 
197  @Nullable
198  private UndoManager getCurrentUndoManager() {
199  return currentDocument == null ? null : getUndoManager(currentDocument);
200  }
201 
202 }
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.getCurrentUndoManager
UndoManager getCurrentUndoManager()
Returns the undo manager for the current document.
Definition: ScriptEditUndoActions.java:198
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.removeDocument
void removeDocument(@NotNull final Document document)
Removes a document.
Definition: ScriptEditUndoActions.java:98
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.aRedo
final Action aRedo
Action called for "redo".
Definition: ScriptEditUndoActions.java:60
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.resetUndo
void resetUndo(@NotNull final Document document)
Forget all undo-able operations for a document.
Definition: ScriptEditUndoActions.java:111
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: ScriptEditUndoActions.java:48
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.getUndoManager
UndoManager getUndoManager(@NotNull final Document document)
Returns the undo manager for a document.
Definition: ScriptEditUndoActions.java:186
net
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.scriptEditRedo
void scriptEditRedo()
Action method for "redo".
Definition: ScriptEditUndoActions.java:135
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.scriptEditUndo
void scriptEditUndo()
Action method for "undo".
Definition: ScriptEditUndoActions.java:119
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.refresh
void refresh()
Refreshes menu actions.
Definition: ScriptEditUndoActions.java:149
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.setCurrentDocument
void setCurrentDocument(@Nullable final Document currentDocument)
Sets the current document.
Definition: ScriptEditUndoActions.java:173
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.undo
final Map< Document, UndoManager > undo
Records an UndoManager for each known Document.
Definition: ScriptEditUndoActions.java:66
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.currentDocument
Document currentDocument
The currently active document.
Definition: ScriptEditUndoActions.java:72
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.undoableEditListener
final UndoableEditListener undoableEditListener
The UndoableEditListener to detect document changes.
Definition: ScriptEditUndoActions.java:79
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.addDocument
void addDocument(@NotNull final Document document)
Adds a document.
Definition: ScriptEditUndoActions.java:88
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions.aUndo
final Action aUndo
Action called for "undo".
Definition: ScriptEditUndoActions.java:54
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.textedit.scripteditor.ScriptEditUndoActions
Implements undo and redo actions.
Definition: ScriptEditUndoActions.java:42