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