source: josm/trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java@ 2025

Last change on this file since 2025 was 2025, checked in by Gubaer, 15 years ago

new: improved dialog for uploading/saving modified layers on exit
new: improved dialog for uploading/saving modified layers if layers are deleted
new: new progress monitor which can delegate rendering to any Swing component
more setters/getters for properties in OSM data classes (fields are @deprecated); started to update references in the code base

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
4import java.util.Iterator;
5import java.util.LinkedList;
6import java.util.Stack;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.command.Command;
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.gui.layer.Layer;
12import org.openstreetmap.josm.gui.layer.OsmDataLayer;
13import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
14import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
15
16public 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}
Note: See TracBrowser for help on using the repository browser.