20 package net.sf.gridarta.gui.undo;
22 import java.util.IdentityHashMap;
24 import javax.swing.Action;
41 import net.
sf.japi.swing.action.ActionBuilder;
42 import net.
sf.japi.swing.action.ActionBuilderFactory;
43 import net.
sf.japi.swing.action.ActionMethod;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
57 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
99 mapModel = mapControl == null ? null : mapControl.getMapModel();
104 public void mapCreated(@NotNull
final MapControl<G, A, R> mapControl,
final boolean interactive) {
127 public void preBeginTransaction() {
132 public void beginTransaction(@NotNull
final String name) {
133 undoModel = undoModels.get(mapModel);
140 assert savedUndoState != null;
142 assert savedUndoModel != null;
150 savedUndoModel.
finish(savedUndoState);
151 if (maxUndoStates > 0) {
159 public void postEndTransaction() {
166 undoModels.put(mapModel,
new UndoModel<>(gameObjectFactory, gameObjectMatchers, mapTransactionListener));
177 assert undoModel != null;
182 mapManager.addMapManagerListener(mapManagerListener);
192 final boolean undoEnabled =
doUndo(
false);
194 aUndo.setEnabled(undoEnabled);
197 assert undoModel != null;
199 aUndo.putValue(Action.NAME, ACTION_BUILDER.format(
"undo.name", undoModel.
undoName()));
207 final boolean redoEnabled =
doRedo(
false);
209 aRedo.setEnabled(redoEnabled);
212 assert redoModel != null;
214 aRedo.putValue(Action.NAME, ACTION_BUILDER.format(
"redo.name", redoModel.
redoName()));
255 if (maxUndoStates > 0) {
257 undoModel.trimToSize(maxUndoStates);
267 private boolean doUndo(
final boolean performAction) {
269 if (tmpMapModel == null) {
274 if (undoModel == null) {
299 private boolean doRedo(
final boolean performAction) {
301 if (tmpMapModel == null) {
306 if (undoModel == null) {
327 public void setAction(@NotNull
final Action action, @NotNull
final String name) {
328 if (name.equals(
"undo")) {
330 }
else if (name.equals(
"redo")) {
333 throw new IllegalArgumentException();
Implements the controller for undo/redo actions.
Action aRedo
Action for "redo" function.
void refreshMenus()
Enable/disable menu entries based on the current state.
A MapModel reflects the data of a map.
A MapManager manages all opened maps.
String undoName()
Return the "undo" operation name.
Maintains the undo state for one map control.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
void setSavedSquares(@NotNull final SavedSquares< G, A, R > savedSquares)
Records the affected map squares.
boolean canRedo()
Return whether a "redo" operation is possible.
void undo()
"Undo" was selected.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Action aUndo
Action for "undo" function.
void finish(@NotNull final UndoState< G, A, R > undoState)
Finishes an undo or redo operation.
SavedSquares< G, A, R > cloneAndClear()
Creates a new instance having the same contents as this instance, then forgets all saves squares in t...
MapModel< G, A, R > mapModel
Map model for the current map.
Holds information to undo/redo one edit operation.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
Interface for listeners listening on map transactions of MapModels.
Reflects a game object (object on a map).
void addMapTransactionListener(@NotNull MapTransactionListener< G, A, R > listener)
Registers a map transaction listener.
Abstract factory for creating GameObject instances.
boolean canUndo()
Return whether an "undo" operation is possible.
Interface for listeners listening to MapManager changes.
GameObjects are the objects based on Archetypes found on maps.
boolean doRedo(final boolean performAction)
Performs the "redo" action.
MapTransactionListener< G, A, R > getMapTransactionListener()
Returns the MapTransactionListener that was passed to the constructor.
void redo()
"Redo" was selected.
Maintains GameObjectMatcher instances.
UndoControl(@NotNull final MapManager< G, A, R > mapManager, @NotNull final GameObjectFactory< G, A, R > gameObjectFactory, @NotNull final GameObjectMatchers gameObjectMatchers)
Create a new instance.
void setMaxUndoStates(final int maxUndoStates)
Sets the maximum number of undo states saved for each map.
boolean doUndo(final boolean performAction)
Performs the "undo" action.
String redoName()
Return the "redo" operation name.
Utility class for ActionBuilder related functions.
Implementation of Undo / Redo for Gridarta.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
void removeEmptySquares(@NotNull final Size2D size)
Removes empty squares outside a given area.
final Map< MapModel< G, A, R >, UndoModel< G, A, R > > undoModels
Contains an UndoModel for each known MapModel.
int maxUndoStates
The maximum number of undo states saved for each map.
Currently nothing more than a marker interface for unification.
Records a set of changed map squares.
int getMaxUndoStates()
Returns the maximum number of undo states saved for each map.
Utility class implementing undo and redo actions.
void trimToSize(final int maxUndoStates)
Discard old undo information.
A getMapArchObject()
Returns the map arch object before the operation started.
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Interface for MapArchObjects.