1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * A single undoable operation.  You must subclass this to implement the state
24 * and behavior for your operation.  Instances of this class are placed and
25 * managed in an {@link UndoManager}.
26 *
27 * @hide
28 */
29public abstract class UndoOperation<DATA> implements Parcelable {
30    UndoOwner mOwner;
31
32    /**
33     * Create a new instance of the operation.
34     * @param owner Who owns the data being modified by this undo state; must be
35     * returned by {@link UndoManager#getOwner(String, Object) UndoManager.getOwner}.
36     */
37    public UndoOperation(UndoOwner owner) {
38        mOwner = owner;
39    }
40
41    /**
42     * Construct from a Parcel.
43     */
44    protected UndoOperation(Parcel src, ClassLoader loader) {
45    }
46
47    /**
48     * Owning object as given to {@link #UndoOperation(UndoOwner)}.
49     */
50    public UndoOwner getOwner() {
51        return mOwner;
52    }
53
54    /**
55     * Synonym for {@link #getOwner()}.{@link android.content.UndoOwner#getData()}.
56     */
57    public DATA getOwnerData() {
58        return (DATA)mOwner.getData();
59    }
60
61    /**
62     * Return true if this undo operation is a member of the given owner.
63     * The default implementation is <code>owner == getOwner()</code>.  You
64     * can override this to provide more sophisticated dependencies between
65     * owners.
66     */
67    public boolean matchOwner(UndoOwner owner) {
68        return owner == getOwner();
69    }
70
71    /**
72     * Return true if this operation actually contains modification data.  The
73     * default implementation always returns true.  If you return false, the
74     * operation will be dropped when the final undo state is being built.
75     */
76    public boolean hasData() {
77        return true;
78    }
79
80    /**
81     * Return true if this operation can be merged with a later operation.
82     * The default implementation always returns true.
83     */
84    public boolean allowMerge() {
85        return true;
86    }
87
88    /**
89     * Called when this undo state is being committed to the undo stack.
90     * The implementation should perform the initial edits and save any state that
91     * may be needed to undo them.
92     */
93    public abstract void commit();
94
95    /**
96     * Called when this undo state is being popped off the undo stack (in to
97     * the temporary redo stack).  The implementation should remove the original
98     * edits and thus restore the target object to its prior value.
99     */
100    public abstract void undo();
101
102    /**
103     * Called when this undo state is being pushed back from the transient
104     * redo stack to the main undo stack.  The implementation should re-apply
105     * the edits that were previously removed by {@link #undo}.
106     */
107    public abstract void redo();
108
109    public int describeContents() {
110        return 0;
111    }
112}
113