KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > operations > LinearUndoViolationDetector


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.commands.operations;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16
17 /**
18  * <p>
19  * An abstract class for detecting violations in a strict linear undo/redo
20  * model. Once a violation is detected, subclasses implement the specific
21  * behavior for indicating whether or not the undo/redo should proceed.
22  * </p>
23  *
24  * @since 3.1
25  */

26 public abstract class LinearUndoViolationDetector implements IOperationApprover {
27
28     /**
29      * Create an instance of LinearUndoViolationDetector.
30      */

31     public LinearUndoViolationDetector() {
32         super();
33     }
34
35     /**
36      * Return a status indicating whether a linear redo violation is allowable.
37      * A linear redo violation is defined as a request to redo a particular
38      * operation even if it is not the most recently added operation to the redo
39      * history.
40      *
41      * @param operation
42      * the operation for which a linear redo violation has been
43      * detected.
44      * @param context
45      * the undo context in which the linear redo violation exists
46      * @param history
47      * the operation history containing the operation
48      * @param info
49      * the IAdaptable (or <code>null</code>) provided by the
50      * caller in order to supply UI information for prompting the
51      * user if necessary. When this parameter is not
52      * <code>null</code>, it should minimally contain an adapter
53      * for the org.eclipse.swt.widgets.Shell.class.
54      *
55      * @return the IStatus describing whether the redo violation is allowed. The
56      * redo will not proceed if the status severity is not
57      * <code>OK</code>, and the caller requesting the redo will be
58      * returned the status that caused the rejection. Specific status
59      * severities will not be interpreted by the history.
60      */

61
62     protected abstract IStatus allowLinearRedoViolation(
63             IUndoableOperation operation, IUndoContext context,
64             IOperationHistory history, IAdaptable info);
65
66     /**
67      * Return a status indicating whether a linear undo violation is allowable.
68      * A linear undo violation is defined as a request to undo a particular
69      * operation even if it is not the most recently added operation to the undo
70      * history.
71      *
72      * @param operation
73      * the operation for which a linear undo violation has been
74      * detected.
75      * @param context
76      * the undo context in which the linear undo violation exists
77      * @param history
78      * the operation history containing the operation
79      * @param info
80      * the IAdaptable (or <code>null</code>) provided by the
81      * caller in order to supply UI information for prompting the
82      * user if necessary. When this parameter is not
83      * <code>null</code>, it should minimally contain an adapter
84      * for the org.eclipse.swt.widgets.Shell.class.
85      *
86      * @return the IStatus describing whether the undo violation is allowed. The
87      * undo will not proceed if the status severity is not
88      * <code>OK</code>, and the caller requesting the undo will be
89      * returned the status that caused the rejection. Specific status
90      * severities will not be interpreted by the history.
91      */

92     protected abstract IStatus allowLinearUndoViolation(
93             IUndoableOperation operation, IUndoContext context,
94             IOperationHistory history, IAdaptable info);
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.eclipse.core.commands.operations.IOperationApprover#proceedRedoing(org.eclipse.core.commands.operations.IUndoableOperation,
100      * org.eclipse.core.commands.operations.IOperationHistory,
101      * org.eclipse.core.runtime.IAdaptable)
102      */

103     public final IStatus proceedRedoing(IUndoableOperation operation,
104             IOperationHistory history, IAdaptable info) {
105         IUndoContext[] contexts = operation.getContexts();
106         for (int i = 0; i < contexts.length; i++) {
107             IUndoContext context = contexts[i];
108             if (history.getRedoOperation(context) != operation) {
109                 IStatus status = allowLinearRedoViolation(operation, context,
110                         history, info);
111                 if (!status.isOK()) {
112                     return status;
113                 }
114             }
115         }
116         return Status.OK_STATUS;
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.eclipse.core.commands.operations.IOperationApprover#proceedUndoing(org.eclipse.core.commands.operations.IUndoableOperation,
123      * org.eclipse.core.commands.operations.IOperationHistory,
124      * org.eclipse.core.runtime.IAdaptable)
125      */

126
127     public final IStatus proceedUndoing(IUndoableOperation operation,
128             IOperationHistory history, IAdaptable info) {
129         IUndoContext[] contexts = operation.getContexts();
130         for (int i = 0; i < contexts.length; i++) {
131             IUndoContext context = contexts[i];
132             if (history.getUndoOperation(context) != operation) {
133                 IStatus status = allowLinearUndoViolation(operation, context,
134                         history, info);
135                 if (!status.isOK()) {
136                     return status;
137                 }
138             }
139         }
140         return Status.OK_STATUS;
141     }
142 }
143
Popular Tags