1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import java.util.Iterator;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.Stack;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.command.Command;
|
---|
10 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
11 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
12 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
13 | import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
|
---|
14 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
15 |
|
---|
16 | public class UndoRedoHandler implements LayerChangeListener {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * All commands that were made on the dataset. Don't write from outside!
|
---|
20 | */
|
---|
21 | public final LinkedList<Command> commands = new LinkedList<Command>();
|
---|
22 | /**
|
---|
23 | * The stack for redoing commands
|
---|
24 | */
|
---|
25 | private final Stack<Command> redoCommands = new Stack<Command>();
|
---|
26 |
|
---|
27 | public final LinkedList<CommandQueueListener> listenerCommands = new LinkedList<CommandQueueListener>();
|
---|
28 |
|
---|
29 |
|
---|
30 | public UndoRedoHandler() {
|
---|
31 | Layer.listeners.add(this);
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Execute the command and add it to the intern command queue.
|
---|
37 | */
|
---|
38 | public void add(final Command c) {
|
---|
39 | c.executeCommand();
|
---|
40 | commands.add(c);
|
---|
41 | redoCommands.clear();
|
---|
42 | if (Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer) {
|
---|
43 | OsmDataLayer data = (OsmDataLayer)Main.map.mapView.getActiveLayer();
|
---|
44 | data.fireDataChange();
|
---|
45 | }
|
---|
46 | fireCommandsChanged();
|
---|
47 |
|
---|
48 | // the command may have changed the selection so tell the listeners about the current situation
|
---|
49 | DataSet.fireSelectionChanged(Main.main.getCurrentDataSet().getSelected());
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Undoes the last added command.
|
---|
54 | */
|
---|
55 | public void undo() {
|
---|
56 | if (commands.isEmpty())
|
---|
57 | return;
|
---|
58 | final Command c = commands.removeLast();
|
---|
59 | c.undoCommand();
|
---|
60 | redoCommands.push(c);
|
---|
61 | if (Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer) {
|
---|
62 | OsmDataLayer data = (OsmDataLayer)Main.map.mapView.getActiveLayer();
|
---|
63 | data.fireDataChange();
|
---|
64 | }
|
---|
65 | fireCommandsChanged();
|
---|
66 | Main.main.getCurrentDataSet().setSelected();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Redoes the last undoed command.
|
---|
71 | * TODO: This has to be moved to a central place in order to support multiple layers.
|
---|
72 | */
|
---|
73 | public void redo() {
|
---|
74 | if (redoCommands.isEmpty())
|
---|
75 | return;
|
---|
76 | final Command c = redoCommands.pop();
|
---|
77 | c.executeCommand();
|
---|
78 | commands.add(c);
|
---|
79 | if (Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer) {
|
---|
80 | OsmDataLayer data = (OsmDataLayer)Main.map.mapView.getActiveLayer();
|
---|
81 | data.fireDataChange();
|
---|
82 | }
|
---|
83 | fireCommandsChanged();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void fireCommandsChanged() {
|
---|
87 | for (final CommandQueueListener l : listenerCommands) {
|
---|
88 | l.commandChanged(commands.size(), redoCommands.size());
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void clean() {
|
---|
93 | redoCommands.clear();
|
---|
94 | commands.clear();
|
---|
95 | fireCommandsChanged();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void clean(Layer layer) {
|
---|
99 | if (layer == null)
|
---|
100 | return;
|
---|
101 | boolean changed = false;
|
---|
102 | for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
|
---|
103 | if (it.next().invalidBecauselayerRemoved(layer)) {
|
---|
104 | it.remove();
|
---|
105 | changed = true;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
|
---|
109 | if (it.next().invalidBecauselayerRemoved(layer)) {
|
---|
110 | it.remove();
|
---|
111 | changed = true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | if (changed) {
|
---|
115 | fireCommandsChanged();
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void layerRemoved(Layer oldLayer) {
|
---|
120 | clean(oldLayer);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void layerAdded(Layer newLayer) {}
|
---|
124 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
|
---|
125 | }
|
---|