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

Last change on this file since 3246 was 3225, checked in by jttt, 14 years ago

Remove long deprecated data change listener

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.LinkedList;
7import java.util.Stack;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.command.Command;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.gui.MapView;
13import org.openstreetmap.josm.gui.layer.Layer;
14import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
15
16public class UndoRedoHandler implements MapView.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 public UndoRedoHandler() {
30 MapView.addLayerChangeListener(this);
31 }
32
33 /**
34 * Execute the command and add it to the intern command queue.
35 */
36 public void addNoRedraw(final Command c) {
37 c.executeCommand();
38 commands.add(c);
39 // Limit the number of commands in the undo list.
40 // Currently you have to undo the commands one by one. If
41 // this changes, a higher default value may be reasonable.
42 if (commands.size() > Main.pref.getInteger("undo.max", 1000)) {
43 commands.removeFirst();
44 }
45 redoCommands.clear();
46 }
47
48 public void afterAdd() {
49 fireCommandsChanged();
50
51 // the command may have changed the selection so tell the listeners about the current situation
52 Main.main.getCurrentDataSet().fireSelectionChanged();
53 }
54
55 /**
56 * Execute the command and add it to the intern command queue.
57 */
58 synchronized public void add(final Command c) {
59 addNoRedraw(c);
60 afterAdd();
61 }
62
63 /**
64 * Undoes the last added command.
65 */
66 synchronized public void undo() {
67 if (commands.isEmpty())
68 return;
69 Collection<? extends OsmPrimitive> oldSelection = Main.main.getCurrentDataSet().getSelected();
70 final Command c = commands.removeLast();
71 c.undoCommand();
72 redoCommands.push(c);
73 fireCommandsChanged();
74 Collection<? extends OsmPrimitive> newSelection = Main.main.getCurrentDataSet().getSelected();
75 if (!oldSelection.equals(newSelection)) {
76 Main.main.getCurrentDataSet().fireSelectionChanged();
77 }
78 }
79
80 /**
81 * Redoes the last undoed command.
82 * TODO: This has to be moved to a central place in order to support multiple layers.
83 */
84 public void redo() {
85 if (redoCommands.isEmpty())
86 return;
87 Collection<? extends OsmPrimitive> oldSelection = Main.main.getCurrentDataSet().getSelected();
88 final Command c = redoCommands.pop();
89 c.executeCommand();
90 commands.add(c);
91 fireCommandsChanged();
92 Collection<? extends OsmPrimitive> newSelection = Main.main.getCurrentDataSet().getSelected();
93 if (!oldSelection.equals(newSelection)) {
94 Main.main.getCurrentDataSet().fireSelectionChanged();
95 }
96 }
97
98 public void fireCommandsChanged() {
99 for (final CommandQueueListener l : listenerCommands) {
100 l.commandChanged(commands.size(), redoCommands.size());
101 }
102 }
103
104 public void clean() {
105 redoCommands.clear();
106 commands.clear();
107 fireCommandsChanged();
108 }
109
110 public void clean(Layer layer) {
111 if (layer == null)
112 return;
113 boolean changed = false;
114 for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
115 if (it.next().invalidBecauselayerRemoved(layer)) {
116 it.remove();
117 changed = true;
118 }
119 }
120 for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
121 if (it.next().invalidBecauselayerRemoved(layer)) {
122 it.remove();
123 changed = true;
124 }
125 }
126 if (changed) {
127 fireCommandsChanged();
128 }
129 }
130
131 public void layerRemoved(Layer oldLayer) {
132 clean(oldLayer);
133 }
134
135 public void layerAdded(Layer newLayer) {}
136 public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
137}
Note: See TracBrowser for help on using the repository browser.