UndoOperation.java revision 3aa49b6fece334ace7525d42c1f6d0b7cdc1fbfb
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 */
27public abstract class UndoOperation<DATA> implements Parcelable {
28    UndoOwner mOwner;
29
30    /**
31     * Create a new instance of the operation.
32     * @param owner Who owns the data being modified by this undo state; must be
33     * returned by {@link UndoManager#getOwner(String, Object) UndoManager.getOwner}.
34     */
35    public UndoOperation(UndoOwner owner) {
36        mOwner = owner;
37    }
38
39    /**
40     * Construct from a Parcel.
41     */
42    protected UndoOperation(Parcel src, ClassLoader loader) {
43    }
44
45    /**
46     * Owning object as given to {@link #UndoOperation(UndoOwner)}.
47     */
48    public UndoOwner getOwner() {
49        return mOwner;
50    }
51
52    /**
53     * Synonym for {@link #getOwner()}.{@link android.content.UndoOwner#getData()}.
54     */
55    public DATA getOwnerData() {
56        return (DATA)mOwner.getData();
57    }
58
59    /**
60     * Return true if this undo operation is a member of the given owner.
61     * The default implementation is <code>owner == getOwner()</code>.  You
62     * can override this to provide more sophisticated dependencies between
63     * owners.
64     */
65    public boolean matchOwner(UndoOwner owner) {
66        return owner == getOwner();
67    }
68
69    /**
70     * Return true if this operation actually contains modification data.  The
71     * default implementation always returns true.  If you return false, the
72     * operation will be dropped when the final undo state is being built.
73     */
74    public boolean hasData() {
75        return true;
76    }
77
78    /**
79     * Return true if this operation can be merged with a later operation.
80     * The default implementation always returns true.
81     */
82    public boolean allowMerge() {
83        return true;
84    }
85
86    /**
87     * Called when this undo state is being committed to the undo stack.
88     * The implementation should perform the initial edits and save any state that
89     * may be needed to undo them.
90     */
91    public abstract void commit();
92
93    /**
94     * Called when this undo state is being popped off the undo stack (in to
95     * the temporary redo stack).  The implementation should remove the original
96     * edits and thus restore the target object to its prior value.
97     */
98    public abstract void undo();
99
100    /**
101     * Called when this undo state is being pushed back from the transient
102     * redo stack to the main undo stack.  The implementation should re-apply
103     * the edits that were previously removed by {@link #undo}.
104     */
105    public abstract void redo();
106
107    public int describeContents() {
108        return 0;
109    }
110}
111