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

Last change on this file since 3425 was 3275, checked in by bastiK, 14 years ago

remove unnecessary java 6 usage

  • Property svn:eol-style set to native
File size: 4.7 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 public final LinkedList<Command> redoCommands = new LinkedList<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 public void undo() {
67 undo(1);
68 }
69
70 /**
71 * Undoes multiple commands.
72 */
73 synchronized public void undo(int num) {
74 if (commands.isEmpty())
75 return;
76 Collection<? extends OsmPrimitive> oldSelection = Main.main.getCurrentDataSet().getSelected();
77 for (int i=1; i<=num; ++i) {
78 final Command c = commands.removeLast();
79 c.undoCommand();
80 redoCommands.addFirst(c);
81 if (commands.isEmpty())
82 break;
83 }
84 fireCommandsChanged();
85 Collection<? extends OsmPrimitive> newSelection = Main.main.getCurrentDataSet().getSelected();
86 if (!oldSelection.equals(newSelection)) {
87 Main.main.getCurrentDataSet().fireSelectionChanged();
88 }
89 }
90
91 /**
92 * Redoes the last undoed command.
93 */
94 public void redo() {
95 redo(1);
96 }
97
98 /**
99 * Redoes multiple commands.
100 */
101 public void redo(int num) {
102 if (redoCommands.isEmpty())
103 return;
104 Collection<? extends OsmPrimitive> oldSelection = Main.main.getCurrentDataSet().getSelected();
105 for (int i=0; i<num; ++i) {
106 final Command c = redoCommands.removeFirst();
107 c.executeCommand();
108 commands.add(c);
109 if (redoCommands.isEmpty())
110 break;
111 }
112 fireCommandsChanged();
113 Collection<? extends OsmPrimitive> newSelection = Main.main.getCurrentDataSet().getSelected();
114 if (!oldSelection.equals(newSelection)) {
115 Main.main.getCurrentDataSet().fireSelectionChanged();
116 }
117 }
118
119 public void fireCommandsChanged() {
120 for (final CommandQueueListener l : listenerCommands) {
121 l.commandChanged(commands.size(), redoCommands.size());
122 }
123 }
124
125 public void clean() {
126 redoCommands.clear();
127 commands.clear();
128 fireCommandsChanged();
129 }
130
131 public void clean(Layer layer) {
132 if (layer == null)
133 return;
134 boolean changed = false;
135 for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
136 if (it.next().invalidBecauselayerRemoved(layer)) {
137 it.remove();
138 changed = true;
139 }
140 }
141 for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
142 if (it.next().invalidBecauselayerRemoved(layer)) {
143 it.remove();
144 changed = true;
145 }
146 }
147 if (changed) {
148 fireCommandsChanged();
149 }
150 }
151
152 public void layerRemoved(Layer oldLayer) {
153 clean(oldLayer);
154 }
155
156 public void layerAdded(Layer newLayer) {}
157 public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
158}
Note: See TracBrowser for help on using the repository browser.