01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.version.undo;
18:
19: import java.util.ArrayList;
20: import java.util.List;
21:
22: import org.apache.wicket.Component;
23: import org.apache.wicket.IClusterable;
24: import org.slf4j.Logger;
25: import org.slf4j.LoggerFactory;
26:
27: /**
28: * A ChangeList is a sequence of changes that can be undone.
29: *
30: * @author Jonathan Locke
31: */
32: class ChangeList implements IClusterable {
33: private static final long serialVersionUID = 1L;
34:
35: /** log. */
36: private static final Logger log = LoggerFactory
37: .getLogger(ChangeList.class);
38:
39: /** the changes. */
40: private final List changes = new ArrayList();
41:
42: /**
43: * A component was added.
44: * @param component the added component
45: */
46: void componentAdded(Component component) {
47: changes.add(new Add(component));
48: }
49:
50: /**
51: * A model is about to change.
52: * @param component the component of which the model changed
53: */
54: void componentModelChanging(Component component) {
55: changes.add(new ModelChange(component));
56: }
57:
58: /**
59: * The state of a component is about to change.
60: * @param change the change object
61: */
62: void componentStateChanging(Change change) {
63: if (log.isDebugEnabled()) {
64: log.debug("RECORD CHANGE: " + change);
65: }
66:
67: changes.add(change);
68: }
69:
70: /**
71: * A component was removed from its parent.
72: * @param component the component that was removed
73: */
74: void componentRemoved(Component component) {
75: changes.add(new Remove(component));
76: }
77:
78: void add(ChangeList list) {
79: changes.addAll(list.changes);
80: }
81:
82: /**
83: * Undo changes (rollback).
84: */
85: void undo() {
86: // Go through changes in reverse time order to undo
87: for (int i = changes.size() - 1; i >= 0; i--) {
88: ((Change) changes.get(i)).undo();
89: }
90: }
91:
92: /**
93: * @see java.lang.Object#toString()
94: */
95: public String toString() {
96: return changes.toString();
97: }
98:
99: }
|